Merge remote branch 'jakzal/routing-tests'

This commit is contained in:
Fabien Potencier 2011-03-21 09:07:58 +01:00
commit a44f8d8de4
14 changed files with 252 additions and 0 deletions

View File

@ -0,0 +1,46 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.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\Annotation;
use Symfony\Component\Routing\Annotation\Route;
class RouteTest extends \PHPUnit_Framework_TestCase
{
/**
* @expectedException \BadMethodCallException
*/
public function testInvalidRouteParameter()
{
$route = new Route(array('foo' => 'bar'));
}
/**
* @dataProvider getValidParameters
*/
public function testRouteParameters($parameter, $value, $getter)
{
$route = new Route(array($parameter => $value));
$this->assertEquals($route->$getter(), $value);
}
public function getValidParameters()
{
return array(
array('value', '/Blog', 'getPattern'),
array('requirements', array('_method' => 'GET'), 'getRequirements'),
array('options', array('segment_separators' => array('/')), 'getOptions'),
array('name', 'blog_index', 'getName'),
array('defaults', array('_controller' => 'MyBlogBundle:Blog:index'), 'getDefaults')
);
}
}

View File

@ -0,0 +1,2 @@
blog_show:
defaults: { _controller: MyBlogBundle:Blog:show }

View File

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8" ?>
<routes xmlns="http://symfony.com/schema/routing"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd">
<route id="blog_show" pattern="/blog/{slug}">
<default key="_controller">MyBundle:Blog:show</default>
<requirement key="_method">GET</requirement>
<option key="segment_separators">/</option>
<!-- </route> -->
</routes>

View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8" ?>
<routes xmlns="http://symfony.com/schema/routing"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd">
<foo>bar</foo>
</routes>

View File

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8" ?>
<routes xmlns="http://symfony.com/schema/routing"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd">
<route id="blog_show" pattern="/blog/{slug}">
<default key="_controller">MyBundle:Blog:show</default>
<requirement key="_method">GET</requirement>
<option key="segment_separators">/</option>
<foo key="bar">baz</foo>
</route>
</routes>

View File

@ -0,0 +1,10 @@
<?php
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\Route;
$collection = new RouteCollection();
$collection->add('blog_show', new Route('/blog/{slug}', array(
'_controller' => 'MyBlogBundle:Blog:show',
)));
return $collection;

View File

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8" ?>
<routes xmlns="http://symfony.com/schema/routing"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd">
<route id="blog_show" pattern="/blog/{slug}">
<default key="_controller">MyBundle:Blog:show</default>
<requirement key="_method">GET</requirement>
<option key="segment_separators">/</option>
</route>
</routes>

View File

@ -0,0 +1,4 @@
blog_show:
pattern: /blog/{slug}
defaults: { _controller: MyBlogBundle:Blog:show }

View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8" ?>
<routes xmlns="http://symfony.com/schema/routing"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd">
<import resource="validpattern.xml" />
</routes>

View File

@ -0,0 +1,2 @@
blog_show:
resource: validpattern.yml

View File

@ -12,6 +12,7 @@
namespace Symfony\Tests\Component\Routing\Loader;
use Symfony\Component\Config\Loader\LoaderResolver;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Routing\Loader\PhpFileLoader;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
@ -31,4 +32,15 @@ class PhpFileLoaderTest extends \PHPUnit_Framework_TestCase
$this->assertTrue($loader->supports('foo.php', 'php'), '->supports() checks the resource type if specified');
$this->assertFalse($loader->supports('foo.php', 'foo'), '->supports() checks the resource type if specified');
}
public function testLoadWithRoute()
{
$loader = new PhpFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
$routeCollection = $loader->load('validpattern.php');
$routes = $routeCollection->all();
$this->assertEquals(1, count($routes), 'One route is loaded');
$this->assertContainsOnly('Symfony\Component\Routing\Route', $routes);
}
}

View File

@ -12,6 +12,7 @@
namespace Symfony\Tests\Component\Routing\Loader;
use Symfony\Component\Config\Loader\LoaderResolver;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Routing\Loader\XmlFileLoader;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
@ -31,4 +32,61 @@ class XmlFileLoaderTest extends \PHPUnit_Framework_TestCase
$this->assertTrue($loader->supports('foo.xml', 'xml'), '->supports() checks the resource type if specified');
$this->assertFalse($loader->supports('foo.xml', 'foo'), '->supports() checks the resource type if specified');
}
public function testLoadWithRoute()
{
$loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
$routeCollection = $loader->load('validpattern.xml');
$routes = $routeCollection->all();
$this->assertEquals(1, count($routes), 'One route is loaded');
$this->assertContainsOnly('Symfony\Component\Routing\Route', $routes);
}
public function testLoadWithImport()
{
$loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
$routeCollection = $loader->load('validresource.xml');
$routes = $routeCollection->all();
$this->assertEquals(1, count($routes), 'One route is loaded');
$this->assertContainsOnly('Symfony\Component\Routing\Route', $routes);
}
/**
* @expectedException \InvalidArgumentException
* @dataProvider getPathsToInvalidFiles
*/
public function testLoadThrowsExceptionWithInvalidFile($filePath)
{
$loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
$loader->load($filePath);
}
/**
* @expectedException \InvalidArgumentException
* @dataProvider getPathsToInvalidFiles
*/
public function testLoadThrowsExceptionWithInvalidFileEvenWithoutSchemaValidation($filePath)
{
$loader = new CustomXmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
$loader->load($filePath);
}
public function getPathsToInvalidFiles()
{
return array(array('nonvalidnode.xml'), array('nonvalidroute.xml'), array('nonvalid.xml'));
}
}
/**
* XmlFileLoader with schema validation turned off
*/
class CustomXmlFileLoader extends XmlFileLoader
{
protected function validate(\DOMDocument $dom)
{
return true;
}
}

View File

@ -60,4 +60,62 @@ class YamlFileLoaderTest extends \PHPUnit_Framework_TestCase
$loader = new YamlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
$loader->load('nonvalidkeys.yml');
}
/**
* @expectedException \InvalidArgumentException
*/
public function testLoadThrowsExceptionWhenIncomplete()
{
$loader = new YamlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
$loader->load('incomplete.yml');
}
public function testLoadWithPattern()
{
$loader = new YamlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
$routeCollection = $loader->load('validpattern.yml');
$routes = $routeCollection->all();
$this->assertEquals(1, count($routes), 'One route is loaded');
$this->assertContainsOnly('Symfony\Component\Routing\Route', $routes);
}
public function testLoadWithResource()
{
$loader = new YamlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
$routeCollection = $loader->load('validresource.yml');
$routes = $routeCollection->all();
$this->assertEquals(1, count($routes), 'One route is loaded');
$this->assertContainsOnly('Symfony\Component\Routing\Route', $routes);
}
/**
* @expectedException \InvalidArgumentException
*/
public function testParseRouteThrowsExceptionWithMissingPattern()
{
$loader = new CustomYamlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
$loader->load('incomplete.yml');
}
}
class CustomYamlFileLoader extends YamlFileLoader
{
public function load($file, $type = null)
{
$path = $this->locator->locate($file);
$config = $this->loadFile($path);
$collection = new RouteCollection();
$collection->addResource(new FileResource($path));
foreach ($config as $name => $config) {
$this->parseRoute($collection, $name, $config, $path);
}
return $collection;
}
}

View File

@ -85,6 +85,13 @@ class RouteTest extends \PHPUnit_Framework_TestCase
$route->setRequirements(array('foo' => array('bar', 'baz')));
}
public function testRequirement()
{
$route = new Route('/{foo}');
$route->setRequirement('foo', '^\d+$');
$this->assertEquals('\d+', $route->getRequirement('foo'), '->setRequirement() removes ^ and $ from the pattern');
}
public function testCompile()
{
$route = new Route('/{foo}');