Create a gist now

Instantly share code, notes, and snippets.

@adamwathan /0.md Secret
Last active Aug 16, 2017

What would you like to do?
Disable exception handling in Laravel acceptance tests

Just a quick helper method you can add to your base TestCase to make it easy to toggle exception handling on the fly. Improved a little bit from the sloppier version I used in the screencast :)

Update 2016-11-09:

At some point since I originally shared this snippet and the accompanying screencast, Laravel started binding App\Exceptions\Handler under the Illuminate\Contracts\Debug\ExceptionHandler interface instead of the concrete class, so I've updated the files below to account for that.

<?php
use App\Exceptions\Handler;
use Illuminate\Contracts\Debug\ExceptionHandler;
class TestCase extends Illuminate\Foundation\Testing\TestCase
{
// Framework-supplied test case methods snipped for brevity
// Use this version if you're on PHP 7
protected function disableExceptionHandling()
{
$this->app->instance(ExceptionHandler::class, new class extends Handler {
public function __construct() {}
public function report(Exception $e)
{
// no-op
}
public function render($request, Exception $e) {
throw $e;
}
});
}
}
<?php
use App\Exceptions\Handler;
use Illuminate\Contracts\Debug\ExceptionHandler;
class TestCase extends Illuminate\Foundation\Testing\TestCase
{
// Framework-supplied test case methods snipped for brevity
// Use this version if you're on PHP 5
protected function disableExceptionHandling()
{
app()->instance(ExceptionHandler::class, new PassThroughHandler);
}
}
class PassThroughHandler extends Handler
{
public function __construct() {}
public function report(Exception $e)
{
// no-op
}
public function render($request, Exception $e)
{
throw $e;
}
}

Thanks for this snippet, it makes the error in some cases way more verbose. There is only 1 but: You need to import the Exception namespace as it will throw an error that it is not compatible with the parent ExceptionHandler.

Thanks for snippet. Awesome 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment