Skip to content

Commit

Permalink
Add withoutExceptionHandling method for testing.
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Apr 10, 2017
1 parent d4ae7b8 commit a171f44
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace Illuminate\Foundation\Testing\Concerns;

use Exception;
use Illuminate\Contracts\Debug\ExceptionHandler;
use Symfony\Component\Console\Application as ConsoleApplication;

trait InteractsWithExceptionHandling
{
/**
* The previous exception handler.
*
* @var ExceptionHandler|null
*/
protected $previousExceptionHandler;

/**
* Restore exception handling.
*
* @return $this
*/
protected function withExceptionHandling()
{
if ($this->previousExceptionHandler) {
$this->app->instance(ExceptionHandler::class, $this->previousExceptionHandler);
}

return $this;
}

/**
* Disable exception handling for the test.
*
* @return $this
*/
protected function withoutExceptionHandling()
{
$this->previousExceptionHandler = app(ExceptionHandler::class);

$this->app->instance(ExceptionHandler::class, new class implements ExceptionHandler {
public function __construct() {}
public function report(Exception $e) {}
public function render($request, Exception $e) {
throw $e;
}
public function renderForConsole($output, Exception $e) {
(new ConsoleApplication)->renderException($e, $output);
}
});

return $this;
}
}
1 change: 1 addition & 0 deletions src/Illuminate/Foundation/Testing/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ abstract class TestCase extends BaseTestCase
Concerns\InteractsWithAuthentication,
Concerns\InteractsWithConsole,
Concerns\InteractsWithDatabase,
Concerns\InteractsWithExceptionHandling,
Concerns\InteractsWithSession,
Concerns\MocksApplicationServices;

Expand Down

0 comments on commit a171f44

Please sign in to comment.