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/Loader/LoaderTest.php
Jeremy Mikola c8c9fba7d9 [Routing] Add optional "type" param for loader hinting when resource strings are ambiguous
Currently, ambiguities only arise for PHP files, as PhpFileLoader and AnnotationFileLoader would both claim support.  Future conflicts may occur if the XML, YAML, or PHP loaders were to receive Directory and Glob loaders (as annotations have).

Since the "type" parameter is optional, loader resolution will default to awarding resolution to the first loader to claim support.  A previous hack in PhpFileLoader to avoid an AnnotationFileLoader conflict was removed, so that should be the only lost backwards compatibility with this patch.  Unit tests were also created for the various loader classes, although only the supports() method is being tested.

This implementation was proposed on the symfony-dev mailing list in response to Fabien's RFC for custom loader notation: http://groups.google.com/group/symfony-devs/browse_thread/thread/3104c1a9e45799d2/20fbe393c1afe088
2010-12-10 09:48:10 +01:00

69 lines
2.1 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\Loader;
use Symfony\Component\Routing\Loader\LoaderResolver;
use Symfony\Component\Routing\Loader\Loader;
use Symfony\Component\Routing\Loader\XmlFileLoader;
class LoaderTest extends \PHPUnit_Framework_TestCase
{
/**
* @covers Symfony\Component\Routing\Loader\Loader::getResolver
* @covers Symfony\Component\Routing\Loader\Loader::setResolver
*/
public function testGetSetResolver()
{
$resolver = new LoaderResolver();
$loader = new ProjectLoader1();
$loader->setResolver($resolver);
$this->assertSame($resolver, $loader->getResolver(), '->setResolver() sets the resolver loader');
}
/**
* @covers Symfony\Component\Routing\Loader\Loader::resolve
*/
public function testResolve()
{
$resolver = new LoaderResolver(array(
$ini = new XmlFileLoader(array()),
));
$loader = new ProjectLoader1();
$loader->setResolver($resolver);
$this->assertSame($ini, $loader->resolve('foo.xml'), '->resolve() finds a loader');
$this->assertSame($loader, $loader->resolve('foo.foo'), '->resolve() finds a loader');
try {
$loader->resolve(new \stdClass());
$this->fail('->resolve() throws an \InvalidArgumentException if the resource cannot be loaded');
} catch (\Exception $e) {
$this->assertInstanceOf('\InvalidArgumentException', $e, '->resolve() throws an \InvalidArgumentException if the resource cannot be loaded');
}
}
}
class ProjectLoader1 extends Loader
{
public function load($resource, $type = null)
{
}
public function supports($resource, $type = null)
{
return is_string($resource) && 'foo' === pathinfo($resource, PATHINFO_EXTENSION);
}
public function getType()
{
}
}