2a9ac68e97e44302a2cb78cba8e636439bdc162a
[yaffs-website] / vendor / symfony / dependency-injection / Tests / Loader / XmlFileLoaderTest.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\Config\FileLocator;
16 use Symfony\Component\Config\Loader\LoaderResolver;
17 use Symfony\Component\Config\Resource\FileResource;
18 use Symfony\Component\Config\Resource\GlobResource;
19 use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
20 use Symfony\Component\DependencyInjection\ContainerBuilder;
21 use Symfony\Component\DependencyInjection\Dumper\PhpDumper;
22 use Symfony\Component\DependencyInjection\Loader\IniFileLoader;
23 use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
24 use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
25 use Symfony\Component\DependencyInjection\Reference;
26 use Symfony\Component\DependencyInjection\Tests\Fixtures\Bar;
27 use Symfony\Component\DependencyInjection\Tests\Fixtures\BarInterface;
28 use Symfony\Component\DependencyInjection\Tests\Fixtures\CaseSensitiveClass;
29 use Symfony\Component\DependencyInjection\Tests\Fixtures\NamedArgumentsDummy;
30 use Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype;
31 use Symfony\Component\ExpressionLanguage\Expression;
32
33 class XmlFileLoaderTest extends TestCase
34 {
35     protected static $fixturesPath;
36
37     public static function setUpBeforeClass()
38     {
39         self::$fixturesPath = realpath(__DIR__.'/../Fixtures/');
40         require_once self::$fixturesPath.'/includes/foo.php';
41         require_once self::$fixturesPath.'/includes/ProjectExtension.php';
42         require_once self::$fixturesPath.'/includes/ProjectWithXsdExtension.php';
43     }
44
45     public function testLoad()
46     {
47         $loader = new XmlFileLoader(new ContainerBuilder(), new FileLocator(self::$fixturesPath.'/ini'));
48
49         try {
50             $loader->load('foo.xml');
51             $this->fail('->load() throws an InvalidArgumentException if the loaded file does not exist');
52         } catch (\Exception $e) {
53             $this->assertInstanceOf('InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if the loaded file does not exist');
54             $this->assertStringStartsWith('The file "foo.xml" does not exist (in:', $e->getMessage(), '->load() throws an InvalidArgumentException if the loaded file does not exist');
55         }
56     }
57
58     public function testParseFile()
59     {
60         $loader = new XmlFileLoader(new ContainerBuilder(), new FileLocator(self::$fixturesPath.'/ini'));
61         $r = new \ReflectionObject($loader);
62         $m = $r->getMethod('parseFileToDOM');
63         $m->setAccessible(true);
64
65         try {
66             $m->invoke($loader, self::$fixturesPath.'/ini/parameters.ini');
67             $this->fail('->parseFileToDOM() throws an InvalidArgumentException if the loaded file is not a valid XML file');
68         } catch (\Exception $e) {
69             $this->assertInstanceOf('Symfony\\Component\\DependencyInjection\\Exception\\InvalidArgumentException', $e, '->parseFileToDOM() throws an InvalidArgumentException if the loaded file is not a valid XML file');
70             $this->assertRegExp(sprintf('#^Unable to parse file ".+%s".$#', 'parameters.ini'), $e->getMessage(), '->parseFileToDOM() throws an InvalidArgumentException if the loaded file is not a valid XML file');
71
72             $e = $e->getPrevious();
73             $this->assertInstanceOf('InvalidArgumentException', $e, '->parseFileToDOM() throws an InvalidArgumentException if the loaded file is not a valid XML file');
74             $this->assertStringStartsWith('[ERROR 4] Start tag expected, \'<\' not found (in', $e->getMessage(), '->parseFileToDOM() throws an InvalidArgumentException if the loaded file is not a valid XML file');
75         }
76
77         $loader = new XmlFileLoader(new ContainerBuilder(), new FileLocator(self::$fixturesPath.'/xml'));
78
79         try {
80             $m->invoke($loader, self::$fixturesPath.'/xml/nonvalid.xml');
81             $this->fail('->parseFileToDOM() throws an InvalidArgumentException if the loaded file does not validate the XSD');
82         } catch (\Exception $e) {
83             $this->assertInstanceOf('Symfony\\Component\\DependencyInjection\\Exception\\InvalidArgumentException', $e, '->parseFileToDOM() throws an InvalidArgumentException if the loaded file does not validate the XSD');
84             $this->assertRegExp(sprintf('#^Unable to parse file ".+%s".$#', 'nonvalid.xml'), $e->getMessage(), '->parseFileToDOM() throws an InvalidArgumentException if the loaded file is not a valid XML file');
85
86             $e = $e->getPrevious();
87             $this->assertInstanceOf('InvalidArgumentException', $e, '->parseFileToDOM() throws an InvalidArgumentException if the loaded file does not validate the XSD');
88             $this->assertStringStartsWith('[ERROR 1845] Element \'nonvalid\': No matching global declaration available for the validation root. (in', $e->getMessage(), '->parseFileToDOM() throws an InvalidArgumentException if the loaded file does not validate the XSD');
89         }
90
91         $xml = $m->invoke($loader, self::$fixturesPath.'/xml/services1.xml');
92         $this->assertInstanceOf('DOMDocument', $xml, '->parseFileToDOM() returns an SimpleXMLElement object');
93     }
94
95     public function testLoadWithExternalEntitiesDisabled()
96     {
97         $disableEntities = libxml_disable_entity_loader(true);
98
99         $containerBuilder = new ContainerBuilder();
100         $loader = new XmlFileLoader($containerBuilder, new FileLocator(self::$fixturesPath.'/xml'));
101         $loader->load('services2.xml');
102
103         libxml_disable_entity_loader($disableEntities);
104
105         $this->assertGreaterThan(0, $containerBuilder->getParameterBag()->all(), 'Parameters can be read from the config file.');
106     }
107
108     public function testLoadParameters()
109     {
110         $container = new ContainerBuilder();
111         $loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
112         $loader->load('services2.xml');
113
114         $actual = $container->getParameterBag()->all();
115         $expected = array(
116             'a string',
117             'foo' => 'bar',
118             'values' => array(
119                 0,
120                 'integer' => 4,
121                 100 => null,
122                 'true',
123                 true,
124                 false,
125                 'on',
126                 'off',
127                 'float' => 1.3,
128                 1000.3,
129                 'a string',
130                 array('foo', 'bar'),
131             ),
132             'mixedcase' => array('MixedCaseKey' => 'value'),
133             'constant' => PHP_EOL,
134         );
135
136         $this->assertEquals($expected, $actual, '->load() converts XML values to PHP ones');
137     }
138
139     public function testLoadImports()
140     {
141         $container = new ContainerBuilder();
142         $resolver = new LoaderResolver(array(
143             new IniFileLoader($container, new FileLocator(self::$fixturesPath.'/ini')),
144             new YamlFileLoader($container, new FileLocator(self::$fixturesPath.'/yml')),
145             $loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml')),
146         ));
147         $loader->setResolver($resolver);
148         $loader->load('services4.xml');
149
150         $actual = $container->getParameterBag()->all();
151         $expected = array(
152             'a string',
153             'foo' => 'bar',
154             'values' => array(
155                 0,
156                 'integer' => 4,
157                 100 => null,
158                 'true',
159                 true,
160                 false,
161                 'on',
162                 'off',
163                 'float' => 1.3,
164                 1000.3,
165                 'a string',
166                 array('foo', 'bar'),
167             ),
168             'mixedcase' => array('MixedCaseKey' => 'value'),
169             'constant' => PHP_EOL,
170             'bar' => '%foo%',
171             'imported_from_ini' => true,
172             'imported_from_yaml' => true,
173             'with_wrong_ext' => 'from yaml',
174         );
175
176         $this->assertEquals(array_keys($expected), array_keys($actual), '->load() imports and merges imported files');
177         $this->assertTrue($actual['imported_from_ini']);
178
179         // Bad import throws no exception due to ignore_errors value.
180         $loader->load('services4_bad_import.xml');
181     }
182
183     public function testLoadAnonymousServices()
184     {
185         $container = new ContainerBuilder();
186         $loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
187         $loader->load('services5.xml');
188         $services = $container->getDefinitions();
189         $this->assertCount(7, $services, '->load() attributes unique ids to anonymous services');
190
191         // anonymous service as an argument
192         $args = $services['foo']->getArguments();
193         $this->assertCount(1, $args, '->load() references anonymous services as "normal" ones');
194         $this->assertInstanceOf('Symfony\\Component\\DependencyInjection\\Reference', $args[0], '->load() converts anonymous services to references to "normal" services');
195         $this->assertArrayHasKey((string) $args[0], $services, '->load() makes a reference to the created ones');
196         $inner = $services[(string) $args[0]];
197         $this->assertEquals('BarClass', $inner->getClass(), '->load() uses the same configuration as for the anonymous ones');
198         $this->assertFalse($inner->isPublic());
199
200         // inner anonymous services
201         $args = $inner->getArguments();
202         $this->assertCount(1, $args, '->load() references anonymous services as "normal" ones');
203         $this->assertInstanceOf('Symfony\\Component\\DependencyInjection\\Reference', $args[0], '->load() converts anonymous services to references to "normal" services');
204         $this->assertArrayHasKey((string) $args[0], $services, '->load() makes a reference to the created ones');
205         $inner = $services[(string) $args[0]];
206         $this->assertEquals('BazClass', $inner->getClass(), '->load() uses the same configuration as for the anonymous ones');
207         $this->assertFalse($inner->isPublic());
208
209         // anonymous service as a property
210         $properties = $services['foo']->getProperties();
211         $property = $properties['p'];
212         $this->assertInstanceOf('Symfony\\Component\\DependencyInjection\\Reference', $property, '->load() converts anonymous services to references to "normal" services');
213         $this->assertArrayHasKey((string) $property, $services, '->load() makes a reference to the created ones');
214         $inner = $services[(string) $property];
215         $this->assertEquals('BuzClass', $inner->getClass(), '->load() uses the same configuration as for the anonymous ones');
216         $this->assertFalse($inner->isPublic());
217
218         // "wild" service
219         $service = $container->findTaggedServiceIds('biz_tag');
220         $this->assertCount(1, $service);
221
222         foreach ($service as $id => $tag) {
223             $service = $container->getDefinition($id);
224         }
225         $this->assertEquals('BizClass', $service->getClass(), '->load() uses the same configuration as for the anonymous ones');
226         $this->assertTrue($service->isPublic());
227
228         // anonymous services are shared when using decoration definitions
229         $container->compile();
230         $services = $container->getDefinitions();
231         $fooArgs = $services['foo']->getArguments();
232         $barArgs = $services['bar']->getArguments();
233         $this->assertSame($fooArgs[0], $barArgs[0]);
234     }
235
236     /**
237      * @group legacy
238      * @expectedDeprecation Top-level anonymous services are deprecated since Symfony 3.4, the "id" attribute will be required in version 4.0 in %sservices_without_id.xml at line 5.
239      */
240     public function testLoadAnonymousServicesWithoutId()
241     {
242         $container = new ContainerBuilder();
243         $loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
244         $loader->load('services_without_id.xml');
245     }
246
247     public function testLoadAnonymousNestedServices()
248     {
249         $container = new ContainerBuilder();
250         $loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
251         $loader->load('nested_service_without_id.xml');
252
253         $this->assertTrue($container->hasDefinition('FooClass'));
254         $arguments = $container->getDefinition('FooClass')->getArguments();
255         $this->assertInstanceOf(Reference::class, array_shift($arguments));
256     }
257
258     public function testLoadServices()
259     {
260         $container = new ContainerBuilder();
261         $loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
262         $loader->load('services6.xml');
263         $services = $container->getDefinitions();
264         $this->assertArrayHasKey('foo', $services, '->load() parses <service> elements');
265         $this->assertFalse($services['not_shared']->isShared(), '->load() parses shared flag');
266         $this->assertInstanceOf('Symfony\\Component\\DependencyInjection\\Definition', $services['foo'], '->load() converts <service> element to Definition instances');
267         $this->assertEquals('FooClass', $services['foo']->getClass(), '->load() parses the class attribute');
268         $this->assertEquals('%path%/foo.php', $services['file']->getFile(), '->load() parses the file tag');
269         $this->assertEquals(array('foo', new Reference('foo'), array(true, false)), $services['arguments']->getArguments(), '->load() parses the argument tags');
270         $this->assertEquals('sc_configure', $services['configurator1']->getConfigurator(), '->load() parses the configurator tag');
271         $this->assertEquals(array(new Reference('baz'), 'configure'), $services['configurator2']->getConfigurator(), '->load() parses the configurator tag');
272         $this->assertEquals(array('BazClass', 'configureStatic'), $services['configurator3']->getConfigurator(), '->load() parses the configurator tag');
273         $this->assertEquals(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');
274         $this->assertEquals(array(array('setBar', array('foo', new Reference('foo'), array(true, false)))), $services['method_call2']->getMethodCalls(), '->load() parses the method_call tag');
275         $this->assertEquals('factory', $services['new_factory1']->getFactory(), '->load() parses the factory tag');
276         $this->assertEquals(array(new Reference('baz'), 'getClass'), $services['new_factory2']->getFactory(), '->load() parses the factory tag');
277         $this->assertEquals(array('BazClass', 'getInstance'), $services['new_factory3']->getFactory(), '->load() parses the factory tag');
278         $this->assertSame(array(null, 'getInstance'), $services['new_factory4']->getFactory(), '->load() accepts factory tag without class');
279
280         $aliases = $container->getAliases();
281         $this->assertArrayHasKey('alias_for_foo', $aliases, '->load() parses <service> elements');
282         $this->assertEquals('foo', (string) $aliases['alias_for_foo'], '->load() parses aliases');
283         $this->assertTrue($aliases['alias_for_foo']->isPublic());
284         $this->assertArrayHasKey('another_alias_for_foo', $aliases);
285         $this->assertEquals('foo', (string) $aliases['another_alias_for_foo']);
286         $this->assertFalse($aliases['another_alias_for_foo']->isPublic());
287
288         $this->assertEquals(array('decorated', null, 0), $services['decorator_service']->getDecoratedService());
289         $this->assertEquals(array('decorated', 'decorated.pif-pouf', 0), $services['decorator_service_with_name']->getDecoratedService());
290         $this->assertEquals(array('decorated', 'decorated.pif-pouf', 5), $services['decorator_service_with_name_and_priority']->getDecoratedService());
291     }
292
293     public function testParsesIteratorArgument()
294     {
295         $container = new ContainerBuilder();
296         $loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
297         $loader->load('services9.xml');
298
299         $lazyDefinition = $container->getDefinition('lazy_context');
300
301         $this->assertEquals(array(new IteratorArgument(array('k1' => new Reference('foo.baz'), 'k2' => new Reference('service_container'))), new IteratorArgument(array())), $lazyDefinition->getArguments(), '->load() parses lazy arguments');
302     }
303
304     public function testParsesTags()
305     {
306         $container = new ContainerBuilder();
307         $loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
308         $loader->load('services10.xml');
309
310         $services = $container->findTaggedServiceIds('foo_tag');
311         $this->assertCount(1, $services);
312
313         foreach ($services as $id => $tagAttributes) {
314             foreach ($tagAttributes as $attributes) {
315                 $this->assertArrayHasKey('other_option', $attributes);
316                 $this->assertEquals('lorem', $attributes['other_option']);
317                 $this->assertArrayHasKey('other-option', $attributes, 'unnormalized tag attributes should not be removed');
318
319                 $this->assertEquals('ciz', $attributes['some_option'], 'no overriding should be done when normalizing');
320                 $this->assertEquals('cat', $attributes['some-option']);
321
322                 $this->assertArrayNotHasKey('an_other_option', $attributes, 'normalization should not be done when an underscore is already found');
323             }
324         }
325     }
326
327     /**
328      * @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
329      */
330     public function testParseTagsWithoutNameThrowsException()
331     {
332         $container = new ContainerBuilder();
333         $loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
334         $loader->load('tag_without_name.xml');
335     }
336
337     /**
338      * @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
339      * @expectedExceptionMessageRegExp /The tag name for service ".+" in .* must be a non-empty string/
340      */
341     public function testParseTagWithEmptyNameThrowsException()
342     {
343         $container = new ContainerBuilder();
344         $loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
345         $loader->load('tag_with_empty_name.xml');
346     }
347
348     public function testDeprecated()
349     {
350         $container = new ContainerBuilder();
351         $loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
352         $loader->load('services_deprecated.xml');
353
354         $this->assertTrue($container->getDefinition('foo')->isDeprecated());
355         $message = 'The "foo" service is deprecated. You should stop using it, as it will soon be removed.';
356         $this->assertSame($message, $container->getDefinition('foo')->getDeprecationMessage('foo'));
357
358         $this->assertTrue($container->getDefinition('bar')->isDeprecated());
359         $message = 'The "bar" service is deprecated.';
360         $this->assertSame($message, $container->getDefinition('bar')->getDeprecationMessage('bar'));
361     }
362
363     public function testConvertDomElementToArray()
364     {
365         $doc = new \DOMDocument('1.0');
366         $doc->loadXML('<foo>bar</foo>');
367         $this->assertEquals('bar', XmlFileLoader::convertDomElementToArray($doc->documentElement), '::convertDomElementToArray() converts a \DomElement to an array');
368
369         $doc = new \DOMDocument('1.0');
370         $doc->loadXML('<foo foo="bar" />');
371         $this->assertEquals(array('foo' => 'bar'), XmlFileLoader::convertDomElementToArray($doc->documentElement), '::convertDomElementToArray() converts a \DomElement to an array');
372
373         $doc = new \DOMDocument('1.0');
374         $doc->loadXML('<foo><foo>bar</foo></foo>');
375         $this->assertEquals(array('foo' => 'bar'), XmlFileLoader::convertDomElementToArray($doc->documentElement), '::convertDomElementToArray() converts a \DomElement to an array');
376
377         $doc = new \DOMDocument('1.0');
378         $doc->loadXML('<foo><foo>bar<foo>bar</foo></foo></foo>');
379         $this->assertEquals(array('foo' => array('value' => 'bar', 'foo' => 'bar')), XmlFileLoader::convertDomElementToArray($doc->documentElement), '::convertDomElementToArray() converts a \DomElement to an array');
380
381         $doc = new \DOMDocument('1.0');
382         $doc->loadXML('<foo><foo></foo></foo>');
383         $this->assertEquals(array('foo' => null), XmlFileLoader::convertDomElementToArray($doc->documentElement), '::convertDomElementToArray() converts a \DomElement to an array');
384
385         $doc = new \DOMDocument('1.0');
386         $doc->loadXML('<foo><foo><!-- foo --></foo></foo>');
387         $this->assertEquals(array('foo' => null), XmlFileLoader::convertDomElementToArray($doc->documentElement), '::convertDomElementToArray() converts a \DomElement to an array');
388
389         $doc = new \DOMDocument('1.0');
390         $doc->loadXML('<foo><foo foo="bar"/><foo foo="bar"/></foo>');
391         $this->assertEquals(array('foo' => array(array('foo' => 'bar'), array('foo' => 'bar'))), XmlFileLoader::convertDomElementToArray($doc->documentElement), '::convertDomElementToArray() converts a \DomElement to an array');
392     }
393
394     public function testExtensions()
395     {
396         $container = new ContainerBuilder();
397         $container->registerExtension(new \ProjectExtension());
398         $container->registerExtension(new \ProjectWithXsdExtension());
399         $loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
400
401         // extension without an XSD
402         $loader->load('extensions/services1.xml');
403         $container->compile();
404         $services = $container->getDefinitions();
405         $parameters = $container->getParameterBag()->all();
406
407         $this->assertArrayHasKey('project.service.bar', $services, '->load() parses extension elements');
408         $this->assertArrayHasKey('project.parameter.bar', $parameters, '->load() parses extension elements');
409
410         $this->assertEquals('BAR', $services['project.service.foo']->getClass(), '->load() parses extension elements');
411         $this->assertEquals('BAR', $parameters['project.parameter.foo'], '->load() parses extension elements');
412
413         // extension with an XSD
414         $container = new ContainerBuilder();
415         $container->registerExtension(new \ProjectExtension());
416         $container->registerExtension(new \ProjectWithXsdExtension());
417         $loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
418         $loader->load('extensions/services2.xml');
419         $container->compile();
420         $services = $container->getDefinitions();
421         $parameters = $container->getParameterBag()->all();
422
423         $this->assertArrayHasKey('project.service.bar', $services, '->load() parses extension elements');
424         $this->assertArrayHasKey('project.parameter.bar', $parameters, '->load() parses extension elements');
425
426         $this->assertEquals('BAR', $services['project.service.foo']->getClass(), '->load() parses extension elements');
427         $this->assertEquals('BAR', $parameters['project.parameter.foo'], '->load() parses extension elements');
428
429         $container = new ContainerBuilder();
430         $container->registerExtension(new \ProjectExtension());
431         $container->registerExtension(new \ProjectWithXsdExtension());
432         $loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
433
434         // extension with an XSD (does not validate)
435         try {
436             $loader->load('extensions/services3.xml');
437             $this->fail('->load() throws an InvalidArgumentException if the configuration does not validate the XSD');
438         } catch (\Exception $e) {
439             $this->assertInstanceOf('Symfony\\Component\\DependencyInjection\\Exception\\InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if the configuration does not validate the XSD');
440             $this->assertRegExp(sprintf('#^Unable to parse file ".+%s".$#', 'services3.xml'), $e->getMessage(), '->load() throws an InvalidArgumentException if the configuration does not validate the XSD');
441
442             $e = $e->getPrevious();
443             $this->assertInstanceOf('InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if the configuration does not validate the XSD');
444             $this->assertContains('The attribute \'bar\' is not allowed', $e->getMessage(), '->load() throws an InvalidArgumentException if the configuration does not validate the XSD');
445         }
446
447         // non-registered extension
448         try {
449             $loader->load('extensions/services4.xml');
450             $this->fail('->load() throws an InvalidArgumentException if the tag is not valid');
451         } catch (\Exception $e) {
452             $this->assertInstanceOf('\InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if the tag is not valid');
453             $this->assertStringStartsWith('There is no extension able to load the configuration for "project:bar" (in', $e->getMessage(), '->load() throws an InvalidArgumentException if the tag is not valid');
454         }
455     }
456
457     public function testExtensionInPhar()
458     {
459         if (\extension_loaded('suhosin') && false === strpos(ini_get('suhosin.executor.include.whitelist'), 'phar')) {
460             $this->markTestSkipped('To run this test, add "phar" to the "suhosin.executor.include.whitelist" settings in your php.ini file.');
461         }
462         if (\defined('HHVM_VERSION')) {
463             $this->markTestSkipped('HHVM makes this test conflict with those run in separate processes.');
464         }
465
466         require_once self::$fixturesPath.'/includes/ProjectWithXsdExtensionInPhar.phar';
467
468         // extension with an XSD in PHAR archive
469         $container = new ContainerBuilder();
470         $container->registerExtension(new \ProjectWithXsdExtensionInPhar());
471         $loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
472         $loader->load('extensions/services6.xml');
473
474         // extension with an XSD in PHAR archive (does not validate)
475         try {
476             $loader->load('extensions/services7.xml');
477             $this->fail('->load() throws an InvalidArgumentException if the configuration does not validate the XSD');
478         } catch (\Exception $e) {
479             $this->assertInstanceOf('Symfony\\Component\\DependencyInjection\\Exception\\InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if the configuration does not validate the XSD');
480             $this->assertRegExp(sprintf('#^Unable to parse file ".+%s".$#', 'services7.xml'), $e->getMessage(), '->load() throws an InvalidArgumentException if the configuration does not validate the XSD');
481
482             $e = $e->getPrevious();
483             $this->assertInstanceOf('InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if the configuration does not validate the XSD');
484             $this->assertContains('The attribute \'bar\' is not allowed', $e->getMessage(), '->load() throws an InvalidArgumentException if the configuration does not validate the XSD');
485         }
486     }
487
488     public function testSupports()
489     {
490         $loader = new XmlFileLoader(new ContainerBuilder(), new FileLocator());
491
492         $this->assertTrue($loader->supports('foo.xml'), '->supports() returns true if the resource is loadable');
493         $this->assertFalse($loader->supports('foo.foo'), '->supports() returns false if the resource is not loadable');
494         $this->assertTrue($loader->supports('with_wrong_ext.yml', 'xml'), '->supports() returns true if the resource with forced type is loadable');
495     }
496
497     public function testNoNamingConflictsForAnonymousServices()
498     {
499         $container = new ContainerBuilder();
500
501         $loader1 = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml/extension1'));
502         $loader1->load('services.xml');
503         $services = $container->getDefinitions();
504         $this->assertCount(3, $services, '->load() attributes unique ids to anonymous services');
505         $loader2 = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml/extension2'));
506         $loader2->load('services.xml');
507         $services = $container->getDefinitions();
508         $this->assertCount(5, $services, '->load() attributes unique ids to anonymous services');
509
510         $services = $container->getDefinitions();
511         $args1 = $services['extension1.foo']->getArguments();
512         $inner1 = $services[(string) $args1[0]];
513         $this->assertEquals('BarClass1', $inner1->getClass(), '->load() uses the same configuration as for the anonymous ones');
514         $args2 = $services['extension2.foo']->getArguments();
515         $inner2 = $services[(string) $args2[0]];
516         $this->assertEquals('BarClass2', $inner2->getClass(), '->load() uses the same configuration as for the anonymous ones');
517     }
518
519     public function testDocTypeIsNotAllowed()
520     {
521         $container = new ContainerBuilder();
522
523         $loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
524
525         // document types are not allowed.
526         try {
527             $loader->load('withdoctype.xml');
528             $this->fail('->load() throws an InvalidArgumentException if the configuration contains a document type');
529         } catch (\Exception $e) {
530             $this->assertInstanceOf('Symfony\\Component\\DependencyInjection\\Exception\\InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if the configuration contains a document type');
531             $this->assertRegExp(sprintf('#^Unable to parse file ".+%s".$#', 'withdoctype.xml'), $e->getMessage(), '->load() throws an InvalidArgumentException if the configuration contains a document type');
532
533             $e = $e->getPrevious();
534             $this->assertInstanceOf('InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if the configuration contains a document type');
535             $this->assertSame('Document types are not allowed.', $e->getMessage(), '->load() throws an InvalidArgumentException if the configuration contains a document type');
536         }
537     }
538
539     public function testXmlNamespaces()
540     {
541         $container = new ContainerBuilder();
542         $loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
543         $loader->load('namespaces.xml');
544         $services = $container->getDefinitions();
545
546         $this->assertArrayHasKey('foo', $services, '->load() parses <srv:service> elements');
547         $this->assertCount(1, $services['foo']->getTag('foo.tag'), '->load parses <srv:tag> elements');
548         $this->assertEquals(array(array('setBar', array('foo'))), $services['foo']->getMethodCalls(), '->load() parses the <srv:call> tag');
549     }
550
551     public function testLoadIndexedArguments()
552     {
553         $container = new ContainerBuilder();
554         $loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
555         $loader->load('services14.xml');
556
557         $this->assertEquals(array('index_0' => 'app'), $container->findDefinition('logger')->getArguments());
558     }
559
560     public function testLoadInlinedServices()
561     {
562         $container = new ContainerBuilder();
563         $loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
564         $loader->load('services21.xml');
565
566         $foo = $container->getDefinition('foo');
567
568         $fooFactory = $foo->getFactory();
569         $this->assertInstanceOf(Reference::class, $fooFactory[0]);
570         $this->assertTrue($container->has((string) $fooFactory[0]));
571         $fooFactoryDefinition = $container->getDefinition((string) $fooFactory[0]);
572         $this->assertSame('FooFactory', $fooFactoryDefinition->getClass());
573         $this->assertSame('createFoo', $fooFactory[1]);
574
575         $fooFactoryFactory = $fooFactoryDefinition->getFactory();
576         $this->assertInstanceOf(Reference::class, $fooFactoryFactory[0]);
577         $this->assertTrue($container->has((string) $fooFactoryFactory[0]));
578         $this->assertSame('Foobar', $container->getDefinition((string) $fooFactoryFactory[0])->getClass());
579         $this->assertSame('createFooFactory', $fooFactoryFactory[1]);
580
581         $fooConfigurator = $foo->getConfigurator();
582         $this->assertInstanceOf(Reference::class, $fooConfigurator[0]);
583         $this->assertTrue($container->has((string) $fooConfigurator[0]));
584         $fooConfiguratorDefinition = $container->getDefinition((string) $fooConfigurator[0]);
585         $this->assertSame('Bar', $fooConfiguratorDefinition->getClass());
586         $this->assertSame('configureFoo', $fooConfigurator[1]);
587
588         $barConfigurator = $fooConfiguratorDefinition->getConfigurator();
589         $this->assertInstanceOf(Reference::class, $barConfigurator[0]);
590         $this->assertSame('Baz', $container->getDefinition((string) $barConfigurator[0])->getClass());
591         $this->assertSame('configureBar', $barConfigurator[1]);
592     }
593
594     /**
595      * @group legacy
596      */
597     public function testType()
598     {
599         $container = new ContainerBuilder();
600         $loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
601         $loader->load('services22.xml');
602
603         $this->assertEquals(array('Bar', 'Baz'), $container->getDefinition('foo')->getAutowiringTypes());
604     }
605
606     public function testAutowire()
607     {
608         $container = new ContainerBuilder();
609         $loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
610         $loader->load('services23.xml');
611
612         $this->assertTrue($container->getDefinition('bar')->isAutowired());
613     }
614
615     public function testClassFromId()
616     {
617         $container = new ContainerBuilder();
618         $loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
619         $loader->load('class_from_id.xml');
620         $container->compile();
621
622         $this->assertEquals(CaseSensitiveClass::class, $container->getDefinition(CaseSensitiveClass::class)->getClass());
623     }
624
625     public function testPrototype()
626     {
627         $container = new ContainerBuilder();
628         $loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
629         $loader->load('services_prototype.xml');
630
631         $ids = array_keys($container->getDefinitions());
632         sort($ids);
633         $this->assertSame(array(Prototype\Foo::class, Prototype\Sub\Bar::class, 'service_container'), $ids);
634
635         $resources = $container->getResources();
636
637         $fixturesDir = \dirname(__DIR__).\DIRECTORY_SEPARATOR.'Fixtures'.\DIRECTORY_SEPARATOR;
638         $resources = array_map('strval', $resources);
639         $this->assertContains((string) (new FileResource($fixturesDir.'xml'.\DIRECTORY_SEPARATOR.'services_prototype.xml')), $resources);
640         $this->assertContains((string) (new GlobResource($fixturesDir.'Prototype', '/*', true)), $resources);
641         $this->assertContains('reflection.Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\Foo', $resources);
642         $this->assertContains('reflection.Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\Sub\Bar', $resources);
643     }
644
645     /**
646      * @group legacy
647      * @expectedDeprecation Using the attribute "class" is deprecated for the service "bar" which is defined as an alias %s.
648      * @expectedDeprecation Using the element "tag" is deprecated for the service "bar" which is defined as an alias %s.
649      * @expectedDeprecation Using the element "factory" is deprecated for the service "bar" which is defined as an alias %s.
650      */
651     public function testAliasDefinitionContainsUnsupportedElements()
652     {
653         $container = new ContainerBuilder();
654         $loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
655
656         $loader->load('legacy_invalid_alias_definition.xml');
657
658         $this->assertTrue($container->has('bar'));
659     }
660
661     public function testArgumentWithKeyOutsideCollection()
662     {
663         $container = new ContainerBuilder();
664         $loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
665         $loader->load('with_key_outside_collection.xml');
666
667         $this->assertSame(array('type' => 'foo', 'bar'), $container->getDefinition('foo')->getArguments());
668     }
669
670     public function testDefaults()
671     {
672         $container = new ContainerBuilder();
673         $loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
674         $loader->load('services28.xml');
675
676         $this->assertFalse($container->getDefinition('with_defaults')->isPublic());
677         $this->assertSame(array('foo' => array(array())), $container->getDefinition('with_defaults')->getTags());
678         $this->assertTrue($container->getDefinition('with_defaults')->isAutowired());
679         $this->assertArrayNotHasKey('public', $container->getDefinition('with_defaults')->getChanges());
680         $this->assertArrayNotHasKey('autowire', $container->getDefinition('with_defaults')->getChanges());
681
682         $container->compile();
683
684         $this->assertTrue($container->getDefinition('no_defaults')->isPublic());
685
686         $this->assertSame(array('foo' => array(array())), $container->getDefinition('no_defaults')->getTags());
687
688         $this->assertFalse($container->getDefinition('no_defaults')->isAutowired());
689
690         $this->assertTrue($container->getDefinition('child_def')->isPublic());
691         $this->assertSame(array('foo' => array(array())), $container->getDefinition('child_def')->getTags());
692         $this->assertFalse($container->getDefinition('child_def')->isAutowired());
693
694         $definitions = $container->getDefinitions();
695         $this->assertSame('service_container', key($definitions));
696
697         array_shift($definitions);
698         $anonymous = current($definitions);
699         $this->assertSame('bar', key($definitions));
700         $this->assertTrue($anonymous->isPublic());
701         $this->assertTrue($anonymous->isAutowired());
702         $this->assertSame(array('foo' => array(array())), $anonymous->getTags());
703     }
704
705     public function testNamedArguments()
706     {
707         $container = new ContainerBuilder();
708         $loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
709         $loader->load('services_named_args.xml');
710
711         $this->assertEquals(array('$apiKey' => 'ABCD', CaseSensitiveClass::class => null), $container->getDefinition(NamedArgumentsDummy::class)->getArguments());
712
713         $container->compile();
714
715         $this->assertEquals(array(null, 'ABCD'), $container->getDefinition(NamedArgumentsDummy::class)->getArguments());
716         $this->assertEquals(array(array('setApiKey', array('123'))), $container->getDefinition(NamedArgumentsDummy::class)->getMethodCalls());
717     }
718
719     public function testInstanceof()
720     {
721         $container = new ContainerBuilder();
722         $loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
723         $loader->load('services_instanceof.xml');
724         $container->compile();
725
726         $definition = $container->getDefinition(Bar::class);
727         $this->assertTrue($definition->isAutowired());
728         $this->assertTrue($definition->isLazy());
729         $this->assertSame(array('foo' => array(array()), 'bar' => array(array())), $definition->getTags());
730     }
731
732     /**
733      * @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
734      * @expectedExceptionMessage The service "child_service" cannot use the "parent" option in the same file where "instanceof" configuration is defined as using both is not supported. Move your child definitions to a separate file.
735      */
736     public function testInstanceOfAndChildDefinitionNotAllowed()
737     {
738         $container = new ContainerBuilder();
739         $loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
740         $loader->load('services_instanceof_with_parent.xml');
741         $container->compile();
742     }
743
744     /**
745      * @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
746      * @expectedExceptionMessage The service "child_service" cannot have a "parent" and also have "autoconfigure". Try setting autoconfigure="false" for the service.
747      */
748     public function testAutoConfigureAndChildDefinitionNotAllowed()
749     {
750         $container = new ContainerBuilder();
751         $loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
752         $loader->load('services_autoconfigure_with_parent.xml');
753         $container->compile();
754     }
755
756     /**
757      * @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
758      * @expectedExceptionMessage Attribute "autowire" on service "child_service" cannot be inherited from "defaults" when a "parent" is set. Move your child definitions to a separate file or define this attribute explicitly.
759      */
760     public function testDefaultsAndChildDefinitionNotAllowed()
761     {
762         $container = new ContainerBuilder();
763         $loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
764         $loader->load('services_defaults_with_parent.xml');
765         $container->compile();
766     }
767
768     public function testAutoConfigureInstanceof()
769     {
770         $container = new ContainerBuilder();
771         $loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
772         $loader->load('services_autoconfigure.xml');
773
774         $this->assertTrue($container->getDefinition('use_defaults_settings')->isAutoconfigured());
775         $this->assertFalse($container->getDefinition('override_defaults_settings_to_false')->isAutoconfigured());
776     }
777
778     public function testBindings()
779     {
780         $container = new ContainerBuilder();
781         $loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
782         $loader->load('services_bindings.xml');
783         $container->compile();
784
785         $definition = $container->getDefinition('bar');
786         $this->assertEquals(array(
787             'NonExistent' => null,
788             BarInterface::class => new Reference(Bar::class),
789             '$foo' => array(null),
790             '$quz' => 'quz',
791             '$factory' => 'factory',
792         ), array_map(function ($v) { return $v->getValues()[0]; }, $definition->getBindings()));
793         $this->assertEquals(array(
794             'quz',
795             null,
796             new Reference(Bar::class),
797             array(null),
798         ), $definition->getArguments());
799
800         $definition = $container->getDefinition(Bar::class);
801         $this->assertEquals(array(
802             null,
803             'factory',
804         ), $definition->getArguments());
805         $this->assertEquals(array(
806             'NonExistent' => null,
807             '$quz' => 'quz',
808             '$factory' => 'factory',
809         ), array_map(function ($v) { return $v->getValues()[0]; }, $definition->getBindings()));
810     }
811
812     public function testTsantosContainer()
813     {
814         $container = new ContainerBuilder();
815         $loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
816         $loader->load('services_tsantos.xml');
817         $container->compile();
818
819         $dumper = new PhpDumper($container);
820         $dump = $dumper->dump();
821         $this->assertStringEqualsFile(self::$fixturesPath.'/php/services_tsantos.php', $dumper->dump());
822     }
823 }