UrlGeneratorTest.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  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->getMock('Psr\Log\LoggerInterface');
  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 testRequiredParamAndEmptyPassed()
  198. {
  199. $routes = $this->getRoutes('test', new Route('/{slug}', array(), array('slug' => '.+')));
  200. $this->getGenerator($routes)->generate('test', array('slug' => ''));
  201. }
  202. public function testSchemeRequirementDoesNothingIfSameCurrentScheme()
  203. {
  204. $routes = $this->getRoutes('test', new Route('/', array(), array(), array(), '', array('http')));
  205. $this->assertEquals('/app.php/', $this->getGenerator($routes)->generate('test'));
  206. $routes = $this->getRoutes('test', new Route('/', array(), array(), array(), '', array('https')));
  207. $this->assertEquals('/app.php/', $this->getGenerator($routes, array('scheme' => 'https'))->generate('test'));
  208. }
  209. public function testSchemeRequirementForcesAbsoluteUrl()
  210. {
  211. $routes = $this->getRoutes('test', new Route('/', array(), array(), array(), '', array('https')));
  212. $this->assertEquals('https://localhost/app.php/', $this->getGenerator($routes)->generate('test'));
  213. $routes = $this->getRoutes('test', new Route('/', array(), array(), array(), '', array('http')));
  214. $this->assertEquals('http://localhost/app.php/', $this->getGenerator($routes, array('scheme' => 'https'))->generate('test'));
  215. }
  216. public function testSchemeRequirementCreatesUrlForFirstRequiredScheme()
  217. {
  218. $routes = $this->getRoutes('test', new Route('/', array(), array(), array(), '', array('Ftp', 'https')));
  219. $this->assertEquals('ftp://localhost/app.php/', $this->getGenerator($routes)->generate('test'));
  220. }
  221. public function testPathWithTwoStartingSlashes()
  222. {
  223. $routes = $this->getRoutes('test', new Route('//path-and-not-domain'));
  224. // this must not generate '//path-and-not-domain' because that would be a network path
  225. $this->assertSame('/path-and-not-domain', $this->getGenerator($routes, array('BaseUrl' => ''))->generate('test'));
  226. }
  227. public function testNoTrailingSlashForMultipleOptionalParameters()
  228. {
  229. $routes = $this->getRoutes('test', new Route('/category/{slug1}/{slug2}/{slug3}', array('slug2' => null, 'slug3' => null)));
  230. $this->assertEquals('/app.php/category/foo', $this->getGenerator($routes)->generate('test', array('slug1' => 'foo')));
  231. }
  232. public function testWithAnIntegerAsADefaultValue()
  233. {
  234. $routes = $this->getRoutes('test', new Route('/{default}', array('default' => 0)));
  235. $this->assertEquals('/app.php/foo', $this->getGenerator($routes)->generate('test', array('default' => 'foo')));
  236. }
  237. public function testNullForOptionalParameterIsIgnored()
  238. {
  239. $routes = $this->getRoutes('test', new Route('/test/{default}', array('default' => 0)));
  240. $this->assertEquals('/app.php/test', $this->getGenerator($routes)->generate('test', array('default' => null)));
  241. }
  242. public function testQueryParamSameAsDefault()
  243. {
  244. $routes = $this->getRoutes('test', new Route('/test', array('page' => 1)));
  245. $this->assertSame('/app.php/test?page=2', $this->getGenerator($routes)->generate('test', array('page' => 2)));
  246. $this->assertSame('/app.php/test', $this->getGenerator($routes)->generate('test', array('page' => 1)));
  247. $this->assertSame('/app.php/test', $this->getGenerator($routes)->generate('test', array('page' => '1')));
  248. $this->assertSame('/app.php/test', $this->getGenerator($routes)->generate('test'));
  249. }
  250. public function testArrayQueryParamSameAsDefault()
  251. {
  252. $routes = $this->getRoutes('test', new Route('/test', array('array' => array('foo', 'bar'))));
  253. $this->assertSame('/app.php/test?array%5B0%5D=bar&array%5B1%5D=foo', $this->getGenerator($routes)->generate('test', array('array' => array('bar', 'foo'))));
  254. $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'))));
  255. $this->assertSame('/app.php/test', $this->getGenerator($routes)->generate('test', array('array' => array('foo', 'bar'))));
  256. $this->assertSame('/app.php/test', $this->getGenerator($routes)->generate('test', array('array' => array(1 => 'bar', 0 => 'foo'))));
  257. $this->assertSame('/app.php/test', $this->getGenerator($routes)->generate('test'));
  258. }
  259. public function testGenerateWithSpecialRouteName()
  260. {
  261. $routes = $this->getRoutes('$péß^a|', new Route('/bar'));
  262. $this->assertSame('/app.php/bar', $this->getGenerator($routes)->generate('$péß^a|'));
  263. }
  264. public function testUrlEncoding()
  265. {
  266. // This tests the encoding of reserved characters that are used for delimiting of URI components (defined in RFC 3986)
  267. // and other special ASCII chars. These chars are tested as static text path, variable path and query param.
  268. $chars = '@:[]/()*\'" +,;-._~&$<>|{}%\\^`!?foo=bar#id';
  269. $routes = $this->getRoutes('test', new Route("/$chars/{varpath}", array(), array('varpath' => '.+')));
  270. $this->assertSame('/app.php/@:%5B%5D/%28%29*%27%22%20+,;-._~%26%24%3C%3E|%7B%7D%25%5C%5E%60!%3Ffoo=bar%23id'
  271. .'/@:%5B%5D/%28%29*%27%22%20+,;-._~%26%24%3C%3E|%7B%7D%25%5C%5E%60!%3Ffoo=bar%23id'
  272. .'?query=%40%3A%5B%5D/%28%29%2A%27%22+%2B%2C%3B-._%7E%26%24%3C%3E%7C%7B%7D%25%5C%5E%60%21%3Ffoo%3Dbar%23id',
  273. $this->getGenerator($routes)->generate('test', array(
  274. 'varpath' => $chars,
  275. 'query' => $chars,
  276. ))
  277. );
  278. }
  279. public function testEncodingOfRelativePathSegments()
  280. {
  281. $routes = $this->getRoutes('test', new Route('/dir/../dir/..'));
  282. $this->assertSame('/app.php/dir/%2E%2E/dir/%2E%2E', $this->getGenerator($routes)->generate('test'));
  283. $routes = $this->getRoutes('test', new Route('/dir/./dir/.'));
  284. $this->assertSame('/app.php/dir/%2E/dir/%2E', $this->getGenerator($routes)->generate('test'));
  285. $routes = $this->getRoutes('test', new Route('/a./.a/a../..a/...'));
  286. $this->assertSame('/app.php/a./.a/a../..a/...', $this->getGenerator($routes)->generate('test'));
  287. }
  288. public function testAdjacentVariables()
  289. {
  290. $routes = $this->getRoutes('test', new Route('/{x}{y}{z}.{_format}', array('z' => 'default-z', '_format' => 'html'), array('y' => '\d+')));
  291. $generator = $this->getGenerator($routes);
  292. $this->assertSame('/app.php/foo123', $generator->generate('test', array('x' => 'foo', 'y' => '123')));
  293. $this->assertSame('/app.php/foo123bar.xml', $generator->generate('test', array('x' => 'foo', 'y' => '123', 'z' => 'bar', '_format' => 'xml')));
  294. // The default requirement for 'x' should not allow the separator '.' in this case because it would otherwise match everything
  295. // and following optional variables like _format could never match.
  296. $this->setExpectedException('Symfony\Component\Routing\Exception\InvalidParameterException');
  297. $generator->generate('test', array('x' => 'do.t', 'y' => '123', 'z' => 'bar', '_format' => 'xml'));
  298. }
  299. public function testOptionalVariableWithNoRealSeparator()
  300. {
  301. $routes = $this->getRoutes('test', new Route('/get{what}', array('what' => 'All')));
  302. $generator = $this->getGenerator($routes);
  303. $this->assertSame('/app.php/get', $generator->generate('test'));
  304. $this->assertSame('/app.php/getSites', $generator->generate('test', array('what' => 'Sites')));
  305. }
  306. public function testRequiredVariableWithNoRealSeparator()
  307. {
  308. $routes = $this->getRoutes('test', new Route('/get{what}Suffix'));
  309. $generator = $this->getGenerator($routes);
  310. $this->assertSame('/app.php/getSitesSuffix', $generator->generate('test', array('what' => 'Sites')));
  311. }
  312. public function testDefaultRequirementOfVariable()
  313. {
  314. $routes = $this->getRoutes('test', new Route('/{page}.{_format}'));
  315. $generator = $this->getGenerator($routes);
  316. $this->assertSame('/app.php/index.mobile.html', $generator->generate('test', array('page' => 'index', '_format' => 'mobile.html')));
  317. }
  318. /**
  319. * @expectedException \Symfony\Component\Routing\Exception\InvalidParameterException
  320. */
  321. public function testDefaultRequirementOfVariableDisallowsSlash()
  322. {
  323. $routes = $this->getRoutes('test', new Route('/{page}.{_format}'));
  324. $this->getGenerator($routes)->generate('test', array('page' => 'index', '_format' => 'sl/ash'));
  325. }
  326. /**
  327. * @expectedException \Symfony\Component\Routing\Exception\InvalidParameterException
  328. */
  329. public function testDefaultRequirementOfVariableDisallowsNextSeparator()
  330. {
  331. $routes = $this->getRoutes('test', new Route('/{page}.{_format}'));
  332. $this->getGenerator($routes)->generate('test', array('page' => 'do.t', '_format' => 'html'));
  333. }
  334. public function testWithHostDifferentFromContext()
  335. {
  336. $routes = $this->getRoutes('test', new Route('/{name}', array(), array(), array(), '{locale}.example.com'));
  337. $this->assertEquals('//fr.example.com/app.php/Fabien', $this->getGenerator($routes)->generate('test', array('name' => 'Fabien', 'locale' => 'fr')));
  338. }
  339. public function testWithHostSameAsContext()
  340. {
  341. $routes = $this->getRoutes('test', new Route('/{name}', array(), array(), array(), '{locale}.example.com'));
  342. $this->assertEquals('/app.php/Fabien', $this->getGenerator($routes, array('host' => 'fr.example.com'))->generate('test', array('name' => 'Fabien', 'locale' => 'fr')));
  343. }
  344. public function testWithHostSameAsContextAndAbsolute()
  345. {
  346. $routes = $this->getRoutes('test', new Route('/{name}', array(), array(), array(), '{locale}.example.com'));
  347. $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));
  348. }
  349. /**
  350. * @expectedException \Symfony\Component\Routing\Exception\InvalidParameterException
  351. */
  352. public function testUrlWithInvalidParameterInHost()
  353. {
  354. $routes = $this->getRoutes('test', new Route('/', array(), array('foo' => 'bar'), array(), '{foo}.example.com'));
  355. $this->getGenerator($routes)->generate('test', array('foo' => 'baz'), UrlGeneratorInterface::ABSOLUTE_PATH);
  356. }
  357. /**
  358. * @expectedException \Symfony\Component\Routing\Exception\InvalidParameterException
  359. */
  360. public function testUrlWithInvalidParameterInHostWhenParamHasADefaultValue()
  361. {
  362. $routes = $this->getRoutes('test', new Route('/', array('foo' => 'bar'), array('foo' => 'bar'), array(), '{foo}.example.com'));
  363. $this->getGenerator($routes)->generate('test', array('foo' => 'baz'), UrlGeneratorInterface::ABSOLUTE_PATH);
  364. }
  365. /**
  366. * @expectedException \Symfony\Component\Routing\Exception\InvalidParameterException
  367. */
  368. public function testUrlWithInvalidParameterEqualsDefaultValueInHost()
  369. {
  370. $routes = $this->getRoutes('test', new Route('/', array('foo' => 'baz'), array('foo' => 'bar'), array(), '{foo}.example.com'));
  371. $this->getGenerator($routes)->generate('test', array('foo' => 'baz'), UrlGeneratorInterface::ABSOLUTE_PATH);
  372. }
  373. public function testUrlWithInvalidParameterInHostInNonStrictMode()
  374. {
  375. $routes = $this->getRoutes('test', new Route('/', array(), array('foo' => 'bar'), array(), '{foo}.example.com'));
  376. $generator = $this->getGenerator($routes);
  377. $generator->setStrictRequirements(false);
  378. $this->assertNull($generator->generate('test', array('foo' => 'baz'), UrlGeneratorInterface::ABSOLUTE_PATH));
  379. }
  380. public function testHostIsCaseInsensitive()
  381. {
  382. $routes = $this->getRoutes('test', new Route('/', array(), array('locale' => 'en|de|fr'), array(), '{locale}.FooBar.com'));
  383. $generator = $this->getGenerator($routes);
  384. $this->assertSame('//EN.FooBar.com/app.php/', $generator->generate('test', array('locale' => 'EN'), UrlGeneratorInterface::NETWORK_PATH));
  385. }
  386. public function testGenerateNetworkPath()
  387. {
  388. $routes = $this->getRoutes('test', new Route('/{name}', array(), array(), array(), '{locale}.example.com', array('http')));
  389. $this->assertSame('//fr.example.com/app.php/Fabien', $this->getGenerator($routes)->generate('test',
  390. array('name' => 'Fabien', 'locale' => 'fr'), UrlGeneratorInterface::NETWORK_PATH), 'network path with different host'
  391. );
  392. $this->assertSame('//fr.example.com/app.php/Fabien?query=string', $this->getGenerator($routes, array('host' => 'fr.example.com'))->generate('test',
  393. array('name' => 'Fabien', 'locale' => 'fr', 'query' => 'string'), UrlGeneratorInterface::NETWORK_PATH), 'network path although host same as context'
  394. );
  395. $this->assertSame('http://fr.example.com/app.php/Fabien', $this->getGenerator($routes, array('scheme' => 'https'))->generate('test',
  396. array('name' => 'Fabien', 'locale' => 'fr'), UrlGeneratorInterface::NETWORK_PATH), 'absolute URL because scheme requirement does not match context'
  397. );
  398. $this->assertSame('http://fr.example.com/app.php/Fabien', $this->getGenerator($routes)->generate('test',
  399. array('name' => 'Fabien', 'locale' => 'fr'), UrlGeneratorInterface::ABSOLUTE_URL), 'absolute URL with same scheme because it is requested'
  400. );
  401. }
  402. public function testGenerateRelativePath()
  403. {
  404. $routes = new RouteCollection();
  405. $routes->add('article', new Route('/{author}/{article}/'));
  406. $routes->add('comments', new Route('/{author}/{article}/comments'));
  407. $routes->add('host', new Route('/{article}', array(), array(), array(), '{author}.example.com'));
  408. $routes->add('scheme', new Route('/{author}/blog', array(), array(), array(), '', array('https')));
  409. $routes->add('unrelated', new Route('/about'));
  410. $generator = $this->getGenerator($routes, array('host' => 'example.com', 'pathInfo' => '/fabien/symfony-is-great/'));
  411. $this->assertSame('comments', $generator->generate('comments',
  412. array('author' => 'fabien', 'article' => 'symfony-is-great'), UrlGeneratorInterface::RELATIVE_PATH)
  413. );
  414. $this->assertSame('comments?page=2', $generator->generate('comments',
  415. array('author' => 'fabien', 'article' => 'symfony-is-great', 'page' => 2), UrlGeneratorInterface::RELATIVE_PATH)
  416. );
  417. $this->assertSame('../twig-is-great/', $generator->generate('article',
  418. array('author' => 'fabien', 'article' => 'twig-is-great'), UrlGeneratorInterface::RELATIVE_PATH)
  419. );
  420. $this->assertSame('../../bernhard/forms-are-great/', $generator->generate('article',
  421. array('author' => 'bernhard', 'article' => 'forms-are-great'), UrlGeneratorInterface::RELATIVE_PATH)
  422. );
  423. $this->assertSame('//bernhard.example.com/app.php/forms-are-great', $generator->generate('host',
  424. array('author' => 'bernhard', 'article' => 'forms-are-great'), UrlGeneratorInterface::RELATIVE_PATH)
  425. );
  426. $this->assertSame('https://example.com/app.php/bernhard/blog', $generator->generate('scheme',
  427. array('author' => 'bernhard'), UrlGeneratorInterface::RELATIVE_PATH)
  428. );
  429. $this->assertSame('../../about', $generator->generate('unrelated',
  430. array(), UrlGeneratorInterface::RELATIVE_PATH)
  431. );
  432. }
  433. /**
  434. * @dataProvider provideRelativePaths
  435. */
  436. public function testGetRelativePath($sourcePath, $targetPath, $expectedPath)
  437. {
  438. $this->assertSame($expectedPath, UrlGenerator::getRelativePath($sourcePath, $targetPath));
  439. }
  440. public function provideRelativePaths()
  441. {
  442. return array(
  443. array(
  444. '/same/dir/',
  445. '/same/dir/',
  446. '',
  447. ),
  448. array(
  449. '/same/file',
  450. '/same/file',
  451. '',
  452. ),
  453. array(
  454. '/',
  455. '/file',
  456. 'file',
  457. ),
  458. array(
  459. '/',
  460. '/dir/file',
  461. 'dir/file',
  462. ),
  463. array(
  464. '/dir/file.html',
  465. '/dir/different-file.html',
  466. 'different-file.html',
  467. ),
  468. array(
  469. '/same/dir/extra-file',
  470. '/same/dir/',
  471. './',
  472. ),
  473. array(
  474. '/parent/dir/',
  475. '/parent/',
  476. '../',
  477. ),
  478. array(
  479. '/parent/dir/extra-file',
  480. '/parent/',
  481. '../',
  482. ),
  483. array(
  484. '/a/b/',
  485. '/x/y/z/',
  486. '../../x/y/z/',
  487. ),
  488. array(
  489. '/a/b/c/d/e',
  490. '/a/c/d',
  491. '../../../c/d',
  492. ),
  493. array(
  494. '/a/b/c//',
  495. '/a/b/c/',
  496. '../',
  497. ),
  498. array(
  499. '/a/b/c/',
  500. '/a/b/c//',
  501. './/',
  502. ),
  503. array(
  504. '/root/a/b/c/',
  505. '/root/x/b/c/',
  506. '../../../x/b/c/',
  507. ),
  508. array(
  509. '/a/b/c/d/',
  510. '/a',
  511. '../../../../a',
  512. ),
  513. array(
  514. '/special-chars/sp%20ce/1€/mäh/e=mc²',
  515. '/special-chars/sp%20ce/1€/<µ>/e=mc²',
  516. '../<µ>/e=mc²',
  517. ),
  518. array(
  519. 'not-rooted',
  520. 'dir/file',
  521. 'dir/file',
  522. ),
  523. array(
  524. '//dir/',
  525. '',
  526. '../../',
  527. ),
  528. array(
  529. '/dir/',
  530. '/dir/file:with-colon',
  531. './file:with-colon',
  532. ),
  533. array(
  534. '/dir/',
  535. '/dir/subdir/file:with-colon',
  536. 'subdir/file:with-colon',
  537. ),
  538. array(
  539. '/dir/',
  540. '/dir/:subdir/',
  541. './:subdir/',
  542. ),
  543. );
  544. }
  545. protected function getGenerator(RouteCollection $routes, array $parameters = array(), $logger = null)
  546. {
  547. $context = new RequestContext('/app.php');
  548. foreach ($parameters as $key => $value) {
  549. $method = 'set'.$key;
  550. $context->$method($value);
  551. }
  552. return new UrlGenerator($routes, $context, $logger);
  553. }
  554. protected function getRoutes($name, Route $route)
  555. {
  556. $routes = new RouteCollection();
  557. $routes->add($name, $route);
  558. return $routes;
  559. }
  560. }