diff options
author | ArthurHoaro <arthur@hoa.ro> | 2020-08-21 10:50:44 +0200 |
---|---|---|
committer | ArthurHoaro <arthur@hoa.ro> | 2020-08-21 10:50:44 +0200 |
commit | 0c6fdbe12bbbb336348666b14b82096f24d5858b (patch) | |
tree | 8fad2829c55f94022e359fa8914e11f80a2afc2a /tests/front/controller/visitor | |
parent | bedbb845eec20363b928b424143787dbe988eefe (diff) | |
download | Shaarli-0c6fdbe12bbbb336348666b14b82096f24d5858b.tar.gz Shaarli-0c6fdbe12bbbb336348666b14b82096f24d5858b.tar.zst Shaarli-0c6fdbe12bbbb336348666b14b82096f24d5858b.zip |
Move error handling to dedicated controller instead of middleware
Diffstat (limited to 'tests/front/controller/visitor')
-rw-r--r-- | tests/front/controller/visitor/ErrorControllerTest.php | 70 |
1 files changed, 70 insertions, 0 deletions
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 @@ | |||
1 | <?php | ||
2 | |||
3 | declare(strict_types=1); | ||
4 | |||
5 | namespace Shaarli\Front\Controller\Visitor; | ||
6 | |||
7 | use PHPUnit\Framework\TestCase; | ||
8 | use Shaarli\Front\Exception\ShaarliFrontException; | ||
9 | use Slim\Http\Request; | ||
10 | use Slim\Http\Response; | ||
11 | |||
12 | class ErrorControllerTest extends TestCase | ||
13 | { | ||
14 | use FrontControllerMockHelper; | ||
15 | |||
16 | /** @var ErrorController */ | ||
17 | protected $controller; | ||
18 | |||
19 | public function setUp(): void | ||
20 | { | ||
21 | $this->createContainer(); | ||
22 | |||
23 | $this->controller = new ErrorController($this->container); | ||
24 | } | ||
25 | |||
26 | /** | ||
27 | * Test displaying error with a ShaarliFrontException: display exception message and use its code for HTTTP code | ||
28 | */ | ||
29 | public function testDisplayFrontExceptionError(): void | ||
30 | { | ||
31 | $request = $this->createMock(Request::class); | ||
32 | $response = new Response(); | ||
33 | |||
34 | $message = 'error message'; | ||
35 | $errorCode = 418; | ||
36 | |||
37 | // Save RainTPL assigned variables | ||
38 | $assignedVariables = []; | ||
39 | $this->assignTemplateVars($assignedVariables); | ||
40 | |||
41 | $result = ($this->controller)( | ||
42 | $request, | ||
43 | $response, | ||
44 | new class($message, $errorCode) extends ShaarliFrontException {} | ||
45 | ); | ||
46 | |||
47 | static::assertSame($errorCode, $result->getStatusCode()); | ||
48 | static::assertSame($message, $assignedVariables['message']); | ||
49 | static::assertArrayNotHasKey('stacktrace', $assignedVariables); | ||
50 | } | ||
51 | |||
52 | /** | ||
53 | * Test displaying error with any exception (no debug): only display an error occurred with HTTP 500. | ||
54 | */ | ||
55 | public function testDisplayAnyExceptionErrorNoDebug(): void | ||
56 | { | ||
57 | $request = $this->createMock(Request::class); | ||
58 | $response = new Response(); | ||
59 | |||
60 | // Save RainTPL assigned variables | ||
61 | $assignedVariables = []; | ||
62 | $this->assignTemplateVars($assignedVariables); | ||
63 | |||
64 | $result = ($this->controller)($request, $response, new \Exception('abc')); | ||
65 | |||
66 | static::assertSame(500, $result->getStatusCode()); | ||
67 | static::assertSame('An unexpected error occurred.', $assignedVariables['message']); | ||
68 | static::assertArrayNotHasKey('stacktrace', $assignedVariables); | ||
69 | } | ||
70 | } | ||