RouteCompilerTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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 array(
  32. array(
  33. 'Static route',
  34. array('/foo'),
  35. '/foo', '#^/foo$#s', array(), array(
  36. array('text', '/foo'),
  37. ),
  38. ),
  39. array(
  40. 'Route with a variable',
  41. array('/foo/{bar}'),
  42. '/foo', '#^/foo/(?P<bar>[^/]++)$#s', array('bar'), array(
  43. array('variable', '/', '[^/]++', 'bar'),
  44. array('text', '/foo'),
  45. ),
  46. ),
  47. array(
  48. 'Route with a variable that has a default value',
  49. array('/foo/{bar}', array('bar' => 'bar')),
  50. '/foo', '#^/foo(?:/(?P<bar>[^/]++))?$#s', array('bar'), array(
  51. array('variable', '/', '[^/]++', 'bar'),
  52. array('text', '/foo'),
  53. ),
  54. ),
  55. array(
  56. 'Route with several variables',
  57. array('/foo/{bar}/{foobar}'),
  58. '/foo', '#^/foo/(?P<bar>[^/]++)/(?P<foobar>[^/]++)$#s', array('bar', 'foobar'), array(
  59. array('variable', '/', '[^/]++', 'foobar'),
  60. array('variable', '/', '[^/]++', 'bar'),
  61. array('text', '/foo'),
  62. ),
  63. ),
  64. array(
  65. 'Route with several variables that have default values',
  66. array('/foo/{bar}/{foobar}', array('bar' => 'bar', 'foobar' => '')),
  67. '/foo', '#^/foo(?:/(?P<bar>[^/]++)(?:/(?P<foobar>[^/]++))?)?$#s', array('bar', 'foobar'), array(
  68. array('variable', '/', '[^/]++', 'foobar'),
  69. array('variable', '/', '[^/]++', 'bar'),
  70. array('text', '/foo'),
  71. ),
  72. ),
  73. array(
  74. 'Route with several variables but some of them have no default values',
  75. array('/foo/{bar}/{foobar}', array('bar' => 'bar')),
  76. '/foo', '#^/foo/(?P<bar>[^/]++)/(?P<foobar>[^/]++)$#s', array('bar', 'foobar'), array(
  77. array('variable', '/', '[^/]++', 'foobar'),
  78. array('variable', '/', '[^/]++', 'bar'),
  79. array('text', '/foo'),
  80. ),
  81. ),
  82. array(
  83. 'Route with an optional variable as the first segment',
  84. array('/{bar}', array('bar' => 'bar')),
  85. '', '#^/(?P<bar>[^/]++)?$#s', array('bar'), array(
  86. array('variable', '/', '[^/]++', 'bar'),
  87. ),
  88. ),
  89. array(
  90. 'Route with a requirement of 0',
  91. array('/{bar}', array('bar' => null), array('bar' => '0')),
  92. '', '#^/(?P<bar>0)?$#s', array('bar'), array(
  93. array('variable', '/', '0', 'bar'),
  94. ),
  95. ),
  96. array(
  97. 'Route with an optional variable as the first segment with requirements',
  98. array('/{bar}', array('bar' => 'bar'), array('bar' => '(foo|bar)')),
  99. '', '#^/(?P<bar>(foo|bar))?$#s', array('bar'), array(
  100. array('variable', '/', '(foo|bar)', 'bar'),
  101. ),
  102. ),
  103. array(
  104. 'Route with only optional variables',
  105. array('/{foo}/{bar}', array('foo' => 'foo', 'bar' => 'bar')),
  106. '', '#^/(?P<foo>[^/]++)?(?:/(?P<bar>[^/]++))?$#s', array('foo', 'bar'), array(
  107. array('variable', '/', '[^/]++', 'bar'),
  108. array('variable', '/', '[^/]++', 'foo'),
  109. ),
  110. ),
  111. array(
  112. 'Route with a variable in last position',
  113. array('/foo-{bar}'),
  114. '/foo-', '#^/foo\-(?P<bar>[^/]++)$#s', array('bar'), array(
  115. array('variable', '-', '[^/]++', 'bar'),
  116. array('text', '/foo'),
  117. ),
  118. ),
  119. array(
  120. 'Route with nested placeholders',
  121. array('/{static{var}static}'),
  122. '/{static', '#^/\{static(?P<var>[^/]+)static\}$#s', array('var'), array(
  123. array('text', 'static}'),
  124. array('variable', '', '[^/]+', 'var'),
  125. array('text', '/{static'),
  126. ),
  127. ),
  128. array(
  129. 'Route without separator between variables',
  130. array('/{w}{x}{y}{z}.{_format}', array('z' => 'default-z', '_format' => 'html'), array('y' => '(y|Y)')),
  131. '', '#^/(?P<w>[^/\.]+)(?P<x>[^/\.]+)(?P<y>(y|Y))(?:(?P<z>[^/\.]++)(?:\.(?P<_format>[^/]++))?)?$#s', array('w', 'x', 'y', 'z', '_format'), array(
  132. array('variable', '.', '[^/]++', '_format'),
  133. array('variable', '', '[^/\.]++', 'z'),
  134. array('variable', '', '(y|Y)', 'y'),
  135. array('variable', '', '[^/\.]+', 'x'),
  136. array('variable', '/', '[^/\.]+', 'w'),
  137. ),
  138. ),
  139. array(
  140. 'Route with a format',
  141. array('/foo/{bar}.{_format}'),
  142. '/foo', '#^/foo/(?P<bar>[^/\.]++)\.(?P<_format>[^/]++)$#s', array('bar', '_format'), array(
  143. array('variable', '.', '[^/]++', '_format'),
  144. array('variable', '/', '[^/\.]++', 'bar'),
  145. array('text', '/foo'),
  146. ),
  147. ),
  148. array(
  149. 'Static non UTF-8 route',
  150. array("/fo\xE9"),
  151. "/fo\xE9", "#^/fo\xE9$#s", array(), array(
  152. array('text', "/fo\xE9"),
  153. ),
  154. ),
  155. array(
  156. 'Route with an explicit UTF-8 requirement',
  157. array('/{bar}', array('bar' => null), array('bar' => '.'), array('utf8' => true)),
  158. '', '#^/(?P<bar>.)?$#su', array('bar'), array(
  159. array('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 array(
  182. array(
  183. 'Static UTF-8 route',
  184. array('/foé'),
  185. '/foé', '#^/foé$#su', array(), array(
  186. array('text', '/foé'),
  187. ),
  188. 'patterns',
  189. ),
  190. array(
  191. 'Route with an implicit UTF-8 requirement',
  192. array('/{bar}', array('bar' => null), array('bar' => 'é')),
  193. '', '#^/(?P<bar>é)?$#su', array('bar'), array(
  194. array('variable', '/', 'é', 'bar', true),
  195. ),
  196. 'requirements',
  197. ),
  198. array(
  199. 'Route with a UTF-8 class requirement',
  200. array('/{bar}', array('bar' => null), array('bar' => '\pM')),
  201. '', '#^/(?P<bar>\pM)?$#su', array('bar'), array(
  202. array('variable', '/', '\pM', 'bar', true),
  203. ),
  204. 'requirements',
  205. ),
  206. array(
  207. 'Route with a UTF-8 separator',
  208. array('/foo/{bar}§{_format}', array(), array(), array('compiler_class' => Utf8RouteCompiler::class)),
  209. '/foo', '#^/foo/(?P<bar>[^/§]++)§(?P<_format>[^/]++)$#su', array('bar', '_format'), array(
  210. array('variable', '§', '[^/]++', '_format', true),
  211. array('variable', '/', '[^/§]++', 'bar', true),
  212. array('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}", array(), array('bar' => '.'), array('utf8' => true));
  232. $compiled = $route->compile();
  233. }
  234. /**
  235. * @expectedException \LogicException
  236. */
  237. public function testRequirementCharsetMismatch()
  238. {
  239. $route = new Route('/foo/{bar}', array(), array('bar' => "\xE9"), array('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 array(
  262. array('09'),
  263. array('123'),
  264. array('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(array("\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(array("\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 array(
  287. array(
  288. 'Route with host pattern',
  289. array('/hello', array(), array(), array(), 'www.example.com'),
  290. '/hello', '#^/hello$#s', array(), array(), array(
  291. array('text', '/hello'),
  292. ),
  293. '#^www\.example\.com$#si', array(), array(
  294. array('text', 'www.example.com'),
  295. ),
  296. ),
  297. array(
  298. 'Route with host pattern and some variables',
  299. array('/hello/{name}', array(), array(), array(), 'www.example.{tld}'),
  300. '/hello', '#^/hello/(?P<name>[^/]++)$#s', array('tld', 'name'), array('name'), array(
  301. array('variable', '/', '[^/]++', 'name'),
  302. array('text', '/hello'),
  303. ),
  304. '#^www\.example\.(?P<tld>[^\.]++)$#si', array('tld'), array(
  305. array('variable', '.', '[^\.]++', 'tld'),
  306. array('text', 'www.example'),
  307. ),
  308. ),
  309. array(
  310. 'Route with variable at beginning of host',
  311. array('/hello', array(), array(), array(), '{locale}.example.{tld}'),
  312. '/hello', '#^/hello$#s', array('locale', 'tld'), array(), array(
  313. array('text', '/hello'),
  314. ),
  315. '#^(?P<locale>[^\.]++)\.example\.(?P<tld>[^\.]++)$#si', array('locale', 'tld'), array(
  316. array('variable', '.', '[^\.]++', 'tld'),
  317. array('text', '.example'),
  318. array('variable', '', '[^\.]++', 'locale'),
  319. ),
  320. ),
  321. array(
  322. 'Route with host variables that has a default value',
  323. array('/hello', array('locale' => 'a', 'tld' => 'b'), array(), array(), '{locale}.example.{tld}'),
  324. '/hello', '#^/hello$#s', array('locale', 'tld'), array(), array(
  325. array('text', '/hello'),
  326. ),
  327. '#^(?P<locale>[^\.]++)\.example\.(?P<tld>[^\.]++)$#si', array('locale', 'tld'), array(
  328. array('variable', '.', '[^\.]++', 'tld'),
  329. array('text', '.example'),
  330. array('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. }