RouteCollectionTest.php 3.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. * @covers Symfony\Component\Routing\RouteCollection::add
  27. * @expectedException InvalidArgumentException
  28. */
  29. public function testAddInvalidRoute()
  30. {
  31. $collection = new RouteCollection();
  32. $route = new Route('/foo');
  33. $collection->add('f o o', $route);
  34. }
  35. public function testAddCollection()
  36. {
  37. $collection = new RouteCollection();
  38. $collection->add('foo', $foo = new Route('/foo'));
  39. $collection1 = new RouteCollection();
  40. $collection1->add('foo', $foo1 = new Route('/foo1'));
  41. $collection1->add('bar', $bar1 = new Route('/bar1'));
  42. $collection->addCollection($collection1);
  43. $this->assertEquals(array('foo' => $foo1, 'bar' => $bar1), $collection->all(), '->addCollection() adds routes from another collection');
  44. $collection = new RouteCollection();
  45. $collection->add('foo', $foo = new Route('/foo'));
  46. $collection1 = new RouteCollection();
  47. $collection1->add('foo', $foo1 = new Route('/foo1'));
  48. $collection->addCollection($collection1, '/foo');
  49. $this->assertEquals('/foo/foo1', $collection->get('foo')->getPattern(), '->addCollection() can add a prefix to all merged routes');
  50. $collection = new RouteCollection();
  51. $collection->addResource($foo = new FileResource(__DIR__.'/Fixtures/foo.xml'));
  52. $collection1 = new RouteCollection();
  53. $collection1->addResource($foo1 = new FileResource(__DIR__.'/Fixtures/foo1.xml'));
  54. $collection->addCollection($collection1);
  55. $this->assertEquals(array($foo, $foo1), $collection->getResources(), '->addCollection() merges resources');
  56. }
  57. public function testAddPrefix()
  58. {
  59. $collection = new RouteCollection();
  60. $collection->add('foo', $foo = new Route('/foo'));
  61. $collection->add('bar', $bar = new Route('/bar'));
  62. $collection->addPrefix('/admin');
  63. $this->assertEquals('/admin/foo', $collection->get('foo')->getPattern(), '->addPrefix() adds a prefix to all routes');
  64. $this->assertEquals('/admin/bar', $collection->get('bar')->getPattern(), '->addPrefix() adds a prefix to all routes');
  65. }
  66. public function testResource()
  67. {
  68. $collection = new RouteCollection();
  69. $collection->addResource($foo = new FileResource(__DIR__.'/Fixtures/foo.xml'));
  70. $this->assertEquals(array($foo), $collection->getResources(), '->addResources() adds a resource');
  71. }
  72. }