namespace Symfony\Component\DependencyInjection\Tests;
use PHPUnit\Framework\TestCase;
-use Symfony\Component\DependencyInjection\Scope;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
-use Symfony\Component\DependencyInjection\Exception\InactiveScopeException;
class ContainerTest extends TestCase
{
$sc = new ProjectServiceContainer();
$sc->set('foo', $obj = new \stdClass());
- $this->assertEquals(array('scoped', 'scoped_foo', 'scoped_synchronized_foo', 'inactive', 'bar', 'foo_bar', 'foo.baz', 'circular', 'throw_exception', 'throws_exception_on_service_configuration', 'service_container', 'foo'), $sc->getServiceIds(), '->getServiceIds() returns defined service ids by getXXXService() methods, followed by service ids defined by set()');
- }
-
- public function testSet()
- {
- $sc = new Container();
- $sc->set('foo', $foo = new \stdClass());
- $this->assertSame($foo, $sc->get('foo'), '->set() sets a service');
- }
-
- public function testSetWithNullResetTheService()
- {
- $sc = new Container();
- $sc->set('foo', null);
- $this->assertFalse($sc->has('foo'), '->set() with null service resets the service');
+ $this->assertEquals(array('service_container', 'internal', 'bar', 'foo_bar', 'foo.baz', 'circular', 'throw_exception', 'throws_exception_on_service_configuration', 'internal_dependency', 'foo'), $sc->getServiceIds(), '->getServiceIds() returns defined service ids by factory methods in the method map, followed by service ids defined by set()');
}
/**
- * @expectedException \InvalidArgumentException
* @group legacy
+ * @expectedDeprecation Generating a dumped container without populating the method map is deprecated since 3.2 and will be unsupported in 4.0. Update your dumper to generate the method map.
*/
- public function testSetDoesNotAllowPrototypeScope()
+ public function testGetLegacyServiceIds()
{
- $c = new Container();
- $c->set('foo', new \stdClass(), Container::SCOPE_PROTOTYPE);
- }
+ $sc = new LegacyProjectServiceContainer();
+ $sc->set('foo', $obj = new \stdClass());
- /**
- * @expectedException \RuntimeException
- * @group legacy
- */
- public function testSetDoesNotAllowInactiveScope()
- {
- $c = new Container();
- $c->addScope(new Scope('foo'));
- $c->set('foo', new \stdClass(), 'foo');
+ $this->assertEquals(array('internal', 'bar', 'foo_bar', 'foo.baz', 'circular', 'throw_exception', 'throws_exception_on_service_configuration', 'service_container', 'foo'), $sc->getServiceIds(), '->getServiceIds() returns defined service ids by getXXXService() methods, followed by service ids defined by set()');
}
- /**
- * @group legacy
- */
- public function testSetAlsoSetsScopedService()
+ public function testSet()
{
- $c = new Container();
- $c->addScope(new Scope('foo'));
- $c->enterScope('foo');
- $c->set('foo', $foo = new \stdClass(), 'foo');
-
- $scoped = $this->getField($c, 'scopedServices');
- $this->assertTrue(isset($scoped['foo']['foo']), '->set() sets a scoped service');
- $this->assertSame($foo, $scoped['foo']['foo'], '->set() sets a scoped service');
+ $sc = new Container();
+ $sc->set('._. \\o/', $foo = new \stdClass());
+ $this->assertSame($foo, $sc->get('._. \\o/'), '->set() sets a service');
}
- /**
- * @group legacy
- */
- public function testSetAlsoCallsSynchronizeService()
+ public function testSetWithNullResetTheService()
{
- $c = new ProjectServiceContainer();
- $c->addScope(new Scope('foo'));
- $c->enterScope('foo');
- $c->set('scoped_synchronized_foo', $bar = new \stdClass(), 'foo');
- $this->assertTrue($c->synchronized, '->set() calls synchronize*Service() if it is defined for the service');
+ $sc = new Container();
+ $sc->set('foo', null);
+ $this->assertFalse($sc->has('foo'), '->set() with null service resets the service');
}
public function testSetReplacesAlias()
{
$sc = new ProjectServiceContainer();
$sc->set('foo', $foo = new \stdClass());
+ $this->assertSame($foo, $sc->get('foo'), '->get() returns the service for the given id');
+ $this->assertSame($foo, $sc->get('Foo'), '->get() returns the service for the given id, and converts id to lowercase');
+ $this->assertSame($sc->__bar, $sc->get('bar'), '->get() returns the service for the given id');
+ $this->assertSame($sc->__foo_bar, $sc->get('foo_bar'), '->get() returns the service if a get*Method() is defined');
+ $this->assertSame($sc->__foo_baz, $sc->get('foo.baz'), '->get() returns the service if a get*Method() is defined');
+
+ $sc->set('bar', $bar = new \stdClass());
+ $this->assertSame($bar, $sc->get('bar'), '->get() prefers to return a service defined with set() than one defined with a getXXXMethod()');
+
+ try {
+ $sc->get('');
+ $this->fail('->get() throws a \InvalidArgumentException exception if the service is empty');
+ } catch (\Exception $e) {
+ $this->assertInstanceOf('Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException', $e, '->get() throws a ServiceNotFoundException exception if the service is empty');
+ }
+ $this->assertNull($sc->get('', ContainerInterface::NULL_ON_INVALID_REFERENCE), '->get() returns null if the service is empty');
+ }
+
+ /**
+ * @group legacy
+ * @expectedDeprecation Generating a dumped container without populating the method map is deprecated since 3.2 and will be unsupported in 4.0. Update your dumper to generate the method map.
+ * @expectedDeprecation Generating a dumped container without populating the method map is deprecated since 3.2 and will be unsupported in 4.0. Update your dumper to generate the method map.
+ * @expectedDeprecation Generating a dumped container without populating the method map is deprecated since 3.2 and will be unsupported in 4.0. Update your dumper to generate the method map.
+ * @expectedDeprecation Generating a dumped container without populating the method map is deprecated since 3.2 and will be unsupported in 4.0. Update your dumper to generate the method map.
+ */
+ public function testLegacyGet()
+ {
+ $sc = new LegacyProjectServiceContainer();
+ $sc->set('foo', $foo = new \stdClass());
+
$this->assertSame($foo, $sc->get('foo'), '->get() returns the service for the given id');
$this->assertSame($foo, $sc->get('Foo'), '->get() returns the service for the given id, and converts id to lowercase');
$this->assertSame($sc->__bar, $sc->get('bar'), '->get() returns the service for the given id');
}
}
- /**
- * @group legacy
- */
- public function testGetReturnsNullOnInactiveScope()
- {
- $sc = new ProjectServiceContainer();
- $this->assertNull($sc->get('inactive', ContainerInterface::NULL_ON_INVALID_REFERENCE));
- }
-
/**
* @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
* @expectedExceptionMessage You have requested a synthetic service ("request"). The DIC does not know how to construct this service.
$this->assertTrue($sc->has('bar'), '->has() returns true if a get*Method() is defined');
$this->assertTrue($sc->has('foo_bar'), '->has() returns true if a get*Method() is defined');
$this->assertTrue($sc->has('foo.baz'), '->has() returns true if a get*Method() is defined');
+ }
+
+ /**
+ * @group legacy
+ * @expectedDeprecation Generating a dumped container without populating the method map is deprecated since 3.2 and will be unsupported in 4.0. Update your dumper to generate the method map.
+ * @expectedDeprecation Generating a dumped container without populating the method map is deprecated since 3.2 and will be unsupported in 4.0. Update your dumper to generate the method map.
+ * @expectedDeprecation Generating a dumped container without populating the method map is deprecated since 3.2 and will be unsupported in 4.0. Update your dumper to generate the method map.
+ * @expectedDeprecation Generating a dumped container without populating the method map is deprecated since 3.2 and will be unsupported in 4.0. Update your dumper to generate the method map.
+ */
+ public function testLegacyHas()
+ {
+ $sc = new LegacyProjectServiceContainer();
+ $sc->set('foo', new \stdClass());
+
+ $this->assertFalse($sc->has('foo1'), '->has() returns false if the service does not exist');
+ $this->assertTrue($sc->has('foo'), '->has() returns true if the service exists');
+ $this->assertTrue($sc->has('bar'), '->has() returns true if a get*Method() is defined');
+ $this->assertTrue($sc->has('foo_bar'), '->has() returns true if a get*Method() is defined');
+ $this->assertTrue($sc->has('foo.baz'), '->has() returns true if a get*Method() is defined');
$this->assertTrue($sc->has('foo\\baz'), '->has() returns true if a get*Method() is defined');
}
$this->assertNull($c->get('bar', ContainerInterface::NULL_ON_INVALID_REFERENCE));
}
- /**
- * @expectedException \Symfony\Component\DependencyInjection\Exception\LogicException
- * @expectedExceptionMessage Resetting the container is not allowed when a scope is active.
- * @group legacy
- */
- public function testCannotResetInActiveScope()
- {
- $c = new Container();
- $c->addScope(new Scope('foo'));
- $c->set('bar', new \stdClass());
-
- $c->enterScope('foo');
-
- $c->reset();
- }
-
- /**
- * @group legacy
- */
- public function testResetAfterLeavingScope()
- {
- $c = new Container();
- $c->addScope(new Scope('foo'));
- $c->set('bar', new \stdClass());
-
- $c->enterScope('foo');
- $c->leaveScope('foo');
-
- $c->reset();
-
- $this->assertNull($c->get('bar', ContainerInterface::NULL_ON_INVALID_REFERENCE));
- }
-
- /**
- * @group legacy
- */
- public function testEnterLeaveCurrentScope()
- {
- $container = new ProjectServiceContainer();
- $container->addScope(new Scope('foo'));
-
- $container->enterScope('foo');
- $container->set('foo', new \stdClass(), 'foo');
- $scoped1 = $container->get('scoped');
- $scopedFoo1 = $container->get('scoped_foo');
-
- $container->enterScope('foo');
- $container->set('foo', new \stdClass(), 'foo');
- $scoped2 = $container->get('scoped');
- $scoped3 = $container->get('SCOPED');
- $scopedFoo2 = $container->get('scoped_foo');
-
- $container->set('foo', null, 'foo');
- $container->leaveScope('foo');
- $scoped4 = $container->get('scoped');
- $scopedFoo3 = $container->get('scoped_foo');
-
- $this->assertNotSame($scoped1, $scoped2);
- $this->assertSame($scoped2, $scoped3);
- $this->assertSame($scoped1, $scoped4);
- $this->assertNotSame($scopedFoo1, $scopedFoo2);
- $this->assertSame($scopedFoo1, $scopedFoo3);
- }
-
- /**
- * @group legacy
- */
- public function testEnterLeaveScopeWithChildScopes()
- {
- $container = new Container();
- $container->addScope(new Scope('foo'));
- $container->addScope(new Scope('bar', 'foo'));
-
- $this->assertFalse($container->isScopeActive('foo'));
-
- $container->enterScope('foo');
- $container->enterScope('bar');
-
- $this->assertTrue($container->isScopeActive('foo'));
- $this->assertFalse($container->has('a'));
-
- $a = new \stdClass();
- $container->set('a', $a, 'bar');
-
- $scoped = $this->getField($container, 'scopedServices');
- $this->assertTrue(isset($scoped['bar']['a']));
- $this->assertSame($a, $scoped['bar']['a']);
- $this->assertTrue($container->has('a'));
-
- $container->leaveScope('foo');
-
- $scoped = $this->getField($container, 'scopedServices');
- $this->assertFalse(isset($scoped['bar']));
- $this->assertFalse($container->isScopeActive('foo'));
- $this->assertFalse($container->has('a'));
- }
-
- /**
- * @group legacy
- */
- public function testEnterScopeRecursivelyWithInactiveChildScopes()
- {
- $container = new Container();
- $container->addScope(new Scope('foo'));
- $container->addScope(new Scope('bar', 'foo'));
-
- $this->assertFalse($container->isScopeActive('foo'));
-
- $container->enterScope('foo');
-
- $this->assertTrue($container->isScopeActive('foo'));
- $this->assertFalse($container->isScopeActive('bar'));
- $this->assertFalse($container->has('a'));
-
- $a = new \stdClass();
- $container->set('a', $a, 'foo');
-
- $scoped = $this->getField($container, 'scopedServices');
- $this->assertTrue(isset($scoped['foo']['a']));
- $this->assertSame($a, $scoped['foo']['a']);
- $this->assertTrue($container->has('a'));
-
- $container->enterScope('foo');
-
- $scoped = $this->getField($container, 'scopedServices');
- $this->assertFalse(isset($scoped['a']));
- $this->assertTrue($container->isScopeActive('foo'));
- $this->assertFalse($container->isScopeActive('bar'));
- $this->assertFalse($container->has('a'));
-
- $container->enterScope('bar');
-
- $this->assertTrue($container->isScopeActive('bar'));
-
- $container->leaveScope('foo');
-
- $this->assertTrue($container->isScopeActive('foo'));
- $this->assertFalse($container->isScopeActive('bar'));
- $this->assertTrue($container->has('a'));
- }
-
- /**
- * @group legacy
- */
- public function testEnterChildScopeRecursively()
- {
- $container = new Container();
- $container->addScope(new Scope('foo'));
- $container->addScope(new Scope('bar', 'foo'));
-
- $container->enterScope('foo');
- $container->enterScope('bar');
-
- $this->assertTrue($container->isScopeActive('bar'));
- $this->assertFalse($container->has('a'));
-
- $a = new \stdClass();
- $container->set('a', $a, 'bar');
-
- $scoped = $this->getField($container, 'scopedServices');
- $this->assertTrue(isset($scoped['bar']['a']));
- $this->assertSame($a, $scoped['bar']['a']);
- $this->assertTrue($container->has('a'));
-
- $container->enterScope('bar');
-
- $scoped = $this->getField($container, 'scopedServices');
- $this->assertFalse(isset($scoped['a']));
- $this->assertTrue($container->isScopeActive('foo'));
- $this->assertTrue($container->isScopeActive('bar'));
- $this->assertFalse($container->has('a'));
-
- $container->leaveScope('bar');
-
- $this->assertTrue($container->isScopeActive('foo'));
- $this->assertTrue($container->isScopeActive('bar'));
- $this->assertTrue($container->has('a'));
- }
-
- /**
- * @expectedException \InvalidArgumentException
- * @group legacy
- */
- public function testEnterScopeNotAdded()
- {
- $container = new Container();
- $container->enterScope('foo');
- }
-
- /**
- * @expectedException \RuntimeException
- * @group legacy
- */
- public function testEnterScopeDoesNotAllowInactiveParentScope()
- {
- $container = new Container();
- $container->addScope(new Scope('foo'));
- $container->addScope(new Scope('bar', 'foo'));
- $container->enterScope('bar');
- }
-
- /**
- * @group legacy
- */
- public function testLeaveScopeNotActive()
- {
- $container = new Container();
- $container->addScope(new Scope('foo'));
-
- try {
- $container->leaveScope('foo');
- $this->fail('->leaveScope() throws a \LogicException if the scope is not active yet');
- } catch (\Exception $e) {
- $this->assertInstanceOf('\LogicException', $e, '->leaveScope() throws a \LogicException if the scope is not active yet');
- $this->assertEquals('The scope "foo" is not active.', $e->getMessage(), '->leaveScope() throws a \LogicException if the scope is not active yet');
- }
-
- try {
- $container->leaveScope('bar');
- $this->fail('->leaveScope() throws a \LogicException if the scope does not exist');
- } catch (\Exception $e) {
- $this->assertInstanceOf('\LogicException', $e, '->leaveScope() throws a \LogicException if the scope does not exist');
- $this->assertEquals('The scope "bar" is not active.', $e->getMessage(), '->leaveScope() throws a \LogicException if the scope does not exist');
- }
- }
-
- /**
- * @expectedException \InvalidArgumentException
- * @dataProvider getLegacyBuiltInScopes
- * @group legacy
- */
- public function testAddScopeDoesNotAllowBuiltInScopes($scope)
- {
- $container = new Container();
- $container->addScope(new Scope($scope));
- }
-
- /**
- * @expectedException \InvalidArgumentException
- * @group legacy
- */
- public function testAddScopeDoesNotAllowExistingScope()
- {
- $container = new Container();
- $container->addScope(new Scope('foo'));
- $container->addScope(new Scope('foo'));
- }
-
- /**
- * @expectedException \InvalidArgumentException
- * @dataProvider getLegacyInvalidParentScopes
- * @group legacy
- */
- public function testAddScopeDoesNotAllowInvalidParentScope($scope)
- {
- $c = new Container();
- $c->addScope(new Scope('foo', $scope));
- }
-
- /**
- * @group legacy
- */
- public function testAddScope()
- {
- $c = new Container();
- $c->addScope(new Scope('foo'));
- $c->addScope(new Scope('bar', 'foo'));
-
- $this->assertSame(array('foo' => 'container', 'bar' => 'foo'), $this->getField($c, 'scopes'));
- $this->assertSame(array('foo' => array('bar'), 'bar' => array()), $this->getField($c, 'scopeChildren'));
-
- $c->addScope(new Scope('baz', 'bar'));
-
- $this->assertSame(array('foo' => 'container', 'bar' => 'foo', 'baz' => 'bar'), $this->getField($c, 'scopes'));
- $this->assertSame(array('foo' => array('bar', 'baz'), 'bar' => array('baz'), 'baz' => array()), $this->getField($c, 'scopeChildren'));
- }
-
- /**
- * @group legacy
- */
- public function testHasScope()
- {
- $c = new Container();
-
- $this->assertFalse($c->hasScope('foo'));
- $c->addScope(new Scope('foo'));
- $this->assertTrue($c->hasScope('foo'));
- }
-
/**
* @expectedException \Exception
* @expectedExceptionMessage Something went terribly wrong!
$this->assertFalse($c->initialized('throws_exception_on_service_configuration'));
}
- /**
- * @group legacy
- */
- public function testIsScopeActive()
- {
- $c = new Container();
-
- $this->assertFalse($c->isScopeActive('foo'));
- $c->addScope(new Scope('foo'));
-
- $this->assertFalse($c->isScopeActive('foo'));
- $c->enterScope('foo');
-
- $this->assertTrue($c->isScopeActive('foo'));
- $c->leaveScope('foo');
-
- $this->assertFalse($c->isScopeActive('foo'));
- }
-
- public function getLegacyInvalidParentScopes()
- {
- return array(
- array(ContainerInterface::SCOPE_PROTOTYPE),
- array('bar'),
- );
- }
-
- public function getLegacyBuiltInScopes()
- {
- return array(
- array(ContainerInterface::SCOPE_CONTAINER),
- array(ContainerInterface::SCOPE_PROTOTYPE),
- );
- }
-
protected function getField($obj, $field)
{
$reflection = new \ReflectionProperty($obj, $field);
{
$class = new \ReflectionClass('Symfony\Component\DependencyInjection\Container');
$clone = $class->getMethod('__clone');
- if (\PHP_VERSION_ID >= 50400) {
- $this->assertFalse($class->isCloneable());
- }
+ $this->assertFalse($class->isCloneable());
$this->assertTrue($clone->isPrivate());
}
+
+ /**
+ * @group legacy
+ * @expectedDeprecation Unsetting the "internal" private service is deprecated since Symfony 3.2 and won't be supported anymore in Symfony 4.0.
+ */
+ public function testUnsetInternalPrivateServiceIsDeprecated()
+ {
+ $c = new ProjectServiceContainer();
+ $c->set('internal', null);
+ }
+
+ /**
+ * @group legacy
+ * @expectedDeprecation Setting the "internal" private service is deprecated since Symfony 3.2 and won't be supported anymore in Symfony 4.0. A new public service will be created instead.
+ */
+ public function testChangeInternalPrivateServiceIsDeprecated()
+ {
+ $c = new ProjectServiceContainer();
+ $c->set('internal', $internal = new \stdClass());
+ $this->assertSame($c->get('internal'), $internal);
+ }
+
+ /**
+ * @group legacy
+ * @expectedDeprecation Checking for the existence of the "internal" private service is deprecated since Symfony 3.2 and won't be supported anymore in Symfony 4.0.
+ */
+ public function testCheckExistenceOfAnInternalPrivateServiceIsDeprecated()
+ {
+ $c = new ProjectServiceContainer();
+ $c->get('internal_dependency');
+ $this->assertTrue($c->has('internal'));
+ }
+
+ /**
+ * @group legacy
+ * @expectedDeprecation Requesting the "internal" private service is deprecated since Symfony 3.2 and won't be supported anymore in Symfony 4.0.
+ */
+ public function testRequestAnInternalSharedPrivateServiceIsDeprecated()
+ {
+ $c = new ProjectServiceContainer();
+ $c->get('internal_dependency');
+ $c->get('internal');
+ }
}
class ProjectServiceContainer extends Container
public $__bar;
public $__foo_bar;
public $__foo_baz;
- public $synchronized;
+ public $__internal;
+ protected $methodMap = array(
+ 'internal' => 'getInternalService',
+ 'bar' => 'getBarService',
+ 'foo_bar' => 'getFooBarService',
+ 'foo.baz' => 'getFoo_BazService',
+ 'circular' => 'getCircularService',
+ 'throw_exception' => 'getThrowExceptionService',
+ 'throws_exception_on_service_configuration' => 'getThrowsExceptionOnServiceConfigurationService',
+ 'internal_dependency' => 'getInternalDependencyService',
+ );
public function __construct()
{
$this->__bar = new \stdClass();
$this->__foo_bar = new \stdClass();
$this->__foo_baz = new \stdClass();
- $this->synchronized = false;
+ $this->__internal = new \stdClass();
+ $this->privates = array('internal' => true);
$this->aliases = array('alias' => 'bar');
}
- protected function getScopedService()
+ protected function getInternalService()
{
- if (!$this->isScopeActive('foo')) {
- throw new \RuntimeException('Invalid call');
- }
+ return $this->services['internal'] = $this->__internal;
+ }
- return $this->services['scoped'] = $this->scopedServices['foo']['scoped'] = new \stdClass();
+ protected function getBarService()
+ {
+ return $this->__bar;
}
- protected function getScopedFooService()
+ protected function getFooBarService()
{
- if (!$this->isScopeActive('foo')) {
- throw new \RuntimeException('invalid call');
- }
+ return $this->__foo_bar;
+ }
- return $this->services['scoped_foo'] = $this->scopedServices['foo']['scoped_foo'] = new \stdClass();
+ protected function getFoo_BazService()
+ {
+ return $this->__foo_baz;
}
- protected function getScopedSynchronizedFooService()
+ protected function getCircularService()
{
- if (!$this->isScopeActive('foo')) {
- throw new \RuntimeException('invalid call');
- }
+ return $this->get('circular');
+ }
- return $this->services['scoped_bar'] = $this->scopedServices['foo']['scoped_bar'] = new \stdClass();
+ protected function getThrowExceptionService()
+ {
+ throw new \Exception('Something went terribly wrong!');
}
- protected function synchronizeFooService()
+ protected function getThrowsExceptionOnServiceConfigurationService()
{
- // Typically get the service to pass it to a setter
- $this->get('foo');
+ $this->services['throws_exception_on_service_configuration'] = $instance = new \stdClass();
+
+ throw new \Exception('Something was terribly wrong while trying to configure the service!');
}
- protected function synchronizeScopedSynchronizedFooService()
+ protected function getInternalDependencyService()
{
- $this->synchronized = true;
+ $this->services['internal_dependency'] = $instance = new \stdClass();
+
+ $instance->internal = isset($this->services['internal']) ? $this->services['internal'] : $this->getInternalService();
+
+ return $instance;
+ }
+}
+
+class LegacyProjectServiceContainer extends Container
+{
+ public $__bar;
+ public $__foo_bar;
+ public $__foo_baz;
+ public $__internal;
+
+ public function __construct()
+ {
+ parent::__construct();
+
+ $this->__bar = new \stdClass();
+ $this->__foo_bar = new \stdClass();
+ $this->__foo_baz = new \stdClass();
+ $this->__internal = new \stdClass();
+ $this->privates = array('internal' => true);
+ $this->aliases = array('alias' => 'bar');
}
- protected function getInactiveService()
+ protected function getInternalService()
{
- throw new InactiveScopeException('request', 'request');
+ return $this->__internal;
}
protected function getBarService()