RouteCompilerTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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. * @group legacy
  166. * @dataProvider provideCompileImplicitUtf8Data
  167. * @expectedDeprecation Using UTF-8 route %s without setting the "utf8" option is deprecated %s.
  168. */
  169. public function testCompileImplicitUtf8Data($name, $arguments, $prefix, $regex, $variables, $tokens, $deprecationType)
  170. {
  171. $r = new \ReflectionClass('Symfony\\Component\\Routing\\Route');
  172. $route = $r->newInstanceArgs($arguments);
  173. $compiled = $route->compile();
  174. $this->assertEquals($prefix, $compiled->getStaticPrefix(), $name.' (static prefix)');
  175. $this->assertEquals($regex, $compiled->getRegex(), $name.' (regex)');
  176. $this->assertEquals($variables, $compiled->getVariables(), $name.' (variables)');
  177. $this->assertEquals($tokens, $compiled->getTokens(), $name.' (tokens)');
  178. }
  179. public function provideCompileImplicitUtf8Data()
  180. {
  181. return [
  182. [
  183. 'Static UTF-8 route',
  184. ['/foé'],
  185. '/foé', '#^/foé$#sDu', [], [
  186. ['text', '/foé'],
  187. ],
  188. 'patterns',
  189. ],
  190. [
  191. 'Route with an implicit UTF-8 requirement',
  192. ['/{bar}', ['bar' => null], ['bar' => 'é']],
  193. '', '#^/(?P<bar>é)?$#sDu', ['bar'], [
  194. ['variable', '/', 'é', 'bar', true],
  195. ],
  196. 'requirements',
  197. ],
  198. [
  199. 'Route with a UTF-8 class requirement',
  200. ['/{bar}', ['bar' => null], ['bar' => '\pM']],
  201. '', '#^/(?P<bar>\pM)?$#sDu', ['bar'], [
  202. ['variable', '/', '\pM', 'bar', true],
  203. ],
  204. 'requirements',
  205. ],
  206. [
  207. 'Route with a UTF-8 separator',
  208. ['/foo/{bar}§{_format}', [], [], ['compiler_class' => Utf8RouteCompiler::class]],
  209. '/foo', '#^/foo/(?P<bar>[^/§]++)§(?P<_format>[^/]++)$#sDu', ['bar', '_format'], [
  210. ['variable', '§', '[^/]++', '_format', true],
  211. ['variable', '/', '[^/§]++', 'bar', true],
  212. ['text', '/foo'],
  213. ],
  214. 'patterns',
  215. ],
  216. ];
  217. }
  218. /**
  219. * @expectedException \LogicException
  220. */
  221. public function testRouteWithSameVariableTwice()
  222. {
  223. $route = new Route('/{name}/{name}');
  224. $compiled = $route->compile();
  225. }
  226. /**
  227. * @expectedException \LogicException
  228. */
  229. public function testRouteCharsetMismatch()
  230. {
  231. $route = new Route("/\xE9/{bar}", [], ['bar' => '.'], ['utf8' => true]);
  232. $compiled = $route->compile();
  233. }
  234. /**
  235. * @expectedException \LogicException
  236. */
  237. public function testRequirementCharsetMismatch()
  238. {
  239. $route = new Route('/foo/{bar}', [], ['bar' => "\xE9"], ['utf8' => true]);
  240. $compiled = $route->compile();
  241. }
  242. /**
  243. * @expectedException \InvalidArgumentException
  244. */
  245. public function testRouteWithFragmentAsPathParameter()
  246. {
  247. $route = new Route('/{_fragment}');
  248. $compiled = $route->compile();
  249. }
  250. /**
  251. * @dataProvider getVariableNamesStartingWithADigit
  252. * @expectedException \DomainException
  253. */
  254. public function testRouteWithVariableNameStartingWithADigit($name)
  255. {
  256. $route = new Route('/{'.$name.'}');
  257. $route->compile();
  258. }
  259. public function getVariableNamesStartingWithADigit()
  260. {
  261. return [
  262. ['09'],
  263. ['123'],
  264. ['1e2'],
  265. ];
  266. }
  267. /**
  268. * @dataProvider provideCompileWithHostData
  269. */
  270. public function testCompileWithHost($name, $arguments, $prefix, $regex, $variables, $pathVariables, $tokens, $hostRegex, $hostVariables, $hostTokens)
  271. {
  272. $r = new \ReflectionClass('Symfony\\Component\\Routing\\Route');
  273. $route = $r->newInstanceArgs($arguments);
  274. $compiled = $route->compile();
  275. $this->assertEquals($prefix, $compiled->getStaticPrefix(), $name.' (static prefix)');
  276. $this->assertEquals($regex, str_replace(["\n", ' '], '', $compiled->getRegex()), $name.' (regex)');
  277. $this->assertEquals($variables, $compiled->getVariables(), $name.' (variables)');
  278. $this->assertEquals($pathVariables, $compiled->getPathVariables(), $name.' (path variables)');
  279. $this->assertEquals($tokens, $compiled->getTokens(), $name.' (tokens)');
  280. $this->assertEquals($hostRegex, str_replace(["\n", ' '], '', $compiled->getHostRegex()), $name.' (host regex)');
  281. $this->assertEquals($hostVariables, $compiled->getHostVariables(), $name.' (host variables)');
  282. $this->assertEquals($hostTokens, $compiled->getHostTokens(), $name.' (host tokens)');
  283. }
  284. public function provideCompileWithHostData()
  285. {
  286. return [
  287. [
  288. 'Route with host pattern',
  289. ['/hello', [], [], [], 'www.example.com'],
  290. '/hello', '#^/hello$#sD', [], [], [
  291. ['text', '/hello'],
  292. ],
  293. '#^www\.example\.com$#sDi', [], [
  294. ['text', 'www.example.com'],
  295. ],
  296. ],
  297. [
  298. 'Route with host pattern and some variables',
  299. ['/hello/{name}', [], [], [], 'www.example.{tld}'],
  300. '/hello', '#^/hello/(?P<name>[^/]++)$#sD', ['tld', 'name'], ['name'], [
  301. ['variable', '/', '[^/]++', 'name'],
  302. ['text', '/hello'],
  303. ],
  304. '#^www\.example\.(?P<tld>[^\.]++)$#sDi', ['tld'], [
  305. ['variable', '.', '[^\.]++', 'tld'],
  306. ['text', 'www.example'],
  307. ],
  308. ],
  309. [
  310. 'Route with variable at beginning of host',
  311. ['/hello', [], [], [], '{locale}.example.{tld}'],
  312. '/hello', '#^/hello$#sD', ['locale', 'tld'], [], [
  313. ['text', '/hello'],
  314. ],
  315. '#^(?P<locale>[^\.]++)\.example\.(?P<tld>[^\.]++)$#sDi', ['locale', 'tld'], [
  316. ['variable', '.', '[^\.]++', 'tld'],
  317. ['text', '.example'],
  318. ['variable', '', '[^\.]++', 'locale'],
  319. ],
  320. ],
  321. [
  322. 'Route with host variables that has a default value',
  323. ['/hello', ['locale' => 'a', 'tld' => 'b'], [], [], '{locale}.example.{tld}'],
  324. '/hello', '#^/hello$#sD', ['locale', 'tld'], [], [
  325. ['text', '/hello'],
  326. ],
  327. '#^(?P<locale>[^\.]++)\.example\.(?P<tld>[^\.]++)$#sDi', ['locale', 'tld'], [
  328. ['variable', '.', '[^\.]++', 'tld'],
  329. ['text', '.example'],
  330. ['variable', '', '[^\.]++', 'locale'],
  331. ],
  332. ],
  333. ];
  334. }
  335. /**
  336. * @expectedException \DomainException
  337. */
  338. public function testRouteWithTooLongVariableName()
  339. {
  340. $route = new Route(sprintf('/{%s}', str_repeat('a', RouteCompiler::VARIABLE_MAXIMUM_LENGTH + 1)));
  341. $route->compile();
  342. }
  343. }
  344. class Utf8RouteCompiler extends RouteCompiler
  345. {
  346. const SEPARATORS = '/§';
  347. }