PageRenderTime 29ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Symfony/Component/Routing/Tests/RouteTest.php

https://github.com/jdewit/symfony
PHP | 201 lines | 163 code | 26 blank | 12 comment | 0 complexity | 6705c4a033212a767f2fd801db6d6b5c 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 testDefaults()
  57. {
  58. $route = new Route('/{foo}');
  59. $route->setDefaults(array('foo' => 'bar'));
  60. $this->assertEquals(array('foo' => 'bar'), $route->getDefaults(), '->setDefaults() sets the defaults');
  61. $this->assertEquals($route, $route->setDefaults(array()), '->setDefaults() implements a fluent interface');
  62. $route->setDefault('foo', 'bar');
  63. $this->assertEquals('bar', $route->getDefault('foo'), '->setDefault() sets a default value');
  64. $route->setDefault('foo2', 'bar2');
  65. $this->assertEquals('bar2', $route->getDefault('foo2'), '->getDefault() return the default value');
  66. $this->assertNull($route->getDefault('not_defined'), '->getDefault() return null if default value is not setted');
  67. $route->setDefault('_controller', $closure = function () { return 'Hello'; });
  68. $this->assertEquals($closure, $route->getDefault('_controller'), '->setDefault() sets a default value');
  69. $route->setDefaults(array('foo' => 'foo'));
  70. $route->addDefaults(array('bar' => 'bar'));
  71. $this->assertEquals($route, $route->addDefaults(array()), '->addDefaults() implements a fluent interface');
  72. $this->assertEquals(array('foo' => 'foo', 'bar' => 'bar'), $route->getDefaults(), '->addDefaults() keep previous defaults');
  73. }
  74. public function testRequirements()
  75. {
  76. $route = new Route('/{foo}');
  77. $route->setRequirements(array('foo' => '\d+'));
  78. $this->assertEquals(array('foo' => '\d+'), $route->getRequirements(), '->setRequirements() sets the requirements');
  79. $this->assertEquals('\d+', $route->getRequirement('foo'), '->getRequirement() returns a requirement');
  80. $this->assertNull($route->getRequirement('bar'), '->getRequirement() returns null if a requirement is not defined');
  81. $route->setRequirements(array('foo' => '^\d+$'));
  82. $this->assertEquals('\d+', $route->getRequirement('foo'), '->getRequirement() removes ^ and $ from the path');
  83. $this->assertEquals($route, $route->setRequirements(array()), '->setRequirements() implements a fluent interface');
  84. $route->setRequirements(array('foo' => '\d+'));
  85. $route->addRequirements(array('bar' => '\d+'));
  86. $this->assertEquals($route, $route->addRequirements(array()), '->addRequirements() implements a fluent interface');
  87. $this->assertEquals(array('foo' => '\d+', 'bar' => '\d+'), $route->getRequirements(), '->addRequirement() keep previous requirements');
  88. }
  89. public function testRequirement()
  90. {
  91. $route = new Route('/{foo}');
  92. $route->setRequirement('foo', '^\d+$');
  93. $this->assertEquals('\d+', $route->getRequirement('foo'), '->setRequirement() removes ^ and $ from the path');
  94. }
  95. /**
  96. * @dataProvider getInvalidRequirements
  97. * @expectedException \InvalidArgumentException
  98. */
  99. public function testSetInvalidRequirement($req)
  100. {
  101. $route = new Route('/{foo}');
  102. $route->setRequirement('foo', $req);
  103. }
  104. public function getInvalidRequirements()
  105. {
  106. return array(
  107. array(''),
  108. array(array()),
  109. array('^$'),
  110. array('^'),
  111. array('$')
  112. );
  113. }
  114. public function testHost()
  115. {
  116. $route = new Route('/');
  117. $route->setHost('{locale}.example.net');
  118. $this->assertEquals('{locale}.example.net', $route->getHost(), '->setHost() sets the host pattern');
  119. }
  120. public function testScheme()
  121. {
  122. $route = new Route('/');
  123. $this->assertEquals(array(), $route->getSchemes(), 'schemes is initialized with array()');
  124. $route->setSchemes('hTTp');
  125. $this->assertEquals(array('http'), $route->getSchemes(), '->setSchemes() accepts a single scheme string and lowercases it');
  126. $route->setSchemes(array('HttpS', 'hTTp'));
  127. $this->assertEquals(array('https', 'http'), $route->getSchemes(), '->setSchemes() accepts an array of schemes and lowercases them');
  128. }
  129. public function testSchemeIsBC()
  130. {
  131. $route = new Route('/');
  132. $route->setRequirement('_scheme', 'http|https');
  133. $this->assertEquals('http|https', $route->getRequirement('_scheme'));
  134. $this->assertEquals(array('http', 'https'), $route->getSchemes());
  135. $route->setSchemes(array('hTTp'));
  136. $this->assertEquals('http', $route->getRequirement('_scheme'));
  137. $route->setSchemes(array());
  138. $this->assertNull($route->getRequirement('_scheme'));
  139. }
  140. public function testMethod()
  141. {
  142. $route = new Route('/');
  143. $this->assertEquals(array(), $route->getMethods(), 'methods is initialized with array()');
  144. $route->setMethods('gEt');
  145. $this->assertEquals(array('GET'), $route->getMethods(), '->setMethods() accepts a single method string and uppercases it');
  146. $route->setMethods(array('gEt', 'PosT'));
  147. $this->assertEquals(array('GET', 'POST'), $route->getMethods(), '->setMethods() accepts an array of methods and uppercases them');
  148. }
  149. public function testMethodIsBC()
  150. {
  151. $route = new Route('/');
  152. $route->setRequirement('_method', 'GET|POST');
  153. $this->assertEquals('GET|POST', $route->getRequirement('_method'));
  154. $this->assertEquals(array('GET', 'POST'), $route->getMethods());
  155. $route->setMethods(array('gEt'));
  156. $this->assertEquals('GET', $route->getRequirement('_method'));
  157. $route->setMethods(array());
  158. $this->assertNull($route->getRequirement('_method'));
  159. }
  160. public function testCondition()
  161. {
  162. $route = new Route('/');
  163. $this->assertEquals(null, $route->getCondition());
  164. $route->setCondition('context.getMethod() == "GET"');
  165. $this->assertEquals('context.getMethod() == "GET"', $route->getCondition());
  166. }
  167. public function testCompile()
  168. {
  169. $route = new Route('/{foo}');
  170. $this->assertInstanceOf('Symfony\Component\Routing\CompiledRoute', $compiled = $route->compile(), '->compile() returns a compiled route');
  171. $this->assertSame($compiled, $route->compile(), '->compile() only compiled the route once if unchanged');
  172. $route->setRequirement('foo', '.*');
  173. $this->assertNotSame($compiled, $route->compile(), '->compile() recompiles if the route was modified');
  174. }
  175. }