/integration-tests/performance-test-engine/vendor/symfony/symfony/src/Symfony/Component/Routing/Tests/RouteCollectionTest.php

https://github.com/societies/SOCIETIES-Platform · PHP · 291 lines · 221 code · 50 blank · 20 comment · 4 complexity · bf62c81c45ab29e08ed19017cb7d6a91 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\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. /**
  26. * @expectedException \InvalidArgumentException
  27. */
  28. public function testAddInvalidRoute()
  29. {
  30. $collection = new RouteCollection();
  31. $route = new Route('/foo');
  32. $collection->add('f o o', $route);
  33. }
  34. public function testOverriddenRoute()
  35. {
  36. $collection = new RouteCollection();
  37. $collection->add('foo', new Route('/foo'));
  38. $collection->add('foo', new Route('/foo1'));
  39. $this->assertEquals('/foo1', $collection->get('foo')->getPattern());
  40. }
  41. public function testDeepOverriddenRoute()
  42. {
  43. $collection = new RouteCollection();
  44. $collection->add('foo', new Route('/foo'));
  45. $collection1 = new RouteCollection();
  46. $collection1->add('foo', new Route('/foo1'));
  47. $collection2 = new RouteCollection();
  48. $collection2->add('foo', new Route('/foo2'));
  49. $collection1->addCollection($collection2);
  50. $collection->addCollection($collection1);
  51. $this->assertEquals('/foo2', $collection1->get('foo')->getPattern());
  52. $this->assertEquals('/foo2', $collection->get('foo')->getPattern());
  53. }
  54. public function testIteratorWithOverriddenRoutes()
  55. {
  56. $collection = new RouteCollection();
  57. $collection->add('foo', new Route('/foo'));
  58. $collection1 = new RouteCollection();
  59. $collection->addCollection($collection1);
  60. $collection1->add('foo', new Route('/foo1'));
  61. $this->assertEquals('/foo1', $this->getFirstNamedRoute($collection, 'foo')->getPattern());
  62. }
  63. public function testCount()
  64. {
  65. $collection = new RouteCollection();
  66. $collection->add('foo', new Route('/foo'));
  67. $collection1 = new RouteCollection();
  68. $collection->addCollection($collection1);
  69. $collection1->add('foo1', new Route('/foo1'));
  70. $this->assertCount(2, $collection);
  71. }
  72. protected function getFirstNamedRoute(RouteCollection $routeCollection, $name)
  73. {
  74. foreach ($routeCollection as $key => $route) {
  75. if ($route instanceof RouteCollection) {
  76. return $this->getFirstNamedRoute($route, $name);
  77. }
  78. if ($name === $key) {
  79. return $route;
  80. }
  81. }
  82. }
  83. public function testAddCollection()
  84. {
  85. if (!class_exists('Symfony\Component\Config\Resource\FileResource')) {
  86. $this->markTestSkipped('The "Config" component is not available');
  87. }
  88. $collection = new RouteCollection();
  89. $collection->add('foo', $foo = new Route('/foo'));
  90. $collection1 = new RouteCollection();
  91. $collection1->add('foo', $foo1 = new Route('/foo1'));
  92. $collection1->add('bar', $bar1 = new Route('/bar1'));
  93. $collection->addCollection($collection1);
  94. $this->assertEquals(array('foo' => $foo1, 'bar' => $bar1), $collection->all(), '->addCollection() adds routes from another collection');
  95. $collection = new RouteCollection();
  96. $collection->add('foo', $foo = new Route('/foo'));
  97. $collection1 = new RouteCollection();
  98. $collection1->add('foo', $foo1 = new Route('/foo1'));
  99. $collection->addCollection($collection1, '/{foo}', array('foo' => 'foo'), array('foo' => '\d+'), array('foo' => 'bar'));
  100. $this->assertEquals('/{foo}/foo1', $collection->get('foo')->getPattern(), '->addCollection() can add a prefix to all merged routes');
  101. $this->assertEquals(array('foo' => 'foo'), $collection->get('foo')->getDefaults(), '->addCollection() can add a prefix to all merged routes');
  102. $this->assertEquals(array('foo' => '\d+'), $collection->get('foo')->getRequirements(), '->addCollection() can add a prefix to all merged routes');
  103. $this->assertEquals(
  104. array('foo' => 'bar', 'compiler_class' => 'Symfony\\Component\\Routing\\RouteCompiler'),
  105. $collection->get('foo')->getOptions(), '->addCollection() can add an option to all merged routes'
  106. );
  107. $collection = new RouteCollection();
  108. $collection->addResource($foo = new FileResource(__DIR__.'/Fixtures/foo.xml'));
  109. $collection1 = new RouteCollection();
  110. $collection1->addResource($foo1 = new FileResource(__DIR__.'/Fixtures/foo1.xml'));
  111. $collection->addCollection($collection1);
  112. $this->assertEquals(array($foo, $foo1), $collection->getResources(), '->addCollection() merges resources');
  113. }
  114. public function testAddPrefix()
  115. {
  116. $collection = new RouteCollection();
  117. $collection->add('foo', $foo = new Route('/foo'));
  118. $collection->add('bar', $bar = new Route('/bar'));
  119. $collection->addPrefix('/{admin}', array('admin' => 'admin'), array('admin' => '\d+'), array('foo' => 'bar'));
  120. $this->assertEquals('/{admin}/foo', $collection->get('foo')->getPattern(), '->addPrefix() adds a prefix to all routes');
  121. $this->assertEquals('/{admin}/bar', $collection->get('bar')->getPattern(), '->addPrefix() adds a prefix to all routes');
  122. $this->assertEquals(array('admin' => 'admin'), $collection->get('foo')->getDefaults(), '->addPrefix() adds a prefix to all routes');
  123. $this->assertEquals(array('admin' => 'admin'), $collection->get('bar')->getDefaults(), '->addPrefix() adds a prefix to all routes');
  124. $this->assertEquals(array('admin' => '\d+'), $collection->get('foo')->getRequirements(), '->addPrefix() adds a prefix to all routes');
  125. $this->assertEquals(array('admin' => '\d+'), $collection->get('bar')->getRequirements(), '->addPrefix() adds a prefix to all routes');
  126. $this->assertEquals(
  127. array('foo' => 'bar', 'compiler_class' => 'Symfony\\Component\\Routing\\RouteCompiler'),
  128. $collection->get('foo')->getOptions(), '->addPrefix() adds an option to all routes'
  129. );
  130. $this->assertEquals(
  131. array('foo' => 'bar', 'compiler_class' => 'Symfony\\Component\\Routing\\RouteCompiler'),
  132. $collection->get('bar')->getOptions(), '->addPrefix() adds an option to all routes'
  133. );
  134. $collection->addPrefix('0');
  135. $this->assertEquals('/0/{admin}', $collection->getPrefix(), '->addPrefix() ensures a prefix must start with a slash and must not end with a slash');
  136. }
  137. public function testAddPrefixOverridesDefaultsAndRequirements()
  138. {
  139. $collection = new RouteCollection();
  140. $collection->add('foo', $foo = new Route('/foo'));
  141. $collection->add('bar', $bar = new Route('/bar', array(), array('_scheme' => 'http')));
  142. $collection->addPrefix('/admin', array(), array('_scheme' => 'https'));
  143. $this->assertEquals('https', $collection->get('foo')->getRequirement('_scheme'), '->addPrefix() overrides existing requirements');
  144. $this->assertEquals('https', $collection->get('bar')->getRequirement('_scheme'), '->addPrefix() overrides existing requirements');
  145. }
  146. public function testAddCollectionOverridesDefaultsAndRequirements()
  147. {
  148. $imported = new RouteCollection();
  149. $imported->add('foo', $foo = new Route('/foo'));
  150. $imported->add('bar', $bar = new Route('/bar', array(), array('_scheme' => 'http')));
  151. $collection = new RouteCollection();
  152. $collection->addCollection($imported, null, array(), array('_scheme' => 'https'));
  153. $this->assertEquals('https', $collection->get('foo')->getRequirement('_scheme'), '->addCollection() overrides existing requirements');
  154. $this->assertEquals('https', $collection->get('bar')->getRequirement('_scheme'), '->addCollection() overrides existing requirements');
  155. }
  156. public function testResource()
  157. {
  158. if (!class_exists('Symfony\Component\Config\Resource\FileResource')) {
  159. $this->markTestSkipped('The "Config" component is not available');
  160. }
  161. $collection = new RouteCollection();
  162. $collection->addResource($foo = new FileResource(__DIR__.'/Fixtures/foo.xml'));
  163. $this->assertEquals(array($foo), $collection->getResources(), '->addResources() adds a resource');
  164. }
  165. public function testUniqueRouteWithGivenName()
  166. {
  167. $collection1 = new RouteCollection();
  168. $collection1->add('foo', new Route('/old'));
  169. $collection2 = new RouteCollection();
  170. $collection3 = new RouteCollection();
  171. $collection3->add('foo', $new = new Route('/new'));
  172. $collection1->addCollection($collection2);
  173. $collection2->addCollection($collection3);
  174. $collection1->add('stay', new Route('/stay'));
  175. $iterator = $collection1->getIterator();
  176. $this->assertSame($new, $collection1->get('foo'), '->get() returns new route that overrode previous one');
  177. // size of 2 because collection1 contains collection2 and /stay but not /old anymore
  178. $this->assertCount(2, $iterator, '->addCollection() removes previous routes when adding new routes with the same name');
  179. $this->assertInstanceOf('Symfony\Component\Routing\RouteCollection', $iterator->current(), '->getIterator returns both Routes and RouteCollections');
  180. $iterator->next();
  181. $this->assertInstanceOf('Symfony\Component\Routing\Route', $iterator->current(), '->getIterator returns both Routes and RouteCollections');
  182. }
  183. public function testGet()
  184. {
  185. $collection1 = new RouteCollection();
  186. $collection1->add('a', $a = new Route('/a'));
  187. $collection2 = new RouteCollection();
  188. $collection2->add('b', $b = new Route('/b'));
  189. $collection1->addCollection($collection2);
  190. $this->assertSame($b, $collection1->get('b'), '->get() returns correct route in child collection');
  191. $this->assertNull($collection2->get('a'), '->get() does not return the route defined in parent collection');
  192. $this->assertNull($collection1->get('non-existent'), '->get() returns null when route does not exist');
  193. $this->assertNull($collection1->get(0), '->get() does not disclose internal child RouteCollection');
  194. }
  195. /**
  196. * @expectedException \InvalidArgumentException
  197. */
  198. public function testCannotSelfJoinCollection()
  199. {
  200. $collection = new RouteCollection();
  201. $collection->addCollection($collection);
  202. }
  203. /**
  204. * @expectedException \InvalidArgumentException
  205. */
  206. public function testCannotAddExistingCollectionToTree()
  207. {
  208. $collection1 = new RouteCollection();
  209. $collection2 = new RouteCollection();
  210. $collection3 = new RouteCollection();
  211. $collection1->addCollection($collection2);
  212. $collection1->addCollection($collection3);
  213. $collection2->addCollection($collection3);
  214. }
  215. public function testPatternDoesNotChangeWhenDefinitionOrderChanges()
  216. {
  217. $collection1 = new RouteCollection();
  218. $collection1->add('a', new Route('/a...'));
  219. $collection2 = new RouteCollection();
  220. $collection2->add('b', new Route('/b...'));
  221. $collection3 = new RouteCollection();
  222. $collection3->add('c', new Route('/c...'));
  223. $rootCollection_A = new RouteCollection();
  224. $collection2->addCollection($collection3, '/c');
  225. $collection1->addCollection($collection2, '/b');
  226. $rootCollection_A->addCollection($collection1, '/a');
  227. // above should mean the same as below
  228. $collection1 = new RouteCollection();
  229. $collection1->add('a', new Route('/a...'));
  230. $collection2 = new RouteCollection();
  231. $collection2->add('b', new Route('/b...'));
  232. $collection3 = new RouteCollection();
  233. $collection3->add('c', new Route('/c...'));
  234. $rootCollection_B = new RouteCollection();
  235. $collection1->addCollection($collection2, '/b');
  236. $collection2->addCollection($collection3, '/c');
  237. $rootCollection_B->addCollection($collection1, '/a');
  238. // test it now
  239. $this->assertEquals($rootCollection_A, $rootCollection_B);
  240. }
  241. }