4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\DependencyInjection\Tests\Loader;
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;
33 class XmlFileLoaderTest extends TestCase
35 protected static $fixturesPath;
37 public static function setUpBeforeClass()
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';
45 public function testLoad()
47 $loader = new XmlFileLoader(new ContainerBuilder(), new FileLocator(self::$fixturesPath.'/ini'));
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');
58 public function testParseFile()
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);
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');
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');
77 $loader = new XmlFileLoader(new ContainerBuilder(), new FileLocator(self::$fixturesPath.'/xml'));
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');
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');
91 $xml = $m->invoke($loader, self::$fixturesPath.'/xml/services1.xml');
92 $this->assertInstanceOf('DOMDocument', $xml, '->parseFileToDOM() returns an SimpleXMLElement object');
95 public function testLoadWithExternalEntitiesDisabled()
97 $disableEntities = libxml_disable_entity_loader(true);
99 $containerBuilder = new ContainerBuilder();
100 $loader = new XmlFileLoader($containerBuilder, new FileLocator(self::$fixturesPath.'/xml'));
101 $loader->load('services2.xml');
103 libxml_disable_entity_loader($disableEntities);
105 $this->assertGreaterThan(0, $containerBuilder->getParameterBag()->all(), 'Parameters can be read from the config file.');
108 public function testLoadParameters()
110 $container = new ContainerBuilder();
111 $loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
112 $loader->load('services2.xml');
114 $actual = $container->getParameterBag()->all();
132 'mixedcase' => array('MixedCaseKey' => 'value'),
133 'constant' => PHP_EOL,
136 $this->assertEquals($expected, $actual, '->load() converts XML values to PHP ones');
139 public function testLoadImports()
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')),
147 $loader->setResolver($resolver);
148 $loader->load('services4.xml');
150 $actual = $container->getParameterBag()->all();
168 'mixedcase' => array('MixedCaseKey' => 'value'),
169 'constant' => PHP_EOL,
171 'imported_from_ini' => true,
172 'imported_from_yaml' => true,
173 'with_wrong_ext' => 'from yaml',
176 $this->assertEquals(array_keys($expected), array_keys($actual), '->load() imports and merges imported files');
177 $this->assertTrue($actual['imported_from_ini']);
179 // Bad import throws no exception due to ignore_errors value.
180 $loader->load('services4_bad_import.xml');
183 public function testLoadAnonymousServices()
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');
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());
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());
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());
219 $service = $container->findTaggedServiceIds('biz_tag');
220 $this->assertCount(1, $service);
222 foreach ($service as $id => $tag) {
223 $service = $container->getDefinition($id);
225 $this->assertEquals('BizClass', $service->getClass(), '->load() uses the same configuration as for the anonymous ones');
226 $this->assertTrue($service->isPublic());
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]);
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.
240 public function testLoadAnonymousServicesWithoutId()
242 $container = new ContainerBuilder();
243 $loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
244 $loader->load('services_without_id.xml');
247 public function testLoadAnonymousNestedServices()
249 $container = new ContainerBuilder();
250 $loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
251 $loader->load('nested_service_without_id.xml');
253 $this->assertTrue($container->hasDefinition('FooClass'));
254 $arguments = $container->getDefinition('FooClass')->getArguments();
255 $this->assertInstanceOf(Reference::class, array_shift($arguments));
258 public function testLoadServices()
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');
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());
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());
293 public function testParsesIteratorArgument()
295 $container = new ContainerBuilder();
296 $loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
297 $loader->load('services9.xml');
299 $lazyDefinition = $container->getDefinition('lazy_context');
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');
304 public function testParsesTags()
306 $container = new ContainerBuilder();
307 $loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
308 $loader->load('services10.xml');
310 $services = $container->findTaggedServiceIds('foo_tag');
311 $this->assertCount(1, $services);
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');
319 $this->assertEquals('ciz', $attributes['some_option'], 'no overriding should be done when normalizing');
320 $this->assertEquals('cat', $attributes['some-option']);
322 $this->assertArrayNotHasKey('an_other_option', $attributes, 'normalization should not be done when an underscore is already found');
328 * @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
330 public function testParseTagsWithoutNameThrowsException()
332 $container = new ContainerBuilder();
333 $loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
334 $loader->load('tag_without_name.xml');
338 * @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
339 * @expectedExceptionMessageRegExp /The tag name for service ".+" in .* must be a non-empty string/
341 public function testParseTagWithEmptyNameThrowsException()
343 $container = new ContainerBuilder();
344 $loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
345 $loader->load('tag_with_empty_name.xml');
348 public function testDeprecated()
350 $container = new ContainerBuilder();
351 $loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
352 $loader->load('services_deprecated.xml');
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'));
358 $this->assertTrue($container->getDefinition('bar')->isDeprecated());
359 $message = 'The "bar" service is deprecated.';
360 $this->assertSame($message, $container->getDefinition('bar')->getDeprecationMessage('bar'));
363 public function testConvertDomElementToArray()
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');
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');
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');
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');
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');
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');
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');
394 public function testExtensions()
396 $container = new ContainerBuilder();
397 $container->registerExtension(new \ProjectExtension());
398 $container->registerExtension(new \ProjectWithXsdExtension());
399 $loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
401 // extension without an XSD
402 $loader->load('extensions/services1.xml');
403 $container->compile();
404 $services = $container->getDefinitions();
405 $parameters = $container->getParameterBag()->all();
407 $this->assertArrayHasKey('project.service.bar', $services, '->load() parses extension elements');
408 $this->assertArrayHasKey('project.parameter.bar', $parameters, '->load() parses extension elements');
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');
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();
423 $this->assertArrayHasKey('project.service.bar', $services, '->load() parses extension elements');
424 $this->assertArrayHasKey('project.parameter.bar', $parameters, '->load() parses extension elements');
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');
429 $container = new ContainerBuilder();
430 $container->registerExtension(new \ProjectExtension());
431 $container->registerExtension(new \ProjectWithXsdExtension());
432 $loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
434 // extension with an XSD (does not validate)
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');
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');
447 // non-registered extension
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');
457 public function testExtensionInPhar()
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.');
462 if (\defined('HHVM_VERSION')) {
463 $this->markTestSkipped('HHVM makes this test conflict with those run in separate processes.');
466 require_once self::$fixturesPath.'/includes/ProjectWithXsdExtensionInPhar.phar';
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');
474 // extension with an XSD in PHAR archive (does not validate)
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');
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');
488 public function testSupports()
490 $loader = new XmlFileLoader(new ContainerBuilder(), new FileLocator());
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');
497 public function testNoNamingConflictsForAnonymousServices()
499 $container = new ContainerBuilder();
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');
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');
519 public function testDocTypeIsNotAllowed()
521 $container = new ContainerBuilder();
523 $loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
525 // document types are not allowed.
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');
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');
539 public function testXmlNamespaces()
541 $container = new ContainerBuilder();
542 $loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
543 $loader->load('namespaces.xml');
544 $services = $container->getDefinitions();
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');
551 public function testLoadIndexedArguments()
553 $container = new ContainerBuilder();
554 $loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
555 $loader->load('services14.xml');
557 $this->assertEquals(array('index_0' => 'app'), $container->findDefinition('logger')->getArguments());
560 public function testLoadInlinedServices()
562 $container = new ContainerBuilder();
563 $loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
564 $loader->load('services21.xml');
566 $foo = $container->getDefinition('foo');
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]);
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]);
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]);
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]);
597 public function testType()
599 $container = new ContainerBuilder();
600 $loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
601 $loader->load('services22.xml');
603 $this->assertEquals(array('Bar', 'Baz'), $container->getDefinition('foo')->getAutowiringTypes());
606 public function testAutowire()
608 $container = new ContainerBuilder();
609 $loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
610 $loader->load('services23.xml');
612 $this->assertTrue($container->getDefinition('bar')->isAutowired());
615 public function testClassFromId()
617 $container = new ContainerBuilder();
618 $loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
619 $loader->load('class_from_id.xml');
620 $container->compile();
622 $this->assertEquals(CaseSensitiveClass::class, $container->getDefinition(CaseSensitiveClass::class)->getClass());
625 public function testPrototype()
627 $container = new ContainerBuilder();
628 $loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
629 $loader->load('services_prototype.xml');
631 $ids = array_keys($container->getDefinitions());
633 $this->assertSame(array(Prototype\Foo::class, Prototype\Sub\Bar::class, 'service_container'), $ids);
635 $resources = $container->getResources();
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);
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.
651 public function testAliasDefinitionContainsUnsupportedElements()
653 $container = new ContainerBuilder();
654 $loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
656 $loader->load('legacy_invalid_alias_definition.xml');
658 $this->assertTrue($container->has('bar'));
661 public function testArgumentWithKeyOutsideCollection()
663 $container = new ContainerBuilder();
664 $loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
665 $loader->load('with_key_outside_collection.xml');
667 $this->assertSame(array('type' => 'foo', 'bar'), $container->getDefinition('foo')->getArguments());
670 public function testDefaults()
672 $container = new ContainerBuilder();
673 $loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
674 $loader->load('services28.xml');
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());
682 $container->compile();
684 $this->assertTrue($container->getDefinition('no_defaults')->isPublic());
686 $this->assertSame(array('foo' => array(array())), $container->getDefinition('no_defaults')->getTags());
688 $this->assertFalse($container->getDefinition('no_defaults')->isAutowired());
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());
694 $definitions = $container->getDefinitions();
695 $this->assertSame('service_container', key($definitions));
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());
705 public function testNamedArguments()
707 $container = new ContainerBuilder();
708 $loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
709 $loader->load('services_named_args.xml');
711 $this->assertEquals(array('$apiKey' => 'ABCD', CaseSensitiveClass::class => null), $container->getDefinition(NamedArgumentsDummy::class)->getArguments());
713 $container->compile();
715 $this->assertEquals(array(null, 'ABCD'), $container->getDefinition(NamedArgumentsDummy::class)->getArguments());
716 $this->assertEquals(array(array('setApiKey', array('123'))), $container->getDefinition(NamedArgumentsDummy::class)->getMethodCalls());
719 public function testInstanceof()
721 $container = new ContainerBuilder();
722 $loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
723 $loader->load('services_instanceof.xml');
724 $container->compile();
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());
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.
736 public function testInstanceOfAndChildDefinitionNotAllowed()
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();
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.
748 public function testAutoConfigureAndChildDefinitionNotAllowed()
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();
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.
760 public function testDefaultsAndChildDefinitionNotAllowed()
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();
768 public function testAutoConfigureInstanceof()
770 $container = new ContainerBuilder();
771 $loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
772 $loader->load('services_autoconfigure.xml');
774 $this->assertTrue($container->getDefinition('use_defaults_settings')->isAutoconfigured());
775 $this->assertFalse($container->getDefinition('override_defaults_settings_to_false')->isAutoconfigured());
778 public function testBindings()
780 $container = new ContainerBuilder();
781 $loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
782 $loader->load('services_bindings.xml');
783 $container->compile();
785 $definition = $container->getDefinition('bar');
786 $this->assertEquals(array(
787 'NonExistent' => null,
788 BarInterface::class => new Reference(Bar::class),
789 '$foo' => array(null),
791 '$factory' => 'factory',
792 ), array_map(function ($v) { return $v->getValues()[0]; }, $definition->getBindings()));
793 $this->assertEquals(array(
796 new Reference(Bar::class),
798 ), $definition->getArguments());
800 $definition = $container->getDefinition(Bar::class);
801 $this->assertEquals(array(
804 ), $definition->getArguments());
805 $this->assertEquals(array(
806 'NonExistent' => null,
808 '$factory' => 'factory',
809 ), array_map(function ($v) { return $v->getValues()[0]; }, $definition->getBindings()));
812 public function testTsantosContainer()
814 $container = new ContainerBuilder();
815 $loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
816 $loader->load('services_tsantos.xml');
817 $container->compile();
819 $dumper = new PhpDumper($container);
820 $dump = $dumper->dump();
821 $this->assertStringEqualsFile(self::$fixturesPath.'/php/services_tsantos.php', $dumper->dump());