123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800 |
- <?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;
- use PHPUnit\Framework\TestCase;
- use Symfony\Component\DependencyInjection\Container;
- use Symfony\Component\DependencyInjection\ContainerInterface;
- use Symfony\Component\DependencyInjection\Exception\InactiveScopeException;
- use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
- use Symfony\Component\DependencyInjection\Scope;
- class ContainerTest extends TestCase
- {
- public function testConstructor()
- {
- $sc = new Container();
- $this->assertSame($sc, $sc->get('service_container'), '__construct() automatically registers itself as a service');
- $sc = new Container(new ParameterBag(array('foo' => 'bar')));
- $this->assertEquals(array('foo' => 'bar'), $sc->getParameterBag()->all(), '__construct() takes an array of parameters as its first argument');
- }
- /**
- * @dataProvider dataForTestCamelize
- */
- public function testCamelize($id, $expected)
- {
- $this->assertEquals($expected, Container::camelize($id), sprintf('Container::camelize("%s")', $id));
- }
- public function dataForTestCamelize()
- {
- return array(
- array('foo_bar', 'FooBar'),
- array('foo.bar', 'Foo_Bar'),
- array('foo.bar_baz', 'Foo_BarBaz'),
- array('foo._bar', 'Foo_Bar'),
- array('foo_.bar', 'Foo_Bar'),
- array('_foo', 'Foo'),
- array('.foo', '_Foo'),
- array('foo_', 'Foo'),
- array('foo.', 'Foo_'),
- array('foo\bar', 'Foo_Bar'),
- );
- }
- /**
- * @dataProvider dataForTestUnderscore
- */
- public function testUnderscore($id, $expected)
- {
- $this->assertEquals($expected, Container::underscore($id), sprintf('Container::underscore("%s")', $id));
- }
- public function dataForTestUnderscore()
- {
- return array(
- array('FooBar', 'foo_bar'),
- array('Foo_Bar', 'foo.bar'),
- array('Foo_BarBaz', 'foo.bar_baz'),
- array('FooBar_BazQux', 'foo_bar.baz_qux'),
- array('_Foo', '.foo'),
- array('Foo_', 'foo.'),
- );
- }
- public function testCompile()
- {
- $sc = new Container(new ParameterBag(array('foo' => 'bar')));
- $this->assertFalse($sc->getParameterBag()->isResolved(), '->compile() resolves the parameter bag');
- $sc->compile();
- $this->assertTrue($sc->getParameterBag()->isResolved(), '->compile() resolves the parameter bag');
- $this->assertInstanceOf('Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag', $sc->getParameterBag(), '->compile() changes the parameter bag to a FrozenParameterBag instance');
- $this->assertEquals(array('foo' => 'bar'), $sc->getParameterBag()->all(), '->compile() copies the current parameters to the new parameter bag');
- }
- public function testIsFrozen()
- {
- $sc = new Container(new ParameterBag(array('foo' => 'bar')));
- $this->assertFalse($sc->isFrozen(), '->isFrozen() returns false if the parameters are not frozen');
- $sc->compile();
- $this->assertTrue($sc->isFrozen(), '->isFrozen() returns true if the parameters are frozen');
- }
- public function testGetParameterBag()
- {
- $sc = new Container();
- $this->assertEquals(array(), $sc->getParameterBag()->all(), '->getParameterBag() returns an empty array if no parameter has been defined');
- }
- public function testGetSetParameter()
- {
- $sc = new Container(new ParameterBag(array('foo' => 'bar')));
- $sc->setParameter('bar', 'foo');
- $this->assertEquals('foo', $sc->getParameter('bar'), '->setParameter() sets the value of a new parameter');
- $sc->setParameter('foo', 'baz');
- $this->assertEquals('baz', $sc->getParameter('foo'), '->setParameter() overrides previously set parameter');
- $sc->setParameter('Foo', 'baz1');
- $this->assertEquals('baz1', $sc->getParameter('foo'), '->setParameter() converts the key to lowercase');
- $this->assertEquals('baz1', $sc->getParameter('FOO'), '->getParameter() converts the key to lowercase');
- try {
- $sc->getParameter('baba');
- $this->fail('->getParameter() thrown an \InvalidArgumentException if the key does not exist');
- } catch (\Exception $e) {
- $this->assertInstanceOf('\InvalidArgumentException', $e, '->getParameter() thrown an \InvalidArgumentException if the key does not exist');
- $this->assertEquals('You have requested a non-existent parameter "baba".', $e->getMessage(), '->getParameter() thrown an \InvalidArgumentException if the key does not exist');
- }
- }
- public function testGetServiceIds()
- {
- $sc = new Container();
- $sc->set('foo', $obj = new \stdClass());
- $sc->set('bar', $obj = new \stdClass());
- $this->assertEquals(array('service_container', 'foo', 'bar'), $sc->getServiceIds(), '->getServiceIds() returns all defined service ids');
- $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('._. \\o/', $foo = new \stdClass());
- $this->assertSame($foo, $sc->get('._. \\o/'), '->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');
- }
- /**
- * @expectedException \InvalidArgumentException
- * @group legacy
- */
- public function testSetDoesNotAllowPrototypeScope()
- {
- $c = new Container();
- $c->set('foo', new \stdClass(), Container::SCOPE_PROTOTYPE);
- }
- /**
- * @expectedException \RuntimeException
- * @group legacy
- */
- public function testSetDoesNotAllowInactiveScope()
- {
- $c = new Container();
- $c->addScope(new Scope('foo'));
- $c->set('foo', new \stdClass(), 'foo');
- }
- /**
- * @group legacy
- */
- public function testSetAlsoSetsScopedService()
- {
- $c = new Container();
- $c->addScope(new Scope('foo'));
- $c->enterScope('foo');
- $c->set('foo', $foo = new \stdClass(), 'foo');
- $scoped = $this->getField($c, 'scopedServices');
- $this->assertArrayHasKey('foo', $scoped['foo'], '->set() sets a scoped service');
- $this->assertSame($foo, $scoped['foo']['foo'], '->set() sets a scoped service');
- }
- /**
- * @group legacy
- */
- public function testSetAlsoCallsSynchronizeService()
- {
- $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');
- }
- public function testSetReplacesAlias()
- {
- $c = new ProjectServiceContainer();
- $c->set('alias', $foo = new \stdClass());
- $this->assertSame($foo, $c->get('alias'), '->set() replaces an existing alias');
- }
- public function testGet()
- {
- $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');
- $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');
- }
- public function testGetThrowServiceNotFoundException()
- {
- $sc = new ProjectServiceContainer();
- $sc->set('foo', $foo = new \stdClass());
- $sc->set('baz', $foo = new \stdClass());
- try {
- $sc->get('foo1');
- $this->fail('->get() throws an Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException if the key does not exist');
- } catch (\Exception $e) {
- $this->assertInstanceOf('Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException', $e, '->get() throws an Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException if the key does not exist');
- $this->assertEquals('You have requested a non-existent service "foo1". Did you mean this: "foo"?', $e->getMessage(), '->get() throws an Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException with some advices');
- }
- try {
- $sc->get('bag');
- $this->fail('->get() throws an Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException if the key does not exist');
- } catch (\Exception $e) {
- $this->assertInstanceOf('Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException', $e, '->get() throws an Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException if the key does not exist');
- $this->assertEquals('You have requested a non-existent service "bag". Did you mean one of these: "bar", "baz"?', $e->getMessage(), '->get() throws an Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException with some advices');
- }
- }
- public function testGetCircularReference()
- {
- $sc = new ProjectServiceContainer();
- try {
- $sc->get('circular');
- $this->fail('->get() throws a ServiceCircularReferenceException if it contains circular reference');
- } catch (\Exception $e) {
- $this->assertInstanceOf('\Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException', $e, '->get() throws a ServiceCircularReferenceException if it contains circular reference');
- $this->assertStringStartsWith('Circular reference detected for service "circular"', $e->getMessage(), '->get() throws a \LogicException if it contains circular reference');
- }
- }
- /**
- * @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.
- */
- public function testGetSyntheticServiceAlwaysThrows()
- {
- require_once __DIR__.'/Fixtures/php/services9.php';
- $container = new \ProjectServiceContainer();
- $container->get('request', ContainerInterface::NULL_ON_INVALID_REFERENCE);
- }
- public function testHas()
- {
- $sc = new ProjectServiceContainer();
- $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');
- }
- public function testInitialized()
- {
- $sc = new ProjectServiceContainer();
- $sc->set('foo', new \stdClass());
- $this->assertTrue($sc->initialized('foo'), '->initialized() returns true if service is loaded');
- $this->assertFalse($sc->initialized('foo1'), '->initialized() returns false if service is not loaded');
- $this->assertFalse($sc->initialized('bar'), '->initialized() returns false if a service is defined, but not currently loaded');
- $this->assertFalse($sc->initialized('alias'), '->initialized() returns false if an aliased service is not initialized');
- $sc->set('bar', new \stdClass());
- $this->assertTrue($sc->initialized('alias'), '->initialized() returns true for alias if aliased service is initialized');
- }
- public function testReset()
- {
- $c = new Container();
- $c->set('bar', new \stdClass());
- $c->reset();
- $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->assertArrayHasKey('a', $scoped['bar']);
- $this->assertSame($a, $scoped['bar']['a']);
- $this->assertTrue($container->has('a'));
- $container->leaveScope('foo');
- $scoped = $this->getField($container, 'scopedServices');
- $this->assertArrayNotHasKey('bar', $scoped);
- $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->assertArrayHasKey('a', $scoped['foo']);
- $this->assertSame($a, $scoped['foo']['a']);
- $this->assertTrue($container->has('a'));
- $container->enterScope('foo');
- $scoped = $this->getField($container, 'scopedServices');
- $this->assertArrayNotHasKey('a', $scoped);
- $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->assertArrayHasKey('a', $scoped['bar']);
- $this->assertSame($a, $scoped['bar']['a']);
- $this->assertTrue($container->has('a'));
- $container->enterScope('bar');
- $scoped = $this->getField($container, 'scopedServices');
- $this->assertArrayNotHasKey('a', $scoped);
- $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!
- */
- public function testGetThrowsException()
- {
- $c = new ProjectServiceContainer();
- try {
- $c->get('throw_exception');
- } catch (\Exception $e) {
- // Do nothing.
- }
- // Retry, to make sure that get*Service() will be called.
- $c->get('throw_exception');
- }
- public function testGetThrowsExceptionOnServiceConfiguration()
- {
- $c = new ProjectServiceContainer();
- try {
- $c->get('throws_exception_on_service_configuration');
- } catch (\Exception $e) {
- // Do nothing.
- }
- $this->assertFalse($c->initialized('throws_exception_on_service_configuration'));
- // Retry, to make sure that get*Service() will be called.
- try {
- $c->get('throws_exception_on_service_configuration');
- } catch (\Exception $e) {
- // Do nothing.
- }
- $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);
- $reflection->setAccessible(true);
- return $reflection->getValue($obj);
- }
- public function testAlias()
- {
- $c = new ProjectServiceContainer();
- $this->assertTrue($c->has('alias'));
- $this->assertSame($c->get('alias'), $c->get('bar'));
- }
- public function testThatCloningIsNotSupported()
- {
- $class = new \ReflectionClass('Symfony\Component\DependencyInjection\Container');
- $clone = $class->getMethod('__clone');
- if (\PHP_VERSION_ID >= 50400) {
- $this->assertFalse($class->isCloneable());
- }
- $this->assertTrue($clone->isPrivate());
- }
- }
- class ProjectServiceContainer extends Container
- {
- public $__bar;
- public $__foo_bar;
- public $__foo_baz;
- public $synchronized;
- public function __construct()
- {
- parent::__construct();
- $this->__bar = new \stdClass();
- $this->__foo_bar = new \stdClass();
- $this->__foo_baz = new \stdClass();
- $this->synchronized = false;
- $this->aliases = array('alias' => 'bar');
- }
- protected function getScopedService()
- {
- if (!$this->isScopeActive('foo')) {
- throw new \RuntimeException('Invalid call');
- }
- return $this->services['scoped'] = $this->scopedServices['foo']['scoped'] = new \stdClass();
- }
- protected function getScopedFooService()
- {
- if (!$this->isScopeActive('foo')) {
- throw new \RuntimeException('invalid call');
- }
- return $this->services['scoped_foo'] = $this->scopedServices['foo']['scoped_foo'] = new \stdClass();
- }
- protected function getScopedSynchronizedFooService()
- {
- if (!$this->isScopeActive('foo')) {
- throw new \RuntimeException('invalid call');
- }
- return $this->services['scoped_bar'] = $this->scopedServices['foo']['scoped_bar'] = new \stdClass();
- }
- protected function synchronizeFooService()
- {
- // Typically get the service to pass it to a setter
- $this->get('foo');
- }
- protected function synchronizeScopedSynchronizedFooService()
- {
- $this->synchronized = true;
- }
- protected function getInactiveService()
- {
- throw new InactiveScopeException('request', 'request');
- }
- protected function getBarService()
- {
- return $this->__bar;
- }
- protected function getFooBarService()
- {
- return $this->__foo_bar;
- }
- protected function getFoo_BazService()
- {
- return $this->__foo_baz;
- }
- protected function getCircularService()
- {
- return $this->get('circular');
- }
- protected function getThrowExceptionService()
- {
- throw new \Exception('Something went terribly wrong!');
- }
- protected function getThrowsExceptionOnServiceConfigurationService()
- {
- $this->services['throws_exception_on_service_configuration'] = $instance = new \stdClass();
- throw new \Exception('Something was terribly wrong while trying to configure the service!');
- }
- }
|