mikeerickson / phpunit-pretty-result-printer

PHPUnit Pretty Result Printer -- make your PHPUnit tests look pretty!
MIT License
1.2k stars 71 forks source link

Cannot resolve Laravel exception handler when must display exceptions after the tests #129

Closed florentpoujol closed 6 years ago

florentpoujol commented 6 years ago

Hello, and first of all thanks for this package, the display is indeed much prettier than PHPUnit's default one !

However version 0.23.x introduced a bug with Laravel when it must display an exception that happended during the test: it can't resolve the exception handler and display the following error instead (twice).

Doesn't happen with version 0.22.

Fatal error: Uncaught Illuminate\Contracts\Container\BindingResolutionException: Target [Illuminate\Contracts\Debug\ExceptionHandler] is not instantiable. in /seed/vendor/laravel/framework/src/Illuminate/Container/Container.php on line 945

Illuminate\Contracts\Container\BindingResolutionException: Target [Illuminate\Contracts\Debug\ExceptionHandler] is not instantiable. in /seed/vendor/laravel/framework/src/Illuminate/Container/Container.php on line 945

Call Stack:
    1.6324   23728128   1. Illuminate\Foundation\Bootstrap\HandleExceptions->handleException() /seed/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php:0
    1.6326   23737792   2. Illuminate\Foundation\Bootstrap\HandleExceptions->renderForConsole() /seed/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php:87
    1.6326   23737792   3. Illuminate\Foundation\Bootstrap\HandleExceptions->getExceptionHandler() /seed/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php:101
    1.6326   23737792   4. Illuminate\Foundation\Application->make() /seed/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php:159
    1.6326   23737792   5. Illuminate\Foundation\Application->make() /seed/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:733
    1.6326   23737792   6. Illuminate\Foundation\Application->resolve() /seed/vendor/laravel/framework/src/Illuminate/Container/Container.php:609
    1.6326   23737792   7. Illuminate\Foundation\Application->build() /seed/vendor/laravel/framework/src/Illuminate/Container/Container.php:658
    1.6326   23737904   8. Illuminate\Foundation\Application->notInstantiable() /seed/vendor/laravel/framework/src/Illuminate/Container/Container.php:785

Same result with Laravel 5.7.6 or 5.7.9, and PHPUnit 7.3.x or 7.4.0, and again, it work great with v0.22, but not with v0.23.0 and v0.23.2.

Thanks for your time !

mikeerickson commented 6 years ago

@florentpoujol First of all, my apologies from breaking things for you. I certainly did work around this area (to add pretty printing for output)

Could you please provide some additional information?

Eliepse commented 6 years ago

I have the same issue with Laravel 5.7.9, PhpUnit 7.4.0 and your plugin at version 0.23.2. It occurs when an \Exception is thrown (with a custom message), except if I use expectException method in my test. Without your plugin, the stacktrace is printed correctly.

florentpoujol commented 6 years ago

Yes, here more info:

So after all the tests have run, PHPUnit display the status of the one that fails, as well as any exception that were thrown either from the test classes themselves, or from within the application's code.

The problem I have lies in displaying this last kind of exception: instead of displaying all the exceptions that were throw -and independently of how many of them were thrown- I only see the error I gave in the first post, exactly twice.

The message indicates that someone tried to resolve the Illuminate\Contracts\Debug\ExceptionHandler interface through Laravel's DI container and that what it resolved isn't an instance and isn't intantiable... While the tests are running, and outside (accessing a controller via the browser), there is no issue resolving the application's exception handler.

There is no issue displaying successes or test failures or Exception throw from the test classes (note: I'm not extra sure about that last one).

The application's instance and the container are flushed during tearDown() so the error would be logical if someone would try to use them after that, but I don't understand WHO would do that.

This is your change from 0.22 to 0.23 https://github.com/mikeerickson/phpunit-pretty-result-printer/compare/0.22.0...0.23.0, so I GUESS that it is getting the exception message that somehow try to use the exception handler (which has a 'render' method), but exception instances are not linked to the application's instance...

I will investigate further tomorrow at work, I'm pretty sure actually that it will end up being a weird side-effect, totally not related to your package...

florentpoujol commented 6 years ago

Fixed. See PR https://github.com/mikeerickson/phpunit-pretty-result-printer/pull/130

I still don't understand why, when the exception was thrown from within Laravel, this is its error handler (instead of PHPUnit's) that is called...

When the exception wasn't thrown from within Laravel, it would just print Undefined index: (the actual error) instead of the BindingResolutionException.

While we are at it, I noticed a coloring bug in the displayed messages: the remainder of lines after the first hyphen are colored in green (see attached image).
phpunit-pretty-printer-color-error

Lines that follow are colored in white as usual.