Skip to content

Instantly share code, notes, and snippets.

@tuto1902
Created July 25, 2017 04:09
Show Gist options
  • Save tuto1902/bb36fbe62533227f3380e08543137dc6 to your computer and use it in GitHub Desktop.
Save tuto1902/bb36fbe62533227f3380e08543137dc6 to your computer and use it in GitHub Desktop.
Laravel TestCase base class with exception handling helper methods
<?php
namespace Tests;
use Exception;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
use App\Exceptions\Handler;
use Illuminate\Contracts\Debug\ExceptionHandler;
abstract class TestCase extends BaseTestCase
{
use CreatesApplication;
protected function setUp ()
{
parent::setUp();
$this->disableExceptionHandling();
}
protected function disableExceptionHandling ()
{
$this->oldExceptionHandler = app()->make(ExceptionHandler::class);
app()->instance(ExceptionHandler::class, new PassThroughHandler);
}
protected function withExceptionHandling ()
{
app()->instance(ExceptionHandler::class, $this->oldExceptionHandler);
return $this;
}
}
class PassThroughHandler 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