I am developing a Laravel package that includes controllers, which I wish to unit test. The problem is if an exception is thrown inside one of these controllers, Laravel's exception handler captures it and outputs a 500 HTTP response. PHPUnit is none the wiser, and tests simply fail to meet the 200 OK assertion. The lack of a stack trace in PHPUnit's output both locally and on services such as Travis CI hinders workflow enormously.
I'm aware that I could rethrow the exception from somewhere such as \App\Exceptions\Handler, but since this is a package, I can't modify those application files (laravel/laravel is simply a dependency for testing, in order to rope in the necessary components for testing controllers).
I would have thought the set_exception_handler() call below in TestCase would work, but weirdly it has no impact whatsoever:
public function createApplication()
{
$app = require __DIR__.'/../bootstrap/app.php';
$app->make(Illuminate\Contracts\Console\Kernel::class)->bootstrap();
set_exception_handler(function ($e) {
throw $e;
});
return $app;
}
Can anyone tell me why the above doesn't work?