aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/front
diff options
context:
space:
mode:
Diffstat (limited to 'tests/front')
-rw-r--r--tests/front/ShaarliMiddlewareTest.php29
-rw-r--r--tests/front/controller/visitor/ErrorControllerTest.php70
2 files changed, 84 insertions, 15 deletions
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
74 } 74 }
75 75
76 /** 76 /**
77 * Test middleware execution with controller throwing a known front exception 77 * Test middleware execution with controller throwing a known front exception.
78 * The exception should be thrown to be later handled by the error handler.
78 */ 79 */
79 public function testMiddlewareExecutionWithFrontException(): void 80 public function testMiddlewareExecutionWithFrontException(): void
80 { 81 {
@@ -99,16 +100,14 @@ class ShaarliMiddlewareTest extends TestCase
99 }); 100 });
100 $this->container->pageBuilder = $pageBuilder; 101 $this->container->pageBuilder = $pageBuilder;
101 102
102 /** @var Response $result */ 103 $this->expectException(LoginBannedException::class);
103 $result = $this->middleware->__invoke($request, $response, $controller);
104 104
105 static::assertInstanceOf(Response::class, $result); 105 $this->middleware->__invoke($request, $response, $controller);
106 static::assertSame(401, $result->getStatusCode());
107 static::assertContains('error', (string) $result->getBody());
108 } 106 }
109 107
110 /** 108 /**
111 * Test middleware execution with controller throwing a not authorized exception 109 * Test middleware execution with controller throwing a not authorized exception
110 * The middle should send a redirection response to the login page.
112 */ 111 */
113 public function testMiddlewareExecutionWithUnauthorizedException(): void 112 public function testMiddlewareExecutionWithUnauthorizedException(): void
114 { 113 {
@@ -136,9 +135,10 @@ class ShaarliMiddlewareTest extends TestCase
136 } 135 }
137 136
138 /** 137 /**
139 * Test middleware execution with controller throwing a not authorized exception 138 * Test middleware execution with controller throwing a not authorized exception.
139 * The exception should be thrown to be later handled by the error handler.
140 */ 140 */
141 public function testMiddlewareExecutionWithServerExceptionWith(): void 141 public function testMiddlewareExecutionWithServerException(): void
142 { 142 {
143 $request = $this->createMock(Request::class); 143 $request = $this->createMock(Request::class);
144 $request->method('getUri')->willReturnCallback(function (): Uri { 144 $request->method('getUri')->willReturnCallback(function (): Uri {
@@ -148,9 +148,11 @@ class ShaarliMiddlewareTest extends TestCase
148 return $uri; 148 return $uri;
149 }); 149 });
150 150
151 $dummyException = new class() extends \Exception {};
152
151 $response = new Response(); 153 $response = new Response();
152 $controller = function (): void { 154 $controller = function () use ($dummyException): void {
153 throw new \Exception(); 155 throw $dummyException;
154 }; 156 };
155 157
156 $parameters = []; 158 $parameters = [];
@@ -165,12 +167,9 @@ class ShaarliMiddlewareTest extends TestCase
165 }) 167 })
166 ; 168 ;
167 169
168 /** @var Response $result */ 170 $this->expectException(get_class($dummyException));
169 $result = $this->middleware->__invoke($request, $response, $controller);
170 171
171 static::assertSame(500, $result->getStatusCode()); 172 $this->middleware->__invoke($request, $response, $controller);
172 static::assertContains('error', (string) $result->getBody());
173 static::assertSame('An unexpected error occurred.', $parameters['message']);
174 } 173 }
175 174
176 public function testMiddlewareExecutionWithUpdates(): void 175 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 @@
1<?php
2
3declare(strict_types=1);
4
5namespace Shaarli\Front\Controller\Visitor;
6
7use PHPUnit\Framework\TestCase;
8use Shaarli\Front\Exception\ShaarliFrontException;
9use Slim\Http\Request;
10use Slim\Http\Response;
11
12class 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}