Last active
April 2, 2023 12:44
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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. 🔢