RouteCompilerTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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$#sD', array(), array(
  36. array('text', '/foo'),
  37. ),
  38. ),
  39. array(
  40. 'Route with a variable',
  41. array('/foo/{bar}'),
  42. '/foo', '#^/foo/(?P<bar>[^/]++)$#sD', 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>[^/]++))?$#sD', 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>[^/]++)$#sD', 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>[^/]++))?)?$#sD', 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>[^/]++)$#sD', 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>[^/]++)?$#sD', 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)?$#sD', 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))?$#sD', 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>[^/]++))?$#sD', 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>[^/]++)$#sD', 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\}$#sD', 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>[^/]++))?)?$#sD', 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>[^/]++)$#sD', array('bar', '_format'), array(
  143. array('variable', '.', '[^/]++', '_format'),
  144. array('variable', '/', '[^/\.]++', 'bar'),
  145. array('text', '/foo'),
  146. ),
  147. ),
  148. );
  149. }
  150. /**
  151. * @expectedException \LogicException
  152. */
  153. public function testRouteWithSameVariableTwice()
  154. {
  155. $route = new Route('/{name}/{name}');
  156. $compiled = $route->compile();
  157. }
  158. /**
  159. * @dataProvider getVariableNamesStartingWithADigit
  160. * @expectedException \DomainException
  161. */
  162. public function testRouteWithVariableNameStartingWithADigit($name)
  163. {
  164. $route = new Route('/{'.$name.'}');
  165. $route->compile();
  166. }
  167. public function getVariableNamesStartingWithADigit()
  168. {
  169. return array(
  170. array('09'),
  171. array('123'),
  172. array('1e2'),
  173. );
  174. }
  175. /**
  176. * @dataProvider provideCompileWithHostData
  177. */
  178. public function testCompileWithHost($name, $arguments, $prefix, $regex, $variables, $pathVariables, $tokens, $hostRegex, $hostVariables, $hostTokens)
  179. {
  180. $r = new \ReflectionClass('Symfony\\Component\\Routing\\Route');
  181. $route = $r->newInstanceArgs($arguments);
  182. $compiled = $route->compile();
  183. $this->assertEquals($prefix, $compiled->getStaticPrefix(), $name.' (static prefix)');
  184. $this->assertEquals($regex, str_replace(array("\n", ' '), '', $compiled->getRegex()), $name.' (regex)');
  185. $this->assertEquals($variables, $compiled->getVariables(), $name.' (variables)');
  186. $this->assertEquals($pathVariables, $compiled->getPathVariables(), $name.' (path variables)');
  187. $this->assertEquals($tokens, $compiled->getTokens(), $name.' (tokens)');
  188. $this->assertEquals($hostRegex, str_replace(array("\n", ' '), '', $compiled->getHostRegex()), $name.' (host regex)');
  189. $this->assertEquals($hostVariables, $compiled->getHostVariables(), $name.' (host variables)');
  190. $this->assertEquals($hostTokens, $compiled->getHostTokens(), $name.' (host tokens)');
  191. }
  192. public function provideCompileWithHostData()
  193. {
  194. return array(
  195. array(
  196. 'Route with host pattern',
  197. array('/hello', array(), array(), array(), 'www.example.com'),
  198. '/hello', '#^/hello$#sD', array(), array(), array(
  199. array('text', '/hello'),
  200. ),
  201. '#^www\.example\.com$#sDi', array(), array(
  202. array('text', 'www.example.com'),
  203. ),
  204. ),
  205. array(
  206. 'Route with host pattern and some variables',
  207. array('/hello/{name}', array(), array(), array(), 'www.example.{tld}'),
  208. '/hello', '#^/hello/(?P<name>[^/]++)$#sD', array('tld', 'name'), array('name'), array(
  209. array('variable', '/', '[^/]++', 'name'),
  210. array('text', '/hello'),
  211. ),
  212. '#^www\.example\.(?P<tld>[^\.]++)$#sDi', array('tld'), array(
  213. array('variable', '.', '[^\.]++', 'tld'),
  214. array('text', 'www.example'),
  215. ),
  216. ),
  217. array(
  218. 'Route with variable at beginning of host',
  219. array('/hello', array(), array(), array(), '{locale}.example.{tld}'),
  220. '/hello', '#^/hello$#sD', array('locale', 'tld'), array(), array(
  221. array('text', '/hello'),
  222. ),
  223. '#^(?P<locale>[^\.]++)\.example\.(?P<tld>[^\.]++)$#sDi', array('locale', 'tld'), array(
  224. array('variable', '.', '[^\.]++', 'tld'),
  225. array('text', '.example'),
  226. array('variable', '', '[^\.]++', 'locale'),
  227. ),
  228. ),
  229. array(
  230. 'Route with host variables that has a default value',
  231. array('/hello', array('locale' => 'a', 'tld' => 'b'), array(), array(), '{locale}.example.{tld}'),
  232. '/hello', '#^/hello$#sD', array('locale', 'tld'), array(), array(
  233. array('text', '/hello'),
  234. ),
  235. '#^(?P<locale>[^\.]++)\.example\.(?P<tld>[^\.]++)$#sDi', array('locale', 'tld'), array(
  236. array('variable', '.', '[^\.]++', 'tld'),
  237. array('text', '.example'),
  238. array('variable', '', '[^\.]++', 'locale'),
  239. ),
  240. ),
  241. );
  242. }
  243. /**
  244. * @expectedException \DomainException
  245. */
  246. public function testRouteWithTooLongVariableName()
  247. {
  248. $route = new Route(sprintf('/{%s}', str_repeat('a', RouteCompiler::VARIABLE_MAXIMUM_LENGTH + 1)));
  249. $route->compile();
  250. }
  251. }