2010-03-18 07:48:03 +00:00
< ? php
/*
2010-04-25 16:06:54 +01:00
* This file is part of the Symfony package .
2011-01-15 13:29:43 +00:00
*
2011-03-06 11:40:06 +00:00
* ( c ) Fabien Potencier < fabien @ symfony . com >
2010-04-07 02:07:59 +01:00
*
2010-03-18 07:48:03 +00:00
* For the full copyright and license information , please view the LICENSE
* file that was distributed with this source code .
*/
2012-03-28 14:43:52 +01:00
namespace Symfony\Component\DependencyInjection\Tests ;
2010-03-18 07:48:03 +00:00
2017-02-08 07:24:27 +00:00
use PHPUnit\Framework\TestCase ;
2010-08-20 22:09:55 +01:00
use Symfony\Component\DependencyInjection\Container ;
use Symfony\Component\DependencyInjection\ContainerInterface ;
2013-03-21 16:34:16 +00:00
use Symfony\Component\DependencyInjection\Exception\InactiveScopeException ;
2018-07-26 10:03:18 +01:00
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag ;
use Symfony\Component\DependencyInjection\Scope ;
2010-03-18 07:48:03 +00:00
2017-02-08 07:24:27 +00:00
class ContainerTest extends TestCase
2010-03-18 07:48:03 +00:00
{
2010-05-06 12:25:53 +01:00
public function testConstructor ()
{
$sc = new Container ();
2010-11-20 01:09:20 +00:00
$this -> assertSame ( $sc , $sc -> get ( 'service_container' ), '__construct() automatically registers itself as a service' );
2010-03-18 07:48:03 +00:00
2010-06-27 17:28:29 +01:00
$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' );
2010-05-06 12:25:53 +01:00
}
2010-03-18 07:48:03 +00:00
2013-07-21 10:38:59 +01:00
/**
* @ 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_' ),
2013-11-25 14:21:26 +00:00
array ( 'foo\bar' , 'Foo_Bar' ),
2013-07-21 10:38:59 +01:00
);
}
2014-08-25 13:14:37 +01:00
/**
* @ 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.' ),
);
}
2011-02-19 18:30:12 +00:00
public function testCompile ()
2010-05-06 12:25:53 +01:00
{
2010-06-27 17:28:29 +01:00
$sc = new Container ( new ParameterBag ( array ( 'foo' => 'bar' )));
2014-08-25 13:14:37 +01:00
$this -> assertFalse ( $sc -> getParameterBag () -> isResolved (), '->compile() resolves the parameter bag' );
2011-01-16 07:12:36 +00:00
$sc -> compile ();
2014-08-25 13:14:37 +01:00
$this -> assertTrue ( $sc -> getParameterBag () -> isResolved (), '->compile() resolves the parameter bag' );
2011-01-16 07:12:36 +00:00
$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' );
2010-06-27 17:28:29 +01:00
}
2010-03-18 07:48:03 +00:00
2010-06-27 17:28:29 +01:00
public function testIsFrozen ()
{
$sc = new Container ( new ParameterBag ( array ( 'foo' => 'bar' )));
$this -> assertFalse ( $sc -> isFrozen (), '->isFrozen() returns false if the parameters are not frozen' );
2011-01-16 07:12:36 +00:00
$sc -> compile ();
2010-06-27 17:28:29 +01:00
$this -> assertTrue ( $sc -> isFrozen (), '->isFrozen() returns true if the parameters are frozen' );
}
2010-03-18 07:48:03 +00:00
2010-06-27 17:28:29 +01:00
public function testGetParameterBag ()
{
$sc = new Container ();
$this -> assertEquals ( array (), $sc -> getParameterBag () -> all (), '->getParameterBag() returns an empty array if no parameter has been defined' );
2010-03-18 07:48:03 +00:00
}
2010-05-06 12:25:53 +01:00
public function testGetSetParameter ()
2010-03-18 07:48:03 +00:00
{
2010-06-27 17:28:29 +01:00
$sc = new Container ( new ParameterBag ( array ( 'foo' => 'bar' )));
2010-05-06 12:25:53 +01:00
$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' );
2010-05-07 15:09:11 +01:00
try {
2010-05-06 12:25:53 +01:00
$sc -> getParameter ( 'baba' );
$this -> fail ( '->getParameter() thrown an \InvalidArgumentException if the key does not exist' );
2010-05-07 15:09:11 +01:00
} catch ( \Exception $e ) {
2010-05-06 12:25:53 +01:00
$this -> assertInstanceOf ( '\InvalidArgumentException' , $e , '->getParameter() thrown an \InvalidArgumentException if the key does not exist' );
2011-04-26 11:31:40 +01:00
$this -> assertEquals ( 'You have requested a non-existent parameter "baba".' , $e -> getMessage (), '->getParameter() thrown an \InvalidArgumentException if the key does not exist' );
2010-05-06 12:25:53 +01:00
}
2010-03-18 07:48:03 +00:00
}
2010-05-06 12:25:53 +01:00
2010-06-27 17:28:29 +01:00
public function testGetServiceIds ()
2010-03-18 07:48:03 +00:00
{
2010-06-27 17:28:29 +01:00
$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' );
2010-05-06 12:25:53 +01:00
2010-06-27 17:28:29 +01:00
$sc = new ProjectServiceContainer ();
2014-08-25 13:14:37 +01:00
$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()' );
2010-03-18 07:48:03 +00:00
}
2010-06-27 17:28:29 +01:00
public function testSet ()
2010-03-18 07:48:03 +00:00
{
2010-05-06 12:25:53 +01:00
$sc = new Container ();
2017-07-08 09:28:14 +01:00
$sc -> set ( '._. \\o/' , $foo = new \stdClass ());
$this -> assertSame ( $foo , $sc -> get ( '._. \\o/' ), '->set() sets a service' );
2010-06-27 17:28:29 +01:00
}
2010-05-06 12:25:53 +01:00
2013-07-25 16:13:34 +01:00
public function testSetWithNullResetTheService ()
{
$sc = new Container ();
$sc -> set ( 'foo' , null );
2014-08-25 13:14:37 +01:00
$this -> assertFalse ( $sc -> has ( 'foo' ), '->set() with null service resets the service' );
2013-07-25 16:13:34 +01:00
}
2011-01-17 22:28:59 +00:00
/**
* @ expectedException \InvalidArgumentException
2015-06-15 03:41:59 +01:00
* @ group legacy
2011-01-17 22:28:59 +00:00
*/
public function testSetDoesNotAllowPrototypeScope ()
{
$c = new Container ();
2014-08-25 13:14:37 +01:00
$c -> set ( 'foo' , new \stdClass (), Container :: SCOPE_PROTOTYPE );
2011-01-17 22:28:59 +00:00
}
/**
* @ expectedException \RuntimeException
2015-06-15 03:41:59 +01:00
* @ group legacy
2011-01-17 22:28:59 +00:00
*/
public function testSetDoesNotAllowInactiveScope ()
{
$c = new Container ();
2011-02-13 09:33:53 +00:00
$c -> addScope ( new Scope ( 'foo' ));
2011-01-17 22:28:59 +00:00
$c -> set ( 'foo' , new \stdClass (), 'foo' );
}
2015-06-15 03:41:59 +01:00
/**
* @ group legacy
*/
2011-01-17 22:28:59 +00:00
public function testSetAlsoSetsScopedService ()
{
$c = new Container ();
2011-02-13 09:33:53 +00:00
$c -> addScope ( new Scope ( 'foo' ));
2011-01-17 22:28:59 +00:00
$c -> enterScope ( 'foo' );
$c -> set ( 'foo' , $foo = new \stdClass (), 'foo' );
2014-08-25 13:14:37 +01:00
$scoped = $this -> getField ( $c , 'scopedServices' );
2017-12-11 21:55:31 +00:00
$this -> assertArrayHasKey ( 'foo' , $scoped [ 'foo' ], '->set() sets a scoped service' );
2014-08-25 13:14:37 +01:00
$this -> assertSame ( $foo , $scoped [ 'foo' ][ 'foo' ], '->set() sets a scoped service' );
}
2015-06-15 03:41:59 +01:00
/**
* @ group legacy
*/
2014-08-25 13:14:37 +01:00
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' );
2011-01-17 22:28:59 +00:00
}
2016-02-09 19:58:01 +00:00
public function testSetReplacesAlias ()
{
$c = new ProjectServiceContainer ();
$c -> set ( 'alias' , $foo = new \stdClass ());
$this -> assertSame ( $foo , $c -> get ( 'alias' ), '->set() replaces an existing alias' );
}
2010-06-27 17:28:29 +01:00
public function testGet ()
{
2010-05-06 12:25:53 +01:00
$sc = new ProjectServiceContainer ();
2010-06-27 17:28:29 +01:00
$sc -> set ( 'foo' , $foo = new \stdClass ());
2017-01-10 11:14:08 +00:00
$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' );
2010-05-06 12:25:53 +01:00
2010-06-27 17:28:29 +01:00
$sc -> set ( 'bar' , $bar = new \stdClass ());
2017-01-10 11:14:08 +00:00
$this -> assertSame ( $bar , $sc -> get ( 'bar' ), '->get() prefers to return a service defined with set() than one defined with a getXXXMethod()' );
2010-05-06 12:25:53 +01:00
2010-05-07 15:09:11 +01:00
try {
2010-06-27 17:28:29 +01:00
$sc -> get ( '' );
$this -> fail ( '->get() throws a \InvalidArgumentException exception if the service is empty' );
2010-05-07 15:09:11 +01:00
} catch ( \Exception $e ) {
2011-05-17 15:26:08 +01:00
$this -> assertInstanceOf ( 'Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException' , $e , '->get() throws a ServiceNotFoundException exception if the service is empty' );
2010-05-06 12:25:53 +01:00
}
2014-08-25 13:14:37 +01:00
$this -> assertNull ( $sc -> get ( '' , ContainerInterface :: NULL_ON_INVALID_REFERENCE ), '->get() returns null if the service is empty' );
2010-03-18 07:48:03 +00:00
}
2013-04-30 18:55:25 +01:00
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' );
2013-05-01 05:24:56 +01:00
$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' );
2013-04-30 18:55:25 +01:00
}
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' );
2013-05-01 05:24:56 +01:00
$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' );
2013-04-30 18:55:25 +01:00
}
}
2011-02-19 18:30:12 +00:00
public function testGetCircularReference ()
{
$sc = new ProjectServiceContainer ();
try {
$sc -> get ( 'circular' );
2011-05-25 08:46:30 +01:00
$this -> fail ( '->get() throws a ServiceCircularReferenceException if it contains circular reference' );
2011-02-19 18:30:12 +00:00
} catch ( \Exception $e ) {
2011-05-25 08:46:30 +01:00
$this -> assertInstanceOf ( '\Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException' , $e , '->get() throws a ServiceCircularReferenceException if it contains circular reference' );
2011-02-19 18:30:12 +00:00
$this -> assertStringStartsWith ( 'Circular reference detected for service "circular"' , $e -> getMessage (), '->get() throws a \LogicException if it contains circular reference' );
}
}
2013-03-21 16:34:16 +00:00
/**
2015-06-15 03:41:59 +01:00
* @ group legacy
2013-03-21 16:34:16 +00:00
*/
public function testGetReturnsNullOnInactiveScope ()
{
$sc = new ProjectServiceContainer ();
$this -> assertNull ( $sc -> get ( 'inactive' , ContainerInterface :: NULL_ON_INVALID_REFERENCE ));
}
2016-09-05 08:18:51 +01:00
/**
* @ expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
2016-09-06 14:11:47 +01:00
* @ expectedExceptionMessage You have requested a synthetic service ( " request " ) . The DIC does not know how to construct this service .
2016-09-05 08:18:51 +01:00
*/
public function testGetSyntheticServiceAlwaysThrows ()
{
require_once __DIR__ . '/Fixtures/php/services9.php' ;
$container = new \ProjectServiceContainer ();
$container -> get ( 'request' , ContainerInterface :: NULL_ON_INVALID_REFERENCE );
}
2010-06-27 17:28:29 +01:00
public function testHas ()
2010-03-18 07:48:03 +00:00
{
2010-06-27 17:28:29 +01:00
$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' );
2013-12-20 21:29:30 +00:00
$this -> assertTrue ( $sc -> has ( 'foo\\baz' ), '->has() returns true if a get*Method() is defined' );
2010-03-18 07:48:03 +00:00
}
2011-01-17 22:28:59 +00:00
2012-04-18 18:26:58 +01:00
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' );
2014-11-25 09:00:53 +00:00
$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' );
2012-04-18 18:26:58 +01:00
}
2015-07-02 23:05:34 +01:00
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 ));
}
2015-06-15 03:41:59 +01:00
/**
* @ group legacy
*/
2011-01-17 22:28:59 +00:00
public function testEnterLeaveCurrentScope ()
{
$container = new ProjectServiceContainer ();
2011-02-13 09:33:53 +00:00
$container -> addScope ( new Scope ( 'foo' ));
2011-01-17 22:28:59 +00:00
$container -> enterScope ( 'foo' );
2016-09-05 08:18:51 +01:00
$container -> set ( 'foo' , new \stdClass (), 'foo' );
2011-01-17 22:28:59 +00:00
$scoped1 = $container -> get ( 'scoped' );
$scopedFoo1 = $container -> get ( 'scoped_foo' );
$container -> enterScope ( 'foo' );
2016-09-05 08:18:51 +01:00
$container -> set ( 'foo' , new \stdClass (), 'foo' );
2011-01-17 22:28:59 +00:00
$scoped2 = $container -> get ( 'scoped' );
2015-05-15 13:54:35 +01:00
$scoped3 = $container -> get ( 'SCOPED' );
2011-01-17 22:28:59 +00:00
$scopedFoo2 = $container -> get ( 'scoped_foo' );
2016-09-05 08:18:51 +01:00
$container -> set ( 'foo' , null , 'foo' );
2011-01-17 22:28:59 +00:00
$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 );
}
2015-06-15 03:41:59 +01:00
/**
* @ group legacy
*/
2011-01-17 22:28:59 +00:00
public function testEnterLeaveScopeWithChildScopes ()
{
$container = new Container ();
2011-02-13 09:33:53 +00:00
$container -> addScope ( new Scope ( 'foo' ));
$container -> addScope ( new Scope ( 'bar' , 'foo' ));
2011-01-17 22:28:59 +00:00
$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' );
2014-08-25 13:14:37 +01:00
$scoped = $this -> getField ( $container , 'scopedServices' );
2017-12-11 21:55:31 +00:00
$this -> assertArrayHasKey ( 'a' , $scoped [ 'bar' ]);
2014-08-25 13:14:37 +01:00
$this -> assertSame ( $a , $scoped [ 'bar' ][ 'a' ]);
2011-01-17 22:28:59 +00:00
$this -> assertTrue ( $container -> has ( 'a' ));
2014-08-25 13:14:37 +01:00
$container -> leaveScope ( 'foo' );
2011-01-17 22:28:59 +00:00
2014-08-25 13:14:37 +01:00
$scoped = $this -> getField ( $container , 'scopedServices' );
2017-12-11 21:55:31 +00:00
$this -> assertArrayNotHasKey ( 'bar' , $scoped );
2011-01-17 22:28:59 +00:00
$this -> assertFalse ( $container -> isScopeActive ( 'foo' ));
$this -> assertFalse ( $container -> has ( 'a' ));
}
2015-06-15 03:41:59 +01:00
/**
* @ group legacy
*/
2012-11-26 14:59:46 +00:00
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' );
2014-08-25 13:14:37 +01:00
$scoped = $this -> getField ( $container , 'scopedServices' );
2017-12-11 21:55:31 +00:00
$this -> assertArrayHasKey ( 'a' , $scoped [ 'foo' ]);
2014-08-25 13:14:37 +01:00
$this -> assertSame ( $a , $scoped [ 'foo' ][ 'a' ]);
2012-11-26 14:59:46 +00:00
$this -> assertTrue ( $container -> has ( 'a' ));
2014-08-25 13:14:37 +01:00
2012-11-26 14:59:46 +00:00
$container -> enterScope ( 'foo' );
2014-08-25 13:14:37 +01:00
$scoped = $this -> getField ( $container , 'scopedServices' );
2017-12-11 21:55:31 +00:00
$this -> assertArrayNotHasKey ( 'a' , $scoped );
2014-08-25 13:14:37 +01:00
$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' );
2012-11-26 14:59:46 +00:00
$this -> assertTrue ( $container -> isScopeActive ( 'foo' ));
$this -> assertFalse ( $container -> isScopeActive ( 'bar' ));
2014-08-25 13:14:37 +01:00
$this -> assertTrue ( $container -> has ( 'a' ));
}
2015-06-15 03:41:59 +01:00
/**
* @ group legacy
*/
2014-08-25 13:14:37 +01:00
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' );
2017-12-11 21:55:31 +00:00
$this -> assertArrayHasKey ( 'a' , $scoped [ 'bar' ]);
2014-08-25 13:14:37 +01:00
$this -> assertSame ( $a , $scoped [ 'bar' ][ 'a' ]);
$this -> assertTrue ( $container -> has ( 'a' ));
$container -> enterScope ( 'bar' );
$scoped = $this -> getField ( $container , 'scopedServices' );
2017-12-11 21:55:31 +00:00
$this -> assertArrayNotHasKey ( 'a' , $scoped );
2014-08-25 13:14:37 +01:00
$this -> assertTrue ( $container -> isScopeActive ( 'foo' ));
$this -> assertTrue ( $container -> isScopeActive ( 'bar' ));
2012-11-26 14:59:46 +00:00
$this -> assertFalse ( $container -> has ( 'a' ));
2014-08-25 13:14:37 +01:00
$container -> leaveScope ( 'bar' );
$this -> assertTrue ( $container -> isScopeActive ( 'foo' ));
$this -> assertTrue ( $container -> isScopeActive ( 'bar' ));
$this -> assertTrue ( $container -> has ( 'a' ));
}
/**
* @ expectedException \InvalidArgumentException
2015-06-15 03:41:59 +01:00
* @ group legacy
2014-08-25 13:14:37 +01:00
*/
public function testEnterScopeNotAdded ()
{
2014-09-22 12:59:59 +01:00
$container = new Container ();
$container -> enterScope ( 'foo' );
2014-08-25 13:14:37 +01:00
}
/**
* @ expectedException \RuntimeException
2015-06-15 03:41:59 +01:00
* @ group legacy
2014-08-25 13:14:37 +01:00
*/
public function testEnterScopeDoesNotAllowInactiveParentScope ()
{
$container = new Container ();
$container -> addScope ( new Scope ( 'foo' ));
$container -> addScope ( new Scope ( 'bar' , 'foo' ));
$container -> enterScope ( 'bar' );
2012-11-26 14:59:46 +00:00
}
2015-06-15 03:41:59 +01:00
/**
* @ group legacy
*/
2011-02-19 18:30:12 +00:00
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' );
}
}
2011-01-17 22:28:59 +00:00
/**
* @ expectedException \InvalidArgumentException
2015-06-15 03:41:59 +01:00
* @ dataProvider getLegacyBuiltInScopes
* @ group legacy
2011-01-17 22:28:59 +00:00
*/
public function testAddScopeDoesNotAllowBuiltInScopes ( $scope )
{
$container = new Container ();
2011-02-13 09:33:53 +00:00
$container -> addScope ( new Scope ( $scope ));
2011-01-17 22:28:59 +00:00
}
/**
* @ expectedException \InvalidArgumentException
2015-06-15 03:41:59 +01:00
* @ group legacy
2011-01-17 22:28:59 +00:00
*/
public function testAddScopeDoesNotAllowExistingScope ()
{
$container = new Container ();
2011-02-13 09:33:53 +00:00
$container -> addScope ( new Scope ( 'foo' ));
$container -> addScope ( new Scope ( 'foo' ));
2011-01-17 22:28:59 +00:00
}
/**
* @ expectedException \InvalidArgumentException
2015-06-15 03:41:59 +01:00
* @ dataProvider getLegacyInvalidParentScopes
* @ group legacy
2011-01-17 22:28:59 +00:00
*/
public function testAddScopeDoesNotAllowInvalidParentScope ( $scope )
{
$c = new Container ();
2011-02-13 09:33:53 +00:00
$c -> addScope ( new Scope ( 'foo' , $scope ));
2011-01-17 22:28:59 +00:00
}
2015-06-15 03:41:59 +01:00
/**
* @ group legacy
*/
2011-01-17 22:28:59 +00:00
public function testAddScope ()
{
$c = new Container ();
2011-02-13 09:33:53 +00:00
$c -> addScope ( new Scope ( 'foo' ));
$c -> addScope ( new Scope ( 'bar' , 'foo' ));
2011-01-17 22:28:59 +00:00
$this -> assertSame ( array ( 'foo' => 'container' , 'bar' => 'foo' ), $this -> getField ( $c , 'scopes' ));
$this -> assertSame ( array ( 'foo' => array ( 'bar' ), 'bar' => array ()), $this -> getField ( $c , 'scopeChildren' ));
2014-08-25 13:14:37 +01:00
$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' ));
2011-01-17 22:28:59 +00:00
}
2015-06-15 03:41:59 +01:00
/**
* @ group legacy
*/
2011-01-17 22:28:59 +00:00
public function testHasScope ()
{
$c = new Container ();
$this -> assertFalse ( $c -> hasScope ( 'foo' ));
2011-02-13 09:33:53 +00:00
$c -> addScope ( new Scope ( 'foo' ));
2011-01-17 22:28:59 +00:00
$this -> assertTrue ( $c -> hasScope ( 'foo' ));
}
2014-08-25 13:14:37 +01:00
/**
2015-09-08 23:15:02 +01:00
* @ expectedException \Exception
2014-08-25 13:14:37 +01:00
* @ expectedExceptionMessage Something went terribly wrong !
*/
2011-03-09 14:38:02 +00:00
public function testGetThrowsException ()
{
$c = new ProjectServiceContainer ();
try {
$c -> get ( 'throw_exception' );
} catch ( \Exception $e ) {
2014-08-25 13:14:37 +01:00
// Do nothing.
2011-03-09 14:38:02 +00:00
}
2014-08-25 13:14:37 +01:00
// Retry, to make sure that get*Service() will be called.
$c -> get ( 'throw_exception' );
2011-03-09 14:38:02 +00:00
}
2012-10-07 19:46:50 +01:00
public function testGetThrowsExceptionOnServiceConfiguration ()
{
$c = new ProjectServiceContainer ();
try {
$c -> get ( 'throws_exception_on_service_configuration' );
} catch ( \Exception $e ) {
2014-08-25 13:14:37 +01:00
// Do nothing.
2012-10-07 19:46:50 +01:00
}
2014-08-25 13:14:37 +01:00
2012-10-11 17:31:20 +01:00
$this -> assertFalse ( $c -> initialized ( 'throws_exception_on_service_configuration' ));
2012-10-07 19:46:50 +01:00
2014-08-25 13:14:37 +01:00
// Retry, to make sure that get*Service() will be called.
2012-10-07 19:46:50 +01:00
try {
$c -> get ( 'throws_exception_on_service_configuration' );
} catch ( \Exception $e ) {
2014-08-25 13:14:37 +01:00
// Do nothing.
2012-10-07 19:46:50 +01:00
}
2012-10-11 17:31:20 +01:00
$this -> assertFalse ( $c -> initialized ( 'throws_exception_on_service_configuration' ));
2012-10-07 19:46:50 +01:00
}
2015-06-15 03:41:59 +01:00
/**
* @ group legacy
*/
2014-08-25 13:14:37 +01:00
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' ));
}
2015-06-15 03:41:59 +01:00
public function getLegacyInvalidParentScopes ()
2011-01-17 22:28:59 +00:00
{
return array (
array ( ContainerInterface :: SCOPE_PROTOTYPE ),
array ( 'bar' ),
);
}
2015-06-15 03:41:59 +01:00
public function getLegacyBuiltInScopes ()
2011-01-17 22:28:59 +00:00
{
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 );
}
2013-06-12 11:05:49 +01:00
public function testAlias ()
{
$c = new ProjectServiceContainer ();
$this -> assertTrue ( $c -> has ( 'alias' ));
$this -> assertSame ( $c -> get ( 'alias' ), $c -> get ( 'bar' ));
}
2015-07-16 09:25:41 +01:00
public function testThatCloningIsNotSupported ()
{
$class = new \ReflectionClass ( 'Symfony\Component\DependencyInjection\Container' );
$clone = $class -> getMethod ( '__clone' );
2017-06-02 06:32:06 +01:00
if ( \PHP_VERSION_ID >= 50400 ) {
2015-07-16 09:25:41 +01:00
$this -> assertFalse ( $class -> isCloneable ());
}
$this -> assertTrue ( $clone -> isPrivate ());
}
2010-05-06 12:25:53 +01:00
}
2010-03-18 07:48:03 +00:00
2010-05-06 12:25:53 +01:00
class ProjectServiceContainer extends Container
{
2016-06-25 17:29:20 +01:00
public $__bar ;
public $__foo_bar ;
public $__foo_baz ;
2014-08-25 13:14:37 +01:00
public $synchronized ;
2010-03-18 07:48:03 +00:00
2010-05-06 12:25:53 +01:00
public function __construct ()
2010-03-18 07:48:03 +00:00
{
2010-05-06 12:25:53 +01:00
parent :: __construct ();
$this -> __bar = new \stdClass ();
$this -> __foo_bar = new \stdClass ();
$this -> __foo_baz = new \stdClass ();
2014-08-25 13:14:37 +01:00
$this -> synchronized = false ;
2013-06-12 11:05:49 +01:00
$this -> aliases = array ( 'alias' => 'bar' );
2010-03-18 07:48:03 +00:00
}
2010-05-06 12:25:53 +01:00
2011-01-17 22:28:59 +00:00
protected function getScopedService ()
{
2014-08-25 13:14:37 +01:00
if ( ! $this -> isScopeActive ( 'foo' )) {
2011-01-17 22:28:59 +00:00
throw new \RuntimeException ( 'Invalid call' );
}
return $this -> services [ 'scoped' ] = $this -> scopedServices [ 'foo' ][ 'scoped' ] = new \stdClass ();
}
protected function getScopedFooService ()
{
2014-08-25 13:14:37 +01:00
if ( ! $this -> isScopeActive ( 'foo' )) {
2011-01-17 22:28:59 +00:00
throw new \RuntimeException ( 'invalid call' );
}
return $this -> services [ 'scoped_foo' ] = $this -> scopedServices [ 'foo' ][ 'scoped_foo' ] = new \stdClass ();
}
2014-08-25 13:14:37 +01:00
protected function getScopedSynchronizedFooService ()
{
if ( ! $this -> isScopeActive ( 'foo' )) {
throw new \RuntimeException ( 'invalid call' );
}
return $this -> services [ 'scoped_bar' ] = $this -> scopedServices [ 'foo' ][ 'scoped_bar' ] = new \stdClass ();
}
2016-09-05 08:18:51 +01:00
protected function synchronizeFooService ()
{
// Typically get the service to pass it to a setter
$this -> get ( 'foo' );
}
2014-08-25 13:14:37 +01:00
protected function synchronizeScopedSynchronizedFooService ()
{
$this -> synchronized = true ;
}
2013-03-21 16:34:16 +00:00
protected function getInactiveService ()
{
throw new InactiveScopeException ( 'request' , 'request' );
}
2010-05-06 12:25:53 +01:00
protected function getBarService ()
2010-03-18 07:48:03 +00:00
{
2010-05-06 12:25:53 +01:00
return $this -> __bar ;
2010-03-18 07:48:03 +00:00
}
2010-03-25 13:04:48 +00:00
2010-05-06 12:25:53 +01:00
protected function getFooBarService ()
2010-03-25 13:04:48 +00:00
{
2010-05-06 12:25:53 +01:00
return $this -> __foo_bar ;
2010-03-25 13:04:48 +00:00
}
2010-05-06 12:25:53 +01:00
protected function getFoo_BazService ()
2010-03-25 13:04:48 +00:00
{
2010-05-06 12:25:53 +01:00
return $this -> __foo_baz ;
2010-03-25 13:04:48 +00:00
}
2011-02-19 18:30:12 +00:00
2011-02-19 18:33:39 +00:00
protected function getCircularService ()
{
2011-02-19 18:30:12 +00:00
return $this -> get ( 'circular' );
}
2011-03-09 14:38:02 +00:00
protected function getThrowExceptionService ()
{
throw new \Exception ( 'Something went terribly wrong!' );
}
2012-10-07 19:46:50 +01:00
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!' );
}
2010-03-18 07:48:03 +00:00
}