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\Config\Resource\FileResource;
17 use Symfony\Component\Routing\Loader\YamlFileLoader;
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());
93 public function testLoadWithResource()
95 $loader = new YamlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
96 $routeCollection = $loader->load('validresource.yml');
97 $routes = $routeCollection->all();
99 $this->assertCount(2, $routes, 'Two routes are loaded');
100 $this->assertContainsOnly('Symfony\Component\Routing\Route', $routes);
102 foreach ($routes as $route) {
103 $this->assertSame('/{foo}/blog/{slug}', $route->getPath());
104 $this->assertSame('123', $route->getDefault('foo'));
105 $this->assertSame('\d+', $route->getRequirement('foo'));
106 $this->assertSame('bar', $route->getOption('foo'));
107 $this->assertSame('', $route->getHost());
108 $this->assertSame('context.getMethod() == "POST"', $route->getCondition());
112 public function testLoadRouteWithControllerAttribute()
114 $loader = new YamlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures/controller')));
115 $routeCollection = $loader->load('routing.yml');
117 $route = $routeCollection->get('app_homepage');
119 $this->assertSame('AppBundle:Homepage:show', $route->getDefault('_controller'));
122 public function testLoadRouteWithoutControllerAttribute()
124 $loader = new YamlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures/controller')));
125 $routeCollection = $loader->load('routing.yml');
127 $route = $routeCollection->get('app_logout');
129 $this->assertNull($route->getDefault('_controller'));
132 public function testLoadRouteWithControllerSetInDefaults()
134 $loader = new YamlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures/controller')));
135 $routeCollection = $loader->load('routing.yml');
137 $route = $routeCollection->get('app_blog');
139 $this->assertSame('AppBundle:Blog:list', $route->getDefault('_controller'));
143 * @expectedException \InvalidArgumentException
144 * @expectedExceptionMessageRegExp /The routing file "[^"]*" must not specify both the "controller" key and the defaults key "_controller" for "app_blog"/
146 public function testOverrideControllerInDefaults()
148 $loader = new YamlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures/controller')));
149 $loader->load('override_defaults.yml');
153 * @dataProvider provideFilesImportingRoutesWithControllers
155 public function testImportRouteWithController($file)
157 $loader = new YamlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures/controller')));
158 $routeCollection = $loader->load($file);
160 $route = $routeCollection->get('app_homepage');
161 $this->assertSame('FrameworkBundle:Template:template', $route->getDefault('_controller'));
163 $route = $routeCollection->get('app_blog');
164 $this->assertSame('FrameworkBundle:Template:template', $route->getDefault('_controller'));
166 $route = $routeCollection->get('app_logout');
167 $this->assertSame('FrameworkBundle:Template:template', $route->getDefault('_controller'));
170 public function provideFilesImportingRoutesWithControllers()
172 yield array('import_controller.yml');
173 yield array('import__controller.yml');
177 * @expectedException \InvalidArgumentException
178 * @expectedExceptionMessageRegExp /The routing file "[^"]*" must not specify both the "controller" key and the defaults key "_controller" for "_static"/
180 public function testImportWithOverriddenController()
182 $loader = new YamlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures/controller')));
183 $loader->load('import_override_defaults.yml');
186 public function testImportRouteWithGlobMatchingSingleFile()
188 $loader = new YamlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures/glob')));
189 $routeCollection = $loader->load('import_single.yml');
191 $route = $routeCollection->get('bar_route');
192 $this->assertSame('AppBundle:Bar:view', $route->getDefault('_controller'));
195 public function testImportRouteWithGlobMatchingMultipleFiles()
197 $loader = new YamlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures/glob')));
198 $routeCollection = $loader->load('import_multiple.yml');
200 $route = $routeCollection->get('bar_route');
201 $this->assertSame('AppBundle:Bar:view', $route->getDefault('_controller'));
203 $route = $routeCollection->get('baz_route');
204 $this->assertSame('AppBundle:Baz:view', $route->getDefault('_controller'));