RouteCompilerTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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(Route::class);
  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 static 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. */
  167. public function testCompileImplicitUtf8Data($name, $arguments, $prefix, $regex, $variables, $tokens, $deprecationType)
  168. {
  169. $this->expectException(\LogicException::class);
  170. $r = new \ReflectionClass(Route::class);
  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 static 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. public function testRouteWithSameVariableTwice()
  218. {
  219. $this->expectException(\LogicException::class);
  220. $route = new Route('/{name}/{name}');
  221. $route->compile();
  222. }
  223. public function testRouteCharsetMismatch()
  224. {
  225. $this->expectException(\LogicException::class);
  226. $route = new Route("/\xE9/{bar}", [], ['bar' => '.'], ['utf8' => true]);
  227. $route->compile();
  228. }
  229. public function testRequirementCharsetMismatch()
  230. {
  231. $this->expectException(\LogicException::class);
  232. $route = new Route('/foo/{bar}', [], ['bar' => "\xE9"], ['utf8' => true]);
  233. $route->compile();
  234. }
  235. public function testRouteWithFragmentAsPathParameter()
  236. {
  237. $this->expectException(\InvalidArgumentException::class);
  238. $route = new Route('/{_fragment}');
  239. $route->compile();
  240. }
  241. /**
  242. * @dataProvider getVariableNamesStartingWithADigit
  243. */
  244. public function testRouteWithVariableNameStartingWithADigit($name)
  245. {
  246. $this->expectException(\DomainException::class);
  247. $route = new Route('/{'.$name.'}');
  248. $route->compile();
  249. }
  250. public static function getVariableNamesStartingWithADigit()
  251. {
  252. return [
  253. ['09'],
  254. ['123'],
  255. ['1e2'],
  256. ];
  257. }
  258. /**
  259. * @dataProvider provideCompileWithHostData
  260. */
  261. public function testCompileWithHost($name, $arguments, $prefix, $regex, $variables, $pathVariables, $tokens, $hostRegex, $hostVariables, $hostTokens)
  262. {
  263. $r = new \ReflectionClass(Route::class);
  264. $route = $r->newInstanceArgs($arguments);
  265. $compiled = $route->compile();
  266. $this->assertEquals($prefix, $compiled->getStaticPrefix(), $name.' (static prefix)');
  267. $this->assertEquals($regex, str_replace(["\n", ' '], '', $compiled->getRegex()), $name.' (regex)');
  268. $this->assertEquals($variables, $compiled->getVariables(), $name.' (variables)');
  269. $this->assertEquals($pathVariables, $compiled->getPathVariables(), $name.' (path variables)');
  270. $this->assertEquals($tokens, $compiled->getTokens(), $name.' (tokens)');
  271. $this->assertEquals($hostRegex, str_replace(["\n", ' '], '', $compiled->getHostRegex()), $name.' (host regex)');
  272. $this->assertEquals($hostVariables, $compiled->getHostVariables(), $name.' (host variables)');
  273. $this->assertEquals($hostTokens, $compiled->getHostTokens(), $name.' (host tokens)');
  274. }
  275. public static function provideCompileWithHostData()
  276. {
  277. return [
  278. [
  279. 'Route with host pattern',
  280. ['/hello', [], [], [], 'www.example.com'],
  281. '/hello', '{^/hello$}sD', [], [], [
  282. ['text', '/hello'],
  283. ],
  284. '{^www\.example\.com$}sDi', [], [
  285. ['text', 'www.example.com'],
  286. ],
  287. ],
  288. [
  289. 'Route with host pattern and some variables',
  290. ['/hello/{name}', [], [], [], 'www.example.{tld}'],
  291. '/hello', '{^/hello/(?P<name>[^/]++)$}sD', ['tld', 'name'], ['name'], [
  292. ['variable', '/', '[^/]++', 'name'],
  293. ['text', '/hello'],
  294. ],
  295. '{^www\.example\.(?P<tld>[^\.]++)$}sDi', ['tld'], [
  296. ['variable', '.', '[^\.]++', 'tld'],
  297. ['text', 'www.example'],
  298. ],
  299. ],
  300. [
  301. 'Route with variable at beginning of host',
  302. ['/hello', [], [], [], '{locale}.example.{tld}'],
  303. '/hello', '{^/hello$}sD', ['locale', 'tld'], [], [
  304. ['text', '/hello'],
  305. ],
  306. '{^(?P<locale>[^\.]++)\.example\.(?P<tld>[^\.]++)$}sDi', ['locale', 'tld'], [
  307. ['variable', '.', '[^\.]++', 'tld'],
  308. ['text', '.example'],
  309. ['variable', '', '[^\.]++', 'locale'],
  310. ],
  311. ],
  312. [
  313. 'Route with host variables that has a default value',
  314. ['/hello', ['locale' => 'a', 'tld' => 'b'], [], [], '{locale}.example.{tld}'],
  315. '/hello', '{^/hello$}sD', ['locale', 'tld'], [], [
  316. ['text', '/hello'],
  317. ],
  318. '{^(?P<locale>[^\.]++)\.example\.(?P<tld>[^\.]++)$}sDi', ['locale', 'tld'], [
  319. ['variable', '.', '[^\.]++', 'tld'],
  320. ['text', '.example'],
  321. ['variable', '', '[^\.]++', 'locale'],
  322. ],
  323. ],
  324. ];
  325. }
  326. public function testRouteWithTooLongVariableName()
  327. {
  328. $this->expectException(\DomainException::class);
  329. $route = new Route(sprintf('/{%s}', str_repeat('a', RouteCompiler::VARIABLE_MAXIMUM_LENGTH + 1)));
  330. $route->compile();
  331. }
  332. /**
  333. * @dataProvider provideRemoveCapturingGroup
  334. */
  335. public function testRemoveCapturingGroup($regex, $requirement)
  336. {
  337. $route = new Route('/{foo}', [], ['foo' => $requirement]);
  338. $this->assertSame($regex, $route->compile()->getRegex());
  339. }
  340. public static function provideRemoveCapturingGroup()
  341. {
  342. yield ['{^/(?P<foo>a(?:b|c)(?:d|e)f)$}sD', 'a(b|c)(d|e)f'];
  343. yield ['{^/(?P<foo>a\(b\)c)$}sD', 'a\(b\)c'];
  344. yield ['{^/(?P<foo>(?:b))$}sD', '(?:b)'];
  345. yield ['{^/(?P<foo>(?(b)b))$}sD', '(?(b)b)'];
  346. yield ['{^/(?P<foo>(*F))$}sD', '(*F)'];
  347. yield ['{^/(?P<foo>(?:(?:foo)))$}sD', '((foo))'];
  348. }
  349. }
  350. class Utf8RouteCompiler extends RouteCompiler
  351. {
  352. public const SEPARATORS = '/§';
  353. }