PageRenderTime 53ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/symfony/routing/Tests/RouteTest.php

https://gitlab.com/ealexis.t/trends
PHP | 239 lines | 184 code | 34 blank | 21 comment | 0 complexity | 197dd361d74c7f23ea3f01dd69d7aad6 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 RouteTest extends \PHPUnit_Framework_TestCase
  13. {
  14. public function testConstructor()
  15. {
  16. $route = new Route('/{foo}', array('foo' => 'bar'), array('foo' => '\d+'), array('foo' => 'bar'), '{locale}.example.com');
  17. $this->assertEquals('/{foo}', $route->getPath(), '__construct() takes a path as its first argument');
  18. $this->assertEquals(array('foo' => 'bar'), $route->getDefaults(), '__construct() takes defaults as its second argument');
  19. $this->assertEquals(array('foo' => '\d+'), $route->getRequirements(), '__construct() takes requirements as its third argument');
  20. $this->assertEquals('bar', $route->getOption('foo'), '__construct() takes options as its fourth argument');
  21. $this->assertEquals('{locale}.example.com', $route->getHost(), '__construct() takes a host pattern as its fifth argument');
  22. $route = new Route('/', array(), array(), array(), '', array('Https'), array('POST', 'put'), 'context.getMethod() == "GET"');
  23. $this->assertEquals(array('https'), $route->getSchemes(), '__construct() takes schemes as its sixth argument and lowercases it');
  24. $this->assertEquals(array('POST', 'PUT'), $route->getMethods(), '__construct() takes methods as its seventh argument and uppercases it');
  25. $this->assertEquals('context.getMethod() == "GET"', $route->getCondition(), '__construct() takes a condition as its eight argument');
  26. $route = new Route('/', array(), array(), array(), '', 'Https', 'Post');
  27. $this->assertEquals(array('https'), $route->getSchemes(), '__construct() takes a single scheme as its sixth argument');
  28. $this->assertEquals(array('POST'), $route->getMethods(), '__construct() takes a single method as its seventh argument');
  29. }
  30. public function testPath()
  31. {
  32. $route = new Route('/{foo}');
  33. $route->setPath('/{bar}');
  34. $this->assertEquals('/{bar}', $route->getPath(), '->setPath() sets the path');
  35. $route->setPath('');
  36. $this->assertEquals('/', $route->getPath(), '->setPath() adds a / at the beginning of the path if needed');
  37. $route->setPath('bar');
  38. $this->assertEquals('/bar', $route->getPath(), '->setPath() adds a / at the beginning of the path if needed');
  39. $this->assertEquals($route, $route->setPath(''), '->setPath() implements a fluent interface');
  40. $route->setPath('//path');
  41. $this->assertEquals('/path', $route->getPath(), '->setPath() does not allow two slashes "//" at the beginning of the path as it would be confused with a network path when generating the path from the route');
  42. }
  43. public function testOptions()
  44. {
  45. $route = new Route('/{foo}');
  46. $route->setOptions(array('foo' => 'bar'));
  47. $this->assertEquals(array_merge(array(
  48. 'compiler_class' => 'Symfony\\Component\\Routing\\RouteCompiler',
  49. ), array('foo' => 'bar')), $route->getOptions(), '->setOptions() sets the options');
  50. $this->assertEquals($route, $route->setOptions(array()), '->setOptions() implements a fluent interface');
  51. $route->setOptions(array('foo' => 'foo'));
  52. $route->addOptions(array('bar' => 'bar'));
  53. $this->assertEquals($route, $route->addOptions(array()), '->addOptions() implements a fluent interface');
  54. $this->assertEquals(array('foo' => 'foo', 'bar' => 'bar', 'compiler_class' => 'Symfony\\Component\\Routing\\RouteCompiler'), $route->getOptions(), '->addDefaults() keep previous defaults');
  55. }
  56. public function testOption()
  57. {
  58. $route = new Route('/{foo}');
  59. $this->assertFalse($route->hasOption('foo'), '->hasOption() return false if option is not set');
  60. $this->assertEquals($route, $route->setOption('foo', 'bar'), '->setOption() implements a fluent interface');
  61. $this->assertEquals('bar', $route->getOption('foo'), '->setOption() sets the option');
  62. $this->assertTrue($route->hasOption('foo'), '->hasOption() return true if option is set');
  63. }
  64. public function testDefaults()
  65. {
  66. $route = new Route('/{foo}');
  67. $route->setDefaults(array('foo' => 'bar'));
  68. $this->assertEquals(array('foo' => 'bar'), $route->getDefaults(), '->setDefaults() sets the defaults');
  69. $this->assertEquals($route, $route->setDefaults(array()), '->setDefaults() implements a fluent interface');
  70. $route->setDefault('foo', 'bar');
  71. $this->assertEquals('bar', $route->getDefault('foo'), '->setDefault() sets a default value');
  72. $route->setDefault('foo2', 'bar2');
  73. $this->assertEquals('bar2', $route->getDefault('foo2'), '->getDefault() return the default value');
  74. $this->assertNull($route->getDefault('not_defined'), '->getDefault() return null if default value is not set');
  75. $route->setDefault('_controller', $closure = function () { return 'Hello'; });
  76. $this->assertEquals($closure, $route->getDefault('_controller'), '->setDefault() sets a default value');
  77. $route->setDefaults(array('foo' => 'foo'));
  78. $route->addDefaults(array('bar' => 'bar'));
  79. $this->assertEquals($route, $route->addDefaults(array()), '->addDefaults() implements a fluent interface');
  80. $this->assertEquals(array('foo' => 'foo', 'bar' => 'bar'), $route->getDefaults(), '->addDefaults() keep previous defaults');
  81. }
  82. public function testRequirements()
  83. {
  84. $route = new Route('/{foo}');
  85. $route->setRequirements(array('foo' => '\d+'));
  86. $this->assertEquals(array('foo' => '\d+'), $route->getRequirements(), '->setRequirements() sets the requirements');
  87. $this->assertEquals('\d+', $route->getRequirement('foo'), '->getRequirement() returns a requirement');
  88. $this->assertNull($route->getRequirement('bar'), '->getRequirement() returns null if a requirement is not defined');
  89. $route->setRequirements(array('foo' => '^\d+$'));
  90. $this->assertEquals('\d+', $route->getRequirement('foo'), '->getRequirement() removes ^ and $ from the path');
  91. $this->assertEquals($route, $route->setRequirements(array()), '->setRequirements() implements a fluent interface');
  92. $route->setRequirements(array('foo' => '\d+'));
  93. $route->addRequirements(array('bar' => '\d+'));
  94. $this->assertEquals($route, $route->addRequirements(array()), '->addRequirements() implements a fluent interface');
  95. $this->assertEquals(array('foo' => '\d+', 'bar' => '\d+'), $route->getRequirements(), '->addRequirement() keep previous requirements');
  96. }
  97. public function testRequirement()
  98. {
  99. $route = new Route('/{foo}');
  100. $this->assertFalse($route->hasRequirement('foo'), '->hasRequirement() return false if requirement is not set');
  101. $route->setRequirement('foo', '^\d+$');
  102. $this->assertEquals('\d+', $route->getRequirement('foo'), '->setRequirement() removes ^ and $ from the path');
  103. $this->assertTrue($route->hasRequirement('foo'), '->hasRequirement() return true if requirement is set');
  104. }
  105. /**
  106. * @dataProvider getInvalidRequirements
  107. * @expectedException \InvalidArgumentException
  108. */
  109. public function testSetInvalidRequirement($req)
  110. {
  111. $route = new Route('/{foo}');
  112. $route->setRequirement('foo', $req);
  113. }
  114. public function getInvalidRequirements()
  115. {
  116. return array(
  117. array(''),
  118. array(array()),
  119. array('^$'),
  120. array('^'),
  121. array('$'),
  122. );
  123. }
  124. public function testHost()
  125. {
  126. $route = new Route('/');
  127. $route->setHost('{locale}.example.net');
  128. $this->assertEquals('{locale}.example.net', $route->getHost(), '->setHost() sets the host pattern');
  129. }
  130. public function testScheme()
  131. {
  132. $route = new Route('/');
  133. $this->assertEquals(array(), $route->getSchemes(), 'schemes is initialized with array()');
  134. $this->assertFalse($route->hasScheme('http'));
  135. $route->setSchemes('hTTp');
  136. $this->assertEquals(array('http'), $route->getSchemes(), '->setSchemes() accepts a single scheme string and lowercases it');
  137. $this->assertTrue($route->hasScheme('htTp'));
  138. $this->assertFalse($route->hasScheme('httpS'));
  139. $route->setSchemes(array('HttpS', 'hTTp'));
  140. $this->assertEquals(array('https', 'http'), $route->getSchemes(), '->setSchemes() accepts an array of schemes and lowercases them');
  141. $this->assertTrue($route->hasScheme('htTp'));
  142. $this->assertTrue($route->hasScheme('httpS'));
  143. }
  144. public function testMethod()
  145. {
  146. $route = new Route('/');
  147. $this->assertEquals(array(), $route->getMethods(), 'methods is initialized with array()');
  148. $route->setMethods('gEt');
  149. $this->assertEquals(array('GET'), $route->getMethods(), '->setMethods() accepts a single method string and uppercases it');
  150. $route->setMethods(array('gEt', 'PosT'));
  151. $this->assertEquals(array('GET', 'POST'), $route->getMethods(), '->setMethods() accepts an array of methods and uppercases them');
  152. }
  153. public function testCondition()
  154. {
  155. $route = new Route('/');
  156. $this->assertSame('', $route->getCondition());
  157. $route->setCondition('context.getMethod() == "GET"');
  158. $this->assertSame('context.getMethod() == "GET"', $route->getCondition());
  159. }
  160. public function testCompile()
  161. {
  162. $route = new Route('/{foo}');
  163. $this->assertInstanceOf('Symfony\Component\Routing\CompiledRoute', $compiled = $route->compile(), '->compile() returns a compiled route');
  164. $this->assertSame($compiled, $route->compile(), '->compile() only compiled the route once if unchanged');
  165. $route->setRequirement('foo', '.*');
  166. $this->assertNotSame($compiled, $route->compile(), '->compile() recompiles if the route was modified');
  167. }
  168. public function testSerialize()
  169. {
  170. $route = new Route('/prefix/{foo}', array('foo' => 'default'), array('foo' => '\d+'));
  171. $serialized = serialize($route);
  172. $unserialized = unserialize($serialized);
  173. $this->assertEquals($route, $unserialized);
  174. $this->assertNotSame($route, $unserialized);
  175. }
  176. /**
  177. * Tests that the compiled version is also serialized to prevent the overhead
  178. * of compiling it again after unserialize.
  179. */
  180. public function testSerializeWhenCompiled()
  181. {
  182. $route = new Route('/prefix/{foo}', array('foo' => 'default'), array('foo' => '\d+'));
  183. $route->setHost('{locale}.example.net');
  184. $route->compile();
  185. $serialized = serialize($route);
  186. $unserialized = unserialize($serialized);
  187. $this->assertEquals($route, $unserialized);
  188. $this->assertNotSame($route, $unserialized);
  189. }
  190. /**
  191. * Tests that the serialized representation of a route in one symfony version
  192. * also works in later symfony versions, i.e. the unserialized route is in the
  193. * same state as another, semantically equivalent, route.
  194. */
  195. public function testSerializedRepresentationKeepsWorking()
  196. {
  197. $serialized = 'C:31:"Symfony\Component\Routing\Route":934:{a:8:{s:4:"path";s:13:"/prefix/{foo}";s:4:"host";s:20:"{locale}.example.net";s:8:"defaults";a:1:{s:3:"foo";s:7:"default";}s:12:"requirements";a:1:{s:3:"foo";s:3:"\d+";}s:7:"options";a:1:{s:14:"compiler_class";s:39:"Symfony\Component\Routing\RouteCompiler";}s:7:"schemes";a:0:{}s:7:"methods";a:0:{}s:8:"compiled";C:39:"Symfony\Component\Routing\CompiledRoute":569:{a:8:{s:4:"vars";a:2:{i:0;s:6:"locale";i:1;s:3:"foo";}s:11:"path_prefix";s:7:"/prefix";s:10:"path_regex";s:30:"#^/prefix(?:/(?P<foo>\d+))?$#s";s:11:"path_tokens";a:2:{i:0;a:4:{i:0;s:8:"variable";i:1;s:1:"/";i:2;s:3:"\d+";i:3;s:3:"foo";}i:1;a:2:{i:0;s:4:"text";i:1;s:7:"/prefix";}}s:9:"path_vars";a:1:{i:0;s:3:"foo";}s:10:"host_regex";s:39:"#^(?P<locale>[^\.]++)\.example\.net$#si";s:11:"host_tokens";a:2:{i:0;a:2:{i:0;s:4:"text";i:1;s:12:".example.net";}i:1;a:4:{i:0;s:8:"variable";i:1;s:0:"";i:2;s:7:"[^\.]++";i:3;s:6:"locale";}}s:9:"host_vars";a:1:{i:0;s:6:"locale";}}}}}';
  198. $unserialized = unserialize($serialized);
  199. $route = new Route('/prefix/{foo}', array('foo' => 'default'), array('foo' => '\d+'));
  200. $route->setHost('{locale}.example.net');
  201. $route->compile();
  202. $this->assertEquals($route, $unserialized);
  203. $this->assertNotSame($route, $unserialized);
  204. }
  205. }