Category Development

A Quickie – Dealing with Observers and Testing in Laravel 5.1

If you use Laravel 5.1, you probably heard about model observers. Basically, they just observe a certain model and when a certain action is done (or it is going to be done) it executes a separate logic. It is really a great way to deal with model events, instead of declaring things right in the EventServiceProvider.

Said that, this morning I had some issues while dealing with observers during testing with PHPUnit.

The Laravel documentation says that you can disable event triggering with something like:

<?php

class ExampleTest extends TestCase
{
    public function testUserRegistration()
    {
        $this->withoutEvents();

        // Test user registration code...
    }
}

(source: http://laravel.com/docs/5.1/testing#mocking-events)

Here’s the bad news: it does not work with observers. Even if you type the $this->withoutEvents(); the observer is loaded and called. Bye bye, isolated testing!

Now, it seems that no one on internet had a similar issue. That’s quite strange because I like observers and I think they are useful, and more elegant than the classic model events declaration.

This is how I solved my problem.

public function testCanSave()
{
    $this->app->bind(MediaUploaderObserver::class, function(){
        return $this->getMockBuilder(MediaUploaderObserver::class)->disableOriginalConstructor()->getMock();
    });

    // test logic goes here...
}

By using the $this->app->bind method to create a new binding in the service container, I managed to solve the problem. The closure passed as the second argument returns a mock of the original MediaUploaderObserver class.

Screenshot from 2015-12-08 14:02:34

And that’s all, folks!

Hostr – A simple PHP CLI tool to manage your hosts file!

Hi everyone! A few hours ago I release on Github my latest version of Hostr, a PHP CLI tool that you can use to manage your hosts file with an expressive and easy syntax.

Before you say it: yes, I know that out, into the wild, there are many other similar tools. However, I wanted to do some practice with the Symfony Console component and, later, with the Box2 tool that I used to create the .phar file.

That’s all. It took me, in total, three/four days of work. And it was really funny.

Cool! How can I try it?

Great! All you have to do is open your terminal, switch to sudo and type the following instructions:

wget http://hellofrancesco.com/projects/hostr.phar 
chmod 755 hostr.phar && mv hostr.phar /usr/local/bin/hostr

The first line will download in the current folder the .phar file.

The next line will change the file permissions to 755 and move it in the /usr/local/bin folder

Done. How can I use it?

Well, now that you have installed it, type

hostr

to verify if it was installed successfully.

Now you can use every single command. There are, actually, 7 commands that you can use.

  • show
  • add
  • remove
  • backup
  • restore
  • tidy
  • check

For more details, Take a look to the project page on Github!

Laravel API Boilerplate – JWT Edition

I have just published on Github my new side-project, made in the last weekend: Laravel API Boilerplate – JWT Edition.

I often found myself in the need of build an API to test an idea. I don’t like to repeat boring operations and, in this last period, I am often using three awesome packages:

So… why don’t work on a simple integration, to solve the problem once for all?

I shared it because I am not the only one to have this needs. Quite sure about it 🙂

So… feel free to take it, PR new features if you want and give my some feedback!

Changin’ the Stack

Sometimes you have to take a break.

No matter how much you love what you do, but studying something new is always exciting and funny. As a developer, I know that is the best part of my job: when you are quite confident with a certain technology…  well, maybe it’s time to learn something new.

Obviously, I am not leaving Laravel and its fantastic community. However, I decided to dedicate a slice of my time to learn a totally different stack: from LAMP, to MEAN.

Probably you already know it. If not, MEAN is an acronym that stands for

  • MongoDB;
  • Express;
  • AngularJS;
  • NodeJS;

I don’t feel like someone can stumble upon serious issues while installing another PHP framework, so I decided to change the entire thing.

3285OS_MEAN Web Development_cover

I just bought “MEAN Web Development” by Amos Q. Haviv  from Packt Publishing. I will report here, on my blog, all my thoughts about this new path, eventually along with something else if I will find something interesting.

My learning path will be the following:

  • learn the NodeJS basics;
  • learn the Express basics;
  • learn the MongoDB basics;
  • learn the AngularJS basics;
  • learn how to create and configure a basic MEAN VM on Vagrant;
  • extra: learn something about Docker;

My primary focus will be on backend technologies and honestly, I can’t wait to reach the MongoDB part. We are in 2015 and I have never experienced the NoSQL paradigm. That sucks.

Let’s see what happens.


Update #1 : June 13th, 2015

Right now, I have just finished the second chapter. The first one is dedicated to the very basics: installing NodeJS and some first tests with MongoDB.

The book is awesomely clear: I love that writing style in IT books.

The second chapter is about NodeJS in a more “deeper” way. It deals with JS event-driven programming in detail, and then explains how to create some really basic server (and a little, little app) with Connect.

Can’t wait to start the 3rd chapter, about Express!


Update #2 : June 14th, 2015

Everything great with the Express chapter and the MongoDB one. A perfect introduction to both concepts.

Now I am a little stuck with Vagrant. I tried this suggested box, but got many issues and problems when using Yeoman. Maybe it’s too early. I will try a different approach: I will use the VM without Yeoman and other solutions (meanjs.org and mean.io). After all, right now I am working only with NodeJS, MongoDB and Express.

No frontend. Let’s see what happens.


Update #3 : June 15th, 2015

I found many issues, related to permissions and users, with arvindr21/mean-box. So, I decided to make a new Vagrant VM starting from the very basic (a classic hashicorp/precise64).

I solved many of these issues, but when trying to install meanjs.org or mean.io projects it failed. Sometimes on a specific package, sometimes for the tar tar.unpack untar error. Sometimes I can successfully scaffold the application using Yeoman, but then, during the npm install, something goes wrong.

Many people explain this with “vagrant vm related problems”. I don’t know: however, many problems related to the virtual machine utility are a real thing. In the next days I will try to use my VM with some basic setup. If it will work, I will publish it on github and here. 🙂

Time to learn something more about Docker!


Update #4: June 17th, 2015

Ok, quick update today.

Here’s my first contribution about MEAN and Vagrant. This is a very basic first proposal of a Vagrantfile with two separate provision files.

I will talk about it soon in more detail

Until then, you can find it here on Github.

Cheers!


Useful Resources

In the following list you will find some interesting article I found about MEAN (or about its single components) around the internet. Hope they will help you. They are not only “MEAN is cool m8” articles, I will hear the opinions from both sides.