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