RouteCollectionTest.php 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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\Tests\Component\Routing;
  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 testOverridenRoute()
  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 testDeepOverridenRoute()
  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 testIteratorWithOverridenRoutes()
  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. protected function getFirstNamedRoute(RouteCollection $routeCollection, $name)
  64. {
  65. foreach ($routeCollection as $key => $route) {
  66. if ($route instanceof RouteCollection) {
  67. return $this->getFirstNamedRoute($route, $name);
  68. }
  69. if ($name === $key) {
  70. return $route;
  71. }
  72. }
  73. }
  74. public function testAddCollection()
  75. {
  76. $collection = new RouteCollection();
  77. $collection->add('foo', $foo = new Route('/foo'));
  78. $collection1 = new RouteCollection();
  79. $collection1->add('foo', $foo1 = new Route('/foo1'));
  80. $collection1->add('bar', $bar1 = new Route('/bar1'));
  81. $collection->addCollection($collection1);
  82. $this->assertEquals(array('foo' => $foo1, 'bar' => $bar1), $collection->all(), '->addCollection() adds routes from another collection');
  83. $collection = new RouteCollection();
  84. $collection->add('foo', $foo = new Route('/foo'));
  85. $collection1 = new RouteCollection();
  86. $collection1->add('foo', $foo1 = new Route('/foo1'));
  87. $collection->addCollection($collection1, '/foo');
  88. $this->assertEquals('/foo/foo1', $collection->get('foo')->getPattern(), '->addCollection() can add a prefix to all merged routes');
  89. $collection = new RouteCollection();
  90. $collection->addResource($foo = new FileResource(__DIR__.'/Fixtures/foo.xml'));
  91. $collection1 = new RouteCollection();
  92. $collection1->addResource($foo1 = new FileResource(__DIR__.'/Fixtures/foo1.xml'));
  93. $collection->addCollection($collection1);
  94. $this->assertEquals(array($foo, $foo1), $collection->getResources(), '->addCollection() merges resources');
  95. }
  96. public function testAddPrefix()
  97. {
  98. $collection = new RouteCollection();
  99. $collection->add('foo', $foo = new Route('/foo'));
  100. $collection->add('bar', $bar = new Route('/bar'));
  101. $collection->addPrefix('/admin');
  102. $this->assertEquals('/admin/foo', $collection->get('foo')->getPattern(), '->addPrefix() adds a prefix to all routes');
  103. $this->assertEquals('/admin/bar', $collection->get('bar')->getPattern(), '->addPrefix() adds a prefix to all routes');
  104. }
  105. public function testResource()
  106. {
  107. $collection = new RouteCollection();
  108. $collection->addResource($foo = new FileResource(__DIR__.'/Fixtures/foo.xml'));
  109. $this->assertEquals(array($foo), $collection->getResources(), '->addResources() adds a resource');
  110. }
  111. }