From 0c6fdbe12bbbb336348666b14b82096f24d5858b Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Fri, 21 Aug 2020 10:50:44 +0200 Subject: Move error handling to dedicated controller instead of middleware --- tests/front/ShaarliMiddlewareTest.php | 29 +++++---- .../controller/visitor/ErrorControllerTest.php | 70 ++++++++++++++++++++++ 2 files changed, 84 insertions(+), 15 deletions(-) create mode 100644 tests/front/controller/visitor/ErrorControllerTest.php (limited to 'tests/front') diff --git a/tests/front/ShaarliMiddlewareTest.php b/tests/front/ShaarliMiddlewareTest.php index d435f506..05aa34a9 100644 --- a/tests/front/ShaarliMiddlewareTest.php +++ b/tests/front/ShaarliMiddlewareTest.php @@ -74,7 +74,8 @@ class ShaarliMiddlewareTest extends TestCase } /** - * Test middleware execution with controller throwing a known front exception + * Test middleware execution with controller throwing a known front exception. + * The exception should be thrown to be later handled by the error handler. */ public function testMiddlewareExecutionWithFrontException(): void { @@ -99,16 +100,14 @@ class ShaarliMiddlewareTest extends TestCase }); $this->container->pageBuilder = $pageBuilder; - /** @var Response $result */ - $result = $this->middleware->__invoke($request, $response, $controller); + $this->expectException(LoginBannedException::class); - static::assertInstanceOf(Response::class, $result); - static::assertSame(401, $result->getStatusCode()); - static::assertContains('error', (string) $result->getBody()); + $this->middleware->__invoke($request, $response, $controller); } /** * Test middleware execution with controller throwing a not authorized exception + * The middle should send a redirection response to the login page. */ public function testMiddlewareExecutionWithUnauthorizedException(): void { @@ -136,9 +135,10 @@ class ShaarliMiddlewareTest extends TestCase } /** - * Test middleware execution with controller throwing a not authorized exception + * Test middleware execution with controller throwing a not authorized exception. + * The exception should be thrown to be later handled by the error handler. */ - public function testMiddlewareExecutionWithServerExceptionWith(): void + public function testMiddlewareExecutionWithServerException(): void { $request = $this->createMock(Request::class); $request->method('getUri')->willReturnCallback(function (): Uri { @@ -148,9 +148,11 @@ class ShaarliMiddlewareTest extends TestCase return $uri; }); + $dummyException = new class() extends \Exception {}; + $response = new Response(); - $controller = function (): void { - throw new \Exception(); + $controller = function () use ($dummyException): void { + throw $dummyException; }; $parameters = []; @@ -165,12 +167,9 @@ class ShaarliMiddlewareTest extends TestCase }) ; - /** @var Response $result */ - $result = $this->middleware->__invoke($request, $response, $controller); + $this->expectException(get_class($dummyException)); - static::assertSame(500, $result->getStatusCode()); - static::assertContains('error', (string) $result->getBody()); - static::assertSame('An unexpected error occurred.', $parameters['message']); + $this->middleware->__invoke($request, $response, $controller); } public function testMiddlewareExecutionWithUpdates(): void diff --git a/tests/front/controller/visitor/ErrorControllerTest.php b/tests/front/controller/visitor/ErrorControllerTest.php new file mode 100644 index 00000000..e497bfef --- /dev/null +++ b/tests/front/controller/visitor/ErrorControllerTest.php @@ -0,0 +1,70 @@ +createContainer(); + + $this->controller = new ErrorController($this->container); + } + + /** + * Test displaying error with a ShaarliFrontException: display exception message and use its code for HTTTP code + */ + public function testDisplayFrontExceptionError(): void + { + $request = $this->createMock(Request::class); + $response = new Response(); + + $message = 'error message'; + $errorCode = 418; + + // Save RainTPL assigned variables + $assignedVariables = []; + $this->assignTemplateVars($assignedVariables); + + $result = ($this->controller)( + $request, + $response, + new class($message, $errorCode) extends ShaarliFrontException {} + ); + + static::assertSame($errorCode, $result->getStatusCode()); + static::assertSame($message, $assignedVariables['message']); + static::assertArrayNotHasKey('stacktrace', $assignedVariables); + } + + /** + * Test displaying error with any exception (no debug): only display an error occurred with HTTP 500. + */ + public function testDisplayAnyExceptionErrorNoDebug(): void + { + $request = $this->createMock(Request::class); + $response = new Response(); + + // Save RainTPL assigned variables + $assignedVariables = []; + $this->assignTemplateVars($assignedVariables); + + $result = ($this->controller)($request, $response, new \Exception('abc')); + + static::assertSame(500, $result->getStatusCode()); + static::assertSame('An unexpected error occurred.', $assignedVariables['message']); + static::assertArrayNotHasKey('stacktrace', $assignedVariables); + } +} -- cgit v1.2.3