a89c58443f7ddc15b2d93c10b38a2619022a8ae4
[yaffs-website] / routing / Tests / Loader / YamlFileLoaderTest.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\Routing\Tests\Loader;
13
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;
18
19 class YamlFileLoaderTest extends TestCase
20 {
21     public function testSupports()
22     {
23         $loader = new YamlFileLoader($this->getMockBuilder('Symfony\Component\Config\FileLocator')->getMock());
24
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');
28
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');
32     }
33
34     public function testLoadDoesNothingIfEmpty()
35     {
36         $loader = new YamlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
37         $collection = $loader->load('empty.yml');
38
39         $this->assertEquals(array(), $collection->all());
40         $this->assertEquals(array(new FileResource(realpath(__DIR__.'/../Fixtures/empty.yml'))), $collection->getResources());
41     }
42
43     /**
44      * @expectedException \InvalidArgumentException
45      * @dataProvider getPathsToInvalidFiles
46      */
47     public function testLoadThrowsExceptionWithInvalidFile($filePath)
48     {
49         $loader = new YamlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
50         $loader->load($filePath);
51     }
52
53     public function getPathsToInvalidFiles()
54     {
55         return array(
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'),
63         );
64     }
65
66     public function testLoadSpecialRouteName()
67     {
68         $loader = new YamlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
69         $routeCollection = $loader->load('special_route_name.yml');
70         $route = $routeCollection->get('#$péß^a|');
71
72         $this->assertInstanceOf('Symfony\Component\Routing\Route', $route);
73         $this->assertSame('/true', $route->getPath());
74     }
75
76     public function testLoadWithRoute()
77     {
78         $loader = new YamlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
79         $routeCollection = $loader->load('validpattern.yml');
80         $route = $routeCollection->get('blog_show');
81
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());
91     }
92
93     public function testLoadWithResource()
94     {
95         $loader = new YamlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
96         $routeCollection = $loader->load('validresource.yml');
97         $routes = $routeCollection->all();
98
99         $this->assertCount(2, $routes, 'Two routes are loaded');
100         $this->assertContainsOnly('Symfony\Component\Routing\Route', $routes);
101
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());
109         }
110     }
111
112     public function testLoadRouteWithControllerAttribute()
113     {
114         $loader = new YamlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures/controller')));
115         $routeCollection = $loader->load('routing.yml');
116
117         $route = $routeCollection->get('app_homepage');
118
119         $this->assertSame('AppBundle:Homepage:show', $route->getDefault('_controller'));
120     }
121
122     public function testLoadRouteWithoutControllerAttribute()
123     {
124         $loader = new YamlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures/controller')));
125         $routeCollection = $loader->load('routing.yml');
126
127         $route = $routeCollection->get('app_logout');
128
129         $this->assertNull($route->getDefault('_controller'));
130     }
131
132     public function testLoadRouteWithControllerSetInDefaults()
133     {
134         $loader = new YamlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures/controller')));
135         $routeCollection = $loader->load('routing.yml');
136
137         $route = $routeCollection->get('app_blog');
138
139         $this->assertSame('AppBundle:Blog:list', $route->getDefault('_controller'));
140     }
141
142     /**
143      * @expectedException \InvalidArgumentException
144      * @expectedExceptionMessageRegExp /The routing file "[^"]*" must not specify both the "controller" key and the defaults key "_controller" for "app_blog"/
145      */
146     public function testOverrideControllerInDefaults()
147     {
148         $loader = new YamlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures/controller')));
149         $loader->load('override_defaults.yml');
150     }
151
152     /**
153      * @dataProvider provideFilesImportingRoutesWithControllers
154      */
155     public function testImportRouteWithController($file)
156     {
157         $loader = new YamlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures/controller')));
158         $routeCollection = $loader->load($file);
159
160         $route = $routeCollection->get('app_homepage');
161         $this->assertSame('FrameworkBundle:Template:template', $route->getDefault('_controller'));
162
163         $route = $routeCollection->get('app_blog');
164         $this->assertSame('FrameworkBundle:Template:template', $route->getDefault('_controller'));
165
166         $route = $routeCollection->get('app_logout');
167         $this->assertSame('FrameworkBundle:Template:template', $route->getDefault('_controller'));
168     }
169
170     public function provideFilesImportingRoutesWithControllers()
171     {
172         yield array('import_controller.yml');
173         yield array('import__controller.yml');
174     }
175
176     /**
177      * @expectedException \InvalidArgumentException
178      * @expectedExceptionMessageRegExp /The routing file "[^"]*" must not specify both the "controller" key and the defaults key "_controller" for "_static"/
179      */
180     public function testImportWithOverriddenController()
181     {
182         $loader = new YamlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures/controller')));
183         $loader->load('import_override_defaults.yml');
184     }
185
186     public function testImportRouteWithGlobMatchingSingleFile()
187     {
188         $loader = new YamlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures/glob')));
189         $routeCollection = $loader->load('import_single.yml');
190
191         $route = $routeCollection->get('bar_route');
192         $this->assertSame('AppBundle:Bar:view', $route->getDefault('_controller'));
193     }
194
195     public function testImportRouteWithGlobMatchingMultipleFiles()
196     {
197         $loader = new YamlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures/glob')));
198         $routeCollection = $loader->load('import_multiple.yml');
199
200         $route = $routeCollection->get('bar_route');
201         $this->assertSame('AppBundle:Bar:view', $route->getDefault('_controller'));
202
203         $route = $routeCollection->get('baz_route');
204         $this->assertSame('AppBundle:Baz:view', $route->getDefault('_controller'));
205     }
206 }