|
13 | 13 | use PHPUnit\Framework\TestCase; |
14 | 14 | use Psr\Http\Message\ResponseInterface; |
15 | 15 | use Psr\Http\Message\ServerRequestInterface; |
| 16 | +use Psr\Http\Message\UriInterface; |
16 | 17 | use Psr\Http\Server\RequestHandlerInterface; |
| 18 | +use RuntimeException; |
17 | 19 | use Slim\Exception\HttpMethodNotAllowedException; |
18 | 20 | use Slim\Exception\HttpNotFoundException; |
19 | 21 | use Slim\Factory\AppFactory; |
20 | 22 | use Slim\Interfaces\DispatcherInterface; |
| 23 | +use Slim\Interfaces\RouterInterface; |
21 | 24 | use Slim\Interfaces\UrlGeneratorInterface; |
22 | 25 | use Slim\Middleware\EndpointMiddleware; |
23 | 26 | use Slim\Middleware\JsonBodyParserMiddleware; |
24 | 27 | use Slim\Middleware\RoutingMiddleware; |
25 | 28 | use Slim\Routing\RouteMatch; |
26 | | -use Slim\Routing\RoutingResults; |
27 | 29 | use Slim\Tests\Traits\AppTestTrait; |
28 | 30 |
|
29 | 31 | final class RoutingMiddlewareTest extends TestCase |
@@ -97,7 +99,7 @@ public function testRouteIsNotStoredOnMethodNotAllowed() |
97 | 99 | } catch (HttpMethodNotAllowedException $exception) { |
98 | 100 | $request = $exception->getRequest(); |
99 | 101 |
|
100 | | - // routingResults is available |
| 102 | + // RouteMatch is available |
101 | 103 | /** @var RouteMatch $routeMatch */ |
102 | 104 | $routeMatch = $request->getAttribute(RouteMatch::class); |
103 | 105 | $test->assertSame(DispatcherInterface::METHOD_NOT_ALLOWED, $routeMatch->getStatus()); |
@@ -140,7 +142,7 @@ public function testRouteIsNotStoredOnNotFound() |
140 | 142 | } catch (HttpNotFoundException $exception) { |
141 | 143 | $request = $exception->getRequest(); |
142 | 144 |
|
143 | | - // routingResults is available |
| 145 | + // RouteMatch is available |
144 | 146 | $routeMatch = $request->getAttribute(RouteMatch::class); |
145 | 147 | $test->assertSame(DispatcherInterface::NOT_FOUND, $routeMatch->getStatus()); |
146 | 148 |
|
@@ -195,4 +197,41 @@ public function testRoutingWithBasePath(): void |
195 | 197 | $this->assertSame('/api/users/123?page=2', $response->getHeaderLine('X-relativeUrlFor')); |
196 | 198 | $this->assertSame('/api/users/123?page=2', $response->getHeaderLine('X-fullUrlFor')); |
197 | 199 | } |
| 200 | + |
| 201 | + public function testMethodNotAllowedThrowsRuntimeExceptionWhenAllowedMethodsPayloadIsInvalid(): void |
| 202 | + { |
| 203 | + $dispatcher = $this->createMock(DispatcherInterface::class); |
| 204 | + $dispatcher |
| 205 | + ->method('dispatch') |
| 206 | + ->willReturn([ |
| 207 | + DispatcherInterface::METHOD_NOT_ALLOWED, |
| 208 | + 'GET', |
| 209 | + ]); |
| 210 | + |
| 211 | + $router = $this->createMock(RouterInterface::class); |
| 212 | + $router |
| 213 | + ->method('getBasePath') |
| 214 | + ->willReturn(''); |
| 215 | + |
| 216 | + $middleware = new RoutingMiddleware($dispatcher, $router); |
| 217 | + |
| 218 | + $request = $this->createMock(ServerRequestInterface::class); |
| 219 | + $uri = $this->createMock(UriInterface::class); |
| 220 | + $uri |
| 221 | + ->method('getPath') |
| 222 | + ->willReturn('/hello/foo'); |
| 223 | + $request |
| 224 | + ->method('getUri') |
| 225 | + ->willReturn($uri); |
| 226 | + $request |
| 227 | + ->method('getMethod') |
| 228 | + ->willReturn('GET'); |
| 229 | + |
| 230 | + $handler = $this->createMock(RequestHandlerInterface::class); |
| 231 | + |
| 232 | + $this->expectException(RuntimeException::class); |
| 233 | + $this->expectExceptionMessage('Dispatcher returned invalid allowed methods.'); |
| 234 | + |
| 235 | + $middleware->process($request, $handler); |
| 236 | + } |
198 | 237 | } |
0 commit comments