PageRenderTime 46ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/symfony/routing/Tests/RouteCompilerTest.php

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