This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/tests/Symfony/Tests/Component/Routing/RouteTest.php
Fabien Potencier b63de46374 [Routing] moved from :var to {var}
This follows the "URI template" notation:

http://code.google.com/p/uri-templates/
http://tools.ietf.org/html/draft-gregorio-uritemplate-04

You need to change all your route definitions from something like:

    /article/:id

to something like:

    /article/{id}
2011-01-11 19:13:16 +01:00

95 lines
4.5 KiB
PHP

<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Tests\Component\Routing;
use Symfony\Component\Routing\Route;
class RouteTest extends \PHPUnit_Framework_TestCase
{
public function testConstructor()
{
$route = new Route('/{foo}', array('foo' => 'bar'), array('foo' => '\d+'), array('foo' => 'bar'));
$this->assertEquals('/{foo}', $route->getPattern(), '__construct() takes a pattern as its first argument');
$this->assertEquals(array('foo' => 'bar'), $route->getDefaults(), '__construct() takes defaults as its second argument');
$this->assertEquals(array('foo' => '\d+'), $route->getRequirements(), '__construct() takes requirements as its third argument');
$this->assertEquals('bar', $route->getOption('foo'), '__construct() takes options as its fourth argument');
}
public function testPattern()
{
$route = new Route('/{foo}');
$route->setPattern('/{bar}');
$this->assertEquals('/{bar}', $route->getPattern(), '->setPattern() sets the pattern');
$route->setPattern('');
$this->assertEquals('/', $route->getPattern(), '->setPattern() adds a / at the beginning of the pattern if needed');
$route->setPattern('bar');
$this->assertEquals('/bar', $route->getPattern(), '->setPattern() adds a / at the beginning of the pattern if needed');
$this->assertEquals($route, $route->setPattern(''), '->setPattern() implements a fluent interface');
}
public function testOptions()
{
$route = new Route('/{foo}');
$route->setOptions(array('foo' => 'bar'));
$this->assertEquals(array_merge(array(
'segment_separators' => array('/', '.'),
'text_regex' => '.+?',
'compiler_class' => 'Symfony\\Component\\Routing\\RouteCompiler',
), array('foo' => 'bar')), $route->getOptions(), '->setOptions() sets the options');
$this->assertEquals($route, $route->setOptions(array()), '->setOptions() implements a fluent interface');
}
/**
* @covers Symfony\Component\Routing\Route::setDefaults
* @covers Symfony\Component\Routing\Route::getDefaults
* @covers Symfony\Component\Routing\Route::setDefault
* @covers Symfony\Component\Routing\Route::getDefault
*/
public function testDefaults()
{
$route = new Route('/{foo}');
$route->setDefaults(array('foo' => 'bar'));
$this->assertEquals(array('foo' => 'bar'), $route->getDefaults(), '->setDefaults() sets the defaults');
$this->assertEquals($route, $route->setDefaults(array()), '->setDefaults() implements a fluent interface');
$route->setDefault('foo', 'bar');
$this->assertEquals('bar', $route->getDefault('foo'), '->setDefault() sets a default value');
$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');
}
public function testRequirements()
{
$route = new Route('/{foo}');
$route->setRequirements(array('foo' => '\d+'));
$this->assertEquals(array('foo' => '\d+'), $route->getRequirements(), '->setRequirements() sets the requirements');
$this->assertEquals('\d+', $route->getRequirement('foo'), '->getRequirement() returns a requirement');
$this->assertNull($route->getRequirement('bar'), '->getRequirement() returns null if a requirement is not defined');
$route->setRequirements(array('foo' => '^\d+$'));
$this->assertEquals('\d+', $route->getRequirement('foo'), '->getRequirement() removes ^ and $ from the pattern');
$this->assertEquals($route, $route->setRequirements(array()), '->setRequirements() implements a fluent interface');
// test that an array requirement throws an exception
$this->setExpectedException('InvalidArgumentException');
$route->setRequirements(array('foo' => array('bar', 'baz')));
}
public function testCompile()
{
$route = new Route('/{foo}');
$this->assertEquals('Symfony\\Component\\Routing\\CompiledRoute', get_class($compiled = $route->compile()), '->compile() returns a compiled route');
$this->assertEquals($compiled, $route->compile(), '->compile() only compiled the route once');
}
}