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 PHPUnit\Framework\TestCase;
15 use Symfony\Component\Config\FileLocator;
16 use Symfony\Component\Routing\Loader\YamlFileLoader;
17 use Symfony\Component\Config\Resource\FileResource;
19 class YamlFileLoaderTest extends TestCase
21 public function testSupports()
23 $loader = new YamlFileLoader($this->getMockBuilder('Symfony\Component\Config\FileLocator')->getMock());
25 $this->assertTrue($loader->supports('foo.yml'), '->supports() returns true if the resource is loadable');
26 $this->assertTrue($loader->supports('foo.yaml'), '->supports() returns true if the resource is loadable');
27 $this->assertFalse($loader->supports('foo.foo'), '->supports() returns true if the resource is loadable');
29 $this->assertTrue($loader->supports('foo.yml', 'yaml'), '->supports() checks the resource type if specified');
30 $this->assertTrue($loader->supports('foo.yaml', 'yaml'), '->supports() checks the resource type if specified');
31 $this->assertFalse($loader->supports('foo.yml', 'foo'), '->supports() checks the resource type if specified');
34 public function testLoadDoesNothingIfEmpty()
36 $loader = new YamlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
37 $collection = $loader->load('empty.yml');
39 $this->assertEquals(array(), $collection->all());
40 $this->assertEquals(array(new FileResource(realpath(__DIR__.'/../Fixtures/empty.yml'))), $collection->getResources());
44 * @expectedException \InvalidArgumentException
45 * @dataProvider getPathsToInvalidFiles
47 public function testLoadThrowsExceptionWithInvalidFile($filePath)
49 $loader = new YamlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
50 $loader->load($filePath);
53 public function getPathsToInvalidFiles()
56 array('nonvalid.yml'),
57 array('nonvalid2.yml'),
58 array('incomplete.yml'),
59 array('nonvalidkeys.yml'),
60 array('nonesense_resource_plus_path.yml'),
61 array('nonesense_type_without_resource.yml'),
62 array('bad_format.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 $route = $routeCollection->get('blog_show');
82 $this->assertInstanceOf('Symfony\Component\Routing\Route', $route);
83 $this->assertSame('/blog/{slug}', $route->getPath());
84 $this->assertSame('{locale}.example.com', $route->getHost());
85 $this->assertSame('MyBundle:Blog:show', $route->getDefault('_controller'));
86 $this->assertSame('\w+', $route->getRequirement('locale'));
87 $this->assertSame('RouteCompiler', $route->getOption('compiler_class'));
88 $this->assertEquals(array('GET', 'POST', 'PUT', 'OPTIONS'), $route->getMethods());
89 $this->assertEquals(array('https'), $route->getSchemes());
90 $this->assertEquals('context.getMethod() == "GET"', $route->getCondition());
96 public function testLegacyRouteDefinitionLoading()
98 $loader = new YamlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
99 $routeCollection = $loader->load('legacy_validpattern.yml');
100 $route = $routeCollection->get('blog_show_legacy');
102 $this->assertInstanceOf('Symfony\Component\Routing\Route', $route);
103 $this->assertSame('/blog/{slug}', $route->getPath());
104 $this->assertSame('{locale}.example.com', $route->getHost());
105 $this->assertSame('MyBundle:Blog:show', $route->getDefault('_controller'));
106 $this->assertSame('\w+', $route->getRequirement('locale'));
107 $this->assertSame('RouteCompiler', $route->getOption('compiler_class'));
108 $this->assertEquals(array('GET', 'POST', 'PUT', 'OPTIONS'), $route->getMethods());
109 $this->assertEquals(array('https'), $route->getSchemes());
110 $this->assertEquals('context.getMethod() == "GET"', $route->getCondition());
113 public function testLoadWithResource()
115 $loader = new YamlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
116 $routeCollection = $loader->load('validresource.yml');
117 $routes = $routeCollection->all();
119 $this->assertCount(2, $routes, 'Two routes are loaded');
120 $this->assertContainsOnly('Symfony\Component\Routing\Route', $routes);
122 foreach ($routes as $route) {
123 $this->assertSame('/{foo}/blog/{slug}', $route->getPath());
124 $this->assertSame('123', $route->getDefault('foo'));
125 $this->assertSame('\d+', $route->getRequirement('foo'));
126 $this->assertSame('bar', $route->getOption('foo'));
127 $this->assertSame('', $route->getHost());
128 $this->assertSame('context.getMethod() == "POST"', $route->getCondition());