Skip to content

Commit dbb6a71

Browse files
authored
Clean up routing middleware
1 parent 2f558e3 commit dbb6a71

7 files changed

Lines changed: 68 additions & 16 deletions

File tree

Slim/Interfaces/RouteInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public function getArguments(): array;
7070
/**
7171
* Set route arguments.
7272
*
73-
* @param array<string,mixed> $arguments The arguments.
73+
* @param array<string,string> $arguments The arguments.
7474
*
7575
* @return RouteInterface
7676
*/

Slim/Middleware/BasePathMiddleware.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,6 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
4545
$basePath = $this->getBasePathByRequestUri($request);
4646
}
4747

48-
// $request = $request->withAttribute(RouteMatch::BASE_PATH_ATTRIBUTE, $basePath);
49-
5048
$this->router->setBasePath($basePath);
5149

5250
return $handler->handle($request);

Slim/Middleware/RoutingMiddleware.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,14 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
4242
{
4343
$requestPath = $request->getUri()->getPath();
4444
$basePath = $this->router->getBasePath();
45-
$dispatchPath = $this->stripBasePath($requestPath, $this->router->getBasePath());
45+
$dispatchPath = $this->stripBasePath($requestPath, $basePath);
4646

4747
$routingResult = $this->dispatcher->dispatch(
4848
$request->getMethod(),
4949
rawurldecode($dispatchPath)
5050
);
5151

52-
$routeMatch = $this->createRouteMatch($routingResult, $basePath);
52+
$routeMatch = $this->createRouteMatch($routingResult);
5353
$request = $request->withAttribute(RouteMatch::class, $routeMatch);
5454

5555
return $handler->handle($request);
@@ -58,7 +58,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
5858
/**
5959
* @param array<int, mixed> $routingResult
6060
*/
61-
private function createRouteMatch(array $routingResult, string $basePath): RouteMatch
61+
private function createRouteMatch(array $routingResult): RouteMatch
6262
{
6363
$status = $routingResult[0] ?? null;
6464

Slim/Routing/Route.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
use Slim\Interfaces\MiddlewareCollectionInterface;
1212
use Slim\Interfaces\RouteInterface;
1313

14+
use function array_key_exists;
15+
1416
final class Route implements RouteInterface, MiddlewareCollectionInterface
1517
{
1618
use MiddlewareCollectionTrait;
@@ -45,7 +47,7 @@ final class Route implements RouteInterface, MiddlewareCollectionInterface
4547
*
4648
* @var array<string, string>
4749
*/
48-
private array $arguments;
50+
private array $arguments = [];
4951

5052
/**
5153
* @param array<string> $methods

tests/Middleware/RoutingMiddlewareTest.php

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,19 @@
1313
use PHPUnit\Framework\TestCase;
1414
use Psr\Http\Message\ResponseInterface;
1515
use Psr\Http\Message\ServerRequestInterface;
16+
use Psr\Http\Message\UriInterface;
1617
use Psr\Http\Server\RequestHandlerInterface;
18+
use RuntimeException;
1719
use Slim\Exception\HttpMethodNotAllowedException;
1820
use Slim\Exception\HttpNotFoundException;
1921
use Slim\Factory\AppFactory;
2022
use Slim\Interfaces\DispatcherInterface;
23+
use Slim\Interfaces\RouterInterface;
2124
use Slim\Interfaces\UrlGeneratorInterface;
2225
use Slim\Middleware\EndpointMiddleware;
2326
use Slim\Middleware\JsonBodyParserMiddleware;
2427
use Slim\Middleware\RoutingMiddleware;
2528
use Slim\Routing\RouteMatch;
26-
use Slim\Routing\RoutingResults;
2729
use Slim\Tests\Traits\AppTestTrait;
2830

2931
final class RoutingMiddlewareTest extends TestCase
@@ -97,7 +99,7 @@ public function testRouteIsNotStoredOnMethodNotAllowed()
9799
} catch (HttpMethodNotAllowedException $exception) {
98100
$request = $exception->getRequest();
99101

100-
// routingResults is available
102+
// RouteMatch is available
101103
/** @var RouteMatch $routeMatch */
102104
$routeMatch = $request->getAttribute(RouteMatch::class);
103105
$test->assertSame(DispatcherInterface::METHOD_NOT_ALLOWED, $routeMatch->getStatus());
@@ -140,7 +142,7 @@ public function testRouteIsNotStoredOnNotFound()
140142
} catch (HttpNotFoundException $exception) {
141143
$request = $exception->getRequest();
142144

143-
// routingResults is available
145+
// RouteMatch is available
144146
$routeMatch = $request->getAttribute(RouteMatch::class);
145147
$test->assertSame(DispatcherInterface::NOT_FOUND, $routeMatch->getStatus());
146148

@@ -195,4 +197,41 @@ public function testRoutingWithBasePath(): void
195197
$this->assertSame('/api/users/123?page=2', $response->getHeaderLine('X-relativeUrlFor'));
196198
$this->assertSame('/api/users/123?page=2', $response->getHeaderLine('X-fullUrlFor'));
197199
}
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+
}
198237
}

tests/Routing/RouteMatchTest.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,7 @@ public function testFoundRouteMatch(): void
4141

4242
public function testNotFoundRouteMatch(): void
4343
{
44-
$basePath = '/api';
45-
46-
$routeMatch = RouteMatch::notFound($basePath);
44+
$routeMatch = RouteMatch::notFound();
4745

4846
$this->assertFalse($routeMatch->isFound());
4947
$this->assertTrue($routeMatch->isNotFound());
@@ -57,9 +55,7 @@ public function testNotFoundRouteMatch(): void
5755
public function testMethodNotAllowedRouteMatch(): void
5856
{
5957
$allowedMethods = ['GET', 'POST'];
60-
$basePath = '/api';
61-
62-
$routeMatch = RouteMatch::methodNotAllowed($allowedMethods, $basePath);
58+
$routeMatch = RouteMatch::methodNotAllowed($allowedMethods);
6359

6460
$this->assertFalse($routeMatch->isFound());
6561
$this->assertFalse($routeMatch->isNotFound());

tests/Routing/RouteTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,23 @@ public function testGetMethodsReturnsCorrectMethods(): void
137137
$this->assertSame($methods, $route->getMethods());
138138
}
139139

140+
public function testSetArgumentsStoresStringArguments(): void
141+
{
142+
$methods = ['GET'];
143+
$pattern = '/users/{id}';
144+
$handler = function () {
145+
return 'handler';
146+
};
147+
148+
$route = new Route($methods, $pattern, $handler);
149+
150+
$arguments = ['id' => '123', 'slug' => 'john-doe'];
151+
$route->setArguments($arguments);
152+
153+
$this->assertSame($arguments, $route->getArguments());
154+
$this->assertSame('123', $route->getArgument('id'));
155+
}
156+
140157
private function createMiddleware(): MiddlewareInterface
141158
{
142159
return new class implements MiddlewareInterface {

0 commit comments

Comments
 (0)