diff options
Diffstat (limited to 'vendor/symfony/routing/Symfony/Component/Routing/Tests/Generator/UrlGeneratorTest.php')
-rw-r--r-- | vendor/symfony/routing/Symfony/Component/Routing/Tests/Generator/UrlGeneratorTest.php | 635 |
1 files changed, 635 insertions, 0 deletions
diff --git a/vendor/symfony/routing/Symfony/Component/Routing/Tests/Generator/UrlGeneratorTest.php b/vendor/symfony/routing/Symfony/Component/Routing/Tests/Generator/UrlGeneratorTest.php new file mode 100644 index 00000000..5f8ef491 --- /dev/null +++ b/vendor/symfony/routing/Symfony/Component/Routing/Tests/Generator/UrlGeneratorTest.php | |||
@@ -0,0 +1,635 @@ | |||
1 | <?php | ||
2 | |||
3 | /* | ||
4 | * This file is part of the Symfony package. | ||
5 | * | ||
6 | * (c) Fabien Potencier <fabien@symfony.com> | ||
7 | * | ||
8 | * For the full copyright and license information, please view the LICENSE | ||
9 | * file that was distributed with this source code. | ||
10 | */ | ||
11 | |||
12 | namespace Symfony\Component\Routing\Tests\Generator; | ||
13 | |||
14 | use Symfony\Component\Routing\RouteCollection; | ||
15 | use Symfony\Component\Routing\Route; | ||
16 | use Symfony\Component\Routing\Generator\UrlGenerator; | ||
17 | use Symfony\Component\Routing\Generator\UrlGeneratorInterface; | ||
18 | use Symfony\Component\Routing\RequestContext; | ||
19 | |||
20 | class UrlGeneratorTest extends \PHPUnit_Framework_TestCase | ||
21 | { | ||
22 | public function testAbsoluteUrlWithPort80() | ||
23 | { | ||
24 | $routes = $this->getRoutes('test', new Route('/testing')); | ||
25 | $url = $this->getGenerator($routes)->generate('test', array(), true); | ||
26 | |||
27 | $this->assertEquals('http://localhost/app.php/testing', $url); | ||
28 | } | ||
29 | |||
30 | public function testAbsoluteSecureUrlWithPort443() | ||
31 | { | ||
32 | $routes = $this->getRoutes('test', new Route('/testing')); | ||
33 | $url = $this->getGenerator($routes, array('scheme' => 'https'))->generate('test', array(), true); | ||
34 | |||
35 | $this->assertEquals('https://localhost/app.php/testing', $url); | ||
36 | } | ||
37 | |||
38 | public function testAbsoluteUrlWithNonStandardPort() | ||
39 | { | ||
40 | $routes = $this->getRoutes('test', new Route('/testing')); | ||
41 | $url = $this->getGenerator($routes, array('httpPort' => 8080))->generate('test', array(), true); | ||
42 | |||
43 | $this->assertEquals('http://localhost:8080/app.php/testing', $url); | ||
44 | } | ||
45 | |||
46 | public function testAbsoluteSecureUrlWithNonStandardPort() | ||
47 | { | ||
48 | $routes = $this->getRoutes('test', new Route('/testing')); | ||
49 | $url = $this->getGenerator($routes, array('httpsPort' => 8080, 'scheme' => 'https'))->generate('test', array(), true); | ||
50 | |||
51 | $this->assertEquals('https://localhost:8080/app.php/testing', $url); | ||
52 | } | ||
53 | |||
54 | public function testRelativeUrlWithoutParameters() | ||
55 | { | ||
56 | $routes = $this->getRoutes('test', new Route('/testing')); | ||
57 | $url = $this->getGenerator($routes)->generate('test', array(), false); | ||
58 | |||
59 | $this->assertEquals('/app.php/testing', $url); | ||
60 | } | ||
61 | |||
62 | public function testRelativeUrlWithParameter() | ||
63 | { | ||
64 | $routes = $this->getRoutes('test', new Route('/testing/{foo}')); | ||
65 | $url = $this->getGenerator($routes)->generate('test', array('foo' => 'bar'), false); | ||
66 | |||
67 | $this->assertEquals('/app.php/testing/bar', $url); | ||
68 | } | ||
69 | |||
70 | public function testRelativeUrlWithNullParameter() | ||
71 | { | ||
72 | $routes = $this->getRoutes('test', new Route('/testing.{format}', array('format' => null))); | ||
73 | $url = $this->getGenerator($routes)->generate('test', array(), false); | ||
74 | |||
75 | $this->assertEquals('/app.php/testing', $url); | ||
76 | } | ||
77 | |||
78 | /** | ||
79 | * @expectedException \Symfony\Component\Routing\Exception\InvalidParameterException | ||
80 | */ | ||
81 | public function testRelativeUrlWithNullParameterButNotOptional() | ||
82 | { | ||
83 | $routes = $this->getRoutes('test', new Route('/testing/{foo}/bar', array('foo' => null))); | ||
84 | // This must raise an exception because the default requirement for "foo" is "[^/]+" which is not met with these params. | ||
85 | // Generating path "/testing//bar" would be wrong as matching this route would fail. | ||
86 | $this->getGenerator($routes)->generate('test', array(), false); | ||
87 | } | ||
88 | |||
89 | public function testRelativeUrlWithOptionalZeroParameter() | ||
90 | { | ||
91 | $routes = $this->getRoutes('test', new Route('/testing/{page}')); | ||
92 | $url = $this->getGenerator($routes)->generate('test', array('page' => 0), false); | ||
93 | |||
94 | $this->assertEquals('/app.php/testing/0', $url); | ||
95 | } | ||
96 | |||
97 | public function testNotPassedOptionalParameterInBetween() | ||
98 | { | ||
99 | $routes = $this->getRoutes('test', new Route('/{slug}/{page}', array('slug' => 'index', 'page' => 0))); | ||
100 | $this->assertSame('/app.php/index/1', $this->getGenerator($routes)->generate('test', array('page' => 1))); | ||
101 | $this->assertSame('/app.php/', $this->getGenerator($routes)->generate('test')); | ||
102 | } | ||
103 | |||
104 | public function testRelativeUrlWithExtraParameters() | ||
105 | { | ||
106 | $routes = $this->getRoutes('test', new Route('/testing')); | ||
107 | $url = $this->getGenerator($routes)->generate('test', array('foo' => 'bar'), false); | ||
108 | |||
109 | $this->assertEquals('/app.php/testing?foo=bar', $url); | ||
110 | } | ||
111 | |||
112 | public function testAbsoluteUrlWithExtraParameters() | ||
113 | { | ||
114 | $routes = $this->getRoutes('test', new Route('/testing')); | ||
115 | $url = $this->getGenerator($routes)->generate('test', array('foo' => 'bar'), true); | ||
116 | |||
117 | $this->assertEquals('http://localhost/app.php/testing?foo=bar', $url); | ||
118 | } | ||
119 | |||
120 | public function testUrlWithNullExtraParameters() | ||
121 | { | ||
122 | $routes = $this->getRoutes('test', new Route('/testing')); | ||
123 | $url = $this->getGenerator($routes)->generate('test', array('foo' => null), true); | ||
124 | |||
125 | $this->assertEquals('http://localhost/app.php/testing', $url); | ||
126 | } | ||
127 | |||
128 | public function testUrlWithExtraParametersFromGlobals() | ||
129 | { | ||
130 | $routes = $this->getRoutes('test', new Route('/testing')); | ||
131 | $generator = $this->getGenerator($routes); | ||
132 | $context = new RequestContext('/app.php'); | ||
133 | $context->setParameter('bar', 'bar'); | ||
134 | $generator->setContext($context); | ||
135 | $url = $generator->generate('test', array('foo' => 'bar')); | ||
136 | |||
137 | $this->assertEquals('/app.php/testing?foo=bar', $url); | ||
138 | } | ||
139 | |||
140 | public function testUrlWithGlobalParameter() | ||
141 | { | ||
142 | $routes = $this->getRoutes('test', new Route('/testing/{foo}')); | ||
143 | $generator = $this->getGenerator($routes); | ||
144 | $context = new RequestContext('/app.php'); | ||
145 | $context->setParameter('foo', 'bar'); | ||
146 | $generator->setContext($context); | ||
147 | $url = $generator->generate('test', array()); | ||
148 | |||
149 | $this->assertEquals('/app.php/testing/bar', $url); | ||
150 | } | ||
151 | |||
152 | public function testGlobalParameterHasHigherPriorityThanDefault() | ||
153 | { | ||
154 | $routes = $this->getRoutes('test', new Route('/{_locale}', array('_locale' => 'en'))); | ||
155 | $generator = $this->getGenerator($routes); | ||
156 | $context = new RequestContext('/app.php'); | ||
157 | $context->setParameter('_locale', 'de'); | ||
158 | $generator->setContext($context); | ||
159 | $url = $generator->generate('test', array()); | ||
160 | |||
161 | $this->assertSame('/app.php/de', $url); | ||
162 | } | ||
163 | |||
164 | /** | ||
165 | * @expectedException \Symfony\Component\Routing\Exception\RouteNotFoundException | ||
166 | */ | ||
167 | public function testGenerateWithoutRoutes() | ||
168 | { | ||
169 | $routes = $this->getRoutes('foo', new Route('/testing/{foo}')); | ||
170 | $this->getGenerator($routes)->generate('test', array(), true); | ||
171 | } | ||
172 | |||
173 | /** | ||
174 | * @expectedException \Symfony\Component\Routing\Exception\MissingMandatoryParametersException | ||
175 | */ | ||
176 | public function testGenerateForRouteWithoutMandatoryParameter() | ||
177 | { | ||
178 | $routes = $this->getRoutes('test', new Route('/testing/{foo}')); | ||
179 | $this->getGenerator($routes)->generate('test', array(), true); | ||
180 | } | ||
181 | |||
182 | /** | ||
183 | * @expectedException \Symfony\Component\Routing\Exception\InvalidParameterException | ||
184 | */ | ||
185 | public function testGenerateForRouteWithInvalidOptionalParameter() | ||
186 | { | ||
187 | $routes = $this->getRoutes('test', new Route('/testing/{foo}', array('foo' => '1'), array('foo' => 'd+'))); | ||
188 | $this->getGenerator($routes)->generate('test', array('foo' => 'bar'), true); | ||
189 | } | ||
190 | |||
191 | /** | ||
192 | * @expectedException \Symfony\Component\Routing\Exception\InvalidParameterException | ||
193 | */ | ||
194 | public function testGenerateForRouteWithInvalidParameter() | ||
195 | { | ||
196 | $routes = $this->getRoutes('test', new Route('/testing/{foo}', array(), array('foo' => '1|2'))); | ||
197 | $this->getGenerator($routes)->generate('test', array('foo' => '0'), true); | ||
198 | } | ||
199 | |||
200 | public function testGenerateForRouteWithInvalidOptionalParameterNonStrict() | ||
201 | { | ||
202 | $routes = $this->getRoutes('test', new Route('/testing/{foo}', array('foo' => '1'), array('foo' => 'd+'))); | ||
203 | $generator = $this->getGenerator($routes); | ||
204 | $generator->setStrictRequirements(false); | ||
205 | $this->assertNull($generator->generate('test', array('foo' => 'bar'), true)); | ||
206 | } | ||
207 | |||
208 | public function testGenerateForRouteWithInvalidOptionalParameterNonStrictWithLogger() | ||
209 | { | ||
210 | if (!interface_exists('Psr\Log\LoggerInterface')) { | ||
211 | $this->markTestSkipped('The "psr/log" package is not available'); | ||
212 | } | ||
213 | |||
214 | $routes = $this->getRoutes('test', new Route('/testing/{foo}', array('foo' => '1'), array('foo' => 'd+'))); | ||
215 | $logger = $this->getMock('Psr\Log\LoggerInterface'); | ||
216 | $logger->expects($this->once()) | ||
217 | ->method('error'); | ||
218 | $generator = $this->getGenerator($routes, array(), $logger); | ||
219 | $generator->setStrictRequirements(false); | ||
220 | $this->assertNull($generator->generate('test', array('foo' => 'bar'), true)); | ||
221 | } | ||
222 | |||
223 | public function testGenerateForRouteWithInvalidParameterButDisabledRequirementsCheck() | ||
224 | { | ||
225 | $routes = $this->getRoutes('test', new Route('/testing/{foo}', array('foo' => '1'), array('foo' => 'd+'))); | ||
226 | $generator = $this->getGenerator($routes); | ||
227 | $generator->setStrictRequirements(null); | ||
228 | $this->assertSame('/app.php/testing/bar', $generator->generate('test', array('foo' => 'bar'))); | ||
229 | } | ||
230 | |||
231 | /** | ||
232 | * @expectedException \Symfony\Component\Routing\Exception\InvalidParameterException | ||
233 | */ | ||
234 | public function testGenerateForRouteWithInvalidMandatoryParameter() | ||
235 | { | ||
236 | $routes = $this->getRoutes('test', new Route('/testing/{foo}', array(), array('foo' => 'd+'))); | ||
237 | $this->getGenerator($routes)->generate('test', array('foo' => 'bar'), true); | ||
238 | } | ||
239 | |||
240 | /** | ||
241 | * @expectedException \Symfony\Component\Routing\Exception\InvalidParameterException | ||
242 | */ | ||
243 | public function testRequiredParamAndEmptyPassed() | ||
244 | { | ||
245 | $routes = $this->getRoutes('test', new Route('/{slug}', array(), array('slug' => '.+'))); | ||
246 | $this->getGenerator($routes)->generate('test', array('slug' => '')); | ||
247 | } | ||
248 | |||
249 | public function testSchemeRequirementDoesNothingIfSameCurrentScheme() | ||
250 | { | ||
251 | $routes = $this->getRoutes('test', new Route('/', array(), array('_scheme' => 'http'))); | ||
252 | $this->assertEquals('/app.php/', $this->getGenerator($routes)->generate('test')); | ||
253 | |||
254 | $routes = $this->getRoutes('test', new Route('/', array(), array('_scheme' => 'https'))); | ||
255 | $this->assertEquals('/app.php/', $this->getGenerator($routes, array('scheme' => 'https'))->generate('test')); | ||
256 | } | ||
257 | |||
258 | public function testSchemeRequirementForcesAbsoluteUrl() | ||
259 | { | ||
260 | $routes = $this->getRoutes('test', new Route('/', array(), array('_scheme' => 'https'))); | ||
261 | $this->assertEquals('https://localhost/app.php/', $this->getGenerator($routes)->generate('test')); | ||
262 | |||
263 | $routes = $this->getRoutes('test', new Route('/', array(), array('_scheme' => 'http'))); | ||
264 | $this->assertEquals('http://localhost/app.php/', $this->getGenerator($routes, array('scheme' => 'https'))->generate('test')); | ||
265 | } | ||
266 | |||
267 | public function testPathWithTwoStartingSlashes() | ||
268 | { | ||
269 | $routes = $this->getRoutes('test', new Route('//path-and-not-domain')); | ||
270 | |||
271 | // this must not generate '//path-and-not-domain' because that would be a network path | ||
272 | $this->assertSame('/path-and-not-domain', $this->getGenerator($routes, array('BaseUrl' => ''))->generate('test')); | ||
273 | } | ||
274 | |||
275 | public function testNoTrailingSlashForMultipleOptionalParameters() | ||
276 | { | ||
277 | $routes = $this->getRoutes('test', new Route('/category/{slug1}/{slug2}/{slug3}', array('slug2' => null, 'slug3' => null))); | ||
278 | |||
279 | $this->assertEquals('/app.php/category/foo', $this->getGenerator($routes)->generate('test', array('slug1' => 'foo'))); | ||
280 | } | ||
281 | |||
282 | public function testWithAnIntegerAsADefaultValue() | ||
283 | { | ||
284 | $routes = $this->getRoutes('test', new Route('/{default}', array('default' => 0))); | ||
285 | |||
286 | $this->assertEquals('/app.php/foo', $this->getGenerator($routes)->generate('test', array('default' => 'foo'))); | ||
287 | } | ||
288 | |||
289 | public function testNullForOptionalParameterIsIgnored() | ||
290 | { | ||
291 | $routes = $this->getRoutes('test', new Route('/test/{default}', array('default' => 0))); | ||
292 | |||
293 | $this->assertEquals('/app.php/test', $this->getGenerator($routes)->generate('test', array('default' => null))); | ||
294 | } | ||
295 | |||
296 | public function testQueryParamSameAsDefault() | ||
297 | { | ||
298 | $routes = $this->getRoutes('test', new Route('/test', array('default' => 'value'))); | ||
299 | |||
300 | $this->assertSame('/app.php/test', $this->getGenerator($routes)->generate('test', array('default' => 'foo'))); | ||
301 | $this->assertSame('/app.php/test', $this->getGenerator($routes)->generate('test', array('default' => 'value'))); | ||
302 | $this->assertSame('/app.php/test', $this->getGenerator($routes)->generate('test')); | ||
303 | } | ||
304 | |||
305 | public function testGenerateWithSpecialRouteName() | ||
306 | { | ||
307 | $routes = $this->getRoutes('$péß^a|', new Route('/bar')); | ||
308 | |||
309 | $this->assertSame('/app.php/bar', $this->getGenerator($routes)->generate('$péß^a|')); | ||
310 | } | ||
311 | |||
312 | public function testUrlEncoding() | ||
313 | { | ||
314 | // This tests the encoding of reserved characters that are used for delimiting of URI components (defined in RFC 3986) | ||
315 | // and other special ASCII chars. These chars are tested as static text path, variable path and query param. | ||
316 | $chars = '@:[]/()*\'" +,;-._~&$<>|{}%\\^`!?foo=bar#id'; | ||
317 | $routes = $this->getRoutes('test', new Route("/$chars/{varpath}", array(), array('varpath' => '.+'))); | ||
318 | $this->assertSame('/app.php/@:%5B%5D/%28%29*%27%22%20+,;-._~%26%24%3C%3E|%7B%7D%25%5C%5E%60!%3Ffoo=bar%23id' | ||
319 | .'/@:%5B%5D/%28%29*%27%22%20+,;-._~%26%24%3C%3E|%7B%7D%25%5C%5E%60!%3Ffoo=bar%23id' | ||
320 | .'?query=%40%3A%5B%5D%2F%28%29%2A%27%22+%2B%2C%3B-._%7E%26%24%3C%3E%7C%7B%7D%25%5C%5E%60%21%3Ffoo%3Dbar%23id', | ||
321 | $this->getGenerator($routes)->generate('test', array( | ||
322 | 'varpath' => $chars, | ||
323 | 'query' => $chars | ||
324 | )) | ||
325 | ); | ||
326 | } | ||
327 | |||
328 | public function testEncodingOfRelativePathSegments() | ||
329 | { | ||
330 | $routes = $this->getRoutes('test', new Route('/dir/../dir/..')); | ||
331 | $this->assertSame('/app.php/dir/%2E%2E/dir/%2E%2E', $this->getGenerator($routes)->generate('test')); | ||
332 | $routes = $this->getRoutes('test', new Route('/dir/./dir/.')); | ||
333 | $this->assertSame('/app.php/dir/%2E/dir/%2E', $this->getGenerator($routes)->generate('test')); | ||
334 | $routes = $this->getRoutes('test', new Route('/a./.a/a../..a/...')); | ||
335 | $this->assertSame('/app.php/a./.a/a../..a/...', $this->getGenerator($routes)->generate('test')); | ||
336 | } | ||
337 | |||
338 | public function testAdjacentVariables() | ||
339 | { | ||
340 | $routes = $this->getRoutes('test', new Route('/{x}{y}{z}.{_format}', array('z' => 'default-z', '_format' => 'html'), array('y' => '\d+'))); | ||
341 | $generator = $this->getGenerator($routes); | ||
342 | $this->assertSame('/app.php/foo123', $generator->generate('test', array('x' => 'foo', 'y' => '123'))); | ||
343 | $this->assertSame('/app.php/foo123bar.xml', $generator->generate('test', array('x' => 'foo', 'y' => '123', 'z' => 'bar', '_format' => 'xml'))); | ||
344 | |||
345 | // The default requirement for 'x' should not allow the separator '.' in this case because it would otherwise match everything | ||
346 | // and following optional variables like _format could never match. | ||
347 | $this->setExpectedException('Symfony\Component\Routing\Exception\InvalidParameterException'); | ||
348 | $generator->generate('test', array('x' => 'do.t', 'y' => '123', 'z' => 'bar', '_format' => 'xml')); | ||
349 | } | ||
350 | |||
351 | public function testOptionalVariableWithNoRealSeparator() | ||
352 | { | ||
353 | $routes = $this->getRoutes('test', new Route('/get{what}', array('what' => 'All'))); | ||
354 | $generator = $this->getGenerator($routes); | ||
355 | |||
356 | $this->assertSame('/app.php/get', $generator->generate('test')); | ||
357 | $this->assertSame('/app.php/getSites', $generator->generate('test', array('what' => 'Sites'))); | ||
358 | } | ||
359 | |||
360 | public function testRequiredVariableWithNoRealSeparator() | ||
361 | { | ||
362 | $routes = $this->getRoutes('test', new Route('/get{what}Suffix')); | ||
363 | $generator = $this->getGenerator($routes); | ||
364 | |||
365 | $this->assertSame('/app.php/getSitesSuffix', $generator->generate('test', array('what' => 'Sites'))); | ||
366 | } | ||
367 | |||
368 | public function testDefaultRequirementOfVariable() | ||
369 | { | ||
370 | $routes = $this->getRoutes('test', new Route('/{page}.{_format}')); | ||
371 | $generator = $this->getGenerator($routes); | ||
372 | |||
373 | $this->assertSame('/app.php/index.mobile.html', $generator->generate('test', array('page' => 'index', '_format' => 'mobile.html'))); | ||
374 | } | ||
375 | |||
376 | /** | ||
377 | * @expectedException \Symfony\Component\Routing\Exception\InvalidParameterException | ||
378 | */ | ||
379 | public function testDefaultRequirementOfVariableDisallowsSlash() | ||
380 | { | ||
381 | $routes = $this->getRoutes('test', new Route('/{page}.{_format}')); | ||
382 | $this->getGenerator($routes)->generate('test', array('page' => 'index', '_format' => 'sl/ash')); | ||
383 | } | ||
384 | |||
385 | /** | ||
386 | * @expectedException \Symfony\Component\Routing\Exception\InvalidParameterException | ||
387 | */ | ||
388 | public function testDefaultRequirementOfVariableDisallowsNextSeparator() | ||
389 | { | ||
390 | $routes = $this->getRoutes('test', new Route('/{page}.{_format}')); | ||
391 | $this->getGenerator($routes)->generate('test', array('page' => 'do.t', '_format' => 'html')); | ||
392 | } | ||
393 | |||
394 | public function testWithHostDifferentFromContext() | ||
395 | { | ||
396 | $routes = $this->getRoutes('test', new Route('/{name}', array(), array(), array(), '{locale}.example.com')); | ||
397 | |||
398 | $this->assertEquals('//fr.example.com/app.php/Fabien', $this->getGenerator($routes)->generate('test', array('name' =>'Fabien', 'locale' => 'fr'))); | ||
399 | } | ||
400 | |||
401 | public function testWithHostSameAsContext() | ||
402 | { | ||
403 | $routes = $this->getRoutes('test', new Route('/{name}', array(), array(), array(), '{locale}.example.com')); | ||
404 | |||
405 | $this->assertEquals('/app.php/Fabien', $this->getGenerator($routes, array('host' => 'fr.example.com'))->generate('test', array('name' =>'Fabien', 'locale' => 'fr'))); | ||
406 | } | ||
407 | |||
408 | public function testWithHostSameAsContextAndAbsolute() | ||
409 | { | ||
410 | $routes = $this->getRoutes('test', new Route('/{name}', array(), array(), array(), '{locale}.example.com')); | ||
411 | |||
412 | $this->assertEquals('http://fr.example.com/app.php/Fabien', $this->getGenerator($routes, array('host' => 'fr.example.com'))->generate('test', array('name' =>'Fabien', 'locale' => 'fr'), true)); | ||
413 | } | ||
414 | |||
415 | /** | ||
416 | * @expectedException \Symfony\Component\Routing\Exception\InvalidParameterException | ||
417 | */ | ||
418 | public function testUrlWithInvalidParameterInHost() | ||
419 | { | ||
420 | $routes = $this->getRoutes('test', new Route('/', array(), array('foo' => 'bar'), array(), '{foo}.example.com')); | ||
421 | $this->getGenerator($routes)->generate('test', array('foo' => 'baz'), false); | ||
422 | } | ||
423 | |||
424 | /** | ||
425 | * @expectedException \Symfony\Component\Routing\Exception\InvalidParameterException | ||
426 | */ | ||
427 | public function testUrlWithInvalidParameterInHostWhenParamHasADefaultValue() | ||
428 | { | ||
429 | $routes = $this->getRoutes('test', new Route('/', array('foo' => 'bar'), array('foo' => 'bar'), array(), '{foo}.example.com')); | ||
430 | $this->getGenerator($routes)->generate('test', array('foo' => 'baz'), false); | ||
431 | } | ||
432 | |||
433 | /** | ||
434 | * @expectedException \Symfony\Component\Routing\Exception\InvalidParameterException | ||
435 | */ | ||
436 | public function testUrlWithInvalidParameterEqualsDefaultValueInHost() | ||
437 | { | ||
438 | $routes = $this->getRoutes('test', new Route('/', array('foo' => 'baz'), array('foo' => 'bar'), array(), '{foo}.example.com')); | ||
439 | $this->getGenerator($routes)->generate('test', array('foo' => 'baz'), false); | ||
440 | } | ||
441 | |||
442 | public function testUrlWithInvalidParameterInHostInNonStrictMode() | ||
443 | { | ||
444 | $routes = $this->getRoutes('test', new Route('/', array(), array('foo' => 'bar'), array(), '{foo}.example.com')); | ||
445 | $generator = $this->getGenerator($routes); | ||
446 | $generator->setStrictRequirements(false); | ||
447 | $this->assertNull($generator->generate('test', array('foo' => 'baz'), false)); | ||
448 | } | ||
449 | |||
450 | public function testGenerateNetworkPath() | ||
451 | { | ||
452 | $routes = $this->getRoutes('test', new Route('/{name}', array(), array('_scheme' => 'http'), array(), '{locale}.example.com')); | ||
453 | |||
454 | $this->assertSame('//fr.example.com/app.php/Fabien', $this->getGenerator($routes)->generate('test', | ||
455 | array('name' =>'Fabien', 'locale' => 'fr'), UrlGeneratorInterface::NETWORK_PATH), 'network path with different host' | ||
456 | ); | ||
457 | $this->assertSame('//fr.example.com/app.php/Fabien?query=string', $this->getGenerator($routes, array('host' => 'fr.example.com'))->generate('test', | ||
458 | array('name' =>'Fabien', 'locale' => 'fr', 'query' => 'string'), UrlGeneratorInterface::NETWORK_PATH), 'network path although host same as context' | ||
459 | ); | ||
460 | $this->assertSame('http://fr.example.com/app.php/Fabien', $this->getGenerator($routes, array('scheme' => 'https'))->generate('test', | ||
461 | array('name' =>'Fabien', 'locale' => 'fr'), UrlGeneratorInterface::NETWORK_PATH), 'absolute URL because scheme requirement does not match context' | ||
462 | ); | ||
463 | $this->assertSame('http://fr.example.com/app.php/Fabien', $this->getGenerator($routes)->generate('test', | ||
464 | array('name' =>'Fabien', 'locale' => 'fr'), UrlGeneratorInterface::ABSOLUTE_URL), 'absolute URL with same scheme because it is requested' | ||
465 | ); | ||
466 | } | ||
467 | |||
468 | public function testGenerateRelativePath() | ||
469 | { | ||
470 | $routes = new RouteCollection(); | ||
471 | $routes->add('article', new Route('/{author}/{article}/')); | ||
472 | $routes->add('comments', new Route('/{author}/{article}/comments')); | ||
473 | $routes->add('host', new Route('/{article}', array(), array(), array(), '{author}.example.com')); | ||
474 | $routes->add('scheme', new Route('/{author}', array(), array('_scheme' => 'https'))); | ||
475 | $routes->add('unrelated', new Route('/about')); | ||
476 | |||
477 | $generator = $this->getGenerator($routes, array('host' => 'example.com', 'pathInfo' => '/fabien/symfony-is-great/')); | ||
478 | |||
479 | $this->assertSame('comments', $generator->generate('comments', | ||
480 | array('author' =>'fabien', 'article' => 'symfony-is-great'), UrlGeneratorInterface::RELATIVE_PATH) | ||
481 | ); | ||
482 | $this->assertSame('comments?page=2', $generator->generate('comments', | ||
483 | array('author' =>'fabien', 'article' => 'symfony-is-great', 'page' => 2), UrlGeneratorInterface::RELATIVE_PATH) | ||
484 | ); | ||
485 | $this->assertSame('../twig-is-great/', $generator->generate('article', | ||
486 | array('author' =>'fabien', 'article' => 'twig-is-great'), UrlGeneratorInterface::RELATIVE_PATH) | ||
487 | ); | ||
488 | $this->assertSame('../../bernhard/forms-are-great/', $generator->generate('article', | ||
489 | array('author' =>'bernhard', 'article' => 'forms-are-great'), UrlGeneratorInterface::RELATIVE_PATH) | ||
490 | ); | ||
491 | $this->assertSame('//bernhard.example.com/app.php/forms-are-great', $generator->generate('host', | ||
492 | array('author' =>'bernhard', 'article' => 'forms-are-great'), UrlGeneratorInterface::RELATIVE_PATH) | ||
493 | ); | ||
494 | $this->assertSame('https://example.com/app.php/bernhard', $generator->generate('scheme', | ||
495 | array('author' =>'bernhard'), UrlGeneratorInterface::RELATIVE_PATH) | ||
496 | ); | ||
497 | $this->assertSame('../../about', $generator->generate('unrelated', | ||
498 | array(), UrlGeneratorInterface::RELATIVE_PATH) | ||
499 | ); | ||
500 | } | ||
501 | |||
502 | /** | ||
503 | * @dataProvider provideRelativePaths | ||
504 | */ | ||
505 | public function testGetRelativePath($sourcePath, $targetPath, $expectedPath) | ||
506 | { | ||
507 | $this->assertSame($expectedPath, UrlGenerator::getRelativePath($sourcePath, $targetPath)); | ||
508 | } | ||
509 | |||
510 | public function provideRelativePaths() | ||
511 | { | ||
512 | return array( | ||
513 | array( | ||
514 | '/same/dir/', | ||
515 | '/same/dir/', | ||
516 | '' | ||
517 | ), | ||
518 | array( | ||
519 | '/same/file', | ||
520 | '/same/file', | ||
521 | '' | ||
522 | ), | ||
523 | array( | ||
524 | '/', | ||
525 | '/file', | ||
526 | 'file' | ||
527 | ), | ||
528 | array( | ||
529 | '/', | ||
530 | '/dir/file', | ||
531 | 'dir/file' | ||
532 | ), | ||
533 | array( | ||
534 | '/dir/file.html', | ||
535 | '/dir/different-file.html', | ||
536 | 'different-file.html' | ||
537 | ), | ||
538 | array( | ||
539 | '/same/dir/extra-file', | ||
540 | '/same/dir/', | ||
541 | './' | ||
542 | ), | ||
543 | array( | ||
544 | '/parent/dir/', | ||
545 | '/parent/', | ||
546 | '../' | ||
547 | ), | ||
548 | array( | ||
549 | '/parent/dir/extra-file', | ||
550 | '/parent/', | ||
551 | '../' | ||
552 | ), | ||
553 | array( | ||
554 | '/a/b/', | ||
555 | '/x/y/z/', | ||
556 | '../../x/y/z/' | ||
557 | ), | ||
558 | array( | ||
559 | '/a/b/c/d/e', | ||
560 | '/a/c/d', | ||
561 | '../../../c/d' | ||
562 | ), | ||
563 | array( | ||
564 | '/a/b/c//', | ||
565 | '/a/b/c/', | ||
566 | '../' | ||
567 | ), | ||
568 | array( | ||
569 | '/a/b/c/', | ||
570 | '/a/b/c//', | ||
571 | './/' | ||
572 | ), | ||
573 | array( | ||
574 | '/root/a/b/c/', | ||
575 | '/root/x/b/c/', | ||
576 | '../../../x/b/c/' | ||
577 | ), | ||
578 | array( | ||
579 | '/a/b/c/d/', | ||
580 | '/a', | ||
581 | '../../../../a' | ||
582 | ), | ||
583 | array( | ||
584 | '/special-chars/sp%20ce/1€/mäh/e=mc²', | ||
585 | '/special-chars/sp%20ce/1€/<µ>/e=mc²', | ||
586 | '../<µ>/e=mc²' | ||
587 | ), | ||
588 | array( | ||
589 | 'not-rooted', | ||
590 | 'dir/file', | ||
591 | 'dir/file' | ||
592 | ), | ||
593 | array( | ||
594 | '//dir/', | ||
595 | '', | ||
596 | '../../' | ||
597 | ), | ||
598 | array( | ||
599 | '/dir/', | ||
600 | '/dir/file:with-colon', | ||
601 | './file:with-colon' | ||
602 | ), | ||
603 | array( | ||
604 | '/dir/', | ||
605 | '/dir/subdir/file:with-colon', | ||
606 | 'subdir/file:with-colon' | ||
607 | ), | ||
608 | array( | ||
609 | '/dir/', | ||
610 | '/dir/:subdir/', | ||
611 | './:subdir/' | ||
612 | ), | ||
613 | ); | ||
614 | } | ||
615 | |||
616 | protected function getGenerator(RouteCollection $routes, array $parameters = array(), $logger = null) | ||
617 | { | ||
618 | $context = new RequestContext('/app.php'); | ||
619 | foreach ($parameters as $key => $value) { | ||
620 | $method = 'set'.$key; | ||
621 | $context->$method($value); | ||
622 | } | ||
623 | $generator = new UrlGenerator($routes, $context, $logger); | ||
624 | |||
625 | return $generator; | ||
626 | } | ||
627 | |||
628 | protected function getRoutes($name, Route $route) | ||
629 | { | ||
630 | $routes = new RouteCollection(); | ||
631 | $routes->add($name, $route); | ||
632 | |||
633 | return $routes; | ||
634 | } | ||
635 | } | ||