X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=tests%2Ffront%2Fcontroller%2Fvisitor%2FErrorControllerTest.php;fp=tests%2Ffront%2Fcontroller%2Fvisitor%2FErrorControllerTest.php;h=e497bfefb874bfcac6206b32292e785480b1c7b2;hb=0c6fdbe12bbbb336348666b14b82096f24d5858b;hp=0000000000000000000000000000000000000000;hpb=bedbb845eec20363b928b424143787dbe988eefe;p=github%2Fshaarli%2FShaarli.git 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); + } +}