RouteCollectionTest.php 14 KB

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