use PHPUnit\Framework\TestCase;
use Symfony\Component\Config\Resource\ResourceInterface;
use Symfony\Component\DependencyInjection\Alias;
+use Symfony\Component\DependencyInjection\Compiler\PassConfig;
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\RuntimeException;
-use Symfony\Component\DependencyInjection\Exception\InactiveScopeException;
-use Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException;
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
use Symfony\Component\DependencyInjection\Loader\ClosureLoader;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
-use Symfony\Component\DependencyInjection\Scope;
+use Symfony\Component\DependencyInjection\ParameterBag\EnvPlaceholderParameterBag;
use Symfony\Component\Config\Resource\FileResource;
use Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition;
use Symfony\Component\ExpressionLanguage\Expression;
$definition->setDeprecated(true);
$builder = new ContainerBuilder();
- $builder->createService($definition, 'deprecated_foo');
+ $builder->setDefinition('deprecated_foo', $definition);
+ $builder->get('deprecated_foo');
}
public function testRegister()
$this->assertTrue($builder->has('bar'), '->has() returns true if a service exists');
}
- public function testGet()
+ /**
+ * @expectedException \Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException
+ * @expectedExceptionMessage You have requested a non-existent service "foo".
+ */
+ public function testGetThrowsExceptionIfServiceDoesNotExist()
+ {
+ $builder = new ContainerBuilder();
+ $builder->get('foo');
+ }
+
+ public function testGetReturnsNullIfServiceDoesNotExistAndInvalidReferenceIsUsed()
{
$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');
+ }
+ /**
+ * @expectedException \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException
+ */
+ public function testGetThrowsCircularReferenceExceptionIfServiceHasReferenceToItself()
+ {
+ $builder = new ContainerBuilder();
+ $builder->register('baz', 'stdClass')->setArguments(array(new Reference('baz')));
+ $builder->get('baz');
+ }
+
+ public function testGetReturnsSameInstanceWhenServiceIsShared()
+ {
+ $builder = new ContainerBuilder();
+ $builder->register('bar', 'stdClass');
+
+ $this->assertTrue($builder->get('bar') === $builder->get('bar'), '->get() always returns the same instance if the service is shared');
+ }
+
+ public function testGetCreatesServiceBasedOnDefinition()
+ {
+ $builder = new ContainerBuilder();
$builder->register('foo', 'stdClass');
+
$this->assertInternalType('object', $builder->get('foo'), '->get() returns the service definition associated with the id');
+ }
+
+ public function testGetReturnsRegisteredService()
+ {
+ $builder = new ContainerBuilder();
$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($bar, $builder->get('bar'), '->get() returns the service associated with the id');
+ }
- $this->assertTrue($builder->get('bar') === $builder->get('bar'), '->get() always returns the same instance if the service is shared');
+ public function testRegisterDoesNotOverrideExistingService()
+ {
+ $builder = new ContainerBuilder();
+ $builder->set('bar', $bar = new \stdClass());
+ $builder->register('bar', 'stdClass');
+
+ $this->assertSame($bar, $builder->get('bar'), '->get() returns the service associated with the id even if a definition has been defined');
}
public function testNonSharedServicesReturnsDifferentInstances()
$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 = new ContainerBuilder();
$builder->setResourceTracking(false);
- $builderCompilerPasses = $builder->getCompiler()->getPassConfig()->getPasses();
- $builder->addCompilerPass($this->getMockBuilder('Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface')->getMock());
+ $defaultPasses = $builder->getCompiler()->getPassConfig()->getPasses();
+ $builder->addCompilerPass($pass1 = $this->getMockBuilder('Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface')->getMock(), PassConfig::TYPE_BEFORE_OPTIMIZATION, -5);
+ $builder->addCompilerPass($pass2 = $this->getMockBuilder('Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface')->getMock(), PassConfig::TYPE_BEFORE_OPTIMIZATION, 10);
- $this->assertCount(count($builder->getCompiler()->getPassConfig()->getPasses()) - 1, $builderCompilerPasses);
+ $passes = $builder->getCompiler()->getPassConfig()->getPasses();
+ $this->assertCount(count($passes) - 2, $defaultPasses);
+ // Pass 1 is executed later
+ $this->assertTrue(array_search($pass1, $passes, true) > array_search($pass2, $passes, true));
}
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('foo1'), '->createService() requires the file defined by the service definition');
$this->assertInstanceOf('\Bar\FooClass', $builder->get('foo2'), '->createService() replaces parameters in the file provided by the service definition');
}
$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 = 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'));
+ $builder->register('foo5', 'Bar\FooClass')->setConfigurator('foo');
+
+ $this->assertTrue($builder->get('foo1')->configured, '->createService() calls the configurator');
+ $this->assertTrue($builder->get('foo2')->configured, '->createService() calls the configurator');
+ $this->assertTrue($builder->get('foo3')->configured, '->createService() calls the configurator');
$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');
$config->setDefinition('foo', new Definition('BazClass'));
$container->merge($config);
$this->assertEquals('BazClass', $container->getDefinition('foo')->getClass(), '->merge() overrides already defined services');
+
+ $container = new ContainerBuilder();
+ $bag = new EnvPlaceholderParameterBag();
+ $bag->get('env(Foo)');
+ $config = new ContainerBuilder($bag);
+ $this->assertSame(array('%env(Bar)%'), $config->resolveEnvPlaceholders(array($bag->get('env(Bar)'))));
+ $container->merge($config);
+ $this->assertEquals(array('Foo' => 0, 'Bar' => 1), $container->getEnvCounters());
}
/**
$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
*/
$this->assertEquals('a', (string) $container->getDefinition('b')->getArgument(0));
}
-}
-class FooClass
-{
+ /**
+ * 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 ProjectContainer extends ContainerBuilder
+class FooClass
{
- public function getFoobazService()
- {
- throw new InactiveScopeException('foo', 'request');
- }
}
class A