aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/front/ShaarliMiddlewareTest.php
diff options
context:
space:
mode:
authorArthurHoaro <arthur@hoa.ro>2020-08-21 10:50:44 +0200
committerArthurHoaro <arthur@hoa.ro>2020-08-21 10:50:44 +0200
commit0c6fdbe12bbbb336348666b14b82096f24d5858b (patch)
tree8fad2829c55f94022e359fa8914e11f80a2afc2a /tests/front/ShaarliMiddlewareTest.php
parentbedbb845eec20363b928b424143787dbe988eefe (diff)
downloadShaarli-0c6fdbe12bbbb336348666b14b82096f24d5858b.tar.gz
Shaarli-0c6fdbe12bbbb336348666b14b82096f24d5858b.tar.zst
Shaarli-0c6fdbe12bbbb336348666b14b82096f24d5858b.zip
Move error handling to dedicated controller instead of middleware
Diffstat (limited to 'tests/front/ShaarliMiddlewareTest.php')
-rw-r--r--tests/front/ShaarliMiddlewareTest.php29
1 files changed, 14 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