RouteTest.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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'));
  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. $route = new Route('/', array(), array(), array(), '', 'Https', 'Post');
  26. $this->assertEquals(array('https'), $route->getSchemes(), '__construct() takes a single scheme as its sixth argument');
  27. $this->assertEquals(array('POST'), $route->getMethods(), '__construct() takes a single method as its seventh argument');
  28. }
  29. public function testPath()
  30. {
  31. $route = new Route('/{foo}');
  32. $route->setPath('/{bar}');
  33. $this->assertEquals('/{bar}', $route->getPath(), '->setPath() sets the path');
  34. $route->setPath('');
  35. $this->assertEquals('/', $route->getPath(), '->setPath() adds a / at the beginning of the path if needed');
  36. $route->setPath('bar');
  37. $this->assertEquals('/bar', $route->getPath(), '->setPath() adds a / at the beginning of the path if needed');
  38. $this->assertEquals($route, $route->setPath(''), '->setPath() implements a fluent interface');
  39. $route->setPath('//path');
  40. $this->assertEquals('/path', $route->getPath(), '->setPath() does not allow two slahes "//" at the beginning of the path as it would be confused with a network path when generating the path from the route');
  41. }
  42. public function testOptions()
  43. {
  44. $route = new Route('/{foo}');
  45. $route->setOptions(array('foo' => 'bar'));
  46. $this->assertEquals(array_merge(array(
  47. 'compiler_class' => 'Symfony\\Component\\Routing\\RouteCompiler',
  48. ), array('foo' => 'bar')), $route->getOptions(), '->setOptions() sets the options');
  49. $this->assertEquals($route, $route->setOptions(array()), '->setOptions() implements a fluent interface');
  50. $route->setOptions(array('foo' => 'foo'));
  51. $route->addOptions(array('bar' => 'bar'));
  52. $this->assertEquals($route, $route->addOptions(array()), '->addOptions() implements a fluent interface');
  53. $this->assertEquals(array('foo' => 'foo', 'bar' => 'bar', 'compiler_class' => 'Symfony\\Component\\Routing\\RouteCompiler'), $route->getOptions(), '->addDefaults() keep previous defaults');
  54. }
  55. public function testDefaults()
  56. {
  57. $route = new Route('/{foo}');
  58. $route->setDefaults(array('foo' => 'bar'));
  59. $this->assertEquals(array('foo' => 'bar'), $route->getDefaults(), '->setDefaults() sets the defaults');
  60. $this->assertEquals($route, $route->setDefaults(array()), '->setDefaults() implements a fluent interface');
  61. $route->setDefault('foo', 'bar');
  62. $this->assertEquals('bar', $route->getDefault('foo'), '->setDefault() sets a default value');
  63. $route->setDefault('foo2', 'bar2');
  64. $this->assertEquals('bar2', $route->getDefault('foo2'), '->getDefault() return the default value');
  65. $this->assertNull($route->getDefault('not_defined'), '->getDefault() return null if default value is not setted');
  66. $route->setDefault('_controller', $closure = function () { return 'Hello'; });
  67. $this->assertEquals($closure, $route->getDefault('_controller'), '->setDefault() sets a default value');
  68. $route->setDefaults(array('foo' => 'foo'));
  69. $route->addDefaults(array('bar' => 'bar'));
  70. $this->assertEquals($route, $route->addDefaults(array()), '->addDefaults() implements a fluent interface');
  71. $this->assertEquals(array('foo' => 'foo', 'bar' => 'bar'), $route->getDefaults(), '->addDefaults() keep previous defaults');
  72. }
  73. public function testRequirements()
  74. {
  75. $route = new Route('/{foo}');
  76. $route->setRequirements(array('foo' => '\d+'));
  77. $this->assertEquals(array('foo' => '\d+'), $route->getRequirements(), '->setRequirements() sets the requirements');
  78. $this->assertEquals('\d+', $route->getRequirement('foo'), '->getRequirement() returns a requirement');
  79. $this->assertNull($route->getRequirement('bar'), '->getRequirement() returns null if a requirement is not defined');
  80. $route->setRequirements(array('foo' => '^\d+$'));
  81. $this->assertEquals('\d+', $route->getRequirement('foo'), '->getRequirement() removes ^ and $ from the path');
  82. $this->assertEquals($route, $route->setRequirements(array()), '->setRequirements() implements a fluent interface');
  83. $route->setRequirements(array('foo' => '\d+'));
  84. $route->addRequirements(array('bar' => '\d+'));
  85. $this->assertEquals($route, $route->addRequirements(array()), '->addRequirements() implements a fluent interface');
  86. $this->assertEquals(array('foo' => '\d+', 'bar' => '\d+'), $route->getRequirements(), '->addRequirement() keep previous requirements');
  87. }
  88. public function testRequirement()
  89. {
  90. $route = new Route('/{foo}');
  91. $route->setRequirement('foo', '^\d+$');
  92. $this->assertEquals('\d+', $route->getRequirement('foo'), '->setRequirement() removes ^ and $ from the path');
  93. }
  94. /**
  95. * @dataProvider getInvalidRequirements
  96. * @expectedException \InvalidArgumentException
  97. */
  98. public function testSetInvalidRequirement($req)
  99. {
  100. $route = new Route('/{foo}');
  101. $route->setRequirement('foo', $req);
  102. }
  103. public function getInvalidRequirements()
  104. {
  105. return array(
  106. array(''),
  107. array(array()),
  108. array('^$'),
  109. array('^'),
  110. array('$')
  111. );
  112. }
  113. public function testHost()
  114. {
  115. $route = new Route('/');
  116. $route->setHost('{locale}.example.net');
  117. $this->assertEquals('{locale}.example.net', $route->getHost(), '->setHost() sets the host pattern');
  118. }
  119. public function testScheme()
  120. {
  121. $route = new Route('/');
  122. $this->assertEquals(array(), $route->getSchemes(), 'schemes is initialized with array()');
  123. $route->setSchemes('hTTp');
  124. $this->assertEquals(array('http'), $route->getSchemes(), '->setSchemes() accepts a single scheme string and lowercases it');
  125. $route->setSchemes(array('HttpS', 'hTTp'));
  126. $this->assertEquals(array('https', 'http'), $route->getSchemes(), '->setSchemes() accepts an array of schemes and lowercases them');
  127. }
  128. public function testSchemeIsBC()
  129. {
  130. $route = new Route('/');
  131. $route->setRequirement('_scheme', 'http|https');
  132. $this->assertEquals('http|https', $route->getRequirement('_scheme'));
  133. $this->assertEquals(array('http', 'https'), $route->getSchemes());
  134. $route->setSchemes(array('hTTp'));
  135. $this->assertEquals('http', $route->getRequirement('_scheme'));
  136. $route->setSchemes(array());
  137. $this->assertNull($route->getRequirement('_scheme'));
  138. }
  139. public function testMethod()
  140. {
  141. $route = new Route('/');
  142. $this->assertEquals(array(), $route->getMethods(), 'methods is initialized with array()');
  143. $route->setMethods('gEt');
  144. $this->assertEquals(array('GET'), $route->getMethods(), '->setMethods() accepts a single method string and uppercases it');
  145. $route->setMethods(array('gEt', 'PosT'));
  146. $this->assertEquals(array('GET', 'POST'), $route->getMethods(), '->setMethods() accepts an array of methods and uppercases them');
  147. }
  148. public function testMethodIsBC()
  149. {
  150. $route = new Route('/');
  151. $route->setRequirement('_method', 'GET|POST');
  152. $this->assertEquals('GET|POST', $route->getRequirement('_method'));
  153. $this->assertEquals(array('GET', 'POST'), $route->getMethods());
  154. $route->setMethods(array('gEt'));
  155. $this->assertEquals('GET', $route->getRequirement('_method'));
  156. $route->setMethods(array());
  157. $this->assertNull($route->getRequirement('_method'));
  158. }
  159. public function testCompile()
  160. {
  161. $route = new Route('/{foo}');
  162. $this->assertInstanceOf('Symfony\Component\Routing\CompiledRoute', $compiled = $route->compile(), '->compile() returns a compiled route');
  163. $this->assertSame($compiled, $route->compile(), '->compile() only compiled the route once if unchanged');
  164. $route->setRequirement('foo', '.*');
  165. $this->assertNotSame($compiled, $route->compile(), '->compile() recompiles if the route was modified');
  166. }
  167. }