RouteCollectionTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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\RouteCollection;
  12. use Symfony\Component\Routing\Route;
  13. use Symfony\Component\Config\Resource\FileResource;
  14. class RouteCollectionTest extends \PHPUnit_Framework_TestCase
  15. {
  16. public function testRoute()
  17. {
  18. $collection = new RouteCollection();
  19. $route = new Route('/foo');
  20. $collection->add('foo', $route);
  21. $this->assertEquals(array('foo' => $route), $collection->all(), '->add() adds a route');
  22. $this->assertEquals($route, $collection->get('foo'), '->get() returns a route by name');
  23. $this->assertNull($collection->get('bar'), '->get() returns null if a route does not exist');
  24. }
  25. public function testOverriddenRoute()
  26. {
  27. $collection = new RouteCollection();
  28. $collection->add('foo', new Route('/foo'));
  29. $collection->add('foo', new Route('/foo1'));
  30. $this->assertEquals('/foo1', $collection->get('foo')->getPath());
  31. }
  32. public function testDeepOverriddenRoute()
  33. {
  34. $collection = new RouteCollection();
  35. $collection->add('foo', new Route('/foo'));
  36. $collection1 = new RouteCollection();
  37. $collection1->add('foo', new Route('/foo1'));
  38. $collection2 = new RouteCollection();
  39. $collection2->add('foo', new Route('/foo2'));
  40. $collection1->addCollection($collection2);
  41. $collection->addCollection($collection1);
  42. $this->assertEquals('/foo2', $collection1->get('foo')->getPath());
  43. $this->assertEquals('/foo2', $collection->get('foo')->getPath());
  44. }
  45. public function testIterator()
  46. {
  47. $collection = new RouteCollection();
  48. $collection->add('foo', new Route('/foo'));
  49. $collection1 = new RouteCollection();
  50. $collection1->add('bar', $bar = new Route('/bar'));
  51. $collection1->add('foo', $foo = new Route('/foo-new'));
  52. $collection->addCollection($collection1);
  53. $collection->add('last', $last = new Route('/last'));
  54. $this->assertInstanceOf('\ArrayIterator', $collection->getIterator());
  55. $this->assertSame(array('bar' => $bar, 'foo' => $foo, 'last' => $last), $collection->getIterator()->getArrayCopy());
  56. }
  57. public function testCount()
  58. {
  59. $collection = new RouteCollection();
  60. $collection->add('foo', new Route('/foo'));
  61. $collection1 = new RouteCollection();
  62. $collection1->add('bar', new Route('/bar'));
  63. $collection->addCollection($collection1);
  64. $this->assertCount(2, $collection);
  65. }
  66. public function testAddCollection()
  67. {
  68. $collection = new RouteCollection();
  69. $collection->add('foo', new Route('/foo'));
  70. $collection1 = new RouteCollection();
  71. $collection1->add('bar', $bar = new Route('/bar'));
  72. $collection1->add('foo', $foo = new Route('/foo-new'));
  73. $collection2 = new RouteCollection();
  74. $collection2->add('grandchild', $grandchild = new Route('/grandchild'));
  75. $collection1->addCollection($collection2);
  76. $collection->addCollection($collection1);
  77. $collection->add('last', $last = new Route('/last'));
  78. $this->assertSame(array('bar' => $bar, 'foo' => $foo, 'grandchild' => $grandchild, 'last' => $last), $collection->all(),
  79. '->addCollection() imports routes of another collection, overrides if necessary and adds them at the end');
  80. }
  81. public function testAddCollectionWithResources()
  82. {
  83. if (!class_exists('Symfony\Component\Config\Resource\FileResource')) {
  84. $this->markTestSkipped('The "Config" component is not available');
  85. }
  86. $collection = new RouteCollection();
  87. $collection->addResource($foo = new FileResource(__DIR__.'/Fixtures/foo.xml'));
  88. $collection1 = new RouteCollection();
  89. $collection1->addResource($foo1 = new FileResource(__DIR__.'/Fixtures/foo1.xml'));
  90. $collection->addCollection($collection1);
  91. $this->assertEquals(array($foo, $foo1), $collection->getResources(), '->addCollection() merges resources');
  92. }
  93. public function testAddDefaultsAndRequirementsAndOptions()
  94. {
  95. $collection = new RouteCollection();
  96. $collection->add('foo', new Route('/{placeholder}'));
  97. $collection1 = new RouteCollection();
  98. $collection1->add('bar', new Route('/{placeholder}',
  99. array('_controller' => 'fixed', 'placeholder' => 'default'), array('placeholder' => '.+'), array('option' => 'value'))
  100. );
  101. $collection->addCollection($collection1);
  102. $collection->addDefaults(array('placeholder' => 'new-default'));
  103. $this->assertEquals(array('placeholder' => 'new-default'), $collection->get('foo')->getDefaults(), '->addDefaults() adds defaults to all routes');
  104. $this->assertEquals(array('_controller' => 'fixed', 'placeholder' => 'new-default'), $collection->get('bar')->getDefaults(),
  105. '->addDefaults() adds defaults to all routes and overwrites existing ones');
  106. $collection->addRequirements(array('placeholder' => '\d+'));
  107. $this->assertEquals(array('placeholder' => '\d+'), $collection->get('foo')->getRequirements(), '->addRequirements() adds requirements to all routes');
  108. $this->assertEquals(array('placeholder' => '\d+'), $collection->get('bar')->getRequirements(),
  109. '->addRequirements() adds requirements to all routes and overwrites existing ones');
  110. $collection->addOptions(array('option' => 'new-value'));
  111. $this->assertEquals(
  112. array('option' => 'new-value', 'compiler_class' => 'Symfony\\Component\\Routing\\RouteCompiler'),
  113. $collection->get('bar')->getOptions(), '->addOptions() adds options to all routes and overwrites existing ones'
  114. );
  115. }
  116. public function testAddPrefix()
  117. {
  118. $collection = new RouteCollection();
  119. $collection->add('foo', $foo = new Route('/foo'));
  120. $collection2 = new RouteCollection();
  121. $collection2->add('bar', $bar = new Route('/bar'));
  122. $collection->addCollection($collection2);
  123. $collection->addPrefix(' / ');
  124. $this->assertSame('/foo', $collection->get('foo')->getPattern(), '->addPrefix() trims the prefix and a single slash has no effect');
  125. $collection->addPrefix('/{admin}', array('admin' => 'admin'), array('admin' => '\d+'));
  126. $this->assertEquals('/{admin}/foo', $collection->get('foo')->getPath(), '->addPrefix() adds a prefix to all routes');
  127. $this->assertEquals('/{admin}/bar', $collection->get('bar')->getPath(), '->addPrefix() adds a prefix to all routes');
  128. $this->assertEquals(array('admin' => 'admin'), $collection->get('foo')->getDefaults(), '->addPrefix() adds defaults to all routes');
  129. $this->assertEquals(array('admin' => 'admin'), $collection->get('bar')->getDefaults(), '->addPrefix() adds defaults to all routes');
  130. $this->assertEquals(array('admin' => '\d+'), $collection->get('foo')->getRequirements(), '->addPrefix() adds requirements to all routes');
  131. $this->assertEquals(array('admin' => '\d+'), $collection->get('bar')->getRequirements(), '->addPrefix() adds requirements to all routes');
  132. $collection->addPrefix('0');
  133. $this->assertEquals('/0/{admin}/foo', $collection->get('foo')->getPattern(), '->addPrefix() ensures a prefix must start with a slash and must not end with a slash');
  134. $collection->addPrefix('/ /');
  135. $this->assertSame('/ /0/{admin}/foo', $collection->get('foo')->getPath(), '->addPrefix() can handle spaces if desired');
  136. $this->assertSame('/ /0/{admin}/bar', $collection->get('bar')->getPath(), 'the route pattern of an added collection is in synch with the added prefix');
  137. }
  138. public function testAddPrefixOverridesDefaultsAndRequirements()
  139. {
  140. $collection = new RouteCollection();
  141. $collection->add('foo', $foo = new Route('/foo'));
  142. $collection->add('bar', $bar = new Route('/bar', array(), array('_scheme' => 'http')));
  143. $collection->addPrefix('/admin', array(), array('_scheme' => 'https'));
  144. $this->assertEquals('https', $collection->get('foo')->getRequirement('_scheme'), '->addPrefix() overrides existing requirements');
  145. $this->assertEquals('https', $collection->get('bar')->getRequirement('_scheme'), '->addPrefix() overrides existing requirements');
  146. }
  147. public function testResource()
  148. {
  149. if (!class_exists('Symfony\Component\Config\Resource\FileResource')) {
  150. $this->markTestSkipped('The "Config" component is not available');
  151. }
  152. $collection = new RouteCollection();
  153. $collection->addResource($foo = new FileResource(__DIR__.'/Fixtures/foo.xml'));
  154. $collection->addResource($bar = new FileResource(__DIR__.'/Fixtures/bar.xml'));
  155. $collection->addResource(new FileResource(__DIR__.'/Fixtures/foo.xml'));
  156. $this->assertEquals(array($foo, $bar), $collection->getResources(),
  157. '->addResource() adds a resource and getResources() only returns unique ones by comparing the string representation');
  158. }
  159. public function testUniqueRouteWithGivenName()
  160. {
  161. $collection1 = new RouteCollection();
  162. $collection1->add('foo', new Route('/old'));
  163. $collection2 = new RouteCollection();
  164. $collection3 = new RouteCollection();
  165. $collection3->add('foo', $new = new Route('/new'));
  166. $collection2->addCollection($collection3);
  167. $collection1->addCollection($collection2);
  168. $this->assertSame($new, $collection1->get('foo'), '->get() returns new route that overrode previous one');
  169. // size of 1 because collection1 contains /new but not /old anymore
  170. $this->assertCount(1, $collection1->getIterator(), '->addCollection() removes previous routes when adding new routes with the same name');
  171. }
  172. public function testGet()
  173. {
  174. $collection1 = new RouteCollection();
  175. $collection1->add('a', $a = new Route('/a'));
  176. $collection2 = new RouteCollection();
  177. $collection2->add('b', $b = new Route('/b'));
  178. $collection1->addCollection($collection2);
  179. $collection1->add('$péß^a|', $c = new Route('/special'));
  180. $this->assertSame($b, $collection1->get('b'), '->get() returns correct route in child collection');
  181. $this->assertSame($c, $collection1->get('$péß^a|'), '->get() can handle special characters');
  182. $this->assertNull($collection2->get('a'), '->get() does not return the route defined in parent collection');
  183. $this->assertNull($collection1->get('non-existent'), '->get() returns null when route does not exist');
  184. $this->assertNull($collection1->get(0), '->get() does not disclose internal child RouteCollection');
  185. }
  186. public function testRemove()
  187. {
  188. $collection = new RouteCollection();
  189. $collection->add('foo', $foo = new Route('/foo'));
  190. $collection1 = new RouteCollection();
  191. $collection1->add('bar', $bar = new Route('/bar'));
  192. $collection->addCollection($collection1);
  193. $collection->add('last', $last = new Route('/last'));
  194. $collection->remove('foo');
  195. $this->assertSame(array('bar' => $bar, 'last' => $last), $collection->all(), '->remove() can remove a single route');
  196. $collection->remove(array('bar', 'last'));
  197. $this->assertSame(array(), $collection->all(), '->remove() accepts an array and can remove multiple routes at once');
  198. }
  199. public function testSetHost()
  200. {
  201. $collection = new RouteCollection();
  202. $routea = new Route('/a');
  203. $routeb = new Route('/b', array(), array(), array(), '{locale}.example.net');
  204. $collection->add('a', $routea);
  205. $collection->add('b', $routeb);
  206. $collection->setHost('{locale}.example.com');
  207. $this->assertEquals('{locale}.example.com', $routea->getHost());
  208. $this->assertEquals('{locale}.example.com', $routeb->getHost());
  209. }
  210. }