UrlGeneratorTest.php 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060
  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 PHPUnit\Framework\TestCase;
  12. use Psr\Log\LoggerInterface;
  13. use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
  14. use Symfony\Component\Routing\Exception\InvalidParameterException;
  15. use Symfony\Component\Routing\Exception\MissingMandatoryParametersException;
  16. use Symfony\Component\Routing\Exception\RouteCircularReferenceException;
  17. use Symfony\Component\Routing\Exception\RouteNotFoundException;
  18. use Symfony\Component\Routing\Generator\UrlGenerator;
  19. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  20. use Symfony\Component\Routing\RequestContext;
  21. use Symfony\Component\Routing\Route;
  22. use Symfony\Component\Routing\RouteCollection;
  23. class UrlGeneratorTest extends TestCase
  24. {
  25. use ExpectDeprecationTrait;
  26. public function testAbsoluteUrlWithPort80()
  27. {
  28. $routes = $this->getRoutes('test', new Route('/testing'));
  29. $url = $this->getGenerator($routes)->generate('test', [], UrlGeneratorInterface::ABSOLUTE_URL);
  30. $this->assertEquals('http://localhost/app.php/testing', $url);
  31. }
  32. public function testAbsoluteSecureUrlWithPort443()
  33. {
  34. $routes = $this->getRoutes('test', new Route('/testing'));
  35. $url = $this->getGenerator($routes, ['scheme' => 'https'])->generate('test', [], UrlGeneratorInterface::ABSOLUTE_URL);
  36. $this->assertEquals('https://localhost/app.php/testing', $url);
  37. }
  38. public function testAbsoluteUrlWithNonStandardPort()
  39. {
  40. $routes = $this->getRoutes('test', new Route('/testing'));
  41. $url = $this->getGenerator($routes, ['httpPort' => 8080])->generate('test', [], UrlGeneratorInterface::ABSOLUTE_URL);
  42. $this->assertEquals('http://localhost:8080/app.php/testing', $url);
  43. }
  44. public function testAbsoluteSecureUrlWithNonStandardPort()
  45. {
  46. $routes = $this->getRoutes('test', new Route('/testing'));
  47. $url = $this->getGenerator($routes, ['httpsPort' => 8080, 'scheme' => 'https'])->generate('test', [], UrlGeneratorInterface::ABSOLUTE_URL);
  48. $this->assertEquals('https://localhost:8080/app.php/testing', $url);
  49. }
  50. public function testRelativeUrlWithoutParameters()
  51. {
  52. $routes = $this->getRoutes('test', new Route('/testing'));
  53. $url = $this->getGenerator($routes)->generate('test', [], UrlGeneratorInterface::ABSOLUTE_PATH);
  54. $this->assertEquals('/app.php/testing', $url);
  55. }
  56. public function testRelativeUrlWithParameter()
  57. {
  58. $routes = $this->getRoutes('test', new Route('/testing/{foo}'));
  59. $url = $this->getGenerator($routes)->generate('test', ['foo' => 'bar'], UrlGeneratorInterface::ABSOLUTE_PATH);
  60. $this->assertEquals('/app.php/testing/bar', $url);
  61. }
  62. public function testRelativeUrlWithNullParameter()
  63. {
  64. $routes = $this->getRoutes('test', new Route('/testing.{format}', ['format' => null]));
  65. $url = $this->getGenerator($routes)->generate('test', [], UrlGeneratorInterface::ABSOLUTE_PATH);
  66. $this->assertEquals('/app.php/testing', $url);
  67. }
  68. public function testRelativeUrlWithNullParameterButNotOptional()
  69. {
  70. $this->expectException(InvalidParameterException::class);
  71. $routes = $this->getRoutes('test', new Route('/testing/{foo}/bar', ['foo' => null]));
  72. // This must raise an exception because the default requirement for "foo" is "[^/]+" which is not met with these params.
  73. // Generating path "/testing//bar" would be wrong as matching this route would fail.
  74. $this->getGenerator($routes)->generate('test', [], UrlGeneratorInterface::ABSOLUTE_PATH);
  75. }
  76. public function testRelativeUrlWithOptionalZeroParameter()
  77. {
  78. $routes = $this->getRoutes('test', new Route('/testing/{page}'));
  79. $url = $this->getGenerator($routes)->generate('test', ['page' => 0], UrlGeneratorInterface::ABSOLUTE_PATH);
  80. $this->assertEquals('/app.php/testing/0', $url);
  81. }
  82. public function testNotPassedOptionalParameterInBetween()
  83. {
  84. $routes = $this->getRoutes('test', new Route('/{slug}/{page}', ['slug' => 'index', 'page' => 0]));
  85. $this->assertSame('/app.php/index/1', $this->getGenerator($routes)->generate('test', ['page' => 1]));
  86. $this->assertSame('/app.php/', $this->getGenerator($routes)->generate('test'));
  87. }
  88. /**
  89. * @dataProvider valuesProvider
  90. */
  91. public function testRelativeUrlWithExtraParameters(string $expectedQueryString, string $parameter, $value)
  92. {
  93. $routes = $this->getRoutes('test', new Route('/testing'));
  94. $url = $this->getGenerator($routes)->generate('test', [$parameter => $value], UrlGeneratorInterface::ABSOLUTE_PATH);
  95. $this->assertSame('/app.php/testing'.$expectedQueryString, $url);
  96. }
  97. /**
  98. * @dataProvider valuesProvider
  99. */
  100. public function testAbsoluteUrlWithExtraParameters(string $expectedQueryString, string $parameter, $value)
  101. {
  102. $routes = $this->getRoutes('test', new Route('/testing'));
  103. $url = $this->getGenerator($routes)->generate('test', [$parameter => $value], UrlGeneratorInterface::ABSOLUTE_URL);
  104. $this->assertSame('http://localhost/app.php/testing'.$expectedQueryString, $url);
  105. }
  106. public static function valuesProvider(): array
  107. {
  108. $stdClass = new \stdClass();
  109. $stdClass->baz = 'bar';
  110. $nestedStdClass = new \stdClass();
  111. $nestedStdClass->nested = $stdClass;
  112. return [
  113. 'null' => ['', 'foo', null],
  114. 'string' => ['?foo=bar', 'foo', 'bar'],
  115. 'boolean-false' => ['?foo=0', 'foo', false],
  116. 'boolean-true' => ['?foo=1', 'foo', true],
  117. 'object implementing __toString()' => ['?foo=bar', 'foo', new StringableObject()],
  118. 'object implementing __toString() but has public property' => ['?foo%5Bfoo%5D=property', 'foo', new StringableObjectWithPublicProperty()],
  119. 'object implementing __toString() in nested array' => ['?foo%5Bbaz%5D=bar', 'foo', ['baz' => new StringableObject()]],
  120. 'object implementing __toString() in nested array but has public property' => ['?foo%5Bbaz%5D%5Bfoo%5D=property', 'foo', ['baz' => new StringableObjectWithPublicProperty()]],
  121. 'stdClass' => ['?foo%5Bbaz%5D=bar', 'foo', $stdClass],
  122. 'stdClass in nested stdClass' => ['?foo%5Bnested%5D%5Bbaz%5D=bar', 'foo', $nestedStdClass],
  123. 'non stringable object' => ['', 'foo', new NonStringableObject()],
  124. 'non stringable object but has public property' => ['?foo%5Bfoo%5D=property', 'foo', new NonStringableObjectWithPublicProperty()],
  125. ];
  126. }
  127. public function testUrlWithExtraParametersFromGlobals()
  128. {
  129. $routes = $this->getRoutes('test', new Route('/testing'));
  130. $generator = $this->getGenerator($routes);
  131. $context = new RequestContext('/app.php');
  132. $context->setParameter('bar', 'bar');
  133. $generator->setContext($context);
  134. $url = $generator->generate('test', ['foo' => 'bar']);
  135. $this->assertEquals('/app.php/testing?foo=bar', $url);
  136. }
  137. public function testUrlWithGlobalParameter()
  138. {
  139. $routes = $this->getRoutes('test', new Route('/testing/{foo}'));
  140. $generator = $this->getGenerator($routes);
  141. $context = new RequestContext('/app.php');
  142. $context->setParameter('foo', 'bar');
  143. $generator->setContext($context);
  144. $url = $generator->generate('test', []);
  145. $this->assertEquals('/app.php/testing/bar', $url);
  146. }
  147. public function testGlobalParameterHasHigherPriorityThanDefault()
  148. {
  149. $routes = $this->getRoutes('test', new Route('/{_locale}', ['_locale' => 'en']));
  150. $generator = $this->getGenerator($routes);
  151. $context = new RequestContext('/app.php');
  152. $context->setParameter('_locale', 'de');
  153. $generator->setContext($context);
  154. $url = $generator->generate('test', []);
  155. $this->assertSame('/app.php/de', $url);
  156. }
  157. public function testGenerateWithDefaultLocale()
  158. {
  159. $routes = new RouteCollection();
  160. $route = new Route('');
  161. $name = 'test';
  162. foreach (['hr' => '/foo', 'en' => '/bar'] as $locale => $path) {
  163. $localizedRoute = clone $route;
  164. $localizedRoute->setDefault('_locale', $locale);
  165. $localizedRoute->setRequirement('_locale', $locale);
  166. $localizedRoute->setDefault('_canonical_route', $name);
  167. $localizedRoute->setPath($path);
  168. $routes->add($name.'.'.$locale, $localizedRoute);
  169. }
  170. $generator = $this->getGenerator($routes, [], null, 'hr');
  171. $this->assertSame(
  172. 'http://localhost/app.php/foo',
  173. $generator->generate($name, [], UrlGeneratorInterface::ABSOLUTE_URL)
  174. );
  175. }
  176. public function testGenerateWithOverriddenParameterLocale()
  177. {
  178. $routes = new RouteCollection();
  179. $route = new Route('');
  180. $name = 'test';
  181. foreach (['hr' => '/foo', 'en' => '/bar'] as $locale => $path) {
  182. $localizedRoute = clone $route;
  183. $localizedRoute->setDefault('_locale', $locale);
  184. $localizedRoute->setRequirement('_locale', $locale);
  185. $localizedRoute->setDefault('_canonical_route', $name);
  186. $localizedRoute->setPath($path);
  187. $routes->add($name.'.'.$locale, $localizedRoute);
  188. }
  189. $generator = $this->getGenerator($routes, [], null, 'hr');
  190. $this->assertSame(
  191. 'http://localhost/app.php/bar',
  192. $generator->generate($name, ['_locale' => 'en'], UrlGeneratorInterface::ABSOLUTE_URL)
  193. );
  194. }
  195. public function testGenerateWithOverriddenParameterLocaleFromRequestContext()
  196. {
  197. $routes = new RouteCollection();
  198. $route = new Route('');
  199. $name = 'test';
  200. foreach (['hr' => '/foo', 'en' => '/bar'] as $locale => $path) {
  201. $localizedRoute = clone $route;
  202. $localizedRoute->setDefault('_locale', $locale);
  203. $localizedRoute->setRequirement('_locale', $locale);
  204. $localizedRoute->setDefault('_canonical_route', $name);
  205. $localizedRoute->setPath($path);
  206. $routes->add($name.'.'.$locale, $localizedRoute);
  207. }
  208. $generator = $this->getGenerator($routes, [], null, 'hr');
  209. $context = new RequestContext('/app.php');
  210. $context->setParameter('_locale', 'en');
  211. $generator->setContext($context);
  212. $this->assertSame(
  213. 'http://localhost/app.php/bar',
  214. $generator->generate($name, [], UrlGeneratorInterface::ABSOLUTE_URL)
  215. );
  216. }
  217. public function testDumpWithLocalizedRoutesPreserveTheGoodLocaleInTheUrl()
  218. {
  219. $routeCollection = new RouteCollection();
  220. $routeCollection->add('foo.en', (new Route('/{_locale}/fork'))->setDefault('_locale', 'en')->setDefault('_canonical_route', 'foo')->setRequirement('_locale', 'en'));
  221. $routeCollection->add('foo.fr', (new Route('/{_locale}/fourchette'))->setDefault('_locale', 'fr')->setDefault('_canonical_route', 'foo')->setRequirement('_locale', 'fr'));
  222. $routeCollection->add('fun.en', (new Route('/fun'))->setDefault('_locale', 'en')->setDefault('_canonical_route', 'fun')->setRequirement('_locale', 'en'));
  223. $routeCollection->add('fun.fr', (new Route('/amusant'))->setDefault('_locale', 'fr')->setDefault('_canonical_route', 'fun')->setRequirement('_locale', 'fr'));
  224. $urlGenerator = $this->getGenerator($routeCollection);
  225. $urlGenerator->getContext()->setParameter('_locale', 'fr');
  226. $this->assertSame('/app.php/fr/fourchette', $urlGenerator->generate('foo'));
  227. $this->assertSame('/app.php/en/fork', $urlGenerator->generate('foo.en'));
  228. $this->assertSame('/app.php/en/fork', $urlGenerator->generate('foo', ['_locale' => 'en']));
  229. $this->assertSame('/app.php/fr/fourchette', $urlGenerator->generate('foo.fr', ['_locale' => 'en']));
  230. $this->assertSame('/app.php/amusant', $urlGenerator->generate('fun'));
  231. $this->assertSame('/app.php/fun', $urlGenerator->generate('fun.en'));
  232. $this->assertSame('/app.php/fun', $urlGenerator->generate('fun', ['_locale' => 'en']));
  233. $this->assertSame('/app.php/amusant', $urlGenerator->generate('fun.fr', ['_locale' => 'en']));
  234. }
  235. public function testGenerateWithoutRoutes()
  236. {
  237. $this->expectException(RouteNotFoundException::class);
  238. $routes = $this->getRoutes('foo', new Route('/testing/{foo}'));
  239. $this->getGenerator($routes)->generate('test', [], UrlGeneratorInterface::ABSOLUTE_URL);
  240. }
  241. public function testGenerateWithInvalidLocale()
  242. {
  243. $this->expectException(RouteNotFoundException::class);
  244. $routes = new RouteCollection();
  245. $route = new Route('');
  246. $name = 'test';
  247. foreach (['hr' => '/foo', 'en' => '/bar'] as $locale => $path) {
  248. $localizedRoute = clone $route;
  249. $localizedRoute->setDefault('_locale', $locale);
  250. $localizedRoute->setRequirement('_locale', $locale);
  251. $localizedRoute->setDefault('_canonical_route', $name);
  252. $localizedRoute->setPath($path);
  253. $routes->add($name.'.'.$locale, $localizedRoute);
  254. }
  255. $generator = $this->getGenerator($routes, [], null, 'fr');
  256. $generator->generate($name);
  257. }
  258. public function testGenerateForRouteWithoutMandatoryParameter()
  259. {
  260. $this->expectException(MissingMandatoryParametersException::class);
  261. $routes = $this->getRoutes('test', new Route('/testing/{foo}'));
  262. $this->getGenerator($routes)->generate('test', [], UrlGeneratorInterface::ABSOLUTE_URL);
  263. }
  264. public function testGenerateForRouteWithInvalidOptionalParameter()
  265. {
  266. $this->expectException(InvalidParameterException::class);
  267. $routes = $this->getRoutes('test', new Route('/testing/{foo}', ['foo' => '1'], ['foo' => 'd+']));
  268. $this->getGenerator($routes)->generate('test', ['foo' => 'bar'], UrlGeneratorInterface::ABSOLUTE_URL);
  269. }
  270. public function testGenerateForRouteWithInvalidParameter()
  271. {
  272. $this->expectException(InvalidParameterException::class);
  273. $routes = $this->getRoutes('test', new Route('/testing/{foo}', [], ['foo' => '1|2']));
  274. $this->getGenerator($routes)->generate('test', ['foo' => '0'], UrlGeneratorInterface::ABSOLUTE_URL);
  275. }
  276. public function testGenerateForRouteWithInvalidOptionalParameterNonStrict()
  277. {
  278. $routes = $this->getRoutes('test', new Route('/testing/{foo}', ['foo' => '1'], ['foo' => 'd+']));
  279. $generator = $this->getGenerator($routes);
  280. $generator->setStrictRequirements(false);
  281. $this->assertSame('', $generator->generate('test', ['foo' => 'bar'], UrlGeneratorInterface::ABSOLUTE_URL));
  282. }
  283. public function testGenerateForRouteWithInvalidOptionalParameterNonStrictWithLogger()
  284. {
  285. $routes = $this->getRoutes('test', new Route('/testing/{foo}', ['foo' => '1'], ['foo' => 'd+']));
  286. $logger = $this->createMock(LoggerInterface::class);
  287. $logger->expects($this->once())
  288. ->method('error');
  289. $generator = $this->getGenerator($routes, [], $logger);
  290. $generator->setStrictRequirements(false);
  291. $this->assertSame('', $generator->generate('test', ['foo' => 'bar'], UrlGeneratorInterface::ABSOLUTE_URL));
  292. }
  293. public function testGenerateForRouteWithInvalidParameterButDisabledRequirementsCheck()
  294. {
  295. $routes = $this->getRoutes('test', new Route('/testing/{foo}', ['foo' => '1'], ['foo' => 'd+']));
  296. $generator = $this->getGenerator($routes);
  297. $generator->setStrictRequirements(null);
  298. $this->assertSame('/app.php/testing/bar', $generator->generate('test', ['foo' => 'bar']));
  299. }
  300. public function testGenerateForRouteWithInvalidMandatoryParameter()
  301. {
  302. $this->expectException(InvalidParameterException::class);
  303. $routes = $this->getRoutes('test', new Route('/testing/{foo}', [], ['foo' => 'd+']));
  304. $this->getGenerator($routes)->generate('test', ['foo' => 'bar'], UrlGeneratorInterface::ABSOLUTE_URL);
  305. }
  306. public function testGenerateForRouteWithInvalidUtf8Parameter()
  307. {
  308. $this->expectException(InvalidParameterException::class);
  309. $routes = $this->getRoutes('test', new Route('/testing/{foo}', [], ['foo' => '\pL+'], ['utf8' => true]));
  310. $this->getGenerator($routes)->generate('test', ['foo' => 'abc123'], UrlGeneratorInterface::ABSOLUTE_URL);
  311. }
  312. public function testRequiredParamAndEmptyPassed()
  313. {
  314. $this->expectException(InvalidParameterException::class);
  315. $routes = $this->getRoutes('test', new Route('/{slug}', [], ['slug' => '.+']));
  316. $this->getGenerator($routes)->generate('test', ['slug' => '']);
  317. }
  318. public function testSchemeRequirementDoesNothingIfSameCurrentScheme()
  319. {
  320. $routes = $this->getRoutes('test', new Route('/', [], [], [], '', ['http']));
  321. $this->assertEquals('/app.php/', $this->getGenerator($routes)->generate('test'));
  322. $routes = $this->getRoutes('test', new Route('/', [], [], [], '', ['https']));
  323. $this->assertEquals('/app.php/', $this->getGenerator($routes, ['scheme' => 'https'])->generate('test'));
  324. }
  325. public function testSchemeRequirementForcesAbsoluteUrl()
  326. {
  327. $routes = $this->getRoutes('test', new Route('/', [], [], [], '', ['https']));
  328. $this->assertEquals('https://localhost/app.php/', $this->getGenerator($routes)->generate('test'));
  329. $routes = $this->getRoutes('test', new Route('/', [], [], [], '', ['http']));
  330. $this->assertEquals('http://localhost/app.php/', $this->getGenerator($routes, ['scheme' => 'https'])->generate('test'));
  331. }
  332. public function testSchemeRequirementCreatesUrlForFirstRequiredScheme()
  333. {
  334. $routes = $this->getRoutes('test', new Route('/', [], [], [], '', ['Ftp', 'https']));
  335. $this->assertEquals('ftp://localhost/app.php/', $this->getGenerator($routes)->generate('test'));
  336. }
  337. public function testPathWithTwoStartingSlashes()
  338. {
  339. $routes = $this->getRoutes('test', new Route('//path-and-not-domain'));
  340. // this must not generate '//path-and-not-domain' because that would be a network path
  341. $this->assertSame('/path-and-not-domain', $this->getGenerator($routes, ['BaseUrl' => ''])->generate('test'));
  342. }
  343. public function testNoTrailingSlashForMultipleOptionalParameters()
  344. {
  345. $routes = $this->getRoutes('test', new Route('/category/{slug1}/{slug2}/{slug3}', ['slug2' => null, 'slug3' => null]));
  346. $this->assertEquals('/app.php/category/foo', $this->getGenerator($routes)->generate('test', ['slug1' => 'foo']));
  347. }
  348. public function testWithAnIntegerAsADefaultValue()
  349. {
  350. $routes = $this->getRoutes('test', new Route('/{default}', ['default' => 0]));
  351. $this->assertEquals('/app.php/foo', $this->getGenerator($routes)->generate('test', ['default' => 'foo']));
  352. }
  353. public function testNullForOptionalParameterIsIgnored()
  354. {
  355. $routes = $this->getRoutes('test', new Route('/test/{default}', ['default' => 0]));
  356. $this->assertEquals('/app.php/test', $this->getGenerator($routes)->generate('test', ['default' => null]));
  357. }
  358. public function testQueryParamSameAsDefault()
  359. {
  360. $routes = $this->getRoutes('test', new Route('/test', ['page' => 1]));
  361. $this->assertSame('/app.php/test?page=2', $this->getGenerator($routes)->generate('test', ['page' => 2]));
  362. $this->assertSame('/app.php/test', $this->getGenerator($routes)->generate('test', ['page' => 1]));
  363. $this->assertSame('/app.php/test', $this->getGenerator($routes)->generate('test', ['page' => '1']));
  364. $this->assertSame('/app.php/test', $this->getGenerator($routes)->generate('test'));
  365. }
  366. public function testArrayQueryParamSameAsDefault()
  367. {
  368. $routes = $this->getRoutes('test', new Route('/test', ['array' => ['foo', 'bar']]));
  369. $this->assertSame('/app.php/test?array%5B0%5D=bar&array%5B1%5D=foo', $this->getGenerator($routes)->generate('test', ['array' => ['bar', 'foo']]));
  370. $this->assertSame('/app.php/test?array%5Ba%5D=foo&array%5Bb%5D=bar', $this->getGenerator($routes)->generate('test', ['array' => ['a' => 'foo', 'b' => 'bar']]));
  371. $this->assertSame('/app.php/test', $this->getGenerator($routes)->generate('test', ['array' => ['foo', 'bar']]));
  372. $this->assertSame('/app.php/test', $this->getGenerator($routes)->generate('test', ['array' => [1 => 'bar', 0 => 'foo']]));
  373. $this->assertSame('/app.php/test', $this->getGenerator($routes)->generate('test'));
  374. }
  375. public function testGenerateWithSpecialRouteName()
  376. {
  377. $routes = $this->getRoutes('$péß^a|', new Route('/bar'));
  378. $this->assertSame('/app.php/bar', $this->getGenerator($routes)->generate('$péß^a|'));
  379. }
  380. public function testUrlEncoding()
  381. {
  382. $expectedPath = '/app.php/@:%5B%5D/%28%29*%27%22%20+,;-._~%26%24%3C%3E|%7B%7D%25%5C%5E%60!%3Ffoo=bar%23id'
  383. .'/@:%5B%5D/%28%29*%27%22%20+,;-._~%26%24%3C%3E|%7B%7D%25%5C%5E%60!%3Ffoo=bar%23id'
  384. .'?query=@:%5B%5D/%28%29*%27%22%20%2B,;-._~%26%24%3C%3E%7C%7B%7D%25%5C%5E%60!?foo%3Dbar%23id';
  385. // This tests the encoding of reserved characters that are used for delimiting of URI components (defined in RFC 3986)
  386. // and other special ASCII chars. These chars are tested as static text path, variable path and query param.
  387. $chars = '@:[]/()*\'" +,;-._~&$<>|{}%\\^`!?foo=bar#id';
  388. $routes = $this->getRoutes('test', new Route("/$chars/{varpath}", [], ['varpath' => '.+']));
  389. $this->assertSame($expectedPath, $this->getGenerator($routes)->generate('test', [
  390. 'varpath' => $chars,
  391. 'query' => $chars,
  392. ]));
  393. }
  394. public function testEncodingOfRelativePathSegments()
  395. {
  396. $routes = $this->getRoutes('test', new Route('/dir/../dir/..'));
  397. $this->assertSame('/app.php/dir/%2E%2E/dir/%2E%2E', $this->getGenerator($routes)->generate('test'));
  398. $routes = $this->getRoutes('test', new Route('/dir/./dir/.'));
  399. $this->assertSame('/app.php/dir/%2E/dir/%2E', $this->getGenerator($routes)->generate('test'));
  400. $routes = $this->getRoutes('test', new Route('/a./.a/a../..a/...'));
  401. $this->assertSame('/app.php/a./.a/a../..a/...', $this->getGenerator($routes)->generate('test'));
  402. }
  403. public function testEncodingOfSlashInPath()
  404. {
  405. $routes = $this->getRoutes('test', new Route('/dir/{path}/dir2', [], ['path' => '.+']));
  406. $this->assertSame('/app.php/dir/foo/bar%2Fbaz/dir2', $this->getGenerator($routes)->generate('test', ['path' => 'foo/bar%2Fbaz']));
  407. }
  408. public function testAdjacentVariables()
  409. {
  410. $routes = $this->getRoutes('test', new Route('/{x}{y}{z}.{_format}', ['z' => 'default-z', '_format' => 'html'], ['y' => '\d+']));
  411. $generator = $this->getGenerator($routes);
  412. $this->assertSame('/app.php/foo123', $generator->generate('test', ['x' => 'foo', 'y' => '123']));
  413. $this->assertSame('/app.php/foo123bar.xml', $generator->generate('test', ['x' => 'foo', 'y' => '123', 'z' => 'bar', '_format' => 'xml']));
  414. // The default requirement for 'x' should not allow the separator '.' in this case because it would otherwise match everything
  415. // and following optional variables like _format could never match.
  416. $this->expectException(InvalidParameterException::class);
  417. $generator->generate('test', ['x' => 'do.t', 'y' => '123', 'z' => 'bar', '_format' => 'xml']);
  418. }
  419. public function testOptionalVariableWithNoRealSeparator()
  420. {
  421. $routes = $this->getRoutes('test', new Route('/get{what}', ['what' => 'All']));
  422. $generator = $this->getGenerator($routes);
  423. $this->assertSame('/app.php/get', $generator->generate('test'));
  424. $this->assertSame('/app.php/getSites', $generator->generate('test', ['what' => 'Sites']));
  425. }
  426. public function testRequiredVariableWithNoRealSeparator()
  427. {
  428. $routes = $this->getRoutes('test', new Route('/get{what}Suffix'));
  429. $generator = $this->getGenerator($routes);
  430. $this->assertSame('/app.php/getSitesSuffix', $generator->generate('test', ['what' => 'Sites']));
  431. }
  432. public function testDefaultRequirementOfVariable()
  433. {
  434. $routes = $this->getRoutes('test', new Route('/{page}.{_format}'));
  435. $generator = $this->getGenerator($routes);
  436. $this->assertSame('/app.php/index.mobile.html', $generator->generate('test', ['page' => 'index', '_format' => 'mobile.html']));
  437. }
  438. public function testImportantVariable()
  439. {
  440. $routes = $this->getRoutes('test', (new Route('/{page}.{!_format}'))->addDefaults(['_format' => 'mobile.html']));
  441. $generator = $this->getGenerator($routes);
  442. $this->assertSame('/app.php/index.xml', $generator->generate('test', ['page' => 'index', '_format' => 'xml']));
  443. $this->assertSame('/app.php/index.mobile.html', $generator->generate('test', ['page' => 'index', '_format' => 'mobile.html']));
  444. $this->assertSame('/app.php/index.mobile.html', $generator->generate('test', ['page' => 'index']));
  445. }
  446. public function testImportantVariableWithNoDefault()
  447. {
  448. $this->expectException(MissingMandatoryParametersException::class);
  449. $routes = $this->getRoutes('test', new Route('/{page}.{!_format}'));
  450. $generator = $this->getGenerator($routes);
  451. $generator->generate('test', ['page' => 'index']);
  452. }
  453. public function testDefaultRequirementOfVariableDisallowsSlash()
  454. {
  455. $this->expectException(InvalidParameterException::class);
  456. $routes = $this->getRoutes('test', new Route('/{page}.{_format}'));
  457. $this->getGenerator($routes)->generate('test', ['page' => 'index', '_format' => 'sl/ash']);
  458. }
  459. public function testDefaultRequirementOfVariableDisallowsNextSeparator()
  460. {
  461. $this->expectException(InvalidParameterException::class);
  462. $routes = $this->getRoutes('test', new Route('/{page}.{_format}'));
  463. $this->getGenerator($routes)->generate('test', ['page' => 'do.t', '_format' => 'html']);
  464. }
  465. public function testWithHostDifferentFromContext()
  466. {
  467. $routes = $this->getRoutes('test', new Route('/{name}', [], [], [], '{locale}.example.com'));
  468. $this->assertEquals('//fr.example.com/app.php/Fabien', $this->getGenerator($routes)->generate('test', ['name' => 'Fabien', 'locale' => 'fr']));
  469. }
  470. public function testWithHostSameAsContext()
  471. {
  472. $routes = $this->getRoutes('test', new Route('/{name}', [], [], [], '{locale}.example.com'));
  473. $this->assertEquals('/app.php/Fabien', $this->getGenerator($routes, ['host' => 'fr.example.com'])->generate('test', ['name' => 'Fabien', 'locale' => 'fr']));
  474. }
  475. public function testWithHostSameAsContextAndAbsolute()
  476. {
  477. $routes = $this->getRoutes('test', new Route('/{name}', [], [], [], '{locale}.example.com'));
  478. $this->assertEquals('http://fr.example.com/app.php/Fabien', $this->getGenerator($routes, ['host' => 'fr.example.com'])->generate('test', ['name' => 'Fabien', 'locale' => 'fr'], UrlGeneratorInterface::ABSOLUTE_URL));
  479. }
  480. public function testUrlWithInvalidParameterInHost()
  481. {
  482. $this->expectException(InvalidParameterException::class);
  483. $routes = $this->getRoutes('test', new Route('/', [], ['foo' => 'bar'], [], '{foo}.example.com'));
  484. $this->getGenerator($routes)->generate('test', ['foo' => 'baz'], UrlGeneratorInterface::ABSOLUTE_PATH);
  485. }
  486. public function testUrlWithInvalidParameterInHostWhenParamHasADefaultValue()
  487. {
  488. $this->expectException(InvalidParameterException::class);
  489. $routes = $this->getRoutes('test', new Route('/', ['foo' => 'bar'], ['foo' => 'bar'], [], '{foo}.example.com'));
  490. $this->getGenerator($routes)->generate('test', ['foo' => 'baz'], UrlGeneratorInterface::ABSOLUTE_PATH);
  491. }
  492. public function testUrlWithInvalidParameterEqualsDefaultValueInHost()
  493. {
  494. $this->expectException(InvalidParameterException::class);
  495. $routes = $this->getRoutes('test', new Route('/', ['foo' => 'baz'], ['foo' => 'bar'], [], '{foo}.example.com'));
  496. $this->getGenerator($routes)->generate('test', ['foo' => 'baz'], UrlGeneratorInterface::ABSOLUTE_PATH);
  497. }
  498. public function testUrlWithInvalidParameterInHostInNonStrictMode()
  499. {
  500. $routes = $this->getRoutes('test', new Route('/', [], ['foo' => 'bar'], [], '{foo}.example.com'));
  501. $generator = $this->getGenerator($routes);
  502. $generator->setStrictRequirements(false);
  503. $this->assertSame('', $generator->generate('test', ['foo' => 'baz'], UrlGeneratorInterface::ABSOLUTE_PATH));
  504. }
  505. public function testHostIsCaseInsensitive()
  506. {
  507. $routes = $this->getRoutes('test', new Route('/', [], ['locale' => 'en|de|fr'], [], '{locale}.FooBar.com'));
  508. $generator = $this->getGenerator($routes);
  509. $this->assertSame('//EN.FooBar.com/app.php/', $generator->generate('test', ['locale' => 'EN'], UrlGeneratorInterface::NETWORK_PATH));
  510. }
  511. public function testDefaultHostIsUsedWhenContextHostIsEmpty()
  512. {
  513. $routes = $this->getRoutes('test', new Route('/path', ['domain' => 'my.fallback.host'], ['domain' => '.+'], [], '{domain}'));
  514. $generator = $this->getGenerator($routes);
  515. $generator->getContext()->setHost('');
  516. $this->assertSame('http://my.fallback.host/app.php/path', $generator->generate('test', [], UrlGeneratorInterface::ABSOLUTE_URL));
  517. }
  518. public function testDefaultHostIsUsedWhenContextHostIsEmptyAndPathReferenceType()
  519. {
  520. $routes = $this->getRoutes('test', new Route('/path', ['domain' => 'my.fallback.host'], ['domain' => '.+'], [], '{domain}'));
  521. $generator = $this->getGenerator($routes);
  522. $generator->getContext()->setHost('');
  523. $this->assertSame('//my.fallback.host/app.php/path', $generator->generate('test', [], UrlGeneratorInterface::ABSOLUTE_PATH));
  524. }
  525. public function testAbsoluteUrlFallbackToPathIfHostIsEmptyAndSchemeIsHttp()
  526. {
  527. $routes = $this->getRoutes('test', new Route('/route'));
  528. $generator = $this->getGenerator($routes);
  529. $generator->getContext()->setHost('');
  530. $generator->getContext()->setScheme('https');
  531. $this->assertSame('/app.php/route', $generator->generate('test', [], UrlGeneratorInterface::ABSOLUTE_URL));
  532. }
  533. public function testAbsoluteUrlFallbackToNetworkIfSchemeIsEmptyAndHostIsNot()
  534. {
  535. $routes = $this->getRoutes('test', new Route('/path'));
  536. $generator = $this->getGenerator($routes);
  537. $generator->getContext()->setHost('example.com');
  538. $generator->getContext()->setScheme('');
  539. $this->assertSame('//example.com/app.php/path', $generator->generate('test', [], UrlGeneratorInterface::ABSOLUTE_URL));
  540. }
  541. public function testAbsoluteUrlFallbackToPathIfSchemeAndHostAreEmpty()
  542. {
  543. $routes = $this->getRoutes('test', new Route('/path'));
  544. $generator = $this->getGenerator($routes);
  545. $generator->getContext()->setHost('');
  546. $generator->getContext()->setScheme('');
  547. $this->assertSame('/app.php/path', $generator->generate('test', [], UrlGeneratorInterface::ABSOLUTE_URL));
  548. }
  549. public function testAbsoluteUrlWithNonHttpSchemeAndEmptyHost()
  550. {
  551. $routes = $this->getRoutes('test', new Route('/path', [], [], [], '', ['file']));
  552. $generator = $this->getGenerator($routes);
  553. $generator->getContext()->setBaseUrl('');
  554. $generator->getContext()->setHost('');
  555. $this->assertSame('file:///path', $generator->generate('test', [], UrlGeneratorInterface::ABSOLUTE_URL));
  556. }
  557. public function testGenerateNetworkPath()
  558. {
  559. $routes = $this->getRoutes('test', new Route('/{name}', [], [], [], '{locale}.example.com', ['http']));
  560. $this->assertSame('//fr.example.com/app.php/Fabien', $this->getGenerator($routes)->generate('test',
  561. ['name' => 'Fabien', 'locale' => 'fr'], UrlGeneratorInterface::NETWORK_PATH), 'network path with different host'
  562. );
  563. $this->assertSame('//fr.example.com/app.php/Fabien?query=string', $this->getGenerator($routes, ['host' => 'fr.example.com'])->generate('test',
  564. ['name' => 'Fabien', 'locale' => 'fr', 'query' => 'string'], UrlGeneratorInterface::NETWORK_PATH), 'network path although host same as context'
  565. );
  566. $this->assertSame('http://fr.example.com/app.php/Fabien', $this->getGenerator($routes, ['scheme' => 'https'])->generate('test',
  567. ['name' => 'Fabien', 'locale' => 'fr'], UrlGeneratorInterface::NETWORK_PATH), 'absolute URL because scheme requirement does not match context'
  568. );
  569. $this->assertSame('http://fr.example.com/app.php/Fabien', $this->getGenerator($routes)->generate('test',
  570. ['name' => 'Fabien', 'locale' => 'fr'], UrlGeneratorInterface::ABSOLUTE_URL), 'absolute URL with same scheme because it is requested'
  571. );
  572. }
  573. public function testGenerateRelativePath()
  574. {
  575. $routes = new RouteCollection();
  576. $routes->add('article', new Route('/{author}/{article}/'));
  577. $routes->add('comments', new Route('/{author}/{article}/comments'));
  578. $routes->add('host', new Route('/{article}', [], [], [], '{author}.example.com'));
  579. $routes->add('scheme', new Route('/{author}/blog', [], [], [], '', ['https']));
  580. $routes->add('unrelated', new Route('/about'));
  581. $generator = $this->getGenerator($routes, ['host' => 'example.com', 'pathInfo' => '/fabien/symfony-is-great/']);
  582. $this->assertSame('comments', $generator->generate('comments',
  583. ['author' => 'fabien', 'article' => 'symfony-is-great'], UrlGeneratorInterface::RELATIVE_PATH)
  584. );
  585. $this->assertSame('comments?page=2', $generator->generate('comments',
  586. ['author' => 'fabien', 'article' => 'symfony-is-great', 'page' => 2], UrlGeneratorInterface::RELATIVE_PATH)
  587. );
  588. $this->assertSame('../twig-is-great/', $generator->generate('article',
  589. ['author' => 'fabien', 'article' => 'twig-is-great'], UrlGeneratorInterface::RELATIVE_PATH)
  590. );
  591. $this->assertSame('../../bernhard/forms-are-great/', $generator->generate('article',
  592. ['author' => 'bernhard', 'article' => 'forms-are-great'], UrlGeneratorInterface::RELATIVE_PATH)
  593. );
  594. $this->assertSame('//bernhard.example.com/app.php/forms-are-great', $generator->generate('host',
  595. ['author' => 'bernhard', 'article' => 'forms-are-great'], UrlGeneratorInterface::RELATIVE_PATH)
  596. );
  597. $this->assertSame('https://example.com/app.php/bernhard/blog', $generator->generate('scheme',
  598. ['author' => 'bernhard'], UrlGeneratorInterface::RELATIVE_PATH)
  599. );
  600. $this->assertSame('../../about', $generator->generate('unrelated',
  601. [], UrlGeneratorInterface::RELATIVE_PATH)
  602. );
  603. }
  604. public function testAliases()
  605. {
  606. $routes = new RouteCollection();
  607. $routes->add('a', new Route('/foo'));
  608. $routes->addAlias('b', 'a');
  609. $routes->addAlias('c', 'b');
  610. $generator = $this->getGenerator($routes);
  611. $this->assertSame('/app.php/foo', $generator->generate('b'));
  612. $this->assertSame('/app.php/foo', $generator->generate('c'));
  613. }
  614. public function testAliasWhichTargetRouteDoesntExist()
  615. {
  616. $this->expectException(RouteNotFoundException::class);
  617. $routes = new RouteCollection();
  618. $routes->addAlias('d', 'non-existent');
  619. $this->getGenerator($routes)->generate('d');
  620. }
  621. /**
  622. * @group legacy
  623. */
  624. public function testDeprecatedAlias()
  625. {
  626. $this->expectDeprecation('Since foo/bar 1.0.0: The "b" route alias is deprecated. You should stop using it, as it will be removed in the future.');
  627. $routes = new RouteCollection();
  628. $routes->add('a', new Route('/foo'));
  629. $routes->addAlias('b', 'a')
  630. ->setDeprecated('foo/bar', '1.0.0', '');
  631. $this->getGenerator($routes)->generate('b');
  632. }
  633. /**
  634. * @group legacy
  635. */
  636. public function testDeprecatedAliasWithCustomMessage()
  637. {
  638. $this->expectDeprecation('Since foo/bar 1.0.0: foo b.');
  639. $routes = new RouteCollection();
  640. $routes->add('a', new Route('/foo'));
  641. $routes->addAlias('b', 'a')
  642. ->setDeprecated('foo/bar', '1.0.0', 'foo %alias_id%.');
  643. $this->getGenerator($routes)->generate('b');
  644. }
  645. /**
  646. * @group legacy
  647. */
  648. public function testTargettingADeprecatedAliasShouldTriggerDeprecation()
  649. {
  650. $this->expectDeprecation('Since foo/bar 1.0.0: foo b.');
  651. $routes = new RouteCollection();
  652. $routes->add('a', new Route('/foo'));
  653. $routes->addAlias('b', 'a')
  654. ->setDeprecated('foo/bar', '1.0.0', 'foo %alias_id%.');
  655. $routes->addAlias('c', 'b');
  656. $this->getGenerator($routes)->generate('c');
  657. }
  658. public function testCircularReferenceShouldThrowAnException()
  659. {
  660. $this->expectException(RouteCircularReferenceException::class);
  661. $this->expectExceptionMessage('Circular reference detected for route "b", path: "b -> a -> b".');
  662. $routes = new RouteCollection();
  663. $routes->addAlias('a', 'b');
  664. $routes->addAlias('b', 'a');
  665. $this->getGenerator($routes)->generate('b');
  666. }
  667. public function testDeepCircularReferenceShouldThrowAnException()
  668. {
  669. $this->expectException(RouteCircularReferenceException::class);
  670. $this->expectExceptionMessage('Circular reference detected for route "b", path: "b -> c -> b".');
  671. $routes = new RouteCollection();
  672. $routes->addAlias('a', 'b');
  673. $routes->addAlias('b', 'c');
  674. $routes->addAlias('c', 'b');
  675. $this->getGenerator($routes)->generate('b');
  676. }
  677. public function testIndirectCircularReferenceShouldThrowAnException()
  678. {
  679. $this->expectException(RouteCircularReferenceException::class);
  680. $this->expectExceptionMessage('Circular reference detected for route "a", path: "a -> b -> c -> a".');
  681. $routes = new RouteCollection();
  682. $routes->addAlias('a', 'b');
  683. $routes->addAlias('b', 'c');
  684. $routes->addAlias('c', 'a');
  685. $this->getGenerator($routes)->generate('a');
  686. }
  687. /**
  688. * @dataProvider provideRelativePaths
  689. */
  690. public function testGetRelativePath($sourcePath, $targetPath, $expectedPath)
  691. {
  692. $this->assertSame($expectedPath, UrlGenerator::getRelativePath($sourcePath, $targetPath));
  693. }
  694. public static function provideRelativePaths()
  695. {
  696. return [
  697. [
  698. '/same/dir/',
  699. '/same/dir/',
  700. '',
  701. ],
  702. [
  703. '/same/file',
  704. '/same/file',
  705. '',
  706. ],
  707. [
  708. '/',
  709. '/file',
  710. 'file',
  711. ],
  712. [
  713. '/',
  714. '/dir/file',
  715. 'dir/file',
  716. ],
  717. [
  718. '/dir/file.html',
  719. '/dir/different-file.html',
  720. 'different-file.html',
  721. ],
  722. [
  723. '/same/dir/extra-file',
  724. '/same/dir/',
  725. './',
  726. ],
  727. [
  728. '/parent/dir/',
  729. '/parent/',
  730. '../',
  731. ],
  732. [
  733. '/parent/dir/extra-file',
  734. '/parent/',
  735. '../',
  736. ],
  737. [
  738. '/a/b/',
  739. '/x/y/z/',
  740. '../../x/y/z/',
  741. ],
  742. [
  743. '/a/b/c/d/e',
  744. '/a/c/d',
  745. '../../../c/d',
  746. ],
  747. [
  748. '/a/b/c//',
  749. '/a/b/c/',
  750. '../',
  751. ],
  752. [
  753. '/a/b/c/',
  754. '/a/b/c//',
  755. './/',
  756. ],
  757. [
  758. '/root/a/b/c/',
  759. '/root/x/b/c/',
  760. '../../../x/b/c/',
  761. ],
  762. [
  763. '/a/b/c/d/',
  764. '/a',
  765. '../../../../a',
  766. ],
  767. [
  768. '/special-chars/sp%20ce/1€/mäh/e=mc²',
  769. '/special-chars/sp%20ce/1€/<µ>/e=mc²',
  770. '../<µ>/e=mc²',
  771. ],
  772. [
  773. 'not-rooted',
  774. 'dir/file',
  775. 'dir/file',
  776. ],
  777. [
  778. '//dir/',
  779. '',
  780. '../../',
  781. ],
  782. [
  783. '/dir/',
  784. '/dir/file:with-colon',
  785. './file:with-colon',
  786. ],
  787. [
  788. '/dir/',
  789. '/dir/subdir/file:with-colon',
  790. 'subdir/file:with-colon',
  791. ],
  792. [
  793. '/dir/',
  794. '/dir/:subdir/',
  795. './:subdir/',
  796. ],
  797. ];
  798. }
  799. public function testFragmentsCanBeAppendedToUrls()
  800. {
  801. $routes = $this->getRoutes('test', new Route('/testing'));
  802. $url = $this->getGenerator($routes)->generate('test', ['_fragment' => 'frag ment'], UrlGeneratorInterface::ABSOLUTE_PATH);
  803. $this->assertEquals('/app.php/testing#frag%20ment', $url);
  804. $url = $this->getGenerator($routes)->generate('test', ['_fragment' => '0'], UrlGeneratorInterface::ABSOLUTE_PATH);
  805. $this->assertEquals('/app.php/testing#0', $url);
  806. }
  807. public function testFragmentsDoNotEscapeValidCharacters()
  808. {
  809. $routes = $this->getRoutes('test', new Route('/testing'));
  810. $url = $this->getGenerator($routes)->generate('test', ['_fragment' => '?/'], UrlGeneratorInterface::ABSOLUTE_PATH);
  811. $this->assertEquals('/app.php/testing#?/', $url);
  812. }
  813. public function testFragmentsCanBeDefinedAsDefaults()
  814. {
  815. $routes = $this->getRoutes('test', new Route('/testing', ['_fragment' => 'fragment']));
  816. $url = $this->getGenerator($routes)->generate('test', [], UrlGeneratorInterface::ABSOLUTE_PATH);
  817. $this->assertEquals('/app.php/testing#fragment', $url);
  818. }
  819. /**
  820. * @dataProvider provideLookAroundRequirementsInPath
  821. */
  822. public function testLookRoundRequirementsInPath($expected, $path, $requirement)
  823. {
  824. $routes = $this->getRoutes('test', new Route($path, [], ['foo' => $requirement, 'baz' => '.+?']));
  825. $this->assertSame($expected, $this->getGenerator($routes)->generate('test', ['foo' => 'a/b', 'baz' => 'c/d/e']));
  826. }
  827. public static function provideLookAroundRequirementsInPath()
  828. {
  829. yield ['/app.php/a/b/b%28ar/c/d/e', '/{foo}/b(ar/{baz}', '.+(?=/b\\(ar/)'];
  830. yield ['/app.php/a/b/bar/c/d/e', '/{foo}/bar/{baz}', '.+(?!$)'];
  831. yield ['/app.php/bar/a/b/bam/c/d/e', '/bar/{foo}/bam/{baz}', '(?<=/bar/).+'];
  832. yield ['/app.php/bar/a/b/bam/c/d/e', '/bar/{foo}/bam/{baz}', '(?<!^).+'];
  833. }
  834. protected function getGenerator(RouteCollection $routes, array $parameters = [], $logger = null, string $defaultLocale = null)
  835. {
  836. $context = new RequestContext('/app.php');
  837. foreach ($parameters as $key => $value) {
  838. $method = 'set'.$key;
  839. $context->$method($value);
  840. }
  841. return new UrlGenerator($routes, $context, $logger, $defaultLocale);
  842. }
  843. protected function getRoutes($name, Route $route)
  844. {
  845. $routes = new RouteCollection();
  846. $routes->add($name, $route);
  847. return $routes;
  848. }
  849. }
  850. class StringableObject
  851. {
  852. public function __toString()
  853. {
  854. return 'bar';
  855. }
  856. }
  857. class StringableObjectWithPublicProperty
  858. {
  859. public $foo = 'property';
  860. public function __toString()
  861. {
  862. return 'bar';
  863. }
  864. }
  865. class NonStringableObject
  866. {
  867. }
  868. class NonStringableObjectWithPublicProperty
  869. {
  870. public $foo = 'property';
  871. }