No Description

RouteCollectionTest.php 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. $collection = new RouteCollection();
  84. $collection->addResource($foo = new FileResource(__DIR__.'/Fixtures/foo.xml'));
  85. $collection1 = new RouteCollection();
  86. $collection1->addResource($foo1 = new FileResource(__DIR__.'/Fixtures/foo1.xml'));
  87. $collection->addCollection($collection1);
  88. $this->assertEquals(array($foo, $foo1), $collection->getResources(), '->addCollection() merges resources');
  89. }
  90. public function testAddDefaultsAndRequirementsAndOptions()
  91. {
  92. $collection = new RouteCollection();
  93. $collection->add('foo', new Route('/{placeholder}'));
  94. $collection1 = new RouteCollection();
  95. $collection1->add('bar', new Route('/{placeholder}',
  96. array('_controller' => 'fixed', 'placeholder' => 'default'), array('placeholder' => '.+'), array('option' => 'value'))
  97. );
  98. $collection->addCollection($collection1);
  99. $collection->addDefaults(array('placeholder' => 'new-default'));
  100. $this->assertEquals(array('placeholder' => 'new-default'), $collection->get('foo')->getDefaults(), '->addDefaults() adds defaults to all routes');
  101. $this->assertEquals(array('_controller' => 'fixed', 'placeholder' => 'new-default'), $collection->get('bar')->getDefaults(),
  102. '->addDefaults() adds defaults to all routes and overwrites existing ones');
  103. $collection->addRequirements(array('placeholder' => '\d+'));
  104. $this->assertEquals(array('placeholder' => '\d+'), $collection->get('foo')->getRequirements(), '->addRequirements() adds requirements to all routes');
  105. $this->assertEquals(array('placeholder' => '\d+'), $collection->get('bar')->getRequirements(),
  106. '->addRequirements() adds requirements to all routes and overwrites existing ones');
  107. $collection->addOptions(array('option' => 'new-value'));
  108. $this->assertEquals(
  109. array('option' => 'new-value', 'compiler_class' => 'Symfony\\Component\\Routing\\RouteCompiler'),
  110. $collection->get('bar')->getOptions(), '->addOptions() adds options to all routes and overwrites existing ones'
  111. );
  112. }
  113. public function testAddPrefix()
  114. {
  115. $collection = new RouteCollection();
  116. $collection->add('foo', $foo = new Route('/foo'));
  117. $collection2 = new RouteCollection();
  118. $collection2->add('bar', $bar = new Route('/bar'));
  119. $collection->addCollection($collection2);
  120. $collection->addPrefix(' / ');
  121. $this->assertSame('/foo', $collection->get('foo')->getPath(), '->addPrefix() trims the prefix and a single slash has no effect');
  122. $collection->addPrefix('/{admin}', array('admin' => 'admin'), array('admin' => '\d+'));
  123. $this->assertEquals('/{admin}/foo', $collection->get('foo')->getPath(), '->addPrefix() adds a prefix to all routes');
  124. $this->assertEquals('/{admin}/bar', $collection->get('bar')->getPath(), '->addPrefix() adds a prefix to all routes');
  125. $this->assertEquals(array('admin' => 'admin'), $collection->get('foo')->getDefaults(), '->addPrefix() adds defaults to all routes');
  126. $this->assertEquals(array('admin' => 'admin'), $collection->get('bar')->getDefaults(), '->addPrefix() adds defaults to all routes');
  127. $this->assertEquals(array('admin' => '\d+'), $collection->get('foo')->getRequirements(), '->addPrefix() adds requirements to all routes');
  128. $this->assertEquals(array('admin' => '\d+'), $collection->get('bar')->getRequirements(), '->addPrefix() adds requirements to all routes');
  129. $collection->addPrefix('0');
  130. $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');
  131. $collection->addPrefix('/ /');
  132. $this->assertSame('/ /0/{admin}/foo', $collection->get('foo')->getPath(), '->addPrefix() can handle spaces if desired');
  133. $this->assertSame('/ /0/{admin}/bar', $collection->get('bar')->getPath(), 'the route pattern of an added collection is in synch with the added prefix');
  134. }
  135. public function testAddPrefixOverridesDefaultsAndRequirements()
  136. {
  137. $collection = new RouteCollection();
  138. $collection->add('foo', $foo = new Route('/foo'));
  139. $collection->add('bar', $bar = new Route('/bar', array(), array('_scheme' => 'http')));
  140. $collection->addPrefix('/admin', array(), array('_scheme' => 'https'));
  141. $this->assertEquals('https', $collection->get('foo')->getRequirement('_scheme'), '->addPrefix() overrides existing requirements');
  142. $this->assertEquals('https', $collection->get('bar')->getRequirement('_scheme'), '->addPrefix() overrides existing requirements');
  143. }
  144. public function testResource()
  145. {
  146. $collection = new RouteCollection();
  147. $collection->addResource($foo = new FileResource(__DIR__.'/Fixtures/foo.xml'));
  148. $collection->addResource($bar = new FileResource(__DIR__.'/Fixtures/bar.xml'));
  149. $collection->addResource(new FileResource(__DIR__.'/Fixtures/foo.xml'));
  150. $this->assertEquals(array($foo, $bar), $collection->getResources(),
  151. '->addResource() adds a resource and getResources() only returns unique ones by comparing the string representation');
  152. }
  153. public function testUniqueRouteWithGivenName()
  154. {
  155. $collection1 = new RouteCollection();
  156. $collection1->add('foo', new Route('/old'));
  157. $collection2 = new RouteCollection();
  158. $collection3 = new RouteCollection();
  159. $collection3->add('foo', $new = new Route('/new'));
  160. $collection2->addCollection($collection3);
  161. $collection1->addCollection($collection2);
  162. $this->assertSame($new, $collection1->get('foo'), '->get() returns new route that overrode previous one');
  163. // size of 1 because collection1 contains /new but not /old anymore
  164. $this->assertCount(1, $collection1->getIterator(), '->addCollection() removes previous routes when adding new routes with the same name');
  165. }
  166. public function testGet()
  167. {
  168. $collection1 = new RouteCollection();
  169. $collection1->add('a', $a = new Route('/a'));
  170. $collection2 = new RouteCollection();
  171. $collection2->add('b', $b = new Route('/b'));
  172. $collection1->addCollection($collection2);
  173. $collection1->add('$péß^a|', $c = new Route('/special'));
  174. $this->assertSame($b, $collection1->get('b'), '->get() returns correct route in child collection');
  175. $this->assertSame($c, $collection1->get('$péß^a|'), '->get() can handle special characters');
  176. $this->assertNull($collection2->get('a'), '->get() does not return the route defined in parent collection');
  177. $this->assertNull($collection1->get('non-existent'), '->get() returns null when route does not exist');
  178. $this->assertNull($collection1->get(0), '->get() does not disclose internal child RouteCollection');
  179. }
  180. public function testRemove()
  181. {
  182. $collection = new RouteCollection();
  183. $collection->add('foo', $foo = new Route('/foo'));
  184. $collection1 = new RouteCollection();
  185. $collection1->add('bar', $bar = new Route('/bar'));
  186. $collection->addCollection($collection1);
  187. $collection->add('last', $last = new Route('/last'));
  188. $collection->remove('foo');
  189. $this->assertSame(array('bar' => $bar, 'last' => $last), $collection->all(), '->remove() can remove a single route');
  190. $collection->remove(array('bar', 'last'));
  191. $this->assertSame(array(), $collection->all(), '->remove() accepts an array and can remove multiple routes at once');
  192. }
  193. public function testSetHost()
  194. {
  195. $collection = new RouteCollection();
  196. $routea = new Route('/a');
  197. $routeb = new Route('/b', array(), array(), array(), '{locale}.example.net');
  198. $collection->add('a', $routea);
  199. $collection->add('b', $routeb);
  200. $collection->setHost('{locale}.example.com');
  201. $this->assertEquals('{locale}.example.com', $routea->getHost());
  202. $this->assertEquals('{locale}.example.com', $routeb->getHost());
  203. }
  204. public function testSetCondition()
  205. {
  206. $collection = new RouteCollection();
  207. $routea = new Route('/a');
  208. $routeb = new Route('/b', array(), array(), array(), '{locale}.example.net', array(), array(), 'context.getMethod() == "GET"');
  209. $collection->add('a', $routea);
  210. $collection->add('b', $routeb);
  211. $collection->setCondition('context.getMethod() == "POST"');
  212. $this->assertEquals('context.getMethod() == "POST"', $routea->getCondition());
  213. $this->assertEquals('context.getMethod() == "POST"', $routeb->getCondition());
  214. }
  215. public function testClone()
  216. {
  217. $collection = new RouteCollection();
  218. $collection->add('a', new Route('/a'));
  219. $collection->add('b', new Route('/b', array('placeholder' => 'default'), array('placeholder' => '.+')));
  220. $clonedCollection = clone $collection;
  221. $this->assertCount(2, $clonedCollection);
  222. $this->assertEquals($collection->get('a'), $clonedCollection->get('a'));
  223. $this->assertNotSame($collection->get('a'), $clonedCollection->get('a'));
  224. $this->assertEquals($collection->get('b'), $clonedCollection->get('b'));
  225. $this->assertNotSame($collection->get('b'), $clonedCollection->get('b'));
  226. }
  227. public function testSetSchemes()
  228. {
  229. $collection = new RouteCollection();
  230. $routea = new Route('/a', array(), array(), array(), '', 'http');
  231. $routeb = new Route('/b');
  232. $collection->add('a', $routea);
  233. $collection->add('b', $routeb);
  234. $collection->setSchemes(array('http', 'https'));
  235. $this->assertEquals(array('http', 'https'), $routea->getSchemes());
  236. $this->assertEquals(array('http', 'https'), $routeb->getSchemes());
  237. }
  238. public function testSetMethods()
  239. {
  240. $collection = new RouteCollection();
  241. $routea = new Route('/a', array(), array(), array(), '', array(), array('GET', 'POST'));
  242. $routeb = new Route('/b');
  243. $collection->add('a', $routea);
  244. $collection->add('b', $routeb);
  245. $collection->setMethods('PUT');
  246. $this->assertEquals(array('PUT'), $routea->getMethods());
  247. $this->assertEquals(array('PUT'), $routeb->getMethods());
  248. }
  249. }