123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992 |
- <?php
- /*
- * This file is part of the Symfony package.
- *
- * (c) Fabien Potencier <fabien@symfony.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Symfony\Component\DependencyInjection\Tests;
- require_once __DIR__.'/Fixtures/includes/classes.php';
- require_once __DIR__.'/Fixtures/includes/ProjectExtension.php';
- use PHPUnit\Framework\TestCase;
- use Symfony\Component\Config\Resource\FileResource;
- use Symfony\Component\Config\Resource\ResourceInterface;
- use Symfony\Component\DependencyInjection\Alias;
- use Symfony\Component\DependencyInjection\ContainerBuilder;
- use Symfony\Component\DependencyInjection\ContainerInterface;
- use Symfony\Component\DependencyInjection\Definition;
- use Symfony\Component\DependencyInjection\DefinitionDecorator;
- use Symfony\Component\DependencyInjection\Exception\InactiveScopeException;
- use Symfony\Component\DependencyInjection\Exception\RuntimeException;
- use Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException;
- use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
- use Symfony\Component\DependencyInjection\Loader\ClosureLoader;
- use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
- use Symfony\Component\DependencyInjection\Reference;
- use Symfony\Component\DependencyInjection\Scope;
- use Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition;
- use Symfony\Component\ExpressionLanguage\Expression;
- class ContainerBuilderTest extends TestCase
- {
- public function testDefinitions()
- {
- $builder = new ContainerBuilder();
- $definitions = array(
- 'foo' => new Definition('Bar\FooClass'),
- 'bar' => new Definition('BarClass'),
- );
- $builder->setDefinitions($definitions);
- $this->assertEquals($definitions, $builder->getDefinitions(), '->setDefinitions() sets the service definitions');
- $this->assertTrue($builder->hasDefinition('foo'), '->hasDefinition() returns true if a service definition exists');
- $this->assertFalse($builder->hasDefinition('foobar'), '->hasDefinition() returns false if a service definition does not exist');
- $builder->setDefinition('foobar', $foo = new Definition('FooBarClass'));
- $this->assertEquals($foo, $builder->getDefinition('foobar'), '->getDefinition() returns a service definition if defined');
- $this->assertSame($builder->setDefinition('foobar', $foo = new Definition('FooBarClass')), $foo, '->setDefinition() implements a fluid interface by returning the service reference');
- $builder->addDefinitions($defs = array('foobar' => new Definition('FooBarClass')));
- $this->assertEquals(array_merge($definitions, $defs), $builder->getDefinitions(), '->addDefinitions() adds the service definitions');
- try {
- $builder->getDefinition('baz');
- $this->fail('->getDefinition() throws a ServiceNotFoundException if the service definition does not exist');
- } catch (ServiceNotFoundException $e) {
- $this->assertEquals('You have requested a non-existent service "baz".', $e->getMessage(), '->getDefinition() throws a ServiceNotFoundException if the service definition does not exist');
- }
- }
- /**
- * @group legacy
- * @expectedDeprecation The "deprecated_foo" service is deprecated. You should stop using it, as it will soon be removed.
- */
- public function testCreateDeprecatedService()
- {
- $definition = new Definition('stdClass');
- $definition->setDeprecated(true);
- $builder = new ContainerBuilder();
- $builder->createService($definition, new \SplObjectStorage(), 'deprecated_foo');
- }
- public function testRegister()
- {
- $builder = new ContainerBuilder();
- $builder->register('foo', 'Bar\FooClass');
- $this->assertTrue($builder->hasDefinition('foo'), '->register() registers a new service definition');
- $this->assertInstanceOf('Symfony\Component\DependencyInjection\Definition', $builder->getDefinition('foo'), '->register() returns the newly created Definition instance');
- }
- public function testHas()
- {
- $builder = new ContainerBuilder();
- $this->assertFalse($builder->has('foo'), '->has() returns false if the service does not exist');
- $builder->register('foo', 'Bar\FooClass');
- $this->assertTrue($builder->has('foo'), '->has() returns true if a service definition exists');
- $builder->set('bar', new \stdClass());
- $this->assertTrue($builder->has('bar'), '->has() returns true if a service exists');
- }
- public function testGet()
- {
- $builder = new ContainerBuilder();
- try {
- $builder->get('foo');
- $this->fail('->get() throws a ServiceNotFoundException if the service does not exist');
- } catch (ServiceNotFoundException $e) {
- $this->assertEquals('You have requested a non-existent service "foo".', $e->getMessage(), '->get() throws a ServiceNotFoundException if the service does not exist');
- }
- $this->assertNull($builder->get('foo', ContainerInterface::NULL_ON_INVALID_REFERENCE), '->get() returns null if the service does not exist and NULL_ON_INVALID_REFERENCE is passed as a second argument');
- $builder->register('foo', 'stdClass');
- $this->assertInternalType('object', $builder->get('foo'), '->get() returns the service definition associated with the id');
- $builder->set('bar', $bar = new \stdClass());
- $this->assertEquals($bar, $builder->get('bar'), '->get() returns the service associated with the id');
- $builder->register('bar', 'stdClass');
- $this->assertEquals($bar, $builder->get('bar'), '->get() returns the service associated with the id even if a definition has been defined');
- $builder->register('baz', 'stdClass')->setArguments(array(new Reference('baz')));
- try {
- @$builder->get('baz');
- $this->fail('->get() throws a ServiceCircularReferenceException if the service has a circular reference to itself');
- } catch (ServiceCircularReferenceException $e) {
- $this->assertEquals('Circular reference detected for service "baz", path: "baz".', $e->getMessage(), '->get() throws a LogicException if the service has a circular reference to itself');
- }
- $this->assertSame($builder->get('bar'), $builder->get('bar'), '->get() always returns the same instance if the service is shared');
- }
- public function testNonSharedServicesReturnsDifferentInstances()
- {
- $builder = new ContainerBuilder();
- $builder->register('bar', 'stdClass')->setShared(false);
- $this->assertNotSame($builder->get('bar'), $builder->get('bar'));
- }
- /**
- * @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
- * @dataProvider provideBadId
- */
- public function testBadAliasId($id)
- {
- $builder = new ContainerBuilder();
- $builder->setAlias($id, 'foo');
- }
- /**
- * @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
- * @dataProvider provideBadId
- */
- public function testBadDefinitionId($id)
- {
- $builder = new ContainerBuilder();
- $builder->setDefinition($id, new Definition('Foo'));
- }
- public function provideBadId()
- {
- return [
- [''],
- ["\0"],
- ["\r"],
- ["\n"],
- ["'"],
- ['ab\\'],
- ];
- }
- /**
- * @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
- * @expectedExceptionMessage You have requested a synthetic service ("foo"). The DIC does not know how to construct this service.
- */
- public function testGetUnsetLoadingServiceWhenCreateServiceThrowsAnException()
- {
- $builder = new ContainerBuilder();
- $builder->register('foo', 'stdClass')->setSynthetic(true);
- // we expect a RuntimeException here as foo is synthetic
- try {
- $builder->get('foo');
- } catch (RuntimeException $e) {
- }
- // we must also have the same RuntimeException here
- $builder->get('foo');
- }
- /**
- * @group legacy
- */
- public function testGetReturnsNullOnInactiveScope()
- {
- $builder = new ContainerBuilder();
- $builder->register('foo', 'stdClass')->setScope('request');
- $this->assertNull($builder->get('foo', ContainerInterface::NULL_ON_INVALID_REFERENCE));
- }
- /**
- * @group legacy
- */
- public function testGetReturnsNullOnInactiveScopeWhenServiceIsCreatedByAMethod()
- {
- $builder = new ProjectContainer();
- $this->assertNull($builder->get('foobaz', ContainerInterface::NULL_ON_INVALID_REFERENCE));
- }
- public function testGetServiceIds()
- {
- $builder = new ContainerBuilder();
- $builder->register('foo', 'stdClass');
- $builder->bar = $bar = new \stdClass();
- $builder->register('bar', 'stdClass');
- $this->assertEquals(array('foo', 'bar', 'service_container'), $builder->getServiceIds(), '->getServiceIds() returns all defined service ids');
- }
- public function testAliases()
- {
- $builder = new ContainerBuilder();
- $builder->register('foo', 'stdClass');
- $builder->setAlias('bar', 'foo');
- $this->assertTrue($builder->hasAlias('bar'), '->hasAlias() returns true if the alias exists');
- $this->assertFalse($builder->hasAlias('foobar'), '->hasAlias() returns false if the alias does not exist');
- $this->assertEquals('foo', (string) $builder->getAlias('bar'), '->getAlias() returns the aliased service');
- $this->assertTrue($builder->has('bar'), '->setAlias() defines a new service');
- $this->assertSame($builder->get('bar'), $builder->get('foo'), '->setAlias() creates a service that is an alias to another one');
- try {
- $builder->setAlias('foobar', 'foobar');
- $this->fail('->setAlias() throws an InvalidArgumentException if the alias references itself');
- } catch (\InvalidArgumentException $e) {
- $this->assertEquals('An alias can not reference itself, got a circular reference on "foobar".', $e->getMessage(), '->setAlias() throws an InvalidArgumentException if the alias references itself');
- }
- try {
- $builder->getAlias('foobar');
- $this->fail('->getAlias() throws an InvalidArgumentException if the alias does not exist');
- } catch (\InvalidArgumentException $e) {
- $this->assertEquals('The service alias "foobar" does not exist.', $e->getMessage(), '->getAlias() throws an InvalidArgumentException if the alias does not exist');
- }
- }
- public function testGetAliases()
- {
- $builder = new ContainerBuilder();
- $builder->setAlias('bar', 'foo');
- $builder->setAlias('foobar', 'foo');
- $builder->setAlias('moo', new Alias('foo', false));
- $aliases = $builder->getAliases();
- $this->assertEquals('foo', (string) $aliases['bar']);
- $this->assertTrue($aliases['bar']->isPublic());
- $this->assertEquals('foo', (string) $aliases['foobar']);
- $this->assertEquals('foo', (string) $aliases['moo']);
- $this->assertFalse($aliases['moo']->isPublic());
- $builder->register('bar', 'stdClass');
- $this->assertFalse($builder->hasAlias('bar'));
- $builder->set('foobar', 'stdClass');
- $builder->set('moo', 'stdClass');
- $this->assertCount(0, $builder->getAliases(), '->getAliases() does not return aliased services that have been overridden');
- }
- public function testSetAliases()
- {
- $builder = new ContainerBuilder();
- $builder->setAliases(array('bar' => 'foo', 'foobar' => 'foo'));
- $aliases = $builder->getAliases();
- $this->assertArrayHasKey('bar', $aliases);
- $this->assertArrayHasKey('foobar', $aliases);
- }
- public function testAddAliases()
- {
- $builder = new ContainerBuilder();
- $builder->setAliases(array('bar' => 'foo'));
- $builder->addAliases(array('foobar' => 'foo'));
- $aliases = $builder->getAliases();
- $this->assertArrayHasKey('bar', $aliases);
- $this->assertArrayHasKey('foobar', $aliases);
- }
- public function testSetReplacesAlias()
- {
- $builder = new ContainerBuilder();
- $builder->setAlias('alias', 'aliased');
- $builder->set('aliased', new \stdClass());
- $builder->set('alias', $foo = new \stdClass());
- $this->assertSame($foo, $builder->get('alias'), '->set() replaces an existing alias');
- }
- public function testAliasesKeepInvalidBehavior()
- {
- $builder = new ContainerBuilder();
- $aliased = new Definition('stdClass');
- $aliased->addMethodCall('setBar', array(new Reference('bar', ContainerInterface::IGNORE_ON_INVALID_REFERENCE)));
- $builder->setDefinition('aliased', $aliased);
- $builder->setAlias('alias', 'aliased');
- $this->assertEquals(new \stdClass(), $builder->get('alias'));
- }
- public function testAddGetCompilerPass()
- {
- $builder = new ContainerBuilder();
- $builder->setResourceTracking(false);
- $builderCompilerPasses = $builder->getCompiler()->getPassConfig()->getPasses();
- $builder->addCompilerPass($this->getMockBuilder('Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface')->getMock());
- $this->assertCount(\count($builder->getCompiler()->getPassConfig()->getPasses()) - 1, $builderCompilerPasses);
- }
- public function testCreateService()
- {
- $builder = new ContainerBuilder();
- $builder->register('foo1', 'Bar\FooClass')->setFile(__DIR__.'/Fixtures/includes/foo.php');
- $this->assertInstanceOf('\Bar\FooClass', $builder->get('foo1'), '->createService() requires the file defined by the service definition');
- $builder->register('foo2', 'Bar\FooClass')->setFile(__DIR__.'/Fixtures/includes/%file%.php');
- $builder->setParameter('file', 'foo');
- $this->assertInstanceOf('\Bar\FooClass', $builder->get('foo2'), '->createService() replaces parameters in the file provided by the service definition');
- }
- public function testCreateProxyWithRealServiceInstantiator()
- {
- $builder = new ContainerBuilder();
- $builder->register('foo1', 'Bar\FooClass')->setFile(__DIR__.'/Fixtures/includes/foo.php');
- $builder->getDefinition('foo1')->setLazy(true);
- $foo1 = $builder->get('foo1');
- $this->assertSame($foo1, $builder->get('foo1'), 'The same proxy is retrieved on multiple subsequent calls');
- $this->assertSame('Bar\FooClass', \get_class($foo1));
- }
- public function testCreateServiceClass()
- {
- $builder = new ContainerBuilder();
- $builder->register('foo1', '%class%');
- $builder->setParameter('class', 'stdClass');
- $this->assertInstanceOf('\stdClass', $builder->get('foo1'), '->createService() replaces parameters in the class provided by the service definition');
- }
- public function testCreateServiceArguments()
- {
- $builder = new ContainerBuilder();
- $builder->register('bar', 'stdClass');
- $builder->register('foo1', 'Bar\FooClass')->addArgument(array('foo' => '%value%', '%value%' => 'foo', new Reference('bar'), '%%unescape_it%%'));
- $builder->setParameter('value', 'bar');
- $this->assertEquals(array('foo' => 'bar', 'bar' => 'foo', $builder->get('bar'), '%unescape_it%'), $builder->get('foo1')->arguments, '->createService() replaces parameters and service references in the arguments provided by the service definition');
- }
- public function testCreateServiceFactory()
- {
- $builder = new ContainerBuilder();
- $builder->register('foo', 'Bar\FooClass')->setFactory('Bar\FooClass::getInstance');
- $builder->register('qux', 'Bar\FooClass')->setFactory(array('Bar\FooClass', 'getInstance'));
- $builder->register('bar', 'Bar\FooClass')->setFactory(array(new Definition('Bar\FooClass'), 'getInstance'));
- $builder->register('baz', 'Bar\FooClass')->setFactory(array(new Reference('bar'), 'getInstance'));
- $this->assertTrue($builder->get('foo')->called, '->createService() calls the factory method to create the service instance');
- $this->assertTrue($builder->get('qux')->called, '->createService() calls the factory method to create the service instance');
- $this->assertTrue($builder->get('bar')->called, '->createService() uses anonymous service as factory');
- $this->assertTrue($builder->get('baz')->called, '->createService() uses another service as factory');
- }
- /**
- * @group legacy
- */
- public function testLegacyCreateServiceFactory()
- {
- $builder = new ContainerBuilder();
- $builder->register('bar', 'Bar\FooClass');
- $builder
- ->register('foo1', 'Bar\FooClass')
- ->setFactoryClass('%foo_class%')
- ->setFactoryMethod('getInstance')
- ->addArgument(array('foo' => '%value%', '%value%' => 'foo', new Reference('bar')))
- ;
- $builder->setParameter('value', 'bar');
- $builder->setParameter('foo_class', 'Bar\FooClass');
- $this->assertTrue($builder->get('foo1')->called, '->createService() calls the factory method to create the service instance');
- $this->assertEquals(array('foo' => 'bar', 'bar' => 'foo', $builder->get('bar')), $builder->get('foo1')->arguments, '->createService() passes the arguments to the factory method');
- }
- /**
- * @group legacy
- */
- public function testLegacyCreateServiceFactoryService()
- {
- $builder = new ContainerBuilder();
- $builder->register('foo_service', 'Bar\FooClass');
- $builder
- ->register('foo', 'Bar\FooClass')
- ->setFactoryService('%foo_service%')
- ->setFactoryMethod('getInstance')
- ;
- $builder->setParameter('foo_service', 'foo_service');
- $this->assertTrue($builder->get('foo')->called, '->createService() calls the factory method to create the service instance');
- }
- public function testCreateServiceMethodCalls()
- {
- $builder = new ContainerBuilder();
- $builder->register('bar', 'stdClass');
- $builder->register('foo1', 'Bar\FooClass')->addMethodCall('setBar', array(array('%value%', new Reference('bar'))));
- $builder->setParameter('value', 'bar');
- $this->assertEquals(array('bar', $builder->get('bar')), $builder->get('foo1')->bar, '->createService() replaces the values in the method calls arguments');
- }
- public function testCreateServiceMethodCallsWithEscapedParam()
- {
- $builder = new ContainerBuilder();
- $builder->register('bar', 'stdClass');
- $builder->register('foo1', 'Bar\FooClass')->addMethodCall('setBar', array(array('%%unescape_it%%')));
- $builder->setParameter('value', 'bar');
- $this->assertEquals(array('%unescape_it%'), $builder->get('foo1')->bar, '->createService() replaces the values in the method calls arguments');
- }
- public function testCreateServiceProperties()
- {
- $builder = new ContainerBuilder();
- $builder->register('bar', 'stdClass');
- $builder->register('foo1', 'Bar\FooClass')->setProperty('bar', array('%value%', new Reference('bar'), '%%unescape_it%%'));
- $builder->setParameter('value', 'bar');
- $this->assertEquals(array('bar', $builder->get('bar'), '%unescape_it%'), $builder->get('foo1')->bar, '->createService() replaces the values in the properties');
- }
- public function testCreateServiceConfigurator()
- {
- $builder = new ContainerBuilder();
- $builder->register('foo1', 'Bar\FooClass')->setConfigurator('sc_configure');
- $this->assertTrue($builder->get('foo1')->configured, '->createService() calls the configurator');
- $builder->register('foo2', 'Bar\FooClass')->setConfigurator(array('%class%', 'configureStatic'));
- $builder->setParameter('class', 'BazClass');
- $this->assertTrue($builder->get('foo2')->configured, '->createService() calls the configurator');
- $builder->register('baz', 'BazClass');
- $builder->register('foo3', 'Bar\FooClass')->setConfigurator(array(new Reference('baz'), 'configure'));
- $this->assertTrue($builder->get('foo3')->configured, '->createService() calls the configurator');
- $builder->register('foo4', 'Bar\FooClass')->setConfigurator(array($builder->getDefinition('baz'), 'configure'));
- $this->assertTrue($builder->get('foo4')->configured, '->createService() calls the configurator');
- $builder->register('foo5', 'Bar\FooClass')->setConfigurator('foo');
- try {
- $builder->get('foo5');
- $this->fail('->createService() throws an InvalidArgumentException if the configure callable is not a valid callable');
- } catch (\InvalidArgumentException $e) {
- $this->assertEquals('The configure callable for class "Bar\FooClass" is not a callable.', $e->getMessage(), '->createService() throws an InvalidArgumentException if the configure callable is not a valid callable');
- }
- }
- /**
- * @expectedException \RuntimeException
- */
- public function testCreateSyntheticService()
- {
- $builder = new ContainerBuilder();
- $builder->register('foo', 'Bar\FooClass')->setSynthetic(true);
- $builder->get('foo');
- }
- public function testCreateServiceWithExpression()
- {
- $builder = new ContainerBuilder();
- $builder->setParameter('bar', 'bar');
- $builder->register('bar', 'BarClass');
- $builder->register('foo', 'Bar\FooClass')->addArgument(array('foo' => new Expression('service("bar").foo ~ parameter("bar")')));
- $this->assertEquals('foobar', $builder->get('foo')->arguments['foo']);
- }
- public function testResolveServices()
- {
- $builder = new ContainerBuilder();
- $builder->register('foo', 'Bar\FooClass');
- $this->assertEquals($builder->get('foo'), $builder->resolveServices(new Reference('foo')), '->resolveServices() resolves service references to service instances');
- $this->assertEquals(array('foo' => array('foo', $builder->get('foo'))), $builder->resolveServices(array('foo' => array('foo', new Reference('foo')))), '->resolveServices() resolves service references to service instances in nested arrays');
- $this->assertEquals($builder->get('foo'), $builder->resolveServices(new Expression('service("foo")')), '->resolveServices() resolves expressions');
- }
- /**
- * @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
- * @expectedExceptionMessage Constructing service "foo" from a parent definition is not supported at build time.
- */
- public function testResolveServicesWithDecoratedDefinition()
- {
- $builder = new ContainerBuilder();
- $builder->setDefinition('grandpa', new Definition('stdClass'));
- $builder->setDefinition('parent', new DefinitionDecorator('grandpa'));
- $builder->setDefinition('foo', new DefinitionDecorator('parent'));
- $builder->get('foo');
- }
- public function testResolveServicesWithCustomDefinitionClass()
- {
- $builder = new ContainerBuilder();
- $builder->setDefinition('foo', new CustomDefinition('stdClass'));
- $this->assertInstanceOf('stdClass', $builder->get('foo'));
- }
- public function testMerge()
- {
- $container = new ContainerBuilder(new ParameterBag(array('bar' => 'foo')));
- $container->setResourceTracking(false);
- $config = new ContainerBuilder(new ParameterBag(array('foo' => 'bar')));
- $container->merge($config);
- $this->assertEquals(array('bar' => 'foo', 'foo' => 'bar'), $container->getParameterBag()->all(), '->merge() merges current parameters with the loaded ones');
- $container = new ContainerBuilder(new ParameterBag(array('bar' => 'foo')));
- $container->setResourceTracking(false);
- $config = new ContainerBuilder(new ParameterBag(array('foo' => '%bar%')));
- $container->merge($config);
- $container->compile();
- $this->assertEquals(array('bar' => 'foo', 'foo' => 'foo'), $container->getParameterBag()->all(), '->merge() evaluates the values of the parameters towards already defined ones');
- $container = new ContainerBuilder(new ParameterBag(array('bar' => 'foo')));
- $container->setResourceTracking(false);
- $config = new ContainerBuilder(new ParameterBag(array('foo' => '%bar%', 'baz' => '%foo%')));
- $container->merge($config);
- $container->compile();
- $this->assertEquals(array('bar' => 'foo', 'foo' => 'foo', 'baz' => 'foo'), $container->getParameterBag()->all(), '->merge() evaluates the values of the parameters towards already defined ones');
- $container = new ContainerBuilder();
- $container->setResourceTracking(false);
- $container->register('foo', 'Bar\FooClass');
- $container->register('bar', 'BarClass');
- $config = new ContainerBuilder();
- $config->setDefinition('baz', new Definition('BazClass'));
- $config->setAlias('alias_for_foo', 'foo');
- $container->merge($config);
- $this->assertEquals(array('foo', 'bar', 'baz'), array_keys($container->getDefinitions()), '->merge() merges definitions already defined ones');
- $aliases = $container->getAliases();
- $this->assertArrayHasKey('alias_for_foo', $aliases);
- $this->assertEquals('foo', (string) $aliases['alias_for_foo']);
- $container = new ContainerBuilder();
- $container->setResourceTracking(false);
- $container->register('foo', 'Bar\FooClass');
- $config->setDefinition('foo', new Definition('BazClass'));
- $container->merge($config);
- $this->assertEquals('BazClass', $container->getDefinition('foo')->getClass(), '->merge() overrides already defined services');
- }
- /**
- * @expectedException \LogicException
- */
- public function testMergeLogicException()
- {
- $container = new ContainerBuilder();
- $container->setResourceTracking(false);
- $container->compile();
- $container->merge(new ContainerBuilder());
- }
- public function testfindTaggedServiceIds()
- {
- $builder = new ContainerBuilder();
- $builder
- ->register('foo', 'Bar\FooClass')
- ->addTag('foo', array('foo' => 'foo'))
- ->addTag('bar', array('bar' => 'bar'))
- ->addTag('foo', array('foofoo' => 'foofoo'))
- ;
- $this->assertEquals($builder->findTaggedServiceIds('foo'), array(
- 'foo' => array(
- array('foo' => 'foo'),
- array('foofoo' => 'foofoo'),
- ),
- ), '->findTaggedServiceIds() returns an array of service ids and its tag attributes');
- $this->assertEquals(array(), $builder->findTaggedServiceIds('foobar'), '->findTaggedServiceIds() returns an empty array if there is annotated services');
- }
- public function testFindUnusedTags()
- {
- $builder = new ContainerBuilder();
- $builder
- ->register('foo', 'Bar\FooClass')
- ->addTag('kernel.event_listener', array('foo' => 'foo'))
- ->addTag('kenrel.event_listener', array('bar' => 'bar'))
- ;
- $builder->findTaggedServiceIds('kernel.event_listener');
- $this->assertEquals(array('kenrel.event_listener'), $builder->findUnusedTags(), '->findUnusedTags() returns an array with unused tags');
- }
- public function testFindDefinition()
- {
- $container = new ContainerBuilder();
- $container->setDefinition('foo', $definition = new Definition('Bar\FooClass'));
- $container->setAlias('bar', 'foo');
- $container->setAlias('foobar', 'bar');
- $this->assertEquals($definition, $container->findDefinition('foobar'), '->findDefinition() returns a Definition');
- }
- public function testAddObjectResource()
- {
- $container = new ContainerBuilder();
- $container->setResourceTracking(false);
- $container->addObjectResource(new \BarClass());
- $this->assertEmpty($container->getResources(), 'No resources get registered without resource tracking');
- $container->setResourceTracking(true);
- $container->addObjectResource(new \BarClass());
- $resources = $container->getResources();
- $this->assertCount(1, $resources, '1 resource was registered');
- /* @var $resource \Symfony\Component\Config\Resource\FileResource */
- $resource = end($resources);
- $this->assertInstanceOf('Symfony\Component\Config\Resource\FileResource', $resource);
- $this->assertSame(realpath(__DIR__.'/Fixtures/includes/classes.php'), realpath($resource->getResource()));
- }
- public function testAddClassResource()
- {
- $container = new ContainerBuilder();
- $container->setResourceTracking(false);
- $container->addClassResource(new \ReflectionClass('BarClass'));
- $this->assertEmpty($container->getResources(), 'No resources get registered without resource tracking');
- $container->setResourceTracking(true);
- $container->addClassResource(new \ReflectionClass('BarClass'));
- $resources = $container->getResources();
- $this->assertCount(1, $resources, '1 resource was registered');
- /* @var $resource \Symfony\Component\Config\Resource\FileResource */
- $resource = end($resources);
- $this->assertInstanceOf('Symfony\Component\Config\Resource\FileResource', $resource);
- $this->assertSame(realpath(__DIR__.'/Fixtures/includes/classes.php'), realpath($resource->getResource()));
- }
- public function testCompilesClassDefinitionsOfLazyServices()
- {
- $container = new ContainerBuilder();
- $this->assertEmpty($container->getResources(), 'No resources get registered without resource tracking');
- $container->register('foo', 'BarClass');
- $container->getDefinition('foo')->setLazy(true);
- $container->compile();
- $classesPath = realpath(__DIR__.'/Fixtures/includes/classes.php');
- $matchingResources = array_filter(
- $container->getResources(),
- function (ResourceInterface $resource) use ($classesPath) {
- return $resource instanceof FileResource && $classesPath === realpath($resource->getResource());
- }
- );
- $this->assertNotEmpty($matchingResources);
- }
- public function testResources()
- {
- $container = new ContainerBuilder();
- $container->addResource($a = new FileResource(__DIR__.'/Fixtures/xml/services1.xml'));
- $container->addResource($b = new FileResource(__DIR__.'/Fixtures/xml/services2.xml'));
- $resources = array();
- foreach ($container->getResources() as $resource) {
- if (false === strpos($resource, '.php')) {
- $resources[] = $resource;
- }
- }
- $this->assertEquals(array($a, $b), $resources, '->getResources() returns an array of resources read for the current configuration');
- $this->assertSame($container, $container->setResources(array()));
- $this->assertEquals(array(), $container->getResources());
- }
- public function testExtension()
- {
- $container = new ContainerBuilder();
- $container->setResourceTracking(false);
- $container->registerExtension($extension = new \ProjectExtension());
- $this->assertSame($container->getExtension('project'), $extension, '->registerExtension() registers an extension');
- $this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('LogicException');
- $container->getExtension('no_registered');
- }
- public function testRegisteredButNotLoadedExtension()
- {
- $extension = $this->getMockBuilder('Symfony\\Component\\DependencyInjection\\Extension\\ExtensionInterface')->getMock();
- $extension->expects($this->once())->method('getAlias')->will($this->returnValue('project'));
- $extension->expects($this->never())->method('load');
- $container = new ContainerBuilder();
- $container->setResourceTracking(false);
- $container->registerExtension($extension);
- $container->compile();
- }
- public function testRegisteredAndLoadedExtension()
- {
- $extension = $this->getMockBuilder('Symfony\\Component\\DependencyInjection\\Extension\\ExtensionInterface')->getMock();
- $extension->expects($this->exactly(2))->method('getAlias')->will($this->returnValue('project'));
- $extension->expects($this->once())->method('load')->with(array(array('foo' => 'bar')));
- $container = new ContainerBuilder();
- $container->setResourceTracking(false);
- $container->registerExtension($extension);
- $container->loadFromExtension('project', array('foo' => 'bar'));
- $container->compile();
- }
- public function testPrivateServiceUser()
- {
- $fooDefinition = new Definition('BarClass');
- $fooUserDefinition = new Definition('BarUserClass', array(new Reference('bar')));
- $container = new ContainerBuilder();
- $container->setResourceTracking(false);
- $fooDefinition->setPublic(false);
- $container->addDefinitions(array(
- 'bar' => $fooDefinition,
- 'bar_user' => $fooUserDefinition,
- ));
- $container->compile();
- $this->assertInstanceOf('BarClass', $container->get('bar_user')->bar);
- }
- /**
- * @expectedException \BadMethodCallException
- */
- public function testThrowsExceptionWhenSetServiceOnAFrozenContainer()
- {
- $container = new ContainerBuilder();
- $container->setResourceTracking(false);
- $container->setDefinition('a', new Definition('stdClass'));
- $container->compile();
- $container->set('a', new \stdClass());
- }
- public function testThrowsExceptionWhenAddServiceOnAFrozenContainer()
- {
- $container = new ContainerBuilder();
- $container->compile();
- $container->set('a', $foo = new \stdClass());
- $this->assertSame($foo, $container->get('a'));
- }
- public function testNoExceptionWhenSetSyntheticServiceOnAFrozenContainer()
- {
- $container = new ContainerBuilder();
- $def = new Definition('stdClass');
- $def->setSynthetic(true);
- $container->setDefinition('a', $def);
- $container->compile();
- $container->set('a', $a = new \stdClass());
- $this->assertEquals($a, $container->get('a'));
- }
- /**
- * @group legacy
- */
- public function testLegacySetOnSynchronizedService()
- {
- $container = new ContainerBuilder();
- $container->register('baz', 'BazClass')
- ->setSynchronized(true)
- ;
- $container->register('bar', 'BarClass')
- ->addMethodCall('setBaz', array(new Reference('baz')))
- ;
- $container->set('baz', $baz = new \BazClass());
- $this->assertSame($baz, $container->get('bar')->getBaz());
- $container->set('baz', $baz = new \BazClass());
- $this->assertSame($baz, $container->get('bar')->getBaz());
- }
- /**
- * @group legacy
- */
- public function testLegacySynchronizedServiceWithScopes()
- {
- $container = new ContainerBuilder();
- $container->addScope(new Scope('foo'));
- $container->register('baz', 'BazClass')
- ->setSynthetic(true)
- ->setSynchronized(true)
- ->setScope('foo')
- ;
- $container->register('bar', 'BarClass')
- ->addMethodCall('setBaz', array(new Reference('baz', ContainerInterface::NULL_ON_INVALID_REFERENCE, false)))
- ;
- $container->compile();
- $container->enterScope('foo');
- $container->set('baz', $outerBaz = new \BazClass(), 'foo');
- $this->assertSame($outerBaz, $container->get('bar')->getBaz());
- $container->enterScope('foo');
- $container->set('baz', $innerBaz = new \BazClass(), 'foo');
- $this->assertSame($innerBaz, $container->get('bar')->getBaz());
- $container->leaveScope('foo');
- $this->assertNotSame($innerBaz, $container->get('bar')->getBaz());
- $this->assertSame($outerBaz, $container->get('bar')->getBaz());
- $container->leaveScope('foo');
- }
- /**
- * @expectedException \BadMethodCallException
- */
- public function testThrowsExceptionWhenSetDefinitionOnAFrozenContainer()
- {
- $container = new ContainerBuilder();
- $container->setResourceTracking(false);
- $container->compile();
- $container->setDefinition('a', new Definition());
- }
- public function testExtensionConfig()
- {
- $container = new ContainerBuilder();
- $configs = $container->getExtensionConfig('foo');
- $this->assertEmpty($configs);
- $first = array('foo' => 'bar');
- $container->prependExtensionConfig('foo', $first);
- $configs = $container->getExtensionConfig('foo');
- $this->assertEquals(array($first), $configs);
- $second = array('ding' => 'dong');
- $container->prependExtensionConfig('foo', $second);
- $configs = $container->getExtensionConfig('foo');
- $this->assertEquals(array($second, $first), $configs);
- }
- public function testAbstractAlias()
- {
- $container = new ContainerBuilder();
- $abstract = new Definition('AbstractClass');
- $abstract->setAbstract(true);
- $container->setDefinition('abstract_service', $abstract);
- $container->setAlias('abstract_alias', 'abstract_service');
- $container->compile();
- $this->assertSame('abstract_service', (string) $container->getAlias('abstract_alias'));
- }
- public function testLazyLoadedService()
- {
- $loader = new ClosureLoader($container = new ContainerBuilder());
- $loader->load(function (ContainerBuilder $container) {
- $container->set('a', new \BazClass());
- $definition = new Definition('BazClass');
- $definition->setLazy(true);
- $container->setDefinition('a', $definition);
- });
- $container->setResourceTracking(true);
- $container->compile();
- $class = new \BazClass();
- $reflectionClass = new \ReflectionClass($class);
- $r = new \ReflectionProperty($container, 'resources');
- $r->setAccessible(true);
- $resources = $r->getValue($container);
- $classInList = false;
- foreach ($resources as $resource) {
- if ($resource->getResource() === $reflectionClass->getFileName()) {
- $classInList = true;
- break;
- }
- }
- $this->assertTrue($classInList);
- }
- public function testInlinedDefinitions()
- {
- $container = new ContainerBuilder();
- $definition = new Definition('BarClass');
- $container->register('bar_user', 'BarUserClass')
- ->addArgument($definition)
- ->setProperty('foo', $definition);
- $container->register('bar', 'BarClass')
- ->setProperty('foo', $definition)
- ->addMethodCall('setBaz', array($definition));
- $barUser = $container->get('bar_user');
- $bar = $container->get('bar');
- $this->assertSame($barUser->foo, $barUser->bar);
- $this->assertSame($bar->foo, $bar->getBaz());
- $this->assertNotSame($bar->foo, $barUser->foo);
- }
- public function testInitializePropertiesBeforeMethodCalls()
- {
- $container = new ContainerBuilder();
- $container->register('foo', 'stdClass');
- $container->register('bar', 'MethodCallClass')
- ->setProperty('simple', 'bar')
- ->setProperty('complex', new Reference('foo'))
- ->addMethodCall('callMe');
- $container->compile();
- $this->assertTrue($container->get('bar')->callPassed(), '->compile() initializes properties before method calls');
- }
- public function testAutowiring()
- {
- $container = new ContainerBuilder();
- $container->register('a', __NAMESPACE__.'\A');
- $bDefinition = $container->register('b', __NAMESPACE__.'\B');
- $bDefinition->setAutowired(true);
- $container->compile();
- $this->assertEquals('a', (string) $container->getDefinition('b')->getArgument(0));
- }
- /**
- * This test checks the trigger of a deprecation note and should not be removed in major releases.
- *
- * @group legacy
- * @expectedDeprecation The "foo" service is deprecated. You should stop using it, as it will soon be removed.
- */
- public function testPrivateServiceTriggersDeprecation()
- {
- $container = new ContainerBuilder();
- $container->register('foo', 'stdClass')
- ->setPublic(false)
- ->setDeprecated(true);
- $container->register('bar', 'stdClass')
- ->setPublic(true)
- ->setProperty('foo', new Reference('foo'));
- $container->compile();
- $container->get('bar');
- }
- }
- class FooClass
- {
- }
- class ProjectContainer extends ContainerBuilder
- {
- public function getFoobazService()
- {
- throw new InactiveScopeException('foo', 'request');
- }
- }
- class A
- {
- }
- class B
- {
- public function __construct(A $a)
- {
- }
- }
|