236bd4da0188458b4af98d9f863cdbec5762eec5
[yaffs-website] / vendor / symfony / dependency-injection / 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\DependencyInjection\Tests\Loader;
13
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\DependencyInjection\ContainerBuilder;
16 use Symfony\Component\DependencyInjection\Reference;
17 use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
18 use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
19 use Symfony\Component\DependencyInjection\Loader\IniFileLoader;
20 use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
21 use Symfony\Component\Config\Loader\LoaderResolver;
22 use Symfony\Component\Config\FileLocator;
23 use Symfony\Component\ExpressionLanguage\Expression;
24
25 class YamlFileLoaderTest extends TestCase
26 {
27     protected static $fixturesPath;
28
29     public static function setUpBeforeClass()
30     {
31         self::$fixturesPath = realpath(__DIR__.'/../Fixtures/');
32         require_once self::$fixturesPath.'/includes/foo.php';
33         require_once self::$fixturesPath.'/includes/ProjectExtension.php';
34     }
35
36     /**
37      * @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
38      * @expectedExceptionMessageRegExp /The file ".+" does not exist./
39      */
40     public function testLoadUnExistFile()
41     {
42         $loader = new YamlFileLoader(new ContainerBuilder(), new FileLocator(self::$fixturesPath.'/ini'));
43         $r = new \ReflectionObject($loader);
44         $m = $r->getMethod('loadFile');
45         $m->setAccessible(true);
46
47         $m->invoke($loader, 'foo.yml');
48     }
49
50     /**
51      * @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
52      * @expectedExceptionMessageRegExp /The file ".+" does not contain valid YAML./
53      */
54     public function testLoadInvalidYamlFile()
55     {
56         $path = self::$fixturesPath.'/ini';
57         $loader = new YamlFileLoader(new ContainerBuilder(), new FileLocator($path));
58         $r = new \ReflectionObject($loader);
59         $m = $r->getMethod('loadFile');
60         $m->setAccessible(true);
61
62         $m->invoke($loader, $path.'/parameters.ini');
63     }
64
65     /**
66      * @dataProvider provideInvalidFiles
67      * @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
68      */
69     public function testLoadInvalidFile($file)
70     {
71         $loader = new YamlFileLoader(new ContainerBuilder(), new FileLocator(self::$fixturesPath.'/yaml'));
72
73         $loader->load($file.'.yml');
74     }
75
76     public function provideInvalidFiles()
77     {
78         return array(
79             array('bad_parameters'),
80             array('bad_imports'),
81             array('bad_import'),
82             array('bad_services'),
83             array('bad_service'),
84             array('bad_calls'),
85             array('bad_format'),
86             array('nonvalid1'),
87             array('nonvalid2'),
88         );
89     }
90
91     public function testLoadParameters()
92     {
93         $container = new ContainerBuilder();
94         $loader = new YamlFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml'));
95         $loader->load('services2.yml');
96         $this->assertEquals(array('foo' => 'bar', 'mixedcase' => array('MixedCaseKey' => 'value'), 'values' => array(true, false, 0, 1000.3), 'bar' => 'foo', 'escape' => '@escapeme', 'foo_bar' => new Reference('foo_bar')), $container->getParameterBag()->all(), '->load() converts YAML keys to lowercase');
97     }
98
99     public function testLoadImports()
100     {
101         $container = new ContainerBuilder();
102         $resolver = new LoaderResolver(array(
103             new IniFileLoader($container, new FileLocator(self::$fixturesPath.'/ini')),
104             new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml')),
105             new PhpFileLoader($container, new FileLocator(self::$fixturesPath.'/php')),
106             $loader = new YamlFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml')),
107         ));
108         $loader->setResolver($resolver);
109         $loader->load('services4.yml');
110
111         $actual = $container->getParameterBag()->all();
112         $expected = array('foo' => 'bar', 'values' => array(true, false), 'bar' => '%foo%', 'escape' => '@escapeme', 'foo_bar' => new Reference('foo_bar'), 'mixedcase' => array('MixedCaseKey' => 'value'), 'imported_from_ini' => true, 'imported_from_xml' => true);
113         $this->assertEquals(array_keys($expected), array_keys($actual), '->load() imports and merges imported files');
114
115         // Bad import throws no exception due to ignore_errors value.
116         $loader->load('services4_bad_import.yml');
117     }
118
119     /**
120      * @group legacy
121      */
122     public function testLegacyLoadServices()
123     {
124         $container = new ContainerBuilder();
125         $loader = new YamlFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml'));
126         $loader->load('legacy-services6.yml');
127         $services = $container->getDefinitions();
128         $this->assertEquals('FooClass', $services['constructor']->getClass());
129         $this->assertEquals('getInstance', $services['constructor']->getFactoryMethod());
130         $this->assertEquals('BazClass', $services['factory_service']->getClass());
131         $this->assertEquals('baz_factory', $services['factory_service']->getFactoryService());
132         $this->assertEquals('getInstance', $services['factory_service']->getFactoryMethod());
133         $this->assertEquals('container', $services['scope.container']->getScope());
134         $this->assertEquals('custom', $services['scope.custom']->getScope());
135         $this->assertEquals('prototype', $services['scope.prototype']->getScope());
136         $this->assertTrue($services['request']->isSynthetic(), '->load() parses the synthetic flag');
137         $this->assertTrue($services['request']->isSynchronized(), '->load() parses the synchronized flag');
138         $this->assertTrue($services['request']->isLazy(), '->load() parses the lazy flag');
139         $this->assertNull($services['request']->getDecoratedService());
140     }
141
142     public function testLoadServices()
143     {
144         $container = new ContainerBuilder();
145         $loader = new YamlFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml'));
146         $loader->load('services6.yml');
147         $services = $container->getDefinitions();
148         $this->assertTrue(isset($services['foo']), '->load() parses service elements');
149         $this->assertFalse($services['not_shared']->isShared(), '->load() parses the shared flag');
150         $this->assertInstanceOf('Symfony\\Component\\DependencyInjection\\Definition', $services['foo'], '->load() converts service element to Definition instances');
151         $this->assertEquals('FooClass', $services['foo']->getClass(), '->load() parses the class attribute');
152         $this->assertEquals('%path%/foo.php', $services['file']->getFile(), '->load() parses the file tag');
153         $this->assertEquals(array('foo', new Reference('foo'), array(true, false)), $services['arguments']->getArguments(), '->load() parses the argument tags');
154         $this->assertEquals('sc_configure', $services['configurator1']->getConfigurator(), '->load() parses the configurator tag');
155         $this->assertEquals(array(new Reference('baz'), 'configure'), $services['configurator2']->getConfigurator(), '->load() parses the configurator tag');
156         $this->assertEquals(array('BazClass', 'configureStatic'), $services['configurator3']->getConfigurator(), '->load() parses the configurator tag');
157         $this->assertEquals(array(array('setBar', array()), array('setBar', array()), array('setBar', array(new Expression('service("foo").foo() ~ (container.hasParameter("foo") ? parameter("foo") : "default")')))), $services['method_call1']->getMethodCalls(), '->load() parses the method_call tag');
158         $this->assertEquals(array(array('setBar', array('foo', new Reference('foo'), array(true, false)))), $services['method_call2']->getMethodCalls(), '->load() parses the method_call tag');
159         $this->assertEquals('factory', $services['new_factory1']->getFactory(), '->load() parses the factory tag');
160         $this->assertEquals(array(new Reference('baz'), 'getClass'), $services['new_factory2']->getFactory(), '->load() parses the factory tag');
161         $this->assertEquals(array('BazClass', 'getInstance'), $services['new_factory3']->getFactory(), '->load() parses the factory tag');
162
163         $aliases = $container->getAliases();
164         $this->assertTrue(isset($aliases['alias_for_foo']), '->load() parses aliases');
165         $this->assertEquals('foo', (string) $aliases['alias_for_foo'], '->load() parses aliases');
166         $this->assertTrue($aliases['alias_for_foo']->isPublic());
167         $this->assertTrue(isset($aliases['another_alias_for_foo']));
168         $this->assertEquals('foo', (string) $aliases['another_alias_for_foo']);
169         $this->assertFalse($aliases['another_alias_for_foo']->isPublic());
170
171         $this->assertEquals(array('decorated', null, 0), $services['decorator_service']->getDecoratedService());
172         $this->assertEquals(array('decorated', 'decorated.pif-pouf', 0), $services['decorator_service_with_name']->getDecoratedService());
173         $this->assertEquals(array('decorated', 'decorated.pif-pouf', 5), $services['decorator_service_with_name_and_priority']->getDecoratedService());
174     }
175
176     public function testLoadFactoryShortSyntax()
177     {
178         $container = new ContainerBuilder();
179         $loader = new YamlFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml'));
180         $loader->load('services14.yml');
181         $services = $container->getDefinitions();
182
183         $this->assertEquals(array(new Reference('baz'), 'getClass'), $services['factory']->getFactory(), '->load() parses the factory tag with service:method');
184         $this->assertEquals(array('FooBacFactory', 'createFooBar'), $services['factory_with_static_call']->getFactory(), '->load() parses the factory tag with Class::method');
185     }
186
187     public function testExtensions()
188     {
189         $container = new ContainerBuilder();
190         $container->registerExtension(new \ProjectExtension());
191         $loader = new YamlFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml'));
192         $loader->load('services10.yml');
193         $container->compile();
194         $services = $container->getDefinitions();
195         $parameters = $container->getParameterBag()->all();
196
197         $this->assertTrue(isset($services['project.service.bar']), '->load() parses extension elements');
198         $this->assertTrue(isset($parameters['project.parameter.bar']), '->load() parses extension elements');
199
200         $this->assertEquals('BAR', $services['project.service.foo']->getClass(), '->load() parses extension elements');
201         $this->assertEquals('BAR', $parameters['project.parameter.foo'], '->load() parses extension elements');
202
203         try {
204             $loader->load('services11.yml');
205             $this->fail('->load() throws an InvalidArgumentException if the tag is not valid');
206         } catch (\Exception $e) {
207             $this->assertInstanceOf('\InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if the tag is not valid');
208             $this->assertStringStartsWith('There is no extension able to load the configuration for "foobarfoobar" (in', $e->getMessage(), '->load() throws an InvalidArgumentException if the tag is not valid');
209         }
210     }
211
212     public function testSupports()
213     {
214         $loader = new YamlFileLoader(new ContainerBuilder(), new FileLocator());
215
216         $this->assertTrue($loader->supports('foo.yml'), '->supports() returns true if the resource is loadable');
217         $this->assertTrue($loader->supports('foo.yaml'), '->supports() returns true if the resource is loadable');
218         $this->assertFalse($loader->supports('foo.foo'), '->supports() returns true if the resource is loadable');
219     }
220
221     public function testNonArrayTagsThrowsException()
222     {
223         $loader = new YamlFileLoader(new ContainerBuilder(), new FileLocator(self::$fixturesPath.'/yaml'));
224         try {
225             $loader->load('badtag1.yml');
226             $this->fail('->load() should throw an exception when the tags key of a service is not an array');
227         } catch (\Exception $e) {
228             $this->assertInstanceOf('Symfony\Component\DependencyInjection\Exception\InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if the tags key is not an array');
229             $this->assertStringStartsWith('Parameter "tags" must be an array for service', $e->getMessage(), '->load() throws an InvalidArgumentException if the tags key is not an array');
230         }
231     }
232
233     /**
234      * @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
235      * @expectedExceptionMessage A "tags" entry must be an array for service
236      */
237     public function testNonArrayTagThrowsException()
238     {
239         $loader = new YamlFileLoader(new ContainerBuilder(), new FileLocator(self::$fixturesPath.'/yaml'));
240         $loader->load('badtag4.yml');
241     }
242
243     public function testTagWithoutNameThrowsException()
244     {
245         $loader = new YamlFileLoader(new ContainerBuilder(), new FileLocator(self::$fixturesPath.'/yaml'));
246         try {
247             $loader->load('badtag2.yml');
248             $this->fail('->load() should throw an exception when a tag is missing the name key');
249         } catch (\Exception $e) {
250             $this->assertInstanceOf('Symfony\Component\DependencyInjection\Exception\InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if a tag is missing the name key');
251             $this->assertStringStartsWith('A "tags" entry is missing a "name" key for service ', $e->getMessage(), '->load() throws an InvalidArgumentException if a tag is missing the name key');
252         }
253     }
254
255     public function testTagWithAttributeArrayThrowsException()
256     {
257         $loader = new YamlFileLoader(new ContainerBuilder(), new FileLocator(self::$fixturesPath.'/yaml'));
258         try {
259             $loader->load('badtag3.yml');
260             $this->fail('->load() should throw an exception when a tag-attribute is not a scalar');
261         } catch (\Exception $e) {
262             $this->assertInstanceOf('Symfony\Component\DependencyInjection\Exception\InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if a tag-attribute is not a scalar');
263             $this->assertStringStartsWith('A "tags" attribute must be of a scalar-type for service "foo_service", tag "foo", attribute "bar"', $e->getMessage(), '->load() throws an InvalidArgumentException if a tag-attribute is not a scalar');
264         }
265     }
266
267     public function testLoadYamlOnlyWithKeys()
268     {
269         $container = new ContainerBuilder();
270         $loader = new YamlFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml'));
271         $loader->load('services21.yml');
272
273         $definition = $container->getDefinition('manager');
274         $this->assertEquals(array(array('setLogger', array(new Reference('logger'))), array('setClass', array('User'))), $definition->getMethodCalls());
275         $this->assertEquals(array(true), $definition->getArguments());
276         $this->assertEquals(array('manager' => array(array('alias' => 'user'))), $definition->getTags());
277     }
278
279     /**
280      * @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
281      * @expectedExceptionMessageRegExp /The tag name for service ".+" in .+ must be a non-empty string/
282      */
283     public function testTagWithEmptyNameThrowsException()
284     {
285         $loader = new YamlFileLoader(new ContainerBuilder(), new FileLocator(self::$fixturesPath.'/yaml'));
286         $loader->load('tag_name_empty_string.yml');
287     }
288
289     /**
290      * @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
291      * @expectedExceptionMessageREgExp /The tag name for service "\.+" must be a non-empty string/
292      */
293     public function testTagWithNonStringNameThrowsException()
294     {
295         $loader = new YamlFileLoader(new ContainerBuilder(), new FileLocator(self::$fixturesPath.'/yaml'));
296         $loader->load('tag_name_no_string.yml');
297     }
298
299     /**
300      * @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
301      */
302     public function testTypesNotArray()
303     {
304         $loader = new YamlFileLoader(new ContainerBuilder(), new FileLocator(self::$fixturesPath.'/yaml'));
305         $loader->load('bad_types1.yml');
306     }
307
308     /**
309      * @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
310      */
311     public function testTypeNotString()
312     {
313         $loader = new YamlFileLoader(new ContainerBuilder(), new FileLocator(self::$fixturesPath.'/yaml'));
314         $loader->load('bad_types2.yml');
315     }
316
317     public function testTypes()
318     {
319         $container = new ContainerBuilder();
320         $loader = new YamlFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml'));
321         $loader->load('services22.yml');
322
323         $this->assertEquals(array('Foo', 'Bar'), $container->getDefinition('foo_service')->getAutowiringTypes());
324         $this->assertEquals(array('Foo'), $container->getDefinition('baz_service')->getAutowiringTypes());
325     }
326
327     public function testAutowire()
328     {
329         $container = new ContainerBuilder();
330         $loader = new YamlFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml'));
331         $loader->load('services23.yml');
332
333         $this->assertTrue($container->getDefinition('bar_service')->isAutowired());
334     }
335
336     /**
337      * @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
338      * @expectedExceptionMessage The value of the "decorates" option for the "bar" service must be the id of the service without the "@" prefix (replace "@foo" with "foo").
339      */
340     public function testDecoratedServicesWithWrongSyntaxThrowsException()
341     {
342         $loader = new YamlFileLoader(new ContainerBuilder(), new FileLocator(self::$fixturesPath.'/yaml'));
343         $loader->load('bad_decorates.yml');
344     }
345 }