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

/vendor/symfony/routing/Symfony/Component/Routing/Tests/RouteTest.php

https://gitlab.com/daniruizcamacho/pfcascensores
PHP | 232 lines | 188 code | 32 blank | 12 comment | 0 complexity | 88ef12b30bd938f7583c2e9afc89b6f9 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. $route->setSchemes('hTTp');
  135. $this->assertEquals(array('http'), $route->getSchemes(), '->setSchemes() accepts a single scheme string and lowercases it');
  136. $route->setSchemes(array('HttpS', 'hTTp'));
  137. $this->assertEquals(array('https', 'http'), $route->getSchemes(), '->setSchemes() accepts an array of schemes and lowercases them');
  138. }
  139. public function testSchemeIsBC()
  140. {
  141. $route = new Route('/');
  142. $route->setRequirement('_scheme', 'http|https');
  143. $this->assertEquals('http|https', $route->getRequirement('_scheme'));
  144. $this->assertEquals(array('http', 'https'), $route->getSchemes());
  145. $route->setSchemes(array('hTTp'));
  146. $this->assertEquals('http', $route->getRequirement('_scheme'));
  147. $route->setSchemes(array());
  148. $this->assertNull($route->getRequirement('_scheme'));
  149. }
  150. public function testMethod()
  151. {
  152. $route = new Route('/');
  153. $this->assertEquals(array(), $route->getMethods(), 'methods is initialized with array()');
  154. $route->setMethods('gEt');
  155. $this->assertEquals(array('GET'), $route->getMethods(), '->setMethods() accepts a single method string and uppercases it');
  156. $route->setMethods(array('gEt', 'PosT'));
  157. $this->assertEquals(array('GET', 'POST'), $route->getMethods(), '->setMethods() accepts an array of methods and uppercases them');
  158. }
  159. public function testMethodIsBC()
  160. {
  161. $route = new Route('/');
  162. $route->setRequirement('_method', 'GET|POST');
  163. $this->assertEquals('GET|POST', $route->getRequirement('_method'));
  164. $this->assertEquals(array('GET', 'POST'), $route->getMethods());
  165. $route->setMethods(array('gEt'));
  166. $this->assertEquals('GET', $route->getRequirement('_method'));
  167. $route->setMethods(array());
  168. $this->assertNull($route->getRequirement('_method'));
  169. }
  170. public function testCondition()
  171. {
  172. $route = new Route('/');
  173. $this->assertEquals(null, $route->getCondition());
  174. $route->setCondition('context.getMethod() == "GET"');
  175. $this->assertEquals('context.getMethod() == "GET"', $route->getCondition());
  176. }
  177. public function testCompile()
  178. {
  179. $route = new Route('/{foo}');
  180. $this->assertInstanceOf('Symfony\Component\Routing\CompiledRoute', $compiled = $route->compile(), '->compile() returns a compiled route');
  181. $this->assertSame($compiled, $route->compile(), '->compile() only compiled the route once if unchanged');
  182. $route->setRequirement('foo', '.*');
  183. $this->assertNotSame($compiled, $route->compile(), '->compile() recompiles if the route was modified');
  184. }
  185. public function testPattern()
  186. {
  187. $route = new Route('/{foo}');
  188. $this->assertEquals('/{foo}', $route->getPattern());
  189. $route->setPattern('/bar');
  190. $this->assertEquals('/bar', $route->getPattern());
  191. }
  192. public function testSerialize()
  193. {
  194. $route = new Route('/{foo}', array('foo' => 'default'), array('foo' => '\d+'));
  195. $serialized = serialize($route);
  196. $unserialized = unserialize($serialized);
  197. $this->assertEquals($route, $unserialized);
  198. $this->assertNotSame($route, $unserialized);
  199. }
  200. }