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