Skip to content

Instantly share code, notes, and snippets.

@adamtomat
Last active April 2, 2023 12:44
Show Gist options
  • Save adamtomat/177adf1f9878ddd6fdd581367bbee7a4 to your computer and use it in GitHub Desktop.
Save adamtomat/177adf1f9878ddd6fdd581367bbee7a4 to your computer and use it in GitHub Desktop.
Laravel 5.4 - Disable Exception Handling for tests, with reminder to enable it when green
<?php
namespace Tests;
use App\Exceptions\Handler;
use Exception;
use Illuminate\Contracts\Debug\ExceptionHandler;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
abstract class TestCase extends BaseTestCase
{
use CreatesApplication;
private $isExceptionHandlingDisabled = false;
protected function tearDown()
{
parent::tearDown();
// Reminder to remove $this->disableExceptionHandling() from your test once it passes.
// Marks the test as incomplete until it's removed.
if ($this->isExceptionHandlingDisabled) {
$this->markTestIncomplete('Exception handling is disabled.');
}
}
protected function disableExceptionHandling()
{
$this->isExceptionHandlingDisabled = true;
// Shout out to Adam Wathan (@adamwathan) for this
$this->app->instance(ExceptionHandler::class, new class extends Handler {
public function __construct() {}
public function report(Exception $e) {}
public function render($request, Exception $e) {
throw $e;
}
});
}
}
@CamKem
Copy link

CamKem commented Apr 2, 2023

Is this needed for Laravel 10? I am doing Interia / Vue / Laravel tests & following an old series of Jeffreys on Laracast.
However, I feel that doing the tests doesn't require exception handling. 🔢

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