1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace DelOlmo\Middleware; |
6
|
|
|
|
7
|
|
|
use Middlewares\Utils\Dispatcher; |
8
|
|
|
use Middlewares\Utils\Factory; |
9
|
|
|
use PHPUnit\Framework\TestCase; |
10
|
|
|
use Psr\Http\Message\ResponseFactoryInterface; |
11
|
|
|
use ReflectionProperty; |
12
|
|
|
use Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory; |
13
|
|
|
use Symfony\Component\Config\Loader\LoaderInterface; |
14
|
|
|
use Symfony\Component\Routing\Exception\NoConfigurationException; |
15
|
|
|
use Symfony\Component\Routing\Exception\ResourceNotFoundException; |
16
|
|
|
use Symfony\Component\Routing\Matcher\RequestMatcherInterface; |
17
|
|
|
use Symfony\Component\Routing\Matcher\UrlMatcher; |
18
|
|
|
use Symfony\Component\Routing\RequestContext; |
19
|
|
|
use Symfony\Component\Routing\Route; |
20
|
|
|
use Symfony\Component\Routing\RouteCollection; |
21
|
|
|
use Symfony\Component\Routing\Router; |
22
|
|
|
|
23
|
|
|
class SymfonyRouterMiddlewareTest extends TestCase |
24
|
|
|
{ |
25
|
|
|
private RouteCollection $routes; |
26
|
|
|
|
27
|
|
|
private Router $router; |
28
|
|
|
|
29
|
|
|
private LoaderInterface $loader; |
30
|
|
|
|
31
|
|
|
protected function setUp() : void |
32
|
|
|
{ |
33
|
|
|
$this->loader = $this->getMockBuilder(LoaderInterface::class)->getMock(); |
34
|
|
|
$this->router = new Router($this->loader, 'routing.yml'); |
35
|
|
|
|
36
|
|
|
$this->routes = new RouteCollection(); |
37
|
|
|
$this->routes->add('test', new Route('/users', [], [], [], '', [], ['GET'])); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function testNonDefaultResponseFactory() : void |
41
|
|
|
{ |
42
|
|
|
$factory = $this->createMock(ResponseFactoryInterface::class); |
43
|
|
|
|
44
|
|
|
$factory |
45
|
|
|
->expects(self::once()) |
46
|
|
|
->method('createResponse') |
47
|
|
|
->with(404); |
48
|
|
|
|
49
|
|
|
$context = $this->createMock(RequestContext::class); |
50
|
|
|
|
51
|
|
|
$router = $this->createMock(Router::class); |
52
|
|
|
|
53
|
|
|
$router |
54
|
|
|
->expects(self::once()) |
55
|
|
|
->method('getContext') |
56
|
|
|
->willReturn($context); |
57
|
|
|
|
58
|
|
|
$request = Factory::createServerRequest('GET', '/'); |
59
|
|
|
|
60
|
|
|
$symfonyRequest = (new HttpFoundationFactory()) |
61
|
|
|
->createRequest($request); |
62
|
|
|
|
63
|
|
|
$context |
64
|
|
|
->expects(self::once()) |
65
|
|
|
->method('fromRequest') |
66
|
|
|
->with($symfonyRequest); |
67
|
|
|
|
68
|
|
|
$router |
69
|
|
|
->expects(self::once()) |
70
|
|
|
->method('matchRequest') |
71
|
|
|
->with($symfonyRequest) |
72
|
|
|
->will(self::throwException(new ResourceNotFoundException())); |
73
|
|
|
|
74
|
|
|
$middleware = new SymfonyRouterMiddleware($router, $factory); |
75
|
|
|
|
76
|
|
|
Dispatcher::run([$middleware], $request); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function testRequestContextBeingUpdated() : void |
80
|
|
|
{ |
81
|
|
|
$context = $this->createMock(RequestContext::class); |
82
|
|
|
|
83
|
|
|
$router = $this->createMock(Router::class); |
84
|
|
|
|
85
|
|
|
$router |
86
|
|
|
->expects(self::once()) |
87
|
|
|
->method('getContext') |
88
|
|
|
->willReturn($context); |
89
|
|
|
|
90
|
|
|
$request = Factory::createServerRequest('GET', '/posts'); |
91
|
|
|
|
92
|
|
|
$symfonyRequest = (new HttpFoundationFactory()) |
93
|
|
|
->createRequest($request); |
94
|
|
|
|
95
|
|
|
$context |
96
|
|
|
->expects(self::once()) |
97
|
|
|
->method('fromRequest') |
98
|
|
|
->with($symfonyRequest); |
99
|
|
|
|
100
|
|
|
$router |
101
|
|
|
->expects(self::once()) |
102
|
|
|
->method('matchRequest') |
103
|
|
|
->with($symfonyRequest) |
104
|
|
|
->willReturn([]); |
105
|
|
|
|
106
|
|
|
$middleware = new SymfonyRouterMiddleware($router); |
107
|
|
|
|
108
|
|
|
Dispatcher::run([$middleware], $request); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function testResourceNotFoundException() : void |
112
|
|
|
{ |
113
|
|
|
$request = Factory::createServerRequest('GET', '/posts'); |
114
|
|
|
|
115
|
|
|
$factory = new HttpFoundationFactory(); |
116
|
|
|
|
117
|
|
|
$symfonyRequest = $factory->createRequest($request); |
118
|
|
|
|
119
|
|
|
$context = (new RequestContext()) |
120
|
|
|
->fromRequest($symfonyRequest); |
121
|
|
|
|
122
|
|
|
$matcher = new UrlMatcher($this->routes, $context); |
123
|
|
|
|
124
|
|
|
$p = new ReflectionProperty($this->router, 'matcher'); |
125
|
|
|
|
126
|
|
|
$p->setAccessible(true); |
127
|
|
|
|
128
|
|
|
$p->setValue($this->router, $matcher); |
129
|
|
|
|
130
|
|
|
$middleware = new SymfonyRouterMiddleware($this->router); |
131
|
|
|
|
132
|
|
|
$response = Dispatcher::run([$middleware], $request); |
133
|
|
|
|
134
|
|
|
self::assertSame(404, $response->getStatusCode()); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function testMethodNotAllowedException() : void |
138
|
|
|
{ |
139
|
|
|
$request = Factory::createServerRequest('POST', '/users'); |
140
|
|
|
|
141
|
|
|
$factory = new HttpFoundationFactory(); |
142
|
|
|
|
143
|
|
|
$symfonyRequest = $factory->createRequest($request); |
144
|
|
|
|
145
|
|
|
$context = (new RequestContext()) |
146
|
|
|
->fromRequest($symfonyRequest); |
147
|
|
|
|
148
|
|
|
$matcher = new UrlMatcher($this->routes, $context); |
149
|
|
|
|
150
|
|
|
$p = new ReflectionProperty($this->router, 'matcher'); |
151
|
|
|
|
152
|
|
|
$p->setAccessible(true); |
153
|
|
|
|
154
|
|
|
$p->setValue($this->router, $matcher); |
155
|
|
|
|
156
|
|
|
$middleware = new SymfonyRouterMiddleware($this->router); |
157
|
|
|
|
158
|
|
|
$response = Dispatcher::run([$middleware], $request); |
159
|
|
|
|
160
|
|
|
self::assertSame(405, $response->getStatusCode()); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
public function testNoConfigurationException() : void |
164
|
|
|
{ |
165
|
|
|
$request = Factory::createServerRequest('POST', '/users'); |
166
|
|
|
|
167
|
|
|
$matcher = $this->createMock(RequestMatcherInterface::class); |
168
|
|
|
|
169
|
|
|
$matcher->method('matchRequest') |
170
|
|
|
->will(self::throwException(new NoConfigurationException())); |
171
|
|
|
|
172
|
|
|
$p = new ReflectionProperty($this->router, 'matcher'); |
173
|
|
|
|
174
|
|
|
$p->setAccessible(true); |
175
|
|
|
|
176
|
|
|
$p->setValue($this->router, $matcher); |
177
|
|
|
|
178
|
|
|
$middleware = new SymfonyRouterMiddleware($this->router); |
179
|
|
|
|
180
|
|
|
$response = Dispatcher::run([$middleware], $request); |
181
|
|
|
|
182
|
|
|
self::assertSame(500, $response->getStatusCode()); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
public function testRouteMatched() : void |
186
|
|
|
{ |
187
|
|
|
$request = Factory::createServerRequest('GET', '/users'); |
188
|
|
|
|
189
|
|
|
$factory = new HttpFoundationFactory(); |
190
|
|
|
|
191
|
|
|
$symfonyRequest = $factory->createRequest($request); |
192
|
|
|
|
193
|
|
|
$context = (new RequestContext()) |
194
|
|
|
->fromRequest($symfonyRequest); |
195
|
|
|
|
196
|
|
|
$matcher = new UrlMatcher($this->routes, $context); |
197
|
|
|
|
198
|
|
|
$p = new ReflectionProperty($this->router, 'matcher'); |
199
|
|
|
|
200
|
|
|
$p->setAccessible(true); |
201
|
|
|
|
202
|
|
|
$p->setValue($this->router, $matcher); |
203
|
|
|
|
204
|
|
|
$middleware = new SymfonyRouterMiddleware($this->router); |
205
|
|
|
|
206
|
|
|
$dummyFn = static function ($request) : void { |
207
|
|
|
echo $request->getAttribute('_route'); |
208
|
|
|
}; |
209
|
|
|
|
210
|
|
|
$response = Dispatcher::run([$middleware, $dummyFn], $request); |
211
|
|
|
|
212
|
|
|
self::assertSame('test', (string) $response->getBody()); |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
|