|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Mouf\Mvc\Splash\Routers; |
|
4
|
|
|
|
|
5
|
|
|
use Cache\Adapter\PHPArray\ArrayCachePool; |
|
6
|
|
|
use Doctrine\Common\Annotations\AnnotationReader; |
|
7
|
|
|
use Doctrine\Common\Annotations\AnnotationRegistry; |
|
8
|
|
|
use Mouf\Mvc\Splash\Controllers\HttpErrorsController; |
|
9
|
|
|
use Mouf\Mvc\Splash\Exception\PageNotFoundException; |
|
10
|
|
|
use Mouf\Mvc\Splash\Exception\SplashMissingParameterException; |
|
11
|
|
|
use Mouf\Mvc\Splash\Fixtures\TestController2; |
|
12
|
|
|
use Mouf\Mvc\Splash\Fixtures\TestFilteredController; |
|
13
|
|
|
use Mouf\Mvc\Splash\Services\ControllerAnalyzer; |
|
14
|
|
|
use Mouf\Mvc\Splash\Services\ControllerRegistry; |
|
15
|
|
|
use Mouf\Mvc\Splash\Services\ParameterFetcherRegistry; |
|
16
|
|
|
use Mouf\Mvc\Splash\Services\SplashUtils; |
|
17
|
|
|
use Mouf\Mvc\Splash\Utils\SplashException; |
|
18
|
|
|
use Mouf\Picotainer\Picotainer; |
|
19
|
|
|
use Psr\Cache\CacheItemPoolInterface; |
|
20
|
|
|
use Zend\Diactoros\Response\HtmlResponse; |
|
21
|
|
|
use Zend\Diactoros\Response\JsonResponse; |
|
22
|
|
|
use Zend\Diactoros\Response\RedirectResponse; |
|
23
|
|
|
use Zend\Diactoros\ServerRequest; |
|
24
|
|
|
use Mouf\Mvc\Splash\Fixtures\TestController3; |
|
25
|
|
|
|
|
26
|
|
|
class SplashDefaultRouterTest extends \PHPUnit_Framework_TestCase |
|
27
|
|
|
{ |
|
28
|
|
|
protected function setUp() |
|
29
|
|
|
{ |
|
30
|
|
|
$loader = require __DIR__.'../../../../../../vendor/autoload.php'; |
|
31
|
|
|
AnnotationRegistry::registerLoader(array($loader, 'loadClass')); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function testRoute() |
|
35
|
|
|
{ |
|
36
|
|
|
$container = new Picotainer([ |
|
37
|
|
|
'controller' => function () { |
|
38
|
|
|
return new TestController2(); |
|
39
|
|
|
}, |
|
40
|
|
|
]); |
|
41
|
|
|
$parameterFetcherRegistry = ParameterFetcherRegistry::buildDefaultControllerRegistry(); |
|
42
|
|
|
$controllerAnalyzer = new ControllerAnalyzer($container, $parameterFetcherRegistry, new AnnotationReader()); |
|
43
|
|
|
$controllerRegistry = new ControllerRegistry($controllerAnalyzer, ['controller']); |
|
44
|
|
|
$defaultRouter = new SplashDefaultRouter($container, [ |
|
45
|
|
|
$controllerRegistry, |
|
46
|
|
|
], $parameterFetcherRegistry); |
|
47
|
|
|
|
|
48
|
|
|
$request = new ServerRequest([], [], '/foo/var/bar', 'GET', 'php://input', |
|
49
|
|
|
[], |
|
50
|
|
|
[], |
|
51
|
|
|
['id' => 42] |
|
52
|
|
|
); |
|
53
|
|
|
$response = new HtmlResponse(''); |
|
54
|
|
|
$response = $defaultRouter($request, $response); |
|
55
|
|
|
$this->assertInstanceOf(JsonResponse::class, $response); |
|
56
|
|
|
/* @var $response JsonResponse */ |
|
57
|
|
|
$decodedResponse = json_decode((string) $response->getBody(), true); |
|
58
|
|
|
$this->assertEquals(42, $decodedResponse['id']); |
|
59
|
|
|
$this->assertEquals('var', $decodedResponse['var']); |
|
60
|
|
|
$this->assertEquals(42, $decodedResponse['id2']); |
|
61
|
|
|
$this->assertEquals(42, $decodedResponse['opt']); |
|
62
|
|
|
|
|
63
|
|
|
// Now, let's test the redirect |
|
64
|
|
|
$request = new ServerRequest([], [], '/foo/var/bar/', 'GET', 'php://input', |
|
65
|
|
|
[], |
|
66
|
|
|
[], |
|
67
|
|
|
['id' => 42] |
|
68
|
|
|
); |
|
69
|
|
|
$response = new HtmlResponse(''); |
|
70
|
|
|
$response = $defaultRouter($request, $response); |
|
71
|
|
|
$this->assertInstanceOf(RedirectResponse::class, $response); |
|
72
|
|
|
$this->assertEquals('/foo/var/bar', $response->getHeader('Location')[0]); |
|
73
|
|
|
|
|
74
|
|
|
// Now, let's test the second kind of redirect |
|
75
|
|
|
$request = new ServerRequest([], [], '/controller', 'GET'); |
|
76
|
|
|
$response = new HtmlResponse(''); |
|
77
|
|
|
$response = $defaultRouter($request, $response); |
|
78
|
|
|
$this->assertInstanceOf(RedirectResponse::class, $response); |
|
79
|
|
|
$this->assertEquals('/controller/', $response->getHeader('Location')[0]); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function testUnknownRoute() |
|
83
|
|
|
{ |
|
84
|
|
|
$container = new Picotainer([ |
|
85
|
|
|
]); |
|
86
|
|
|
$parameterFetcherRegistry = ParameterFetcherRegistry::buildDefaultControllerRegistry(); |
|
87
|
|
|
$defaultRouter = new SplashDefaultRouter($container, [], $parameterFetcherRegistry); |
|
88
|
|
|
|
|
89
|
|
|
$request = new ServerRequest([], [], '/foo', 'GET'); |
|
90
|
|
|
$response = new HtmlResponse(''); |
|
91
|
|
|
$response = $defaultRouter($request, $response, function () { |
|
92
|
|
|
return new HtmlResponse('Not found', 404); |
|
93
|
|
|
}); |
|
94
|
|
|
$this->assertInstanceOf(HtmlResponse::class, $response); |
|
95
|
|
|
/* @var $response HtmlResponse */ |
|
96
|
|
|
$this->assertEquals(404, $response->getStatusCode()); |
|
97
|
|
|
$this->assertEquals('Not found', (string) $response->getBody()); |
|
98
|
|
|
|
|
99
|
|
|
// Now, let's retry without a $out parameter and let's check we get an exception |
|
100
|
|
|
$this->expectException(PageNotFoundException::class); |
|
101
|
|
|
$response = $defaultRouter($request, $response); |
|
|
|
|
|
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
|
|
105
|
|
View Code Duplication |
public function testEmojiRoute() |
|
|
|
|
|
|
106
|
|
|
{ |
|
107
|
|
|
$container = new Picotainer([ |
|
108
|
|
|
'controller' => function () { |
|
109
|
|
|
return new TestController3(); |
|
110
|
|
|
}, |
|
111
|
|
|
]); |
|
112
|
|
|
$parameterFetcherRegistry = ParameterFetcherRegistry::buildDefaultControllerRegistry(); |
|
113
|
|
|
$controllerAnalyzer = new ControllerAnalyzer($container, $parameterFetcherRegistry, new AnnotationReader()); |
|
114
|
|
|
$controllerRegistry = new ControllerRegistry($controllerAnalyzer, ['controller']); |
|
115
|
|
|
$defaultRouter = new SplashDefaultRouter($container, [ |
|
116
|
|
|
$controllerRegistry, |
|
117
|
|
|
], $parameterFetcherRegistry); |
|
118
|
|
|
|
|
119
|
|
|
$request = new ServerRequest([], [], '/'.urlencode('🍕'), 'GET'); |
|
120
|
|
|
$response = new HtmlResponse(''); |
|
121
|
|
|
$response = $defaultRouter($request, $response); |
|
122
|
|
|
$this->assertInstanceOf(JsonResponse::class, $response); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
public function testUnknownRouteWith404Handler() |
|
126
|
|
|
{ |
|
127
|
|
|
$container = new Picotainer([ |
|
128
|
|
|
]); |
|
129
|
|
|
$parameterFetcherRegistry = ParameterFetcherRegistry::buildDefaultControllerRegistry(); |
|
130
|
|
|
$defaultRouter = new SplashDefaultRouter($container, [], $parameterFetcherRegistry); |
|
131
|
|
|
$errorsController = HttpErrorsController::createDefault(); |
|
132
|
|
|
$defaultRouter->setHttp404Handler($errorsController); |
|
133
|
|
|
|
|
134
|
|
|
$request = new ServerRequest([], [], '/foo', 'GET'); |
|
135
|
|
|
$response = new HtmlResponse(''); |
|
136
|
|
|
$response = $defaultRouter($request, $response); |
|
137
|
|
|
/* @var $response HtmlResponse */ |
|
138
|
|
|
|
|
139
|
|
|
// Now, let's retry without a $out parameter and let's check we get an exception |
|
140
|
|
|
$response = $defaultRouter($request, $response); |
|
141
|
|
|
$this->assertEquals(404, $response->getStatusCode()); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
public function testRootUrlError() |
|
145
|
|
|
{ |
|
146
|
|
|
$container = new Picotainer([ |
|
147
|
|
|
]); |
|
148
|
|
|
$parameterFetcherRegistry = ParameterFetcherRegistry::buildDefaultControllerRegistry(); |
|
149
|
|
|
$defaultRouter = new SplashDefaultRouter($container, [], $parameterFetcherRegistry, null, null, SplashUtils::MODE_STRICT, true, '/baseUrl/'); |
|
150
|
|
|
|
|
151
|
|
|
$request = new ServerRequest([], [], '/foo', 'GET'); |
|
152
|
|
|
$response = new HtmlResponse(''); |
|
153
|
|
|
$this->expectException(SplashException::class); |
|
154
|
|
|
$response = $defaultRouter($request, $response); |
|
|
|
|
|
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
View Code Duplication |
public function testMissingCompulsoryParameter() |
|
|
|
|
|
|
158
|
|
|
{ |
|
159
|
|
|
$container = new Picotainer([ |
|
160
|
|
|
'controller' => function () { |
|
161
|
|
|
return new TestController2(); |
|
162
|
|
|
}, |
|
163
|
|
|
]); |
|
164
|
|
|
$parameterFetcherRegistry = ParameterFetcherRegistry::buildDefaultControllerRegistry(); |
|
165
|
|
|
$controllerAnalyzer = new ControllerAnalyzer($container, $parameterFetcherRegistry, new AnnotationReader()); |
|
166
|
|
|
$controllerRegistry = new ControllerRegistry($controllerAnalyzer, ['controller']); |
|
167
|
|
|
$defaultRouter = new SplashDefaultRouter($container, [ |
|
168
|
|
|
$controllerRegistry, |
|
169
|
|
|
], $parameterFetcherRegistry); |
|
170
|
|
|
|
|
171
|
|
|
// We need an ID parameter |
|
172
|
|
|
$request = new ServerRequest([], [], '/foo/var/bar', 'GET'); |
|
173
|
|
|
$response = new HtmlResponse(''); |
|
174
|
|
|
$this->expectException(SplashMissingParameterException::class); |
|
175
|
|
|
$response = $defaultRouter($request, $response); |
|
|
|
|
|
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
View Code Duplication |
public function testMissingCompulsoryParameterWithHandler() |
|
|
|
|
|
|
179
|
|
|
{ |
|
180
|
|
|
$container = new Picotainer([ |
|
181
|
|
|
'controller' => function () { |
|
182
|
|
|
return new TestController2(); |
|
183
|
|
|
}, |
|
184
|
|
|
]); |
|
185
|
|
|
$parameterFetcherRegistry = ParameterFetcherRegistry::buildDefaultControllerRegistry(); |
|
186
|
|
|
$controllerAnalyzer = new ControllerAnalyzer($container, $parameterFetcherRegistry, new AnnotationReader()); |
|
187
|
|
|
$controllerRegistry = new ControllerRegistry($controllerAnalyzer, ['controller']); |
|
188
|
|
|
$defaultRouter = new SplashDefaultRouter($container, [ |
|
189
|
|
|
$controllerRegistry, |
|
190
|
|
|
], $parameterFetcherRegistry); |
|
191
|
|
|
|
|
192
|
|
|
$errorsController = HttpErrorsController::createDefault(); |
|
193
|
|
|
$defaultRouter->setHttp400Handler($errorsController); |
|
194
|
|
|
|
|
195
|
|
|
// We need an ID parameter |
|
196
|
|
|
$request = new ServerRequest([], [], '/foo/var/bar', 'GET'); |
|
197
|
|
|
$response = new HtmlResponse(''); |
|
198
|
|
|
$response = $defaultRouter($request, $response); |
|
199
|
|
|
$this->assertEquals(400, $response->getStatusCode()); |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
View Code Duplication |
public function testExceptionWithHandler() |
|
|
|
|
|
|
203
|
|
|
{ |
|
204
|
|
|
$container = new Picotainer([ |
|
205
|
|
|
'controller' => function () { |
|
206
|
|
|
return new TestController2(); |
|
207
|
|
|
}, |
|
208
|
|
|
]); |
|
209
|
|
|
$parameterFetcherRegistry = ParameterFetcherRegistry::buildDefaultControllerRegistry(); |
|
210
|
|
|
$controllerAnalyzer = new ControllerAnalyzer($container, $parameterFetcherRegistry, new AnnotationReader()); |
|
211
|
|
|
$controllerRegistry = new ControllerRegistry($controllerAnalyzer, ['controller']); |
|
212
|
|
|
$defaultRouter = new SplashDefaultRouter($container, [ |
|
213
|
|
|
$controllerRegistry, |
|
214
|
|
|
], $parameterFetcherRegistry); |
|
215
|
|
|
|
|
216
|
|
|
$errorsController = HttpErrorsController::createDefault(); |
|
217
|
|
|
$defaultRouter->setHttp500Handler($errorsController); |
|
218
|
|
|
|
|
219
|
|
|
// We need an ID parameter |
|
220
|
|
|
$request = new ServerRequest([], [], '/controller/triggerException', 'GET'); |
|
221
|
|
|
$response = new HtmlResponse(''); |
|
222
|
|
|
$response = $defaultRouter($request, $response); |
|
223
|
|
|
$this->assertEquals(500, $response->getStatusCode()); |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
public function testPurgeUrlCache() |
|
227
|
|
|
{ |
|
228
|
|
|
$cache = $this->prophesize(CacheItemPoolInterface::class); |
|
229
|
|
|
$cache->deleteItem('splashUrlNodes')->shouldBeCalled(); |
|
230
|
|
|
|
|
231
|
|
|
$container = new Picotainer([]); |
|
232
|
|
|
$parameterFetcherRegistry = ParameterFetcherRegistry::buildDefaultControllerRegistry(); |
|
233
|
|
|
$defaultRouter = new SplashDefaultRouter($container, [], $parameterFetcherRegistry, $cache->reveal()); |
|
234
|
|
|
$defaultRouter->purgeUrlsCache(); |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
View Code Duplication |
public function testFilters() |
|
|
|
|
|
|
238
|
|
|
{ |
|
239
|
|
|
$container = new Picotainer([ |
|
240
|
|
|
'controller' => function () { |
|
241
|
|
|
return new TestFilteredController(); |
|
242
|
|
|
}, |
|
243
|
|
|
]); |
|
244
|
|
|
$parameterFetcherRegistry = ParameterFetcherRegistry::buildDefaultControllerRegistry(); |
|
245
|
|
|
$controllerAnalyzer = new ControllerAnalyzer($container, $parameterFetcherRegistry, new AnnotationReader()); |
|
246
|
|
|
$controllerRegistry = new ControllerRegistry($controllerAnalyzer, ['controller']); |
|
247
|
|
|
$defaultRouter = new SplashDefaultRouter($container, [ |
|
248
|
|
|
$controllerRegistry, |
|
249
|
|
|
], $parameterFetcherRegistry); |
|
250
|
|
|
|
|
251
|
|
|
$request = new ServerRequest([], [], '/foo', 'GET'); |
|
252
|
|
|
$response = new HtmlResponse(''); |
|
253
|
|
|
$response = $defaultRouter($request, $response); |
|
254
|
|
|
$this->assertEquals('42bar', (string) $response->getBody()); |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
public function testExpirationTag() |
|
258
|
|
|
{ |
|
259
|
|
|
$container = new Picotainer([ |
|
260
|
|
|
'controller' => function () { |
|
261
|
|
|
return new TestController2(); |
|
262
|
|
|
}, |
|
263
|
|
|
]); |
|
264
|
|
|
$parameterFetcherRegistry = ParameterFetcherRegistry::buildDefaultControllerRegistry(); |
|
265
|
|
|
$controllerAnalyzer = new ControllerAnalyzer($container, $parameterFetcherRegistry, new AnnotationReader()); |
|
266
|
|
|
$controllerRegistry = new ControllerRegistry($controllerAnalyzer, ['controller']); |
|
267
|
|
|
$defaultRouter = new SplashDefaultRouter($container, [ |
|
268
|
|
|
$controllerRegistry, |
|
269
|
|
|
], $parameterFetcherRegistry, new ArrayCachePool()); |
|
270
|
|
|
|
|
271
|
|
|
$request = new ServerRequest([], [], '/foo/var/bar', 'GET', 'php://input', |
|
272
|
|
|
[], |
|
273
|
|
|
[], |
|
274
|
|
|
['id' => 42] |
|
275
|
|
|
); |
|
276
|
|
|
$response = new HtmlResponse(''); |
|
277
|
|
|
$response = $defaultRouter($request, $response); |
|
278
|
|
|
$this->assertInstanceOf(JsonResponse::class, $response); |
|
279
|
|
|
|
|
280
|
|
|
// Now, let's make another request (this time, we should go through the cache with unchanged etag) |
|
281
|
|
|
$response2 = $defaultRouter($request, $response); |
|
282
|
|
|
$this->assertInstanceOf(JsonResponse::class, $response2); |
|
283
|
|
|
} |
|
284
|
|
|
} |
|
285
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.