diff --git a/src/Concerns/InteractsWithExceptionHandling.php b/src/Concerns/InteractsWithExceptionHandling.php index 3a75c00..3397407 100644 --- a/src/Concerns/InteractsWithExceptionHandling.php +++ b/src/Concerns/InteractsWithExceptionHandling.php @@ -48,6 +48,11 @@ public function report(Exception $e) { } + public function shouldReport(Exception $e) + { + return false; + } + public function render($request, Exception $e) { if ($e instanceof NotFoundHttpException) { diff --git a/tests/Stubs/ExceptionHandlerStub.php b/tests/Stubs/ExceptionHandlerStub.php new file mode 100644 index 0000000..535698f --- /dev/null +++ b/tests/Stubs/ExceptionHandlerStub.php @@ -0,0 +1,40 @@ +method()} {$request->url()}", null, $e->getCode() + ); + } + + throw $e; + } + + public function renderForConsole($output, Exception $e) + { + (new ConsoleApplication)->renderException($e, $output); + } +} diff --git a/tests/Stubs/OutputStub.php b/tests/Stubs/OutputStub.php new file mode 100644 index 0000000..7a35ec1 --- /dev/null +++ b/tests/Stubs/OutputStub.php @@ -0,0 +1,22 @@ +app = new Application(); + $this->previousExceptionHandler = "MyExceptionHandler"; + $this->withExceptionHandling(); + $this->assertEquals( + app(ExceptionHandler::class), + $this->previousExceptionHandler + ); + } + + /** + * @test + */ + public function withoutExceptionHandling_disable_exception_handling_for_the_test() + { + $this->app = new Application(); + $this->app->instance(ExceptionHandler::class, new ExceptionHandlerStub()); + $this->assertNull($this->previousExceptionHandler); + $this->withoutExceptionHandling(); + $this->assertInstanceOf( + ExceptionHandler::class, + $this->previousExceptionHandler + ); + } + + /** + * @test + */ + public function withExceptionHandling_throw_exception_NotFoundHttpException() + { + $this->expectException(NotFoundHttpException::class); + $this->expectExceptionMessage('Abort 404'); + $this->app = new Application(); + $this->app->instance(ExceptionHandler::class, new class {}); + + $this->withoutExceptionHandling(); + abort(404, 'Abort 404'); + } + + + /** + * @test + */ + public function report_of_instance_ExceptionHandler_on_Application_does_nothing() + { + $this->app = new Application(); + $this->app->instance(ExceptionHandler::class, new class {}); + + $this->withoutExceptionHandling(); + $this->assertNull(app(ExceptionHandler::class)->report(new Exception)); + } + + /** + * @test + */ + public function render_of_instance_ExceptionHandler_on_Application_throw_exception_NotFoundHttpException() + { + $this->expectException(NotFoundHttpException::class); + $this->expectExceptionMessage('GET http://localhost'); + + $this->app = new Application(); + $this->app->instance(ExceptionHandler::class, new class {}); + + $request = new class { + public function method() { return "GET"; } + public function url() { return "http://localhost"; } + public function getCode() { return 404; } + }; + + $this->withoutExceptionHandling(); + app(ExceptionHandler::class)->render($request, new NotFoundHttpException); + } + + /** + * @test + */ + public function render_of_instance_ExceptionHandler_on_Application_throw_exception_anyone() + { + $this->expectException(Exception::class); + $this->expectExceptionMessage('My Exception'); + + $this->app = new Application(); + $this->app->instance(ExceptionHandler::class, new class {}); + + $request = new class {}; + + $this->withoutExceptionHandling(); + + app(ExceptionHandler::class)->render($request, new Exception('My Exception')); + } + + /** + * @test + */ + public function renderForConsole_throw_exception_to_console_and_does_nothing() + { + $this->app = new Application(); + $this->app->instance(ExceptionHandler::class, new class {}); + $output = new OutputStub; + $this->withoutExceptionHandling(); + + $this->assertNull( + app(ExceptionHandler::class) + ->renderForConsole($output, new Exception) + ); + } +} diff --git a/tests/Unit/InteractsWithSessionTest.php b/tests/Unit/InteractsWithSessionTest.php index d14026c..cecbafd 100644 --- a/tests/Unit/InteractsWithSessionTest.php +++ b/tests/Unit/InteractsWithSessionTest.php @@ -2,8 +2,9 @@ namespace Laravel\BrowserKitTesting\Tests\Unit; -use Laravel\BrowserKitTesting\Concerns\InteractsWithSession; +use Illuminate\Foundation\Application; use Laravel\BrowserKitTesting\Tests\TestCase; +use Laravel\BrowserKitTesting\Concerns\InteractsWithSession; class InteractsWithSessionTest extends TestCase { @@ -160,6 +161,23 @@ public function has($key) { return true; } $this->assertSessionHasErrors(['foo', 'bar']); } + /** + * @test + */ + public function check_if_exists_errors_with_value_on_session() + { + $this->app = new Application(); + $this->app['session.store'] = new class { + public function get($key) { + return new class { + public function get($key) { return ['bar']; } + }; + } + public function has($key) { return true; } + }; + $this->assertSessionHasErrors(['foo' => 'bar']); + } + /** * @test */