[Routing] add missing unit tests for Route and RouteCollection classes
This commit is contained in:
parent
f36f666548
commit
7bbd56892d
@ -252,4 +252,47 @@ class RouteCollectionTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertEquals('{locale}.example.com', $routea->getHost());
|
||||
$this->assertEquals('{locale}.example.com', $routeb->getHost());
|
||||
}
|
||||
|
||||
public function testClone()
|
||||
{
|
||||
$collection = new RouteCollection();
|
||||
$collection->add('a', new Route('/a'));
|
||||
$collection->add('b', new Route('/b', array('placeholder' => 'default'), array('placeholder' => '.+')));
|
||||
|
||||
$clonedCollection = clone $collection;
|
||||
|
||||
$this->assertCount(2, $clonedCollection);
|
||||
$this->assertEquals($collection->get('a'), $clonedCollection->get('a'));
|
||||
$this->assertNotSame($collection->get('a'), $clonedCollection->get('a'));
|
||||
$this->assertEquals($collection->get('b'), $clonedCollection->get('b'));
|
||||
$this->assertNotSame($collection->get('b'), $clonedCollection->get('b'));
|
||||
}
|
||||
|
||||
public function testSetSchemes()
|
||||
{
|
||||
$collection = new RouteCollection();
|
||||
$routea = new Route('/a', array(), array(), array(), '', 'http');
|
||||
$routeb = new Route('/b');
|
||||
$collection->add('a', $routea);
|
||||
$collection->add('b', $routeb);
|
||||
|
||||
$collection->setSchemes(array('http', 'https'));
|
||||
|
||||
$this->assertEquals(array('http', 'https'), $routea->getSchemes());
|
||||
$this->assertEquals(array('http', 'https'), $routeb->getSchemes());
|
||||
}
|
||||
|
||||
public function testSetMethods()
|
||||
{
|
||||
$collection = new RouteCollection();
|
||||
$routea = new Route('/a', array(), array(), array(), '', array(), array('GET', 'POST'));
|
||||
$routeb = new Route('/b');
|
||||
$collection->add('a', $routea);
|
||||
$collection->add('b', $routeb);
|
||||
|
||||
$collection->setMethods('PUT');
|
||||
|
||||
$this->assertEquals(array('PUT'), $routea->getMethods());
|
||||
$this->assertEquals(array('PUT'), $routeb->getMethods());
|
||||
}
|
||||
}
|
||||
|
@ -62,6 +62,15 @@ class RouteTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertEquals(array('foo' => 'foo', 'bar' => 'bar', 'compiler_class' => 'Symfony\\Component\\Routing\\RouteCompiler'), $route->getOptions(), '->addDefaults() keep previous defaults');
|
||||
}
|
||||
|
||||
public function testOption()
|
||||
{
|
||||
$route = new Route('/{foo}');
|
||||
$this->assertFalse($route->hasOption('foo'), '->hasOption() return false if option is not set');
|
||||
$this->assertEquals($route, $route->setOption('foo', 'bar'), '->setOption() implements a fluent interface');
|
||||
$this->assertEquals('bar', $route->getOption('foo'), '->setOption() sets the option');
|
||||
$this->assertTrue($route->hasOption('foo'), '->hasOption() return true if option is set');
|
||||
}
|
||||
|
||||
public function testDefaults()
|
||||
{
|
||||
$route = new Route('/{foo}');
|
||||
@ -74,7 +83,7 @@ class RouteTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
$route->setDefault('foo2', 'bar2');
|
||||
$this->assertEquals('bar2', $route->getDefault('foo2'), '->getDefault() return the default value');
|
||||
$this->assertNull($route->getDefault('not_defined'), '->getDefault() return null if default value is not setted');
|
||||
$this->assertNull($route->getDefault('not_defined'), '->getDefault() return null if default value is not set');
|
||||
|
||||
$route->setDefault('_controller', $closure = function () { return 'Hello'; });
|
||||
$this->assertEquals($closure, $route->getDefault('_controller'), '->setDefault() sets a default value');
|
||||
@ -105,8 +114,10 @@ class RouteTest extends \PHPUnit_Framework_TestCase
|
||||
public function testRequirement()
|
||||
{
|
||||
$route = new Route('/{foo}');
|
||||
$this->assertFalse($route->hasRequirement('foo'), '->hasRequirement() return false if requirement is not set');
|
||||
$route->setRequirement('foo', '^\d+$');
|
||||
$this->assertEquals('\d+', $route->getRequirement('foo'), '->setRequirement() removes ^ and $ from the path');
|
||||
$this->assertTrue($route->hasRequirement('foo'), '->hasRequirement() return true if requirement is set');
|
||||
}
|
||||
|
||||
/**
|
||||
@ -197,4 +208,24 @@ class RouteTest extends \PHPUnit_Framework_TestCase
|
||||
$route->setRequirement('foo', '.*');
|
||||
$this->assertNotSame($compiled, $route->compile(), '->compile() recompiles if the route was modified');
|
||||
}
|
||||
|
||||
public function testPattern()
|
||||
{
|
||||
$route = new Route('/{foo}');
|
||||
$this->assertEquals('/{foo}', $route->getPattern());
|
||||
|
||||
$route->setPattern('/bar');
|
||||
$this->assertEquals('/bar', $route->getPattern());
|
||||
}
|
||||
|
||||
public function testSerialize()
|
||||
{
|
||||
$route = new Route('/{foo}', array('foo' => 'default'), array('foo' => '\d+'));
|
||||
|
||||
$serialized = serialize($route);
|
||||
$unserialized = unserialize($serialized);
|
||||
|
||||
$this->assertEquals($route, $unserialized);
|
||||
$this->assertNotSame($route, $unserialized);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user