diff options
author | ArthurHoaro <arthur@hoa.ro> | 2020-09-12 21:41:58 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-09-12 21:41:58 +0200 |
commit | 0d930454a2892715e691f9c7713e26a3bb4ee96c (patch) | |
tree | 770197f23a6ec512f54bd22578ec971f53408dc3 /tests/front | |
parent | 4af591ff3c5db4dea5b6c437527f6f9b12917570 (diff) | |
parent | d52ab0b1e99aa0c494f389092dce1e926296032d (diff) | |
download | Shaarli-0d930454a2892715e691f9c7713e26a3bb4ee96c.tar.gz Shaarli-0d930454a2892715e691f9c7713e26a3bb4ee96c.tar.zst Shaarli-0d930454a2892715e691f9c7713e26a3bb4ee96c.zip |
Merge pull request #1553 from ArthurHoaro/fix/404-page
Properly handle 404 errors
Diffstat (limited to 'tests/front')
-rw-r--r-- | tests/front/controller/visitor/ErrorNotFoundControllerTest.php | 81 | ||||
-rw-r--r-- | tests/front/controller/visitor/FrontControllerMockHelper.php | 1 |
2 files changed, 81 insertions, 1 deletions
diff --git a/tests/front/controller/visitor/ErrorNotFoundControllerTest.php b/tests/front/controller/visitor/ErrorNotFoundControllerTest.php new file mode 100644 index 00000000..625467b1 --- /dev/null +++ b/tests/front/controller/visitor/ErrorNotFoundControllerTest.php | |||
@@ -0,0 +1,81 @@ | |||
1 | <?php | ||
2 | |||
3 | declare(strict_types=1); | ||
4 | |||
5 | namespace Shaarli\Front\Controller\Visitor; | ||
6 | |||
7 | use PHPUnit\Framework\TestCase; | ||
8 | use Slim\Http\Request; | ||
9 | use Slim\Http\Response; | ||
10 | use Slim\Http\Uri; | ||
11 | |||
12 | class ErrorNotFoundControllerTest extends TestCase | ||
13 | { | ||
14 | use FrontControllerMockHelper; | ||
15 | |||
16 | /** @var ErrorNotFoundController */ | ||
17 | protected $controller; | ||
18 | |||
19 | public function setUp(): void | ||
20 | { | ||
21 | $this->createContainer(); | ||
22 | |||
23 | $this->controller = new ErrorNotFoundController($this->container); | ||
24 | } | ||
25 | |||
26 | /** | ||
27 | * Test displaying 404 error | ||
28 | */ | ||
29 | public function testDisplayNotFoundError(): void | ||
30 | { | ||
31 | $request = $this->createMock(Request::class); | ||
32 | $request->expects(static::once())->method('getRequestTarget')->willReturn('/'); | ||
33 | $request->method('getUri')->willReturnCallback(function (): Uri { | ||
34 | $uri = $this->createMock(Uri::class); | ||
35 | $uri->method('getBasePath')->willReturn('/subfolder'); | ||
36 | |||
37 | return $uri; | ||
38 | }); | ||
39 | |||
40 | $response = new Response(); | ||
41 | |||
42 | // Save RainTPL assigned variables | ||
43 | $assignedVariables = []; | ||
44 | $this->assignTemplateVars($assignedVariables); | ||
45 | |||
46 | $result = ($this->controller)( | ||
47 | $request, | ||
48 | $response | ||
49 | ); | ||
50 | |||
51 | static::assertSame(404, $result->getStatusCode()); | ||
52 | static::assertSame('404', (string) $result->getBody()); | ||
53 | static::assertSame('Requested page could not be found.', $assignedVariables['error_message']); | ||
54 | } | ||
55 | |||
56 | /** | ||
57 | * Test displaying 404 error from REST API | ||
58 | */ | ||
59 | public function testDisplayNotFoundErrorFromAPI(): void | ||
60 | { | ||
61 | $request = $this->createMock(Request::class); | ||
62 | $request->expects(static::once())->method('getRequestTarget')->willReturn('/sufolder/api/v1/links'); | ||
63 | $request->method('getUri')->willReturnCallback(function (): Uri { | ||
64 | $uri = $this->createMock(Uri::class); | ||
65 | $uri->method('getBasePath')->willReturn('/subfolder'); | ||
66 | |||
67 | return $uri; | ||
68 | }); | ||
69 | |||
70 | $response = new Response(); | ||
71 | |||
72 | // Save RainTPL assigned variables | ||
73 | $assignedVariables = []; | ||
74 | $this->assignTemplateVars($assignedVariables); | ||
75 | |||
76 | $result = ($this->controller)($request, $response); | ||
77 | |||
78 | static::assertSame(404, $result->getStatusCode()); | ||
79 | static::assertSame([], $assignedVariables); | ||
80 | } | ||
81 | } | ||
diff --git a/tests/front/controller/visitor/FrontControllerMockHelper.php b/tests/front/controller/visitor/FrontControllerMockHelper.php index e0bd4ecf..927e7f0a 100644 --- a/tests/front/controller/visitor/FrontControllerMockHelper.php +++ b/tests/front/controller/visitor/FrontControllerMockHelper.php | |||
@@ -94,7 +94,6 @@ trait FrontControllerMockHelper | |||
94 | protected function assignTemplateVars(array &$variables): void | 94 | protected function assignTemplateVars(array &$variables): void |
95 | { | 95 | { |
96 | $this->container->pageBuilder | 96 | $this->container->pageBuilder |
97 | ->expects(static::atLeastOnce()) | ||
98 | ->method('assign') | 97 | ->method('assign') |
99 | ->willReturnCallback(function ($key, $value) use (&$variables) { | 98 | ->willReturnCallback(function ($key, $value) use (&$variables) { |
100 | $variables[$key] = $value; | 99 | $variables[$key] = $value; |