4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Routing\Tests\Loader
;
14 use Symfony\Component\Config\FileLocator
;
15 use Symfony\Component\Routing\Loader\YamlFileLoader
;
16 use Symfony\Component\Config\
Resource\FileResource
;
18 class YamlFileLoaderTest
extends \PHPUnit_Framework_TestCase
20 protected function setUp()
22 if (!class_exists('Symfony\Component\Config\FileLocator')) {
23 $this->markTestSkipped('The "Config" component is not available');
26 if (!class_exists('Symfony\Component\Yaml\Yaml')) {
27 $this->markTestSkipped('The "Yaml" component is not available');
31 public function testSupports()
33 $loader = new YamlFileLoader($this->getMock('Symfony\Component\Config\FileLocator'));
35 $this->assertTrue($loader->supports('foo.yml'), '->supports() returns true if the resource is loadable');
36 $this->assertFalse($loader->supports('foo.foo'), '->supports() returns true if the resource is loadable');
38 $this->assertTrue($loader->supports('foo.yml', 'yaml'), '->supports() checks the resource type if specified');
39 $this->assertFalse($loader->supports('foo.yml', 'foo'), '->supports() checks the resource type if specified');
42 public function testLoadDoesNothingIfEmpty()
44 $loader = new YamlFileLoader(new FileLocator(array(__DIR__
.'/../Fixtures')));
45 $collection = $loader->load('empty.yml');
47 $this->assertEquals(array(), $collection->all());
48 $this->assertEquals(array(new FileResource(realpath(__DIR__
.'/../Fixtures/empty.yml'))), $collection->getResources());
52 * @expectedException \InvalidArgumentException
53 * @dataProvider getPathsToInvalidFiles
55 public function testLoadThrowsExceptionWithInvalidFile($filePath)
57 $loader = new YamlFileLoader(new FileLocator(array(__DIR__
.'/../Fixtures')));
58 $loader->load($filePath);
61 public function getPathsToInvalidFiles()
63 return array(array('nonvalid.yml'), array('nonvalid2.yml'), array('incomplete.yml'), array('nonvalidkeys.yml'), array('nonesense_resource_plus_path.yml'), array('nonesense_type_without_resource.yml'));
66 public function testLoadSpecialRouteName()
68 $loader = new YamlFileLoader(new FileLocator(array(__DIR__
.'/../Fixtures')));
69 $routeCollection = $loader->load('special_route_name.yml');
70 $route = $routeCollection->get('#$péß^a|');
72 $this->assertInstanceOf('Symfony\Component\Routing\Route', $route);
73 $this->assertSame('/true', $route->getPath());
76 public function testLoadWithRoute()
78 $loader = new YamlFileLoader(new FileLocator(array(__DIR__
.'/../Fixtures')));
79 $routeCollection = $loader->load('validpattern.yml');
80 $routes = $routeCollection->all();
82 $this->assertCount(2, $routes, 'Two routes are loaded');
83 $this->assertContainsOnly('Symfony\Component\Routing\Route', $routes);
85 foreach ($routes as $route) {
86 $this->assertSame('/blog/{slug}', $route->getPath());
87 $this->assertSame('{locale}.example.com', $route->getHost());
88 $this->assertSame('MyBundle:Blog:show', $route->getDefault('_controller'));
89 $this->assertSame('\w+', $route->getRequirement('locale'));
90 $this->assertSame('RouteCompiler', $route->getOption('compiler_class'));
91 $this->assertEquals(array('GET', 'POST', 'PUT', 'OPTIONS'), $route->getMethods());
92 $this->assertEquals(array('https'), $route->getSchemes());
96 public function testLoadWithResource()
98 $loader = new YamlFileLoader(new FileLocator(array(__DIR__
.'/../Fixtures')));
99 $routeCollection = $loader->load('validresource.yml');
100 $routes = $routeCollection->all();
102 $this->assertCount(2, $routes, 'Two routes are loaded');
103 $this->assertContainsOnly('Symfony\Component\Routing\Route', $routes);
105 foreach ($routes as $route) {
106 $this->assertSame('/{foo}/blog/{slug}', $route->getPath());
107 $this->assertSame('123', $route->getDefault('foo'));
108 $this->assertSame('\d+', $route->getRequirement('foo'));
109 $this->assertSame('bar', $route->getOption('foo'));
110 $this->assertSame('', $route->getHost());