123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439 |
- <?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\Tests\Component\DependencyInjection;
-
- use Symfony\Component\DependencyInjection\Scope;
- use Symfony\Component\DependencyInjection\Container;
- use Symfony\Component\DependencyInjection\ContainerInterface;
- use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
-
- class ContainerTest extends \PHPUnit_Framework_TestCase
- {
- /**
- * @covers Symfony\Component\DependencyInjection\Container::__construct
- */
- 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');
- }
-
- /**
- * @covers Symfony\Component\DependencyInjection\Container::compile
- */
- public function testCompile()
- {
- $sc = new Container(new ParameterBag(array('foo' => 'bar')));
- $sc->compile();
- $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');
- }
-
- /**
- * @covers Symfony\Component\DependencyInjection\Container::isFrozen
- */
- 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');
- }
-
- /**
- * @covers Symfony\Component\DependencyInjection\Container::getParameterBag
- */
- public function testGetParameterBag()
- {
- $sc = new Container();
- $this->assertEquals(array(), $sc->getParameterBag()->all(), '->getParameterBag() returns an empty array if no parameter has been defined');
- }
-
- /**
- * @covers Symfony\Component\DependencyInjection\Container::setParameter
- * @covers Symfony\Component\DependencyInjection\Container::getParameter
- */
- 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');
- }
- }
-
- /**
- * @covers Symfony\Component\DependencyInjection\Container::getServiceIds
- */
- 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();
- $this->assertEquals(array('scoped', 'scoped_foo', 'bar', 'foo_bar', 'foo.baz', 'circular', 'throw_exception', 'service_container'), $sc->getServiceIds(), '->getServiceIds() returns defined service ids by getXXXService() methods');
- }
-
- /**
- * @covers Symfony\Component\DependencyInjection\Container::set
- */
- public function testSet()
- {
- $sc = new Container();
- $sc->set('foo', $foo = new \stdClass());
- $this->assertEquals($foo, $sc->get('foo'), '->set() sets a service');
- }
-
- /**
- * @expectedException \InvalidArgumentException
- */
- public function testSetDoesNotAllowPrototypeScope()
- {
- $c = new Container();
- $c->set('foo', new \stdClass(), 'prototype');
- }
-
- /**
- * @expectedException \RuntimeException
- */
- public function testSetDoesNotAllowInactiveScope()
- {
- $c = new Container();
- $c->addScope(new Scope('foo'));
- $c->set('foo', new \stdClass(), 'foo');
- }
-
- public function testSetAlsoSetsScopedService()
- {
- $c = new Container();
- $c->addScope(new Scope('foo'));
- $c->enterScope('foo');
- $c->set('foo', $foo = new \stdClass(), 'foo');
-
- $services = $this->getField($c, 'scopedServices');
- $this->assertTrue(isset($services['foo']['foo']));
- $this->assertSame($foo, $services['foo']['foo']);
- }
-
- /**
- * @covers Symfony\Component\DependencyInjection\Container::get
- */
- public function testGet()
- {
- $sc = new ProjectServiceContainer();
- $sc->set('foo', $foo = new \stdClass());
- $this->assertEquals($foo, $sc->get('foo'), '->get() returns the service for the given id');
- $this->assertEquals($sc->__bar, $sc->get('bar'), '->get() returns the service for the given id');
- $this->assertEquals($sc->__foo_bar, $sc->get('foo_bar'), '->get() returns the service if a get*Method() is defined');
- $this->assertEquals($sc->__foo_baz, $sc->get('foo.baz'), '->get() returns the service if a get*Method() is defined');
-
- $sc->set('bar', $bar = new \stdClass());
- $this->assertEquals($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));
- }
-
- 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');
- }
- }
-
- /**
- * @covers Symfony\Component\DependencyInjection\Container::has
- */
- 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');
- }
-
- public function testEnterLeaveCurrentScope()
- {
- $container = new ProjectServiceContainer();
- $container->addScope(new Scope('foo'));
-
- $container->enterScope('foo');
- $scoped1 = $container->get('scoped');
- $scopedFoo1 = $container->get('scoped_foo');
-
- $container->enterScope('foo');
- $scoped2 = $container->get('scoped');
- $scoped3 = $container->get('scoped');
- $scopedFoo2 = $container->get('scoped_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);
- }
-
- 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');
-
- $services = $this->getField($container, 'scopedServices');
- $this->assertTrue(isset($services['bar']['a']));
- $this->assertSame($a, $services['bar']['a']);
-
- $this->assertTrue($container->has('a'));
- $container->leaveScope('foo');
-
- $services = $this->getField($container, 'scopedServices');
- $this->assertFalse(isset($services['bar']));
-
- $this->assertFalse($container->isScopeActive('foo'));
- $this->assertFalse($container->has('a'));
- }
-
- 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 getBuiltInScopes
- */
- public function testAddScopeDoesNotAllowBuiltInScopes($scope)
- {
- $container = new Container();
- $container->addScope(new Scope($scope));
- }
-
- /**
- * @expectedException \InvalidArgumentException
- */
- public function testAddScopeDoesNotAllowExistingScope()
- {
- $container = new Container();
- $container->addScope(new Scope('foo'));
- $container->addScope(new Scope('foo'));
- }
-
- /**
- * @expectedException \InvalidArgumentException
- * @dataProvider getInvalidParentScopes
- */
- public function testAddScopeDoesNotAllowInvalidParentScope($scope)
- {
- $c = new Container();
- $c->addScope(new Scope('foo', $scope));
- }
-
- 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'));
- }
-
- public function testHasScope()
- {
- $c = new Container();
-
- $this->assertFalse($c->hasScope('foo'));
- $c->addScope(new Scope('foo'));
- $this->assertTrue($c->hasScope('foo'));
- }
-
- 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 testGetThrowsException()
- {
- $c = new ProjectServiceContainer();
-
- try {
- $c->get('throw_exception');
- $this->fail();
- } catch (\Exception $e) {
- $this->assertEquals('Something went terribly wrong!', $e->getMessage());
- }
-
- try {
- $c->get('throw_exception');
- $this->fail();
- } catch (\Exception $e) {
- $this->assertEquals('Something went terribly wrong!', $e->getMessage());
- }
- }
-
- public function getInvalidParentScopes()
- {
- return array(
- array(ContainerInterface::SCOPE_PROTOTYPE),
- array('bar'),
- );
- }
-
- public function getBuiltInScopes()
- {
- 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);
- }
- }
-
- class ProjectServiceContainer extends Container
- {
- public $__bar, $__foo_bar, $__foo_baz;
-
- public function __construct()
- {
- parent::__construct();
-
- $this->__bar = new \stdClass();
- $this->__foo_bar = new \stdClass();
- $this->__foo_baz = new \stdClass();
- }
-
- protected function getScopedService()
- {
- if (!isset($this->scopedServices['foo'])) {
- throw new \RuntimeException('Invalid call');
- }
-
- return $this->services['scoped'] = $this->scopedServices['foo']['scoped'] = new \stdClass();
- }
-
- protected function getScopedFooService()
- {
- if (!isset($this->scopedServices['foo'])) {
- throw new \RuntimeException('invalid call');
- }
-
- return $this->services['scoped_foo'] = $this->scopedServices['foo']['scoped_foo'] = new \stdClass();
- }
-
- 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!');
- }
- }
|