Security update for Core, with self-updated composer
[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, PHP_INT_MAX), '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, PHP_INT_MAX), '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         $this->assertTrue($actual['imported_from_ini']);
115
116         // Bad import throws no exception due to ignore_errors value.
117         $loader->load('services4_bad_import.yml');
118     }
119
120     public function testLoadServices()
121     {
122         $container = new ContainerBuilder();
123         $loader = new YamlFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml'));
124         $loader->load('services6.yml');
125         $services = $container->getDefinitions();
126         $this->assertTrue(isset($services['foo']), '->load() parses service elements');
127         $this->assertFalse($services['not_shared']->isShared(), '->load() parses the shared flag');
128         $this->assertInstanceOf('Symfony\\Component\\DependencyInjection\\Definition', $services['foo'], '->load() converts service element to Definition instances');
129         $this->assertEquals('FooClass', $services['foo']->getClass(), '->load() parses the class attribute');
130         $this->assertEquals('%path%/foo.php', $services['file']->getFile(), '->load() parses the file tag');
131         $this->assertEquals(array('foo', new Reference('foo'), array(true, false)), $services['arguments']->getArguments(), '->load() parses the argument tags');
132         $this->assertEquals('sc_configure', $services['configurator1']->getConfigurator(), '->load() parses the configurator tag');
133         $this->assertEquals(array(new Reference('baz'), 'configure'), $services['configurator2']->getConfigurator(), '->load() parses the configurator tag');
134         $this->assertEquals(array('BazClass', 'configureStatic'), $services['configurator3']->getConfigurator(), '->load() parses the configurator tag');
135         $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');
136         $this->assertEquals(array(array('setBar', array('foo', new Reference('foo'), array(true, false)))), $services['method_call2']->getMethodCalls(), '->load() parses the method_call tag');
137         $this->assertEquals('factory', $services['new_factory1']->getFactory(), '->load() parses the factory tag');
138         $this->assertEquals(array(new Reference('baz'), 'getClass'), $services['new_factory2']->getFactory(), '->load() parses the factory tag');
139         $this->assertEquals(array('BazClass', 'getInstance'), $services['new_factory3']->getFactory(), '->load() parses the factory tag');
140
141         $aliases = $container->getAliases();
142         $this->assertTrue(isset($aliases['alias_for_foo']), '->load() parses aliases');
143         $this->assertEquals('foo', (string) $aliases['alias_for_foo'], '->load() parses aliases');
144         $this->assertTrue($aliases['alias_for_foo']->isPublic());
145         $this->assertTrue(isset($aliases['another_alias_for_foo']));
146         $this->assertEquals('foo', (string) $aliases['another_alias_for_foo']);
147         $this->assertFalse($aliases['another_alias_for_foo']->isPublic());
148
149         $this->assertEquals(array('decorated', null, 0), $services['decorator_service']->getDecoratedService());
150         $this->assertEquals(array('decorated', 'decorated.pif-pouf', 0), $services['decorator_service_with_name']->getDecoratedService());
151         $this->assertEquals(array('decorated', 'decorated.pif-pouf', 5), $services['decorator_service_with_name_and_priority']->getDecoratedService());
152     }
153
154     public function testLoadFactoryShortSyntax()
155     {
156         $container = new ContainerBuilder();
157         $loader = new YamlFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml'));
158         $loader->load('services14.yml');
159         $services = $container->getDefinitions();
160
161         $this->assertEquals(array(new Reference('baz'), 'getClass'), $services['factory']->getFactory(), '->load() parses the factory tag with service:method');
162         $this->assertEquals(array('FooBacFactory', 'createFooBar'), $services['factory_with_static_call']->getFactory(), '->load() parses the factory tag with Class::method');
163     }
164
165     public function testLoadConfiguratorShortSyntax()
166     {
167         $container = new ContainerBuilder();
168         $loader = new YamlFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml'));
169         $loader->load('services_configurator_short_syntax.yml');
170         $services = $container->getDefinitions();
171
172         $this->assertEquals(array(new Reference('foo_bar_configurator'), 'configure'), $services['foo_bar']->getConfigurator(), '->load() parses the configurator tag with service:method');
173         $this->assertEquals(array('FooBarConfigurator', 'configureFooBar'), $services['foo_bar_with_static_call']->getConfigurator(), '->load() parses the configurator tag with Class::method');
174     }
175
176     public function testExtensions()
177     {
178         $container = new ContainerBuilder();
179         $container->registerExtension(new \ProjectExtension());
180         $loader = new YamlFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml'));
181         $loader->load('services10.yml');
182         $container->compile();
183         $services = $container->getDefinitions();
184         $parameters = $container->getParameterBag()->all();
185
186         $this->assertTrue(isset($services['project.service.bar']), '->load() parses extension elements');
187         $this->assertTrue(isset($parameters['project.parameter.bar']), '->load() parses extension elements');
188
189         $this->assertEquals('BAR', $services['project.service.foo']->getClass(), '->load() parses extension elements');
190         $this->assertEquals('BAR', $parameters['project.parameter.foo'], '->load() parses extension elements');
191
192         try {
193             $loader->load('services11.yml');
194             $this->fail('->load() throws an InvalidArgumentException if the tag is not valid');
195         } catch (\Exception $e) {
196             $this->assertInstanceOf('\InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if the tag is not valid');
197             $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');
198         }
199     }
200
201     public function testSupports()
202     {
203         $loader = new YamlFileLoader(new ContainerBuilder(), new FileLocator());
204
205         $this->assertTrue($loader->supports('foo.yml'), '->supports() returns true if the resource is loadable');
206         $this->assertTrue($loader->supports('foo.yaml'), '->supports() returns true if the resource is loadable');
207         $this->assertFalse($loader->supports('foo.foo'), '->supports() returns true if the resource is loadable');
208     }
209
210     public function testNonArrayTagsThrowsException()
211     {
212         $loader = new YamlFileLoader(new ContainerBuilder(), new FileLocator(self::$fixturesPath.'/yaml'));
213         try {
214             $loader->load('badtag1.yml');
215             $this->fail('->load() should throw an exception when the tags key of a service is not an array');
216         } catch (\Exception $e) {
217             $this->assertInstanceOf('Symfony\Component\DependencyInjection\Exception\InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if the tags key is not an array');
218             $this->assertStringStartsWith('Parameter "tags" must be an array for service', $e->getMessage(), '->load() throws an InvalidArgumentException if the tags key is not an array');
219         }
220     }
221
222     /**
223      * @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
224      * @expectedExceptionMessage A "tags" entry must be an array for service
225      */
226     public function testNonArrayTagThrowsException()
227     {
228         $loader = new YamlFileLoader(new ContainerBuilder(), new FileLocator(self::$fixturesPath.'/yaml'));
229         $loader->load('badtag4.yml');
230     }
231
232     public function testTagWithoutNameThrowsException()
233     {
234         $loader = new YamlFileLoader(new ContainerBuilder(), new FileLocator(self::$fixturesPath.'/yaml'));
235         try {
236             $loader->load('badtag2.yml');
237             $this->fail('->load() should throw an exception when a tag is missing the name key');
238         } catch (\Exception $e) {
239             $this->assertInstanceOf('Symfony\Component\DependencyInjection\Exception\InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if a tag is missing the name key');
240             $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');
241         }
242     }
243
244     public function testTagWithAttributeArrayThrowsException()
245     {
246         $loader = new YamlFileLoader(new ContainerBuilder(), new FileLocator(self::$fixturesPath.'/yaml'));
247         try {
248             $loader->load('badtag3.yml');
249             $this->fail('->load() should throw an exception when a tag-attribute is not a scalar');
250         } catch (\Exception $e) {
251             $this->assertInstanceOf('Symfony\Component\DependencyInjection\Exception\InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if a tag-attribute is not a scalar');
252             $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');
253         }
254     }
255
256     public function testLoadYamlOnlyWithKeys()
257     {
258         $container = new ContainerBuilder();
259         $loader = new YamlFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml'));
260         $loader->load('services21.yml');
261
262         $definition = $container->getDefinition('manager');
263         $this->assertEquals(array(array('setLogger', array(new Reference('logger'))), array('setClass', array('User'))), $definition->getMethodCalls());
264         $this->assertEquals(array(true), $definition->getArguments());
265         $this->assertEquals(array('manager' => array(array('alias' => 'user'))), $definition->getTags());
266     }
267
268     /**
269      * @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
270      * @expectedExceptionMessageRegExp /The tag name for service ".+" in .+ must be a non-empty string/
271      */
272     public function testTagWithEmptyNameThrowsException()
273     {
274         $loader = new YamlFileLoader(new ContainerBuilder(), new FileLocator(self::$fixturesPath.'/yaml'));
275         $loader->load('tag_name_empty_string.yml');
276     }
277
278     /**
279      * @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
280      * @expectedExceptionMessageREgExp /The tag name for service "\.+" must be a non-empty string/
281      */
282     public function testTagWithNonStringNameThrowsException()
283     {
284         $loader = new YamlFileLoader(new ContainerBuilder(), new FileLocator(self::$fixturesPath.'/yaml'));
285         $loader->load('tag_name_no_string.yml');
286     }
287
288     /**
289      * @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
290      */
291     public function testTypesNotArray()
292     {
293         $loader = new YamlFileLoader(new ContainerBuilder(), new FileLocator(self::$fixturesPath.'/yaml'));
294         $loader->load('bad_types1.yml');
295     }
296
297     /**
298      * @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
299      */
300     public function testTypeNotString()
301     {
302         $loader = new YamlFileLoader(new ContainerBuilder(), new FileLocator(self::$fixturesPath.'/yaml'));
303         $loader->load('bad_types2.yml');
304     }
305
306     public function testTypes()
307     {
308         $container = new ContainerBuilder();
309         $loader = new YamlFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml'));
310         $loader->load('services22.yml');
311
312         $this->assertEquals(array('Foo', 'Bar'), $container->getDefinition('foo_service')->getAutowiringTypes());
313         $this->assertEquals(array('Foo'), $container->getDefinition('baz_service')->getAutowiringTypes());
314     }
315
316     public function testAutowire()
317     {
318         $container = new ContainerBuilder();
319         $loader = new YamlFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml'));
320         $loader->load('services23.yml');
321
322         $this->assertTrue($container->getDefinition('bar_service')->isAutowired());
323     }
324
325     /**
326      * @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
327      * @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").
328      */
329     public function testDecoratedServicesWithWrongSyntaxThrowsException()
330     {
331         $loader = new YamlFileLoader(new ContainerBuilder(), new FileLocator(self::$fixturesPath.'/yaml'));
332         $loader->load('bad_decorates.yml');
333     }
334 }