RouteCompilerTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Routing\Route;
  13. use Symfony\Component\Routing\RouteCompiler;
  14. class RouteCompilerTest extends TestCase
  15. {
  16. /**
  17. * @dataProvider provideCompileData
  18. */
  19. public function testCompile($name, $arguments, $prefix, $regex, $variables, $tokens)
  20. {
  21. $r = new \ReflectionClass('Symfony\\Component\\Routing\\Route');
  22. $route = $r->newInstanceArgs($arguments);
  23. $compiled = $route->compile();
  24. $this->assertEquals($prefix, $compiled->getStaticPrefix(), $name.' (static prefix)');
  25. $this->assertEquals($regex, $compiled->getRegex(), $name.' (regex)');
  26. $this->assertEquals($variables, $compiled->getVariables(), $name.' (variables)');
  27. $this->assertEquals($tokens, $compiled->getTokens(), $name.' (tokens)');
  28. }
  29. public function provideCompileData()
  30. {
  31. return [
  32. [
  33. 'Static route',
  34. ['/foo'],
  35. '/foo', '#^/foo$#sD', [], [
  36. ['text', '/foo'],
  37. ],
  38. ],
  39. [
  40. 'Route with a variable',
  41. ['/foo/{bar}'],
  42. '/foo', '#^/foo/(?P<bar>[^/]++)$#sD', ['bar'], [
  43. ['variable', '/', '[^/]++', 'bar'],
  44. ['text', '/foo'],
  45. ],
  46. ],
  47. [
  48. 'Route with a variable that has a default value',
  49. ['/foo/{bar}', ['bar' => 'bar']],
  50. '/foo', '#^/foo(?:/(?P<bar>[^/]++))?$#sD', ['bar'], [
  51. ['variable', '/', '[^/]++', 'bar'],
  52. ['text', '/foo'],
  53. ],
  54. ],
  55. [
  56. 'Route with several variables',
  57. ['/foo/{bar}/{foobar}'],
  58. '/foo', '#^/foo/(?P<bar>[^/]++)/(?P<foobar>[^/]++)$#sD', ['bar', 'foobar'], [
  59. ['variable', '/', '[^/]++', 'foobar'],
  60. ['variable', '/', '[^/]++', 'bar'],
  61. ['text', '/foo'],
  62. ],
  63. ],
  64. [
  65. 'Route with several variables that have default values',
  66. ['/foo/{bar}/{foobar}', ['bar' => 'bar', 'foobar' => '']],
  67. '/foo', '#^/foo(?:/(?P<bar>[^/]++)(?:/(?P<foobar>[^/]++))?)?$#sD', ['bar', 'foobar'], [
  68. ['variable', '/', '[^/]++', 'foobar'],
  69. ['variable', '/', '[^/]++', 'bar'],
  70. ['text', '/foo'],
  71. ],
  72. ],
  73. [
  74. 'Route with several variables but some of them have no default values',
  75. ['/foo/{bar}/{foobar}', ['bar' => 'bar']],
  76. '/foo', '#^/foo/(?P<bar>[^/]++)/(?P<foobar>[^/]++)$#sD', ['bar', 'foobar'], [
  77. ['variable', '/', '[^/]++', 'foobar'],
  78. ['variable', '/', '[^/]++', 'bar'],
  79. ['text', '/foo'],
  80. ],
  81. ],
  82. [
  83. 'Route with an optional variable as the first segment',
  84. ['/{bar}', ['bar' => 'bar']],
  85. '', '#^/(?P<bar>[^/]++)?$#sD', ['bar'], [
  86. ['variable', '/', '[^/]++', 'bar'],
  87. ],
  88. ],
  89. [
  90. 'Route with a requirement of 0',
  91. ['/{bar}', ['bar' => null], ['bar' => '0']],
  92. '', '#^/(?P<bar>0)?$#sD', ['bar'], [
  93. ['variable', '/', '0', 'bar'],
  94. ],
  95. ],
  96. [
  97. 'Route with an optional variable as the first segment with requirements',
  98. ['/{bar}', ['bar' => 'bar'], ['bar' => '(foo|bar)']],
  99. '', '#^/(?P<bar>(?:foo|bar))?$#sD', ['bar'], [
  100. ['variable', '/', '(?:foo|bar)', 'bar'],
  101. ],
  102. ],
  103. [
  104. 'Route with only optional variables',
  105. ['/{foo}/{bar}', ['foo' => 'foo', 'bar' => 'bar']],
  106. '', '#^/(?P<foo>[^/]++)?(?:/(?P<bar>[^/]++))?$#sD', ['foo', 'bar'], [
  107. ['variable', '/', '[^/]++', 'bar'],
  108. ['variable', '/', '[^/]++', 'foo'],
  109. ],
  110. ],
  111. [
  112. 'Route with a variable in last position',
  113. ['/foo-{bar}'],
  114. '/foo-', '#^/foo\-(?P<bar>[^/]++)$#sD', ['bar'], [
  115. ['variable', '-', '[^/]++', 'bar'],
  116. ['text', '/foo'],
  117. ],
  118. ],
  119. [
  120. 'Route with nested placeholders',
  121. ['/{static{var}static}'],
  122. '/{static', '#^/\{static(?P<var>[^/]+)static\}$#sD', ['var'], [
  123. ['text', 'static}'],
  124. ['variable', '', '[^/]+', 'var'],
  125. ['text', '/{static'],
  126. ],
  127. ],
  128. [
  129. 'Route without separator between variables',
  130. ['/{w}{x}{y}{z}.{_format}', ['z' => 'default-z', '_format' => 'html'], ['y' => '(y|Y)']],
  131. '', '#^/(?P<w>[^/\.]+)(?P<x>[^/\.]+)(?P<y>(?:y|Y))(?:(?P<z>[^/\.]++)(?:\.(?P<_format>[^/]++))?)?$#sD', ['w', 'x', 'y', 'z', '_format'], [
  132. ['variable', '.', '[^/]++', '_format'],
  133. ['variable', '', '[^/\.]++', 'z'],
  134. ['variable', '', '(?:y|Y)', 'y'],
  135. ['variable', '', '[^/\.]+', 'x'],
  136. ['variable', '/', '[^/\.]+', 'w'],
  137. ],
  138. ],
  139. [
  140. 'Route with a format',
  141. ['/foo/{bar}.{_format}'],
  142. '/foo', '#^/foo/(?P<bar>[^/\.]++)\.(?P<_format>[^/]++)$#sD', ['bar', '_format'], [
  143. ['variable', '.', '[^/]++', '_format'],
  144. ['variable', '/', '[^/\.]++', 'bar'],
  145. ['text', '/foo'],
  146. ],
  147. ],
  148. [
  149. 'Static non UTF-8 route',
  150. ["/fo\xE9"],
  151. "/fo\xE9", "#^/fo\xE9$#sD", [], [
  152. ['text', "/fo\xE9"],
  153. ],
  154. ],
  155. [
  156. 'Route with an explicit UTF-8 requirement',
  157. ['/{bar}', ['bar' => null], ['bar' => '.'], ['utf8' => true]],
  158. '', '#^/(?P<bar>.)?$#sDu', ['bar'], [
  159. ['variable', '/', '.', 'bar', true],
  160. ],
  161. ],
  162. ];
  163. }
  164. /**
  165. * @dataProvider provideCompileImplicitUtf8Data
  166. * @expectedException \LogicException
  167. */
  168. public function testCompileImplicitUtf8Data($name, $arguments, $prefix, $regex, $variables, $tokens, $deprecationType)
  169. {
  170. $r = new \ReflectionClass('Symfony\\Component\\Routing\\Route');
  171. $route = $r->newInstanceArgs($arguments);
  172. $compiled = $route->compile();
  173. $this->assertEquals($prefix, $compiled->getStaticPrefix(), $name.' (static prefix)');
  174. $this->assertEquals($regex, $compiled->getRegex(), $name.' (regex)');
  175. $this->assertEquals($variables, $compiled->getVariables(), $name.' (variables)');
  176. $this->assertEquals($tokens, $compiled->getTokens(), $name.' (tokens)');
  177. }
  178. public function provideCompileImplicitUtf8Data()
  179. {
  180. return [
  181. [
  182. 'Static UTF-8 route',
  183. ['/foé'],
  184. '/foé', '#^/foé$#sDu', [], [
  185. ['text', '/foé'],
  186. ],
  187. 'patterns',
  188. ],
  189. [
  190. 'Route with an implicit UTF-8 requirement',
  191. ['/{bar}', ['bar' => null], ['bar' => 'é']],
  192. '', '#^/(?P<bar>é)?$#sDu', ['bar'], [
  193. ['variable', '/', 'é', 'bar', true],
  194. ],
  195. 'requirements',
  196. ],
  197. [
  198. 'Route with a UTF-8 class requirement',
  199. ['/{bar}', ['bar' => null], ['bar' => '\pM']],
  200. '', '#^/(?P<bar>\pM)?$#sDu', ['bar'], [
  201. ['variable', '/', '\pM', 'bar', true],
  202. ],
  203. 'requirements',
  204. ],
  205. [
  206. 'Route with a UTF-8 separator',
  207. ['/foo/{bar}§{_format}', [], [], ['compiler_class' => Utf8RouteCompiler::class]],
  208. '/foo', '#^/foo/(?P<bar>[^/§]++)§(?P<_format>[^/]++)$#sDu', ['bar', '_format'], [
  209. ['variable', '§', '[^/]++', '_format', true],
  210. ['variable', '/', '[^/§]++', 'bar', true],
  211. ['text', '/foo'],
  212. ],
  213. 'patterns',
  214. ],
  215. ];
  216. }
  217. /**
  218. * @expectedException \LogicException
  219. */
  220. public function testRouteWithSameVariableTwice()
  221. {
  222. $route = new Route('/{name}/{name}');
  223. $compiled = $route->compile();
  224. }
  225. /**
  226. * @expectedException \LogicException
  227. */
  228. public function testRouteCharsetMismatch()
  229. {
  230. $route = new Route("/\xE9/{bar}", [], ['bar' => '.'], ['utf8' => true]);
  231. $compiled = $route->compile();
  232. }
  233. /**
  234. * @expectedException \LogicException
  235. */
  236. public function testRequirementCharsetMismatch()
  237. {
  238. $route = new Route('/foo/{bar}', [], ['bar' => "\xE9"], ['utf8' => true]);
  239. $compiled = $route->compile();
  240. }
  241. /**
  242. * @expectedException \InvalidArgumentException
  243. */
  244. public function testRouteWithFragmentAsPathParameter()
  245. {
  246. $route = new Route('/{_fragment}');
  247. $compiled = $route->compile();
  248. }
  249. /**
  250. * @dataProvider getVariableNamesStartingWithADigit
  251. * @expectedException \DomainException
  252. */
  253. public function testRouteWithVariableNameStartingWithADigit($name)
  254. {
  255. $route = new Route('/{'.$name.'}');
  256. $route->compile();
  257. }
  258. public function getVariableNamesStartingWithADigit()
  259. {
  260. return [
  261. ['09'],
  262. ['123'],
  263. ['1e2'],
  264. ];
  265. }
  266. /**
  267. * @dataProvider provideCompileWithHostData
  268. */
  269. public function testCompileWithHost($name, $arguments, $prefix, $regex, $variables, $pathVariables, $tokens, $hostRegex, $hostVariables, $hostTokens)
  270. {
  271. $r = new \ReflectionClass('Symfony\\Component\\Routing\\Route');
  272. $route = $r->newInstanceArgs($arguments);
  273. $compiled = $route->compile();
  274. $this->assertEquals($prefix, $compiled->getStaticPrefix(), $name.' (static prefix)');
  275. $this->assertEquals($regex, str_replace(["\n", ' '], '', $compiled->getRegex()), $name.' (regex)');
  276. $this->assertEquals($variables, $compiled->getVariables(), $name.' (variables)');
  277. $this->assertEquals($pathVariables, $compiled->getPathVariables(), $name.' (path variables)');
  278. $this->assertEquals($tokens, $compiled->getTokens(), $name.' (tokens)');
  279. $this->assertEquals($hostRegex, str_replace(["\n", ' '], '', $compiled->getHostRegex()), $name.' (host regex)');
  280. $this->assertEquals($hostVariables, $compiled->getHostVariables(), $name.' (host variables)');
  281. $this->assertEquals($hostTokens, $compiled->getHostTokens(), $name.' (host tokens)');
  282. }
  283. public function provideCompileWithHostData()
  284. {
  285. return [
  286. [
  287. 'Route with host pattern',
  288. ['/hello', [], [], [], 'www.example.com'],
  289. '/hello', '#^/hello$#sD', [], [], [
  290. ['text', '/hello'],
  291. ],
  292. '#^www\.example\.com$#sDi', [], [
  293. ['text', 'www.example.com'],
  294. ],
  295. ],
  296. [
  297. 'Route with host pattern and some variables',
  298. ['/hello/{name}', [], [], [], 'www.example.{tld}'],
  299. '/hello', '#^/hello/(?P<name>[^/]++)$#sD', ['tld', 'name'], ['name'], [
  300. ['variable', '/', '[^/]++', 'name'],
  301. ['text', '/hello'],
  302. ],
  303. '#^www\.example\.(?P<tld>[^\.]++)$#sDi', ['tld'], [
  304. ['variable', '.', '[^\.]++', 'tld'],
  305. ['text', 'www.example'],
  306. ],
  307. ],
  308. [
  309. 'Route with variable at beginning of host',
  310. ['/hello', [], [], [], '{locale}.example.{tld}'],
  311. '/hello', '#^/hello$#sD', ['locale', 'tld'], [], [
  312. ['text', '/hello'],
  313. ],
  314. '#^(?P<locale>[^\.]++)\.example\.(?P<tld>[^\.]++)$#sDi', ['locale', 'tld'], [
  315. ['variable', '.', '[^\.]++', 'tld'],
  316. ['text', '.example'],
  317. ['variable', '', '[^\.]++', 'locale'],
  318. ],
  319. ],
  320. [
  321. 'Route with host variables that has a default value',
  322. ['/hello', ['locale' => 'a', 'tld' => 'b'], [], [], '{locale}.example.{tld}'],
  323. '/hello', '#^/hello$#sD', ['locale', 'tld'], [], [
  324. ['text', '/hello'],
  325. ],
  326. '#^(?P<locale>[^\.]++)\.example\.(?P<tld>[^\.]++)$#sDi', ['locale', 'tld'], [
  327. ['variable', '.', '[^\.]++', 'tld'],
  328. ['text', '.example'],
  329. ['variable', '', '[^\.]++', 'locale'],
  330. ],
  331. ],
  332. ];
  333. }
  334. /**
  335. * @expectedException \DomainException
  336. */
  337. public function testRouteWithTooLongVariableName()
  338. {
  339. $route = new Route(sprintf('/{%s}', str_repeat('a', RouteCompiler::VARIABLE_MAXIMUM_LENGTH + 1)));
  340. $route->compile();
  341. }
  342. /**
  343. * @dataProvider provideRemoveCapturingGroup
  344. */
  345. public function testRemoveCapturingGroup($regex, $requirement)
  346. {
  347. $route = new Route('/{foo}', [], ['foo' => $requirement]);
  348. $this->assertSame($regex, $route->compile()->getRegex());
  349. }
  350. public function provideRemoveCapturingGroup()
  351. {
  352. yield ['#^/(?P<foo>a(?:b|c)(?:d|e)f)$#sD', 'a(b|c)(d|e)f'];
  353. yield ['#^/(?P<foo>a\(b\)c)$#sD', 'a\(b\)c'];
  354. yield ['#^/(?P<foo>(?:b))$#sD', '(?:b)'];
  355. yield ['#^/(?P<foo>(?(b)b))$#sD', '(?(b)b)'];
  356. yield ['#^/(?P<foo>(*F))$#sD', '(*F)'];
  357. yield ['#^/(?P<foo>(?:(?:foo)))$#sD', '((foo))'];
  358. }
  359. }
  360. class Utf8RouteCompiler extends RouteCompiler
  361. {
  362. const SEPARATORS = '/§';
  363. }