2010-06-24 07:46:50 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of the Symfony package.
|
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
2010-06-24 07:46:50 +01:00
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
2010-08-20 22:09:55 +01:00
|
|
|
namespace Symfony\Tests\Component\Routing;
|
2010-06-24 07:46:50 +01:00
|
|
|
|
2010-08-20 22:09:55 +01:00
|
|
|
use Symfony\Component\Routing\RouteCollection;
|
|
|
|
use Symfony\Component\Routing\Route;
|
2011-02-10 15:15:51 +00:00
|
|
|
use Symfony\Component\Config\Resource\FileResource;
|
2010-06-24 07:46:50 +01:00
|
|
|
|
|
|
|
class RouteCollectionTest extends \PHPUnit_Framework_TestCase
|
|
|
|
{
|
|
|
|
public function testRoute()
|
|
|
|
{
|
|
|
|
$collection = new RouteCollection();
|
|
|
|
$route = new Route('/foo');
|
2010-11-23 08:42:19 +00:00
|
|
|
$collection->add('foo', $route);
|
|
|
|
$this->assertEquals(array('foo' => $route), $collection->all(), '->add() adds a route');
|
|
|
|
$this->assertEquals($route, $collection->get('foo'), '->get() returns a route by name');
|
|
|
|
$this->assertNull($collection->get('bar'), '->get() returns null if a route does not exist');
|
2010-06-24 07:46:50 +01:00
|
|
|
}
|
|
|
|
|
2010-10-17 19:15:26 +01:00
|
|
|
/**
|
|
|
|
* @expectedException InvalidArgumentException
|
|
|
|
*/
|
|
|
|
public function testAddInvalidRoute()
|
|
|
|
{
|
|
|
|
$collection = new RouteCollection();
|
|
|
|
$route = new Route('/foo');
|
2010-11-23 08:42:19 +00:00
|
|
|
$collection->add('f o o', $route);
|
2010-10-17 19:15:26 +01:00
|
|
|
}
|
|
|
|
|
2011-09-30 08:20:51 +01:00
|
|
|
public function testOverridenRoute()
|
|
|
|
{
|
|
|
|
$collection = new RouteCollection();
|
|
|
|
$collection->add('foo', new Route('/foo'));
|
|
|
|
$collection->add('foo', new Route('/foo1'));
|
|
|
|
|
|
|
|
$this->assertEquals('/foo1', $collection->get('foo')->getPattern());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDeepOverridenRoute()
|
|
|
|
{
|
|
|
|
$collection = new RouteCollection();
|
|
|
|
$collection->add('foo', new Route('/foo'));
|
|
|
|
|
|
|
|
$collection1 = new RouteCollection();
|
|
|
|
$collection1->add('foo', new Route('/foo1'));
|
|
|
|
|
|
|
|
$collection2 = new RouteCollection();
|
|
|
|
$collection2->add('foo', new Route('/foo2'));
|
|
|
|
|
|
|
|
$collection1->addCollection($collection2);
|
|
|
|
$collection->addCollection($collection1);
|
|
|
|
|
|
|
|
$this->assertEquals('/foo2', $collection1->get('foo')->getPattern());
|
|
|
|
$this->assertEquals('/foo2', $collection->get('foo')->getPattern());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testIteratorWithOverridenRoutes()
|
|
|
|
{
|
|
|
|
$collection = new RouteCollection();
|
|
|
|
$collection->add('foo', new Route('/foo'));
|
|
|
|
|
|
|
|
$collection1 = new RouteCollection();
|
|
|
|
$collection->addCollection($collection1);
|
|
|
|
$collection1->add('foo', new Route('/foo1'));
|
|
|
|
|
|
|
|
$this->assertEquals('/foo1', $this->getFirstNamedRoute($collection, 'foo')->getPattern());
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getFirstNamedRoute(RouteCollection $routeCollection, $name)
|
|
|
|
{
|
|
|
|
foreach ($routeCollection as $key => $route) {
|
|
|
|
if ($route instanceof RouteCollection) {
|
|
|
|
return $this->getFirstNamedRoute($route, $name);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($name === $key) {
|
|
|
|
return $route;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-24 07:46:50 +01:00
|
|
|
public function testAddCollection()
|
|
|
|
{
|
|
|
|
$collection = new RouteCollection();
|
2010-11-23 08:42:19 +00:00
|
|
|
$collection->add('foo', $foo = new Route('/foo'));
|
2010-06-24 07:46:50 +01:00
|
|
|
$collection1 = new RouteCollection();
|
2010-11-23 08:42:19 +00:00
|
|
|
$collection1->add('foo', $foo1 = new Route('/foo1'));
|
|
|
|
$collection1->add('bar', $bar1 = new Route('/bar1'));
|
2010-06-24 07:46:50 +01:00
|
|
|
$collection->addCollection($collection1);
|
2010-11-23 08:42:19 +00:00
|
|
|
$this->assertEquals(array('foo' => $foo1, 'bar' => $bar1), $collection->all(), '->addCollection() adds routes from another collection');
|
2010-06-24 07:46:50 +01:00
|
|
|
|
|
|
|
$collection = new RouteCollection();
|
2010-11-23 08:42:19 +00:00
|
|
|
$collection->add('foo', $foo = new Route('/foo'));
|
2010-06-24 07:46:50 +01:00
|
|
|
$collection1 = new RouteCollection();
|
2010-11-23 08:42:19 +00:00
|
|
|
$collection1->add('foo', $foo1 = new Route('/foo1'));
|
2011-10-23 10:56:23 +01:00
|
|
|
$collection->addCollection($collection1, '/{foo}', array('foo' => 'foo'), array('foo' => '\d+'));
|
|
|
|
$this->assertEquals('/{foo}/foo1', $collection->get('foo')->getPattern(), '->addCollection() can add a prefix to all merged routes');
|
|
|
|
$this->assertEquals(array('foo' => 'foo'), $collection->get('foo')->getDefaults(), '->addCollection() can add a prefix to all merged routes');
|
|
|
|
$this->assertEquals(array('foo' => '\d+'), $collection->get('foo')->getRequirements(), '->addCollection() can add a prefix to all merged routes');
|
2010-06-24 07:46:50 +01:00
|
|
|
|
|
|
|
$collection = new RouteCollection();
|
2010-06-28 08:15:15 +01:00
|
|
|
$collection->addResource($foo = new FileResource(__DIR__.'/Fixtures/foo.xml'));
|
2010-06-24 07:46:50 +01:00
|
|
|
$collection1 = new RouteCollection();
|
2010-06-28 08:15:15 +01:00
|
|
|
$collection1->addResource($foo1 = new FileResource(__DIR__.'/Fixtures/foo1.xml'));
|
2010-06-24 07:46:50 +01:00
|
|
|
$collection->addCollection($collection1);
|
|
|
|
$this->assertEquals(array($foo, $foo1), $collection->getResources(), '->addCollection() merges resources');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAddPrefix()
|
|
|
|
{
|
|
|
|
$collection = new RouteCollection();
|
2010-11-23 08:42:19 +00:00
|
|
|
$collection->add('foo', $foo = new Route('/foo'));
|
|
|
|
$collection->add('bar', $bar = new Route('/bar'));
|
2011-10-23 10:56:23 +01:00
|
|
|
$collection->addPrefix('/{admin}', array('admin' => 'admin'), array('admin' => '\d+'));
|
|
|
|
$this->assertEquals('/{admin}/foo', $collection->get('foo')->getPattern(), '->addPrefix() adds a prefix to all routes');
|
|
|
|
$this->assertEquals('/{admin}/bar', $collection->get('bar')->getPattern(), '->addPrefix() adds a prefix to all routes');
|
|
|
|
$this->assertEquals(array('admin' => 'admin'), $collection->get('foo')->getDefaults(), '->addPrefix() adds a prefix to all routes');
|
|
|
|
$this->assertEquals(array('admin' => 'admin'), $collection->get('bar')->getDefaults(), '->addPrefix() adds a prefix to all routes');
|
|
|
|
$this->assertEquals(array('admin' => '\d+'), $collection->get('foo')->getRequirements(), '->addPrefix() adds a prefix to all routes');
|
|
|
|
$this->assertEquals(array('admin' => '\d+'), $collection->get('bar')->getRequirements(), '->addPrefix() adds a prefix to all routes');
|
2010-06-24 07:46:50 +01:00
|
|
|
}
|
|
|
|
|
2011-10-23 13:45:47 +01:00
|
|
|
public function testAddPrefixOverridesDefaultsAndRequirements()
|
|
|
|
{
|
|
|
|
$collection = new RouteCollection();
|
|
|
|
$collection->add('foo', $foo = new Route('/foo'));
|
|
|
|
$collection->add('bar', $bar = new Route('/bar', array(), array('_scheme' => 'http')));
|
|
|
|
$collection->addPrefix('/admin', array(), array('_scheme' => 'https'));
|
|
|
|
|
|
|
|
$this->assertEquals('https', $collection->get('foo')->getRequirement('_scheme'), '->addPrefix() overrides existing requirements');
|
|
|
|
$this->assertEquals('https', $collection->get('bar')->getRequirement('_scheme'), '->addPrefix() overrides existing requirements');
|
|
|
|
}
|
|
|
|
|
2010-06-24 07:46:50 +01:00
|
|
|
public function testResource()
|
|
|
|
{
|
|
|
|
$collection = new RouteCollection();
|
2010-06-28 08:15:15 +01:00
|
|
|
$collection->addResource($foo = new FileResource(__DIR__.'/Fixtures/foo.xml'));
|
2010-06-24 07:46:50 +01:00
|
|
|
$this->assertEquals(array($foo), $collection->getResources(), '->addResources() adds a resource');
|
|
|
|
}
|
|
|
|
}
|