RouteCompilerTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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 Symfony\Component\Routing\Route;
  12. class RouteCompilerTest extends \PHPUnit_Framework_TestCase
  13. {
  14. /**
  15. * @dataProvider provideCompileData
  16. */
  17. public function testCompile($name, $arguments, $prefix, $regex, $variables, $tokens)
  18. {
  19. $r = new \ReflectionClass('Symfony\\Component\\Routing\\Route');
  20. $route = $r->newInstanceArgs($arguments);
  21. $compiled = $route->compile();
  22. $this->assertEquals($prefix, $compiled->getStaticPrefix(), $name.' (static prefix)');
  23. $this->assertEquals($regex, $compiled->getRegex(), $name.' (regex)');
  24. $this->assertEquals($variables, $compiled->getVariables(), $name.' (variables)');
  25. $this->assertEquals($tokens, $compiled->getTokens(), $name.' (tokens)');
  26. }
  27. public function provideCompileData()
  28. {
  29. return array(
  30. array(
  31. 'Static route',
  32. array('/foo'),
  33. '/foo', '#^/foo$#s', array(), array(
  34. array('text', '/foo'),
  35. )),
  36. array(
  37. 'Route with a variable',
  38. array('/foo/{bar}'),
  39. '/foo', '#^/foo/(?P<bar>[^/]++)$#s', array('bar'), array(
  40. array('variable', '/', '[^/]++', 'bar'),
  41. array('text', '/foo'),
  42. )),
  43. array(
  44. 'Route with a variable that has a default value',
  45. array('/foo/{bar}', array('bar' => 'bar')),
  46. '/foo', '#^/foo(?:/(?P<bar>[^/]++))?$#s', array('bar'), array(
  47. array('variable', '/', '[^/]++', 'bar'),
  48. array('text', '/foo'),
  49. )),
  50. array(
  51. 'Route with several variables',
  52. array('/foo/{bar}/{foobar}'),
  53. '/foo', '#^/foo/(?P<bar>[^/]++)/(?P<foobar>[^/]++)$#s', array('bar', 'foobar'), array(
  54. array('variable', '/', '[^/]++', 'foobar'),
  55. array('variable', '/', '[^/]++', 'bar'),
  56. array('text', '/foo'),
  57. )),
  58. array(
  59. 'Route with several variables that have default values',
  60. array('/foo/{bar}/{foobar}', array('bar' => 'bar', 'foobar' => '')),
  61. '/foo', '#^/foo(?:/(?P<bar>[^/]++)(?:/(?P<foobar>[^/]++))?)?$#s', array('bar', 'foobar'), array(
  62. array('variable', '/', '[^/]++', 'foobar'),
  63. array('variable', '/', '[^/]++', 'bar'),
  64. array('text', '/foo'),
  65. )),
  66. array(
  67. 'Route with several variables but some of them have no default values',
  68. array('/foo/{bar}/{foobar}', array('bar' => 'bar')),
  69. '/foo', '#^/foo/(?P<bar>[^/]++)/(?P<foobar>[^/]++)$#s', array('bar', 'foobar'), array(
  70. array('variable', '/', '[^/]++', 'foobar'),
  71. array('variable', '/', '[^/]++', 'bar'),
  72. array('text', '/foo'),
  73. )),
  74. array(
  75. 'Route with an optional variable as the first segment',
  76. array('/{bar}', array('bar' => 'bar')),
  77. '', '#^/(?P<bar>[^/]++)?$#s', array('bar'), array(
  78. array('variable', '/', '[^/]++', 'bar'),
  79. )),
  80. array(
  81. 'Route with a requirement of 0',
  82. array('/{bar}', array('bar' => null), array('bar' => '0')),
  83. '', '#^/(?P<bar>0)?$#s', array('bar'), array(
  84. array('variable', '/', '0', 'bar'),
  85. )),
  86. array(
  87. 'Route with an optional variable as the first segment with requirements',
  88. array('/{bar}', array('bar' => 'bar'), array('bar' => '(foo|bar)')),
  89. '', '#^/(?P<bar>(foo|bar))?$#s', array('bar'), array(
  90. array('variable', '/', '(foo|bar)', 'bar'),
  91. )),
  92. array(
  93. 'Route with only optional variables',
  94. array('/{foo}/{bar}', array('foo' => 'foo', 'bar' => 'bar')),
  95. '', '#^/(?P<foo>[^/]++)?(?:/(?P<bar>[^/]++))?$#s', array('foo', 'bar'), array(
  96. array('variable', '/', '[^/]++', 'bar'),
  97. array('variable', '/', '[^/]++', 'foo'),
  98. )),
  99. array(
  100. 'Route with a variable in last position',
  101. array('/foo-{bar}'),
  102. '/foo', '#^/foo\-(?P<bar>[^/]++)$#s', array('bar'), array(
  103. array('variable', '-', '[^/]++', 'bar'),
  104. array('text', '/foo'),
  105. )),
  106. array(
  107. 'Route with nested placeholders',
  108. array('/{static{var}static}'),
  109. '/{static', '#^/\{static(?P<var>[^/]+)static\}$#s', array('var'), array(
  110. array('text', 'static}'),
  111. array('variable', '', '[^/]+', 'var'),
  112. array('text', '/{static'),
  113. )),
  114. array(
  115. 'Route without separator between variables',
  116. array('/{w}{x}{y}{z}.{_format}', array('z' => 'default-z', '_format' => 'html'), array('y' => '(y|Y)')),
  117. '', '#^/(?P<w>[^/\.]+)(?P<x>[^/\.]+)(?P<y>(y|Y))(?:(?P<z>[^/\.]++)(?:\.(?P<_format>[^/]++))?)?$#s', array('w', 'x', 'y', 'z', '_format'), array(
  118. array('variable', '.', '[^/]++', '_format'),
  119. array('variable', '', '[^/\.]++', 'z'),
  120. array('variable', '', '(y|Y)', 'y'),
  121. array('variable', '', '[^/\.]+', 'x'),
  122. array('variable', '/', '[^/\.]+', 'w'),
  123. )),
  124. array(
  125. 'Route with a format',
  126. array('/foo/{bar}.{_format}'),
  127. '/foo', '#^/foo/(?P<bar>[^/\.]++)\.(?P<_format>[^/]++)$#s', array('bar', '_format'), array(
  128. array('variable', '.', '[^/]++', '_format'),
  129. array('variable', '/', '[^/\.]++', 'bar'),
  130. array('text', '/foo'),
  131. )),
  132. );
  133. }
  134. /**
  135. * @expectedException \LogicException
  136. */
  137. public function testRouteWithSameVariableTwice()
  138. {
  139. $route = new Route('/{name}/{name}');
  140. $compiled = $route->compile();
  141. }
  142. /**
  143. * @dataProvider getNumericVariableNames
  144. * @expectedException \DomainException
  145. */
  146. public function testRouteWithNumericVariableName($name)
  147. {
  148. $route = new Route('/{'. $name.'}');
  149. $route->compile();
  150. }
  151. public function getNumericVariableNames()
  152. {
  153. return array(
  154. array('09'),
  155. array('123'),
  156. array('1e2')
  157. );
  158. }
  159. /**
  160. * @dataProvider provideCompileWithHostData
  161. */
  162. public function testCompileWithHost($name, $arguments, $prefix, $regex, $variables, $pathVariables, $tokens, $hostRegex, $hostVariables, $hostTokens)
  163. {
  164. $r = new \ReflectionClass('Symfony\\Component\\Routing\\Route');
  165. $route = $r->newInstanceArgs($arguments);
  166. $compiled = $route->compile();
  167. $this->assertEquals($prefix, $compiled->getStaticPrefix(), $name.' (static prefix)');
  168. $this->assertEquals($regex, str_replace(array("\n", ' '), '', $compiled->getRegex()), $name.' (regex)');
  169. $this->assertEquals($variables, $compiled->getVariables(), $name.' (variables)');
  170. $this->assertEquals($pathVariables, $compiled->getPathVariables(), $name.' (path variables)');
  171. $this->assertEquals($tokens, $compiled->getTokens(), $name.' (tokens)');
  172. $this->assertEquals($hostRegex, str_replace(array("\n", ' '), '', $compiled->getHostRegex()), $name.' (host regex)');
  173. $this->assertEquals($hostVariables, $compiled->getHostVariables(), $name.' (host variables)');
  174. $this->assertEquals($hostTokens, $compiled->getHostTokens(), $name.' (host tokens)');
  175. }
  176. public function provideCompileWithHostData()
  177. {
  178. return array(
  179. array(
  180. 'Route with host pattern',
  181. array('/hello', array(), array(), array(), 'www.example.com'),
  182. '/hello', '#^/hello$#s', array(), array(), array(
  183. array('text', '/hello'),
  184. ),
  185. '#^www\.example\.com$#s', array(), array(
  186. array('text', 'www.example.com'),
  187. ),
  188. ),
  189. array(
  190. 'Route with host pattern and some variables',
  191. array('/hello/{name}', array(), array(), array(), 'www.example.{tld}'),
  192. '/hello', '#^/hello/(?P<name>[^/]++)$#s', array('tld', 'name'), array('name'), array(
  193. array('variable', '/', '[^/]++', 'name'),
  194. array('text', '/hello'),
  195. ),
  196. '#^www\.example\.(?P<tld>[^\.]++)$#s', array('tld'), array(
  197. array('variable', '.', '[^\.]++', 'tld'),
  198. array('text', 'www.example'),
  199. ),
  200. ),
  201. array(
  202. 'Route with variable at beginning of host',
  203. array('/hello', array(), array(), array(), '{locale}.example.{tld}'),
  204. '/hello', '#^/hello$#s', array('locale', 'tld'), array(), array(
  205. array('text', '/hello'),
  206. ),
  207. '#^(?P<locale>[^\.]++)\.example\.(?P<tld>[^\.]++)$#s', array('locale', 'tld'), array(
  208. array('variable', '.', '[^\.]++', 'tld'),
  209. array('text', '.example'),
  210. array('variable', '', '[^\.]++', 'locale'),
  211. ),
  212. ),
  213. array(
  214. 'Route with host variables that has a default value',
  215. array('/hello', array('locale' => 'a', 'tld' => 'b'), array(), array(), '{locale}.example.{tld}'),
  216. '/hello', '#^/hello$#s', array('locale', 'tld'), array(), array(
  217. array('text', '/hello'),
  218. ),
  219. '#^(?P<locale>[^\.]++)\.example\.(?P<tld>[^\.]++)$#s', array('locale', 'tld'), array(
  220. array('variable', '.', '[^\.]++', 'tld'),
  221. array('text', '.example'),
  222. array('variable', '', '[^\.]++', 'locale'),
  223. ),
  224. ),
  225. );
  226. }
  227. }