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 --- .../controller/visitor/ErrorControllerTest.php | 70 ++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 tests/front/controller/visitor/ErrorControllerTest.php (limited to 'tests/front/controller/visitor/ErrorControllerTest.php') 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 From a5a9cf23acd1248585173aa32757d9720b5f2d62 Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Tue, 29 Sep 2020 14:41:40 +0200 Subject: Compatibility with PHPUnit 9 --- tests/front/controller/visitor/ErrorControllerTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tests/front/controller/visitor/ErrorControllerTest.php') diff --git a/tests/front/controller/visitor/ErrorControllerTest.php b/tests/front/controller/visitor/ErrorControllerTest.php index e497bfef..75408cf4 100644 --- a/tests/front/controller/visitor/ErrorControllerTest.php +++ b/tests/front/controller/visitor/ErrorControllerTest.php @@ -4,8 +4,8 @@ declare(strict_types=1); namespace Shaarli\Front\Controller\Visitor; -use PHPUnit\Framework\TestCase; use Shaarli\Front\Exception\ShaarliFrontException; +use Shaarli\TestCase; use Slim\Http\Request; use Slim\Http\Response; -- cgit v1.2.3