Collision — Better debug workflow with PHPUnit and Laravel

Few weeks ago I released a package called Collision, the main objective was to provide a pretty error handler on command-line/console applications, specially on my other side project called Laravel Zero. Let’s take a look at the description Laravel News’s article made about collision:

Collision is a new package by Nuno Maduro that provides error handling for command line applications. It’s built on top of the Whoops package and provides adapters for both the Laravel artisan console and PHPUnit.

A common problem when we are using the feature testing suite of Laravel is that we get a false positive result from PHPUnit that is our tests are given a green result, even though our code is wrong. Let’s take a look at an example:

So, using the test:

public function testBasicTest()
{
$this->get('/')
->assertSee('Foo bar');
}

And the routes file:

Route::get('/', function () {
$a->thisIsACodeMistake();
    return 'Foo bar';
});

You got the following PHPUnit result:

This happens because the ‘Foo bar’ is actually present on the Whoops exception trace that is included on the response of our get method.

One of the features that got released recently, is the combination of the PHPUnit Adapter and the Laravel Adapter. What it does is actually warn you that your test did pass, but you actually have a mistake in your code.

With Collision, you get the following PHPUnit result:

This combination of adapters go even futher when you are working with Laravel Internal Exceptions.

So, using the test:

public function testBasicTest()
{
$response = $this->get('/');

$response->assertStatus(302);// Expects an ValidationException.
}

And the routes file:

Route::get('/', function (Request $request) {
// This throws an ValidationException
$request->validate([
'title' => 'required|unique:posts|max:255',
'body' => 'required',
]);
return view('welcome');
});

You still got the PHPUnit result:

This happens because Collision understands that the caught exception should not be render on the rendered in console since it is the normal application behavior, since the request was not valid and we were expecting an 302. Cool right?

If you want to install Collision in your project click on the link below and checkout the README file.

https://github.com/nunomaduro/collision

Give it a try! And tell me what do you think! 👍