namespace Symfony\Component\DependencyInjection\Tests\Loader;
use PHPUnit\Framework\TestCase;
+use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\DependencyInjection\Loader\IniFileLoader;
use Symfony\Component\Config\Loader\LoaderResolver;
use Symfony\Component\Config\FileLocator;
+use Symfony\Component\Config\Resource\FileResource;
+use Symfony\Component\Config\Resource\GlobResource;
+use Symfony\Component\DependencyInjection\Tests\Fixtures\Bar;
+use Symfony\Component\DependencyInjection\Tests\Fixtures\BarInterface;
+use Symfony\Component\DependencyInjection\Tests\Fixtures\CaseSensitiveClass;
+use Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype;
+use Symfony\Component\DependencyInjection\Tests\Fixtures\NamedArgumentsDummy;
use Symfony\Component\ExpressionLanguage\Expression;
class XmlFileLoaderTest extends TestCase
libxml_disable_entity_loader($disableEntities);
- $this->assertTrue(count($containerBuilder->getParameterBag()->all()) > 0, 'Parameters can be read from the config file.');
+ $this->assertGreaterThan(0, $containerBuilder->getParameterBag()->all(), 'Parameters can be read from the config file.');
}
public function testLoadParameters()
'bar' => '%foo%',
'imported_from_ini' => true,
'imported_from_yaml' => true,
+ 'with_wrong_ext' => 'from yaml',
);
$this->assertEquals(array_keys($expected), array_keys($actual), '->load() imports and merges imported files');
$loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
$loader->load('services5.xml');
$services = $container->getDefinitions();
- $this->assertCount(6, $services, '->load() attributes unique ids to anonymous services');
+ $this->assertCount(7, $services, '->load() attributes unique ids to anonymous services');
// anonymous service as an argument
$args = $services['foo']->getArguments();
$this->assertCount(1, $args, '->load() references anonymous services as "normal" ones');
$this->assertInstanceOf('Symfony\\Component\\DependencyInjection\\Reference', $args[0], '->load() converts anonymous services to references to "normal" services');
- $this->assertTrue(isset($services[(string) $args[0]]), '->load() makes a reference to the created ones');
+ $this->assertArrayHasKey((string) $args[0], $services, '->load() makes a reference to the created ones');
$inner = $services[(string) $args[0]];
$this->assertEquals('BarClass', $inner->getClass(), '->load() uses the same configuration as for the anonymous ones');
$this->assertFalse($inner->isPublic());
$args = $inner->getArguments();
$this->assertCount(1, $args, '->load() references anonymous services as "normal" ones');
$this->assertInstanceOf('Symfony\\Component\\DependencyInjection\\Reference', $args[0], '->load() converts anonymous services to references to "normal" services');
- $this->assertTrue(isset($services[(string) $args[0]]), '->load() makes a reference to the created ones');
+ $this->assertArrayHasKey((string) $args[0], $services, '->load() makes a reference to the created ones');
$inner = $services[(string) $args[0]];
$this->assertEquals('BazClass', $inner->getClass(), '->load() uses the same configuration as for the anonymous ones');
$this->assertFalse($inner->isPublic());
$properties = $services['foo']->getProperties();
$property = $properties['p'];
$this->assertInstanceOf('Symfony\\Component\\DependencyInjection\\Reference', $property, '->load() converts anonymous services to references to "normal" services');
- $this->assertTrue(isset($services[(string) $property]), '->load() makes a reference to the created ones');
+ $this->assertArrayHasKey((string) $property, $services, '->load() makes a reference to the created ones');
$inner = $services[(string) $property];
$this->assertEquals('BuzClass', $inner->getClass(), '->load() uses the same configuration as for the anonymous ones');
$this->assertFalse($inner->isPublic());
$this->assertSame($fooArgs[0], $barArgs[0]);
}
+ /**
+ * @group legacy
+ * @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.
+ */
+ public function testLoadAnonymousServicesWithoutId()
+ {
+ $container = new ContainerBuilder();
+ $loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
+ $loader->load('services_without_id.xml');
+ }
+
+ public function testLoadAnonymousNestedServices()
+ {
+ $container = new ContainerBuilder();
+ $loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
+ $loader->load('nested_service_without_id.xml');
+
+ $this->assertTrue($container->hasDefinition('FooClass'));
+ $arguments = $container->getDefinition('FooClass')->getArguments();
+ $this->assertInstanceOf(Reference::class, array_shift($arguments));
+ }
+
public function testLoadServices()
{
$container = new ContainerBuilder();
$loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
$loader->load('services6.xml');
$services = $container->getDefinitions();
- $this->assertTrue(isset($services['foo']), '->load() parses <service> elements');
+ $this->assertArrayHasKey('foo', $services, '->load() parses <service> elements');
$this->assertFalse($services['not_shared']->isShared(), '->load() parses shared flag');
$this->assertInstanceOf('Symfony\\Component\\DependencyInjection\\Definition', $services['foo'], '->load() converts <service> element to Definition instances');
$this->assertEquals('FooClass', $services['foo']->getClass(), '->load() parses the class attribute');
$this->assertEquals('factory', $services['new_factory1']->getFactory(), '->load() parses the factory tag');
$this->assertEquals(array(new Reference('baz'), 'getClass'), $services['new_factory2']->getFactory(), '->load() parses the factory tag');
$this->assertEquals(array('BazClass', 'getInstance'), $services['new_factory3']->getFactory(), '->load() parses the factory tag');
+ $this->assertSame(array(null, 'getInstance'), $services['new_factory4']->getFactory(), '->load() accepts factory tag without class');
$aliases = $container->getAliases();
- $this->assertTrue(isset($aliases['alias_for_foo']), '->load() parses <service> elements');
+ $this->assertArrayHasKey('alias_for_foo', $aliases, '->load() parses <service> elements');
$this->assertEquals('foo', (string) $aliases['alias_for_foo'], '->load() parses aliases');
$this->assertTrue($aliases['alias_for_foo']->isPublic());
- $this->assertTrue(isset($aliases['another_alias_for_foo']));
+ $this->assertArrayHasKey('another_alias_for_foo', $aliases);
$this->assertEquals('foo', (string) $aliases['another_alias_for_foo']);
$this->assertFalse($aliases['another_alias_for_foo']->isPublic());
$this->assertEquals(array('decorated', 'decorated.pif-pouf', 5), $services['decorator_service_with_name_and_priority']->getDecoratedService());
}
+ public function testParsesIteratorArgument()
+ {
+ $container = new ContainerBuilder();
+ $loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
+ $loader->load('services9.xml');
+
+ $lazyDefinition = $container->getDefinition('lazy_context');
+
+ $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');
+ }
+
public function testParsesTags()
{
$container = new ContainerBuilder();
$services = $container->getDefinitions();
$parameters = $container->getParameterBag()->all();
- $this->assertTrue(isset($services['project.service.bar']), '->load() parses extension elements');
- $this->assertTrue(isset($parameters['project.parameter.bar']), '->load() parses extension elements');
+ $this->assertArrayHasKey('project.service.bar', $services, '->load() parses extension elements');
+ $this->assertArrayHasKey('project.parameter.bar', $parameters, '->load() parses extension elements');
$this->assertEquals('BAR', $services['project.service.foo']->getClass(), '->load() parses extension elements');
$this->assertEquals('BAR', $parameters['project.parameter.foo'], '->load() parses extension elements');
$services = $container->getDefinitions();
$parameters = $container->getParameterBag()->all();
- $this->assertTrue(isset($services['project.service.bar']), '->load() parses extension elements');
- $this->assertTrue(isset($parameters['project.parameter.bar']), '->load() parses extension elements');
+ $this->assertArrayHasKey('project.service.bar', $services, '->load() parses extension elements');
+ $this->assertArrayHasKey('project.parameter.bar', $parameters, '->load() parses extension elements');
$this->assertEquals('BAR', $services['project.service.foo']->getClass(), '->load() parses extension elements');
$this->assertEquals('BAR', $parameters['project.parameter.foo'], '->load() parses extension elements');
if (extension_loaded('suhosin') && false === strpos(ini_get('suhosin.executor.include.whitelist'), 'phar')) {
$this->markTestSkipped('To run this test, add "phar" to the "suhosin.executor.include.whitelist" settings in your php.ini file.');
}
+ if (defined('HHVM_VERSION')) {
+ $this->markTestSkipped('HHVM makes this test conflict with those run in separate processes.');
+ }
require_once self::$fixturesPath.'/includes/ProjectWithXsdExtensionInPhar.phar';
$loader = new XmlFileLoader(new ContainerBuilder(), new FileLocator());
$this->assertTrue($loader->supports('foo.xml'), '->supports() returns true if the resource is loadable');
- $this->assertFalse($loader->supports('foo.foo'), '->supports() returns true if the resource is loadable');
+ $this->assertFalse($loader->supports('foo.foo'), '->supports() returns false if the resource is not loadable');
+ $this->assertTrue($loader->supports('with_wrong_ext.yml', 'xml'), '->supports() returns true if the resource with forced type is loadable');
}
public function testNoNamingConflictsForAnonymousServices()
$loader1 = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml/extension1'));
$loader1->load('services.xml');
$services = $container->getDefinitions();
- $this->assertCount(2, $services, '->load() attributes unique ids to anonymous services');
+ $this->assertCount(3, $services, '->load() attributes unique ids to anonymous services');
$loader2 = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml/extension2'));
$loader2->load('services.xml');
$services = $container->getDefinitions();
- $this->assertCount(4, $services, '->load() attributes unique ids to anonymous services');
+ $this->assertCount(5, $services, '->load() attributes unique ids to anonymous services');
$services = $container->getDefinitions();
$args1 = $services['extension1.foo']->getArguments();
$loader->load('namespaces.xml');
$services = $container->getDefinitions();
- $this->assertTrue(isset($services['foo']), '->load() parses <srv:service> elements');
- $this->assertEquals(1, count($services['foo']->getTag('foo.tag')), '->load parses <srv:tag> elements');
+ $this->assertArrayHasKey('foo', $services, '->load() parses <srv:service> elements');
+ $this->assertCount(1, $services['foo']->getTag('foo.tag'), '->load parses <srv:tag> elements');
$this->assertEquals(array(array('setBar', array('foo'))), $services['foo']->getMethodCalls(), '->load() parses the <srv:call> tag');
}
$foo = $container->getDefinition('foo');
$fooFactory = $foo->getFactory();
- $this->assertInstanceOf('Symfony\Component\DependencyInjection\Definition', $fooFactory[0]);
- $this->assertSame('FooFactory', $fooFactory[0]->getClass());
+ $this->assertInstanceOf(Reference::class, $fooFactory[0]);
+ $this->assertTrue($container->has((string) $fooFactory[0]));
+ $fooFactoryDefinition = $container->getDefinition((string) $fooFactory[0]);
+ $this->assertSame('FooFactory', $fooFactoryDefinition->getClass());
$this->assertSame('createFoo', $fooFactory[1]);
- $fooFactoryFactory = $fooFactory[0]->getFactory();
- $this->assertInstanceOf('Symfony\Component\DependencyInjection\Definition', $fooFactoryFactory[0]);
- $this->assertSame('Foobar', $fooFactoryFactory[0]->getClass());
+ $fooFactoryFactory = $fooFactoryDefinition->getFactory();
+ $this->assertInstanceOf(Reference::class, $fooFactoryFactory[0]);
+ $this->assertTrue($container->has((string) $fooFactoryFactory[0]));
+ $this->assertSame('Foobar', $container->getDefinition((string) $fooFactoryFactory[0])->getClass());
$this->assertSame('createFooFactory', $fooFactoryFactory[1]);
$fooConfigurator = $foo->getConfigurator();
- $this->assertInstanceOf('Symfony\Component\DependencyInjection\Definition', $fooConfigurator[0]);
- $this->assertSame('Bar', $fooConfigurator[0]->getClass());
+ $this->assertInstanceOf(Reference::class, $fooConfigurator[0]);
+ $this->assertTrue($container->has((string) $fooConfigurator[0]));
+ $fooConfiguratorDefinition = $container->getDefinition((string) $fooConfigurator[0]);
+ $this->assertSame('Bar', $fooConfiguratorDefinition->getClass());
$this->assertSame('configureFoo', $fooConfigurator[1]);
- $barConfigurator = $fooConfigurator[0]->getConfigurator();
- $this->assertInstanceOf('Symfony\Component\DependencyInjection\Definition', $barConfigurator[0]);
- $this->assertSame('Baz', $barConfigurator[0]->getClass());
+ $barConfigurator = $fooConfiguratorDefinition->getConfigurator();
+ $this->assertInstanceOf(Reference::class, $barConfigurator[0]);
+ $this->assertSame('Baz', $container->getDefinition((string) $barConfigurator[0])->getClass());
$this->assertSame('configureBar', $barConfigurator[1]);
}
+ /**
+ * @group legacy
+ */
public function testType()
{
$container = new ContainerBuilder();
$this->assertTrue($container->getDefinition('bar')->isAutowired());
}
+ public function testClassFromId()
+ {
+ $container = new ContainerBuilder();
+ $loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
+ $loader->load('class_from_id.xml');
+ $container->compile();
+
+ $this->assertEquals(CaseSensitiveClass::class, $container->getDefinition(CaseSensitiveClass::class)->getClass());
+ }
+
+ public function testPrototype()
+ {
+ $container = new ContainerBuilder();
+ $loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
+ $loader->load('services_prototype.xml');
+
+ $ids = array_keys($container->getDefinitions());
+ sort($ids);
+ $this->assertSame(array(Prototype\Foo::class, Prototype\Sub\Bar::class, 'service_container'), $ids);
+
+ $resources = $container->getResources();
+
+ $fixturesDir = dirname(__DIR__).DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR;
+ $this->assertTrue(false !== array_search(new FileResource($fixturesDir.'xml'.DIRECTORY_SEPARATOR.'services_prototype.xml'), $resources));
+ $this->assertTrue(false !== array_search(new GlobResource($fixturesDir.'Prototype', '/*', true), $resources));
+ $resources = array_map('strval', $resources);
+ $this->assertContains('reflection.Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\Foo', $resources);
+ $this->assertContains('reflection.Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\Sub\Bar', $resources);
+ }
+
/**
* @group legacy
* @expectedDeprecation Using the attribute "class" is deprecated for the service "bar" which is defined as an alias %s.
$this->assertSame(array('type' => 'foo', 'bar'), $container->getDefinition('foo')->getArguments());
}
+
+ public function testDefaults()
+ {
+ $container = new ContainerBuilder();
+ $loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
+ $loader->load('services28.xml');
+
+ $this->assertFalse($container->getDefinition('with_defaults')->isPublic());
+ $this->assertSame(array('foo' => array(array())), $container->getDefinition('with_defaults')->getTags());
+ $this->assertTrue($container->getDefinition('with_defaults')->isAutowired());
+ $this->assertArrayNotHasKey('public', $container->getDefinition('with_defaults')->getChanges());
+ $this->assertArrayNotHasKey('autowire', $container->getDefinition('with_defaults')->getChanges());
+
+ $container->compile();
+
+ $this->assertTrue($container->getDefinition('no_defaults')->isPublic());
+
+ $this->assertSame(array('foo' => array(array())), $container->getDefinition('no_defaults')->getTags());
+
+ $this->assertFalse($container->getDefinition('no_defaults')->isAutowired());
+
+ $this->assertTrue($container->getDefinition('child_def')->isPublic());
+ $this->assertSame(array('foo' => array(array())), $container->getDefinition('child_def')->getTags());
+ $this->assertFalse($container->getDefinition('child_def')->isAutowired());
+
+ $definitions = $container->getDefinitions();
+ $this->assertSame('service_container', key($definitions));
+
+ array_shift($definitions);
+ $anonymous = current($definitions);
+ $this->assertSame('bar', key($definitions));
+ $this->assertTrue($anonymous->isPublic());
+ $this->assertTrue($anonymous->isAutowired());
+ $this->assertSame(array('foo' => array(array())), $anonymous->getTags());
+ }
+
+ public function testNamedArguments()
+ {
+ $container = new ContainerBuilder();
+ $loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
+ $loader->load('services_named_args.xml');
+
+ $this->assertEquals(array('$apiKey' => 'ABCD', CaseSensitiveClass::class => null), $container->getDefinition(NamedArgumentsDummy::class)->getArguments());
+
+ $container->compile();
+
+ $this->assertEquals(array(null, 'ABCD'), $container->getDefinition(NamedArgumentsDummy::class)->getArguments());
+ $this->assertEquals(array(array('setApiKey', array('123'))), $container->getDefinition(NamedArgumentsDummy::class)->getMethodCalls());
+ }
+
+ public function testInstanceof()
+ {
+ $container = new ContainerBuilder();
+ $loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
+ $loader->load('services_instanceof.xml');
+ $container->compile();
+
+ $definition = $container->getDefinition(Bar::class);
+ $this->assertTrue($definition->isAutowired());
+ $this->assertTrue($definition->isLazy());
+ $this->assertSame(array('foo' => array(array()), 'bar' => array(array())), $definition->getTags());
+ }
+
+ /**
+ * @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
+ * @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.
+ */
+ public function testInstanceOfAndChildDefinitionNotAllowed()
+ {
+ $container = new ContainerBuilder();
+ $loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
+ $loader->load('services_instanceof_with_parent.xml');
+ $container->compile();
+ }
+
+ /**
+ * @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
+ * @expectedExceptionMessage The service "child_service" cannot have a "parent" and also have "autoconfigure". Try setting autoconfigure="false" for the service.
+ */
+ public function testAutoConfigureAndChildDefinitionNotAllowed()
+ {
+ $container = new ContainerBuilder();
+ $loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
+ $loader->load('services_autoconfigure_with_parent.xml');
+ $container->compile();
+ }
+
+ /**
+ * @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
+ * @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.
+ */
+ public function testDefaultsAndChildDefinitionNotAllowed()
+ {
+ $container = new ContainerBuilder();
+ $loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
+ $loader->load('services_defaults_with_parent.xml');
+ $container->compile();
+ }
+
+ public function testAutoConfigureInstanceof()
+ {
+ $container = new ContainerBuilder();
+ $loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
+ $loader->load('services_autoconfigure.xml');
+
+ $this->assertTrue($container->getDefinition('use_defaults_settings')->isAutoconfigured());
+ $this->assertFalse($container->getDefinition('override_defaults_settings_to_false')->isAutoconfigured());
+ }
+
+ public function testBindings()
+ {
+ $container = new ContainerBuilder();
+ $loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
+ $loader->load('services_bindings.xml');
+ $container->compile();
+
+ $definition = $container->getDefinition('bar');
+ $this->assertEquals(array(
+ 'NonExistent' => null,
+ BarInterface::class => new Reference(Bar::class),
+ '$foo' => array(null),
+ '$quz' => 'quz',
+ '$factory' => 'factory',
+ ), array_map(function ($v) { return $v->getValues()[0]; }, $definition->getBindings()));
+ $this->assertEquals(array(
+ 'quz',
+ null,
+ new Reference(Bar::class),
+ array(null),
+ ), $definition->getArguments());
+
+ $definition = $container->getDefinition(Bar::class);
+ $this->assertEquals(array(
+ null,
+ 'factory',
+ ), $definition->getArguments());
+ $this->assertEquals(array(
+ 'NonExistent' => null,
+ '$quz' => 'quz',
+ '$factory' => 'factory',
+ ), array_map(function ($v) { return $v->getValues()[0]; }, $definition->getBindings()));
+ }
}