No Description

UrlGeneratorTest.php 30KB

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