-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
RoutingMiddlewareTest.php
189 lines (160 loc) · 7.94 KB
/
RoutingMiddlewareTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php
/**
* Slim Framework (https://slimframework.com)
*
* @license https://github.com/slimphp/Slim/blob/4.x/LICENSE.md (MIT License)
*/
declare(strict_types=1);
namespace Slim\Tests\Middleware;
use FastRoute\Dispatcher;
use Prophecy\Argument;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Slim\CallableResolver;
use Slim\Exception\HttpMethodNotAllowedException;
use Slim\Exception\HttpNotFoundException;
use Slim\Interfaces\RouteParserInterface;
use Slim\Interfaces\RouteResolverInterface;
use Slim\Middleware\RoutingMiddleware;
use Slim\Routing\RouteCollector;
use Slim\Routing\RouteParser;
use Slim\Routing\RouteResolver;
use Slim\Routing\RoutingResults;
use Slim\Tests\TestCase;
class RoutingMiddlewareTest extends TestCase
{
protected function getRouteCollector()
{
$callableResolver = new CallableResolver();
$responseFactory = $this->getResponseFactory();
$routeCollector = new RouteCollector($responseFactory, $callableResolver);
$routeCollector->map(['GET'], '/hello/{name}', null);
return $routeCollector;
}
public function testRouteIsStoredOnSuccessfulMatch()
{
$responseFactory = $this->getResponseFactory();
$mw = (function (ServerRequestInterface $request) use ($responseFactory) {
// route is available
$route = $request->getAttribute('route');
$this->assertNotNull($route);
$this->assertEquals('foo', $route->getArgument('name'));
// routeParser is available
$routeParser = $request->getAttribute('routeParser');
$this->assertNotNull($routeParser);
$this->assertInstanceOf(RouteParserInterface::class, $routeParser);
// routingResults is available
$routingResults = $request->getAttribute('routingResults');
$this->assertInstanceOf(RoutingResults::class, $routingResults);
return $responseFactory->createResponse();
})->bindTo($this);
$routeCollector = $this->getRouteCollector();
$routeParser = new RouteParser($routeCollector);
$routeResolver = new RouteResolver($routeCollector);
$mw2 = new RoutingMiddleware($routeResolver, $routeParser);
$request = $this->createServerRequest('https://example.com:443/hello/foo', 'GET');
$middlewareDispatcher = $this->createMiddlewareDispatcher(
$this->createMock(RequestHandlerInterface::class),
null
);
$middlewareDispatcher->addCallable($mw);
$middlewareDispatcher->addMiddleware($mw2);
$middlewareDispatcher->handle($request);
}
public function testRouteIsNotStoredOnMethodNotAllowed()
{
$routeCollector = $this->getRouteCollector();
$routeParser = new RouteParser($routeCollector);
$routeResolver = new RouteResolver($routeCollector);
$routingMiddleware = new RoutingMiddleware($routeResolver, $routeParser);
$request = $this->createServerRequest('https://example.com:443/hello/foo', 'POST');
$requestHandlerProphecy = $this->prophesize(RequestHandlerInterface::class);
/** @var RequestHandlerInterface $requestHandler */
$requestHandler = $requestHandlerProphecy->reveal();
$middlewareDispatcher = $this->createMiddlewareDispatcher($requestHandler, null);
$middlewareDispatcher->addMiddleware($routingMiddleware);
try {
$middlewareDispatcher->handle($request);
$this->fail('HTTP method should not have been allowed');
} catch (HttpMethodNotAllowedException $exception) {
$request = $exception->getRequest();
// route is not available
$route = $request->getAttribute('route');
$this->assertNull($route);
// routeParser is available
$routeParser = $request->getAttribute('routeParser');
$this->assertNotNull($routeParser);
$this->assertInstanceOf(RouteParserInterface::class, $routeParser);
// routingResults is available
$routingResults = $request->getAttribute('routingResults');
$this->assertInstanceOf(RoutingResults::class, $routingResults);
$this->assertEquals(Dispatcher::METHOD_NOT_ALLOWED, $routingResults->getRouteStatus());
}
}
public function testRouteIsNotStoredOnNotFound()
{
$routeCollector = $this->getRouteCollector();
$routeParser = new RouteParser($routeCollector);
$routeResolver = new RouteResolver($routeCollector);
$routingMiddleware = new RoutingMiddleware($routeResolver, $routeParser);
$request = $this->createServerRequest('https://example.com:443/goodbye', 'GET');
$requestHandlerProphecy = $this->prophesize(RequestHandlerInterface::class);
/** @var RequestHandlerInterface $requestHandler */
$requestHandler = $requestHandlerProphecy->reveal();
$middlewareDispatcher = $this->createMiddlewareDispatcher($requestHandler, null);
$middlewareDispatcher->addMiddleware($routingMiddleware);
try {
$middlewareDispatcher->handle($request);
$this->fail('HTTP route should not have been found');
} catch (HttpNotFoundException $exception) {
$request = $exception->getRequest();
// route is not available
$route = $request->getAttribute('route');
$this->assertNull($route);
// routeParser is available
$routeParser = $request->getAttribute('routeParser');
$this->assertNotNull($routeParser);
$this->assertInstanceOf(RouteParserInterface::class, $routeParser);
// routingResults is available
$routingResults = $request->getAttribute('routingResults');
$this->assertInstanceOf(RoutingResults::class, $routingResults);
$this->assertEquals(Dispatcher::NOT_FOUND, $routingResults->getRouteStatus());
}
}
/**
* @expectedException RuntimeException
* @expectedExceptionMessage An unexpected error occurred while performing routing.
*/
public function testPerformRoutingThrowsExceptionOnInvalidRoutingResultsRouteStatus()
{
// Prophesize the `RoutingResults` instance that would return an invalid route
// status when the method `getRouteStatus()` gets called.
$routingResultsProphecy = $this->prophesize(RoutingResults::class);
/** @noinspection PhpUndefinedMethodInspection */
$routingResultsProphecy->getRouteStatus()
->willReturn(-1)
->shouldBeCalledOnce();
/** @var RoutingResults $routingResults */
$routingResults = $routingResultsProphecy->reveal();
// Prophesize the `RouteParserInterface` instance will be created.
$routeParserProphecy = $this->prophesize(RouteParser::class);
/** @var RouteParserInterface $routeParser */
$routeParser = $routeParserProphecy->reveal();
// Prophesize the `RouteResolverInterface` that would return the `RoutingResults`
// defined above, when the method `computeRoutingResults()` gets called.
$routeResolverProphecy = $this->prophesize(RouteResolverInterface::class);
/** @noinspection PhpUndefinedMethodInspection */
$routeResolverProphecy->computeRoutingResults(Argument::any(), Argument::any())
->willReturn($routingResults)
->shouldBeCalled();
/** @var RouteResolverInterface $routeResolver */
$routeResolver = $routeResolverProphecy->reveal();
// Create the server request.
$request = $this->createServerRequest('https://example.com:443/hello/foo', 'GET');
// Create the routing middleware with the `RouteResolverInterface` defined
// above. Perform the routing, which should throw the RuntimeException.
$m = new RoutingMiddleware($routeResolver, $routeParser);
/** @noinspection PhpUnhandledExceptionInspection */
$m->performRouting($request);
}
}