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 .
2010-03-18 07:48:03 +00:00
* ( c ) Fabien Potencier < fabien . potencier @ symfony - project . 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 .
*/
namespace Symfony\Tests\Components\DependencyInjection ;
2010-07-15 14:11:33 +01:00
use Symfony\Components\DependencyInjection\ContainerBuilder ;
2010-06-27 17:28:29 +01:00
use Symfony\Components\DependencyInjection\ContainerInterface ;
2010-03-18 07:48:03 +00:00
use Symfony\Components\DependencyInjection\Definition ;
use Symfony\Components\DependencyInjection\Reference ;
2010-06-27 17:28:29 +01:00
use Symfony\Components\DependencyInjection\ParameterBag\ParameterBag ;
2010-07-15 14:11:33 +01:00
use Symfony\Components\DependencyInjection\Resource\FileResource ;
2010-03-18 07:48:03 +00:00
2010-07-04 17:56:48 +01:00
require_once __DIR__ . '/Fixtures/includes/classes.php' ;
2010-07-15 14:11:33 +01:00
class ContainerBuilderTest extends \PHPUnit_Framework_TestCase
2010-03-18 07:48:03 +00:00
{
2010-06-27 17:28:29 +01:00
/**
2010-07-15 14:11:33 +01:00
* @ covers Symfony\Components\DependencyInjection\ContainerBuilder :: setDefinitions
* @ covers Symfony\Components\DependencyInjection\ContainerBuilder :: getDefinitions
* @ covers Symfony\Components\DependencyInjection\ContainerBuilder :: setDefinition
* @ covers Symfony\Components\DependencyInjection\ContainerBuilder :: getDefinition
2010-06-27 17:28:29 +01:00
*/
2010-05-06 12:25:53 +01:00
public function testDefinitions ()
2010-03-18 07:48:03 +00:00
{
2010-07-15 14:11:33 +01:00
$builder = new ContainerBuilder ();
2010-05-06 12:25:53 +01:00
$definitions = array (
'foo' => new Definition ( 'FooClass' ),
'bar' => new Definition ( 'BarClass' ),
);
$builder -> setDefinitions ( $definitions );
$this -> assertEquals ( $definitions , $builder -> getDefinitions (), '->setDefinitions() sets the service definitions' );
$this -> assertTrue ( $builder -> hasDefinition ( 'foo' ), '->hasDefinition() returns true if a service definition exists' );
$this -> assertFalse ( $builder -> hasDefinition ( 'foobar' ), '->hasDefinition() returns false if a service definition does not exist' );
$builder -> setDefinition ( 'foobar' , $foo = new Definition ( 'FooBarClass' ));
$this -> assertEquals ( $foo , $builder -> getDefinition ( 'foobar' ), '->getDefinition() returns a service definition if defined' );
$this -> assertTrue ( $builder -> setDefinition ( 'foobar' , $foo = new Definition ( 'FooBarClass' )) === $foo , '->setDefinition() implements a fuild interface by returning the service reference' );
$builder -> addDefinitions ( $defs = array ( 'foobar' => new Definition ( 'FooBarClass' )));
$this -> assertEquals ( array_merge ( $definitions , $defs ), $builder -> getDefinitions (), '->addDefinitions() adds the service definitions' );
2010-05-07 15:09:11 +01:00
try {
2010-05-06 12:25:53 +01:00
$builder -> getDefinition ( 'baz' );
$this -> fail ( '->getDefinition() throws an InvalidArgumentException if the service definition 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 , '->getDefinition() throws an InvalidArgumentException if the service definition does not exist' );
$this -> assertEquals ( 'The service definition "baz" does not exist.' , $e -> getMessage (), '->getDefinition() throws an InvalidArgumentException if the service definition does not exist' );
}
2010-03-18 07:48:03 +00:00
}
2010-05-06 12:25:53 +01:00
2010-06-27 17:28:29 +01:00
/**
2010-07-15 14:11:33 +01:00
* @ covers Symfony\Components\DependencyInjection\ContainerBuilder :: register
2010-06-27 17:28:29 +01:00
*/
2010-05-06 12:25:53 +01:00
public function testRegister ()
2010-03-18 07:48:03 +00:00
{
2010-07-15 14:11:33 +01:00
$builder = new ContainerBuilder ();
2010-05-06 12:25:53 +01:00
$builder -> register ( 'foo' , 'FooClass' );
$this -> assertTrue ( $builder -> hasDefinition ( 'foo' ), '->register() registers a new service definition' );
$this -> assertInstanceOf ( 'Symfony\Components\DependencyInjection\Definition' , $builder -> getDefinition ( 'foo' ), '->register() returns the newly created Definition instance' );
2010-03-18 07:48:03 +00:00
}
2010-05-06 12:25:53 +01:00
2010-06-27 17:28:29 +01:00
/**
2010-07-15 14:11:33 +01:00
* @ covers Symfony\Components\DependencyInjection\ContainerBuilder :: has
2010-06-27 17:28:29 +01:00
*/
public function testHas ()
2010-03-18 07:48:03 +00:00
{
2010-07-15 14:11:33 +01:00
$builder = new ContainerBuilder ();
2010-06-27 17:28:29 +01:00
$this -> assertFalse ( $builder -> has ( 'foo' ), '->has() returns false if the service does not exist' );
2010-05-06 12:25:53 +01:00
$builder -> register ( 'foo' , 'FooClass' );
2010-06-27 17:28:29 +01:00
$this -> assertTrue ( $builder -> has ( 'foo' ), '->has() returns true if a service definition exists' );
$builder -> set ( 'bar' , new \stdClass ());
$this -> assertTrue ( $builder -> has ( 'bar' ), '->has() returns true if a service exists' );
2010-03-18 07:48:03 +00:00
}
2010-05-06 12:25:53 +01:00
2010-06-27 17:28:29 +01:00
/**
2010-07-15 14:11:33 +01:00
* @ covers Symfony\Components\DependencyInjection\ContainerBuilder :: get
2010-06-27 17:28:29 +01:00
*/
public function testGet ()
2010-03-18 07:48:03 +00:00
{
2010-07-15 14:11:33 +01:00
$builder = new ContainerBuilder ();
2010-05-07 15:09:11 +01:00
try {
2010-06-27 17:28:29 +01:00
$builder -> get ( 'foo' );
$this -> fail ( '->get() throws an InvalidArgumentException if the service does not exist' );
2010-05-07 15:09:11 +01:00
} catch ( \Exception $e ) {
2010-06-27 17:28:29 +01:00
$this -> assertInstanceOf ( '\InvalidArgumentException' , $e , '->get() throws an InvalidArgumentException if the service does not exist' );
$this -> assertEquals ( 'The service definition "foo" does not exist.' , $e -> getMessage (), '->get() throws an InvalidArgumentException if the service does not exist' );
2010-05-06 12:25:53 +01:00
}
2010-06-27 17:28:29 +01:00
$this -> assertNull ( $builder -> get ( 'foo' , ContainerInterface :: NULL_ON_INVALID_REFERENCE ), '->get() returns null if the service does not exist and NULL_ON_INVALID_REFERENCE is passed as a second argument' );
2010-05-06 12:25:53 +01:00
$builder -> register ( 'foo' , 'stdClass' );
2010-06-27 17:28:29 +01:00
$this -> assertType ( 'object' , $builder -> get ( 'foo' ), '->get() returns the service definition associated with the id' );
$builder -> set ( 'bar' , $bar = new \stdClass ());
$this -> assertEquals ( $bar , $builder -> get ( 'bar' ), '->get() returns the service associated with the id' );
2010-05-06 12:25:53 +01:00
$builder -> register ( 'bar' , 'stdClass' );
2010-06-27 17:28:29 +01:00
$this -> assertEquals ( $bar , $builder -> get ( 'bar' ), '->get() returns the service associated with the id even if a definition has been defined' );
2010-05-06 12:25:53 +01:00
$builder -> register ( 'baz' , 'stdClass' ) -> setArguments ( array ( new Reference ( 'baz' )));
2010-05-07 15:09:11 +01:00
try {
2010-06-27 17:28:29 +01:00
@ $builder -> get ( 'baz' );
$this -> fail ( '->get() throws a LogicException if the service has a circular reference to itself' );
2010-05-07 15:09:11 +01:00
} catch ( \Exception $e ) {
2010-06-27 17:28:29 +01:00
$this -> assertInstanceOf ( '\LogicException' , $e , '->get() throws a LogicException if the service has a circular reference to itself' );
$this -> assertEquals ( 'The service "baz" has a circular reference to itself.' , $e -> getMessage (), '->get() throws a LogicException if the service has a circular reference to itself' );
2010-05-06 12:25:53 +01:00
}
$builder -> register ( 'foobar' , 'stdClass' ) -> setShared ( true );
2010-06-27 17:28:29 +01:00
$this -> assertTrue ( $builder -> get ( 'bar' ) === $builder -> get ( 'bar' ), '->get() always returns the same instance if the service is shared' );
2010-03-18 07:48:03 +00:00
}
2010-05-06 12:25:53 +01:00
2010-06-27 17:28:29 +01:00
/**
2010-07-15 14:11:33 +01:00
* @ covers Symfony\Components\DependencyInjection\ContainerBuilder :: getServiceIds
2010-06-27 17:28:29 +01:00
*/
2010-05-06 12:25:53 +01:00
public function testGetServiceIds ()
2010-03-18 07:48:03 +00:00
{
2010-07-15 14:11:33 +01:00
$builder = new ContainerBuilder ();
2010-05-06 12:25:53 +01:00
$builder -> register ( 'foo' , 'stdClass' );
$builder -> bar = $bar = new \stdClass ();
$builder -> register ( 'bar' , 'stdClass' );
$this -> assertEquals ( array ( 'foo' , 'bar' , 'service_container' ), $builder -> getServiceIds (), '->getServiceIds() returns all defined service ids' );
2010-03-18 07:48:03 +00:00
}
2010-06-27 17:28:29 +01:00
/**
2010-07-15 14:11:33 +01:00
* @ covers Symfony\Components\DependencyInjection\ContainerBuilder :: setAlias
* @ covers Symfony\Components\DependencyInjection\ContainerBuilder :: hasAlias
* @ covers Symfony\Components\DependencyInjection\ContainerBuilder :: getAlias
2010-06-27 17:28:29 +01:00
*/
2010-05-06 12:25:53 +01:00
public function testAliases ()
2010-03-18 07:48:03 +00:00
{
2010-07-15 14:11:33 +01:00
$builder = new ContainerBuilder ();
2010-05-06 12:25:53 +01:00
$builder -> register ( 'foo' , 'stdClass' );
$builder -> setAlias ( 'bar' , 'foo' );
$this -> assertTrue ( $builder -> hasAlias ( 'bar' ), '->hasAlias() returns true if the alias exists' );
$this -> assertFalse ( $builder -> hasAlias ( 'foobar' ), '->hasAlias() returns false if the alias does not exist' );
$this -> assertEquals ( 'foo' , $builder -> getAlias ( 'bar' ), '->getAlias() returns the aliased service' );
2010-06-27 17:28:29 +01:00
$this -> assertTrue ( $builder -> has ( 'bar' ), '->setAlias() defines a new service' );
$this -> assertTrue ( $builder -> get ( 'bar' ) === $builder -> get ( 'foo' ), '->setAlias() creates a service that is an alias to another one' );
2010-05-06 12:25:53 +01:00
2010-05-07 15:09:11 +01:00
try {
2010-05-06 12:25:53 +01:00
$builder -> getAlias ( 'foobar' );
$this -> fail ( '->getAlias() throws an InvalidArgumentException if the alias 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 , '->getAlias() throws an InvalidArgumentException if the alias does not exist' );
$this -> assertEquals ( 'The service alias "foobar" does not exist.' , $e -> getMessage (), '->getAlias() throws an InvalidArgumentException if the alias does not exist' );
}
2010-03-18 07:48:03 +00:00
}
2010-05-06 12:25:53 +01:00
2010-06-27 17:28:29 +01:00
/**
2010-07-15 14:11:33 +01:00
* @ covers Symfony\Components\DependencyInjection\ContainerBuilder :: getAliases
2010-06-27 17:28:29 +01:00
*/
2010-05-06 12:25:53 +01:00
public function testGetAliases ()
2010-03-18 07:48:03 +00:00
{
2010-07-15 14:11:33 +01:00
$builder = new ContainerBuilder ();
2010-05-06 12:25:53 +01:00
$builder -> setAlias ( 'bar' , 'foo' );
$builder -> setAlias ( 'foobar' , 'foo' );
$this -> assertEquals ( array ( 'bar' => 'foo' , 'foobar' => 'foo' ), $builder -> getAliases (), '->getAliases() returns all service aliases' );
$builder -> register ( 'bar' , 'stdClass' );
$this -> assertEquals ( array ( 'foobar' => 'foo' ), $builder -> getAliases (), '->getAliases() does not return aliased services that have been overridden' );
2010-06-27 17:28:29 +01:00
$builder -> set ( 'foobar' , 'stdClass' );
2010-05-06 12:25:53 +01:00
$this -> assertEquals ( array (), $builder -> getAliases (), '->getAliases() does not return aliased services that have been overridden' );
2010-03-18 07:48:03 +00:00
}
2010-05-06 12:25:53 +01:00
2010-06-27 17:28:29 +01:00
/**
2010-07-15 14:11:33 +01:00
* @ covers Symfony\Components\DependencyInjection\ContainerBuilder :: setAliases
2010-06-27 17:28:29 +01:00
*/
public function testSetAliases ()
{
2010-07-15 14:11:33 +01:00
$builder = new ContainerBuilder ();
2010-06-27 17:28:29 +01:00
$builder -> setAliases ( array ( 'bar' => 'foo' , 'foobar' => 'foo' ));
$this -> assertEquals ( array ( 'bar' => 'foo' , 'foobar' => 'foo' ), $builder -> getAliases (), '->getAliases() returns all service aliases' );
}
/**
2010-07-15 14:11:33 +01:00
* @ covers Symfony\Components\DependencyInjection\ContainerBuilder :: addAliases
2010-06-27 17:28:29 +01:00
*/
public function testAddAliases ()
{
2010-07-15 14:11:33 +01:00
$builder = new ContainerBuilder ();
2010-06-27 17:28:29 +01:00
$builder -> setAliases ( array ( 'bar' => 'foo' ));
$builder -> addAliases ( array ( 'foobar' => 'foo' ));
$this -> assertEquals ( array ( 'bar' => 'foo' , 'foobar' => 'foo' ), $builder -> getAliases (), '->getAliases() returns all service aliases' );
}
/**
2010-07-15 14:11:33 +01:00
* @ covers Symfony\Components\DependencyInjection\ContainerBuilder :: createService
2010-06-27 17:28:29 +01:00
*/
2010-05-06 12:25:53 +01:00
public function testCreateService ()
2010-03-18 07:48:03 +00:00
{
2010-07-15 14:11:33 +01:00
$builder = new ContainerBuilder ();
2010-07-04 17:56:48 +01:00
$builder -> register ( 'foo1' , 'FooClass' ) -> setFile ( __DIR__ . '/Fixtures/includes/foo.php' );
2010-06-27 17:28:29 +01:00
$this -> assertInstanceOf ( '\FooClass' , $builder -> get ( 'foo1' ), '->createService() requires the file defined by the service definition' );
2010-07-04 17:56:48 +01:00
$builder -> register ( 'foo2' , 'FooClass' ) -> setFile ( __DIR__ . '/Fixtures/includes/%file%.php' );
2010-05-06 12:25:53 +01:00
$builder -> setParameter ( 'file' , 'foo' );
2010-06-27 17:28:29 +01:00
$this -> assertInstanceOf ( '\FooClass' , $builder -> get ( 'foo2' ), '->createService() replaces parameters in the file provided by the service definition' );
2010-03-18 07:48:03 +00:00
}
2010-05-06 12:25:53 +01:00
2010-06-27 17:28:29 +01:00
/**
2010-07-15 14:11:33 +01:00
* @ covers Symfony\Components\DependencyInjection\ContainerBuilder :: createService
2010-06-27 17:28:29 +01:00
*/
2010-05-06 12:25:53 +01:00
public function testCreateServiceClass ()
2010-03-18 07:48:03 +00:00
{
2010-07-15 14:11:33 +01:00
$builder = new ContainerBuilder ();
2010-05-06 12:25:53 +01:00
$builder -> register ( 'foo1' , '%class%' );
$builder -> setParameter ( 'class' , 'stdClass' );
2010-06-27 17:28:29 +01:00
$this -> assertInstanceOf ( '\stdClass' , $builder -> get ( 'foo1' ), '->createService() replaces parameters in the class provided by the service definition' );
2010-03-18 07:48:03 +00:00
}
2010-06-27 17:28:29 +01:00
/**
2010-07-15 14:11:33 +01:00
* @ covers Symfony\Components\DependencyInjection\ContainerBuilder :: createService
2010-06-27 17:28:29 +01:00
*/
2010-05-06 12:25:53 +01:00
public function testCreateServiceArguments ()
{
2010-07-15 14:11:33 +01:00
$builder = new ContainerBuilder ();
2010-05-06 12:25:53 +01:00
$builder -> register ( 'bar' , 'stdClass' );
$builder -> register ( 'foo1' , 'FooClass' ) -> addArgument ( array ( 'foo' => '%value%' , '%value%' => 'foo' , new Reference ( 'bar' )));
$builder -> setParameter ( 'value' , 'bar' );
2010-06-27 17:28:29 +01:00
$this -> assertEquals ( array ( 'foo' => 'bar' , 'bar' => 'foo' , $builder -> get ( 'bar' )), $builder -> get ( 'foo1' ) -> arguments , '->createService() replaces parameters and service references in the arguments provided by the service definition' );
2010-05-06 12:25:53 +01:00
}
2010-03-18 07:48:03 +00:00
2010-06-27 17:28:29 +01:00
/**
2010-07-15 14:11:33 +01:00
* @ covers Symfony\Components\DependencyInjection\ContainerBuilder :: createService
2010-06-27 17:28:29 +01:00
*/
2010-07-05 08:47:14 +01:00
public function testCreateServiceFactoryMethod ()
2010-05-06 12:25:53 +01:00
{
2010-07-15 14:11:33 +01:00
$builder = new ContainerBuilder ();
2010-05-06 12:25:53 +01:00
$builder -> register ( 'bar' , 'stdClass' );
2010-07-05 08:47:14 +01:00
$builder -> register ( 'foo1' , 'FooClass' ) -> setFactoryMethod ( 'getInstance' ) -> addArgument ( array ( 'foo' => '%value%' , '%value%' => 'foo' , new Reference ( 'bar' )));
2010-05-06 12:25:53 +01:00
$builder -> setParameter ( 'value' , 'bar' );
2010-07-05 08:47:14 +01:00
$this -> assertTrue ( $builder -> get ( 'foo1' ) -> called , '->createService() calls the factory method to create the service instance' );
$this -> assertEquals ( array ( 'foo' => 'bar' , 'bar' => 'foo' , $builder -> get ( 'bar' )), $builder -> get ( 'foo1' ) -> arguments , '->createService() passes the arguments to the factory method' );
2010-05-06 12:25:53 +01:00
}
2010-03-18 07:48:03 +00:00
2010-07-04 17:56:48 +01:00
/**
2010-07-15 14:11:33 +01:00
* @ covers Symfony\Components\DependencyInjection\ContainerBuilder :: createService
2010-07-04 17:56:48 +01:00
*/
public function testCreateServiceFactoryService ()
{
2010-07-15 14:11:33 +01:00
$builder = new ContainerBuilder ();
2010-07-04 17:56:48 +01:00
$builder -> register ( 'baz_service' ) -> setFactoryService ( 'baz_factory' ) -> setFactoryMethod ( 'getInstance' );
$builder -> register ( 'baz_factory' , 'BazClass' );
$this -> assertType ( 'BazClass' , $builder -> get ( 'baz_service' ));
}
2010-06-27 17:28:29 +01:00
/**
2010-07-15 14:11:33 +01:00
* @ covers Symfony\Components\DependencyInjection\ContainerBuilder :: createService
2010-06-27 17:28:29 +01:00
*/
2010-05-06 12:25:53 +01:00
public function testCreateServiceMethodCalls ()
{
2010-07-15 14:11:33 +01:00
$builder = new ContainerBuilder ();
2010-05-06 12:25:53 +01:00
$builder -> register ( 'bar' , 'stdClass' );
$builder -> register ( 'foo1' , 'FooClass' ) -> addMethodCall ( 'setBar' , array ( array ( '%value%' , new Reference ( 'bar' ))));
$builder -> setParameter ( 'value' , 'bar' );
2010-06-27 17:28:29 +01:00
$this -> assertEquals ( array ( 'bar' , $builder -> get ( 'bar' )), $builder -> get ( 'foo1' ) -> bar , '->createService() replaces the values in the method calls arguments' );
2010-05-06 12:25:53 +01:00
}
2010-03-18 07:48:03 +00:00
2010-06-27 17:28:29 +01:00
/**
2010-07-15 14:11:33 +01:00
* @ covers Symfony\Components\DependencyInjection\ContainerBuilder :: createService
2010-06-27 17:28:29 +01:00
*/
2010-05-06 12:25:53 +01:00
public function testCreateServiceConfigurator ()
{
2010-07-15 14:11:33 +01:00
$builder = new ContainerBuilder ();
2010-05-06 12:25:53 +01:00
$builder -> register ( 'foo1' , 'FooClass' ) -> setConfigurator ( 'sc_configure' );
2010-06-27 17:28:29 +01:00
$this -> assertTrue ( $builder -> get ( 'foo1' ) -> configured , '->createService() calls the configurator' );
2010-05-06 12:25:53 +01:00
$builder -> register ( 'foo2' , 'FooClass' ) -> setConfigurator ( array ( '%class%' , 'configureStatic' ));
$builder -> setParameter ( 'class' , 'BazClass' );
2010-06-27 17:28:29 +01:00
$this -> assertTrue ( $builder -> get ( 'foo2' ) -> configured , '->createService() calls the configurator' );
2010-05-06 12:25:53 +01:00
$builder -> register ( 'baz' , 'BazClass' );
$builder -> register ( 'foo3' , 'FooClass' ) -> setConfigurator ( array ( new Reference ( 'baz' ), 'configure' ));
2010-06-27 17:28:29 +01:00
$this -> assertTrue ( $builder -> get ( 'foo3' ) -> configured , '->createService() calls the configurator' );
2010-05-06 12:25:53 +01:00
$builder -> register ( 'foo4' , 'FooClass' ) -> setConfigurator ( 'foo' );
2010-05-07 15:09:11 +01:00
try {
2010-06-27 17:28:29 +01:00
$builder -> get ( 'foo4' );
2010-05-06 12:25:53 +01:00
$this -> fail ( '->createService() throws an InvalidArgumentException if the configure callable is not a valid callable' );
2010-05-07 15:09:11 +01:00
} catch ( \Exception $e ) {
2010-05-06 12:25:53 +01:00
$this -> assertInstanceOf ( '\InvalidArgumentException' , $e , '->createService() throws an InvalidArgumentException if the configure callable is not a valid callable' );
$this -> assertEquals ( 'The configure callable for class "FooClass" is not a callable.' , $e -> getMessage (), '->createService() throws an InvalidArgumentException if the configure callable is not a valid callable' );
}
2010-03-18 07:48:03 +00:00
}
2010-05-06 12:25:53 +01:00
2010-06-27 17:28:29 +01:00
/**
2010-07-15 14:11:33 +01:00
* @ covers Symfony\Components\DependencyInjection\ContainerBuilder :: resolveServices
2010-06-27 17:28:29 +01:00
*/
2010-05-06 12:25:53 +01:00
public function testResolveServices ()
2010-03-18 07:48:03 +00:00
{
2010-07-15 14:11:33 +01:00
$builder = new ContainerBuilder ();
2010-05-06 12:25:53 +01:00
$builder -> register ( 'foo' , 'FooClass' );
2010-06-27 17:28:29 +01:00
$this -> assertEquals ( $builder -> get ( 'foo' ), $builder -> resolveServices ( new Reference ( 'foo' )), '->resolveServices() resolves service references to service instances' );
$this -> assertEquals ( array ( 'foo' => array ( 'foo' , $builder -> get ( 'foo' ))), $builder -> resolveServices ( array ( 'foo' => array ( 'foo' , new Reference ( 'foo' )))), '->resolveServices() resolves service references to service instances in nested arrays' );
2010-03-18 07:48:03 +00:00
}
2010-06-27 17:28:29 +01:00
/**
2010-07-15 14:11:33 +01:00
* @ covers Symfony\Components\DependencyInjection\ContainerBuilder :: merge
2010-06-27 17:28:29 +01:00
*/
2010-05-06 12:25:53 +01:00
public function testMerge ()
2010-03-18 07:48:03 +00:00
{
2010-07-15 14:11:33 +01:00
$container = new ContainerBuilder ( new ParameterBag ( array ( 'bar' => 'foo' )));
$config = new ContainerBuilder ( new ParameterBag ( array ( 'foo' => 'bar' )));
2010-05-06 12:25:53 +01:00
$container -> merge ( $config );
2010-06-27 17:28:29 +01:00
$this -> assertEquals ( array ( 'bar' => 'foo' , 'foo' => 'bar' ), $container -> getParameterBag () -> all (), '->merge() merges current parameters with the loaded ones' );
2010-05-06 12:25:53 +01:00
2010-07-15 14:11:33 +01:00
$container = new ContainerBuilder ( new ParameterBag ( array ( 'bar' => 'foo' )));
$config = new ContainerBuilder ( new ParameterBag ( array ( 'foo' => '%bar%' )));
2010-05-06 12:25:53 +01:00
$container -> merge ( $config );
2010-07-15 14:11:33 +01:00
////// FIXME
2010-07-16 08:15:22 +01:00
$container -> freeze ();
2010-06-27 17:28:29 +01:00
$this -> assertEquals ( array ( 'bar' => 'foo' , 'foo' => 'foo' ), $container -> getParameterBag () -> all (), '->merge() evaluates the values of the parameters towards already defined ones' );
2010-05-06 12:25:53 +01:00
2010-07-15 14:11:33 +01:00
$container = new ContainerBuilder ( new ParameterBag ( array ( 'bar' => 'foo' )));
$config = new ContainerBuilder ( new ParameterBag ( array ( 'foo' => '%bar%' , 'baz' => '%foo%' )));
2010-05-06 12:25:53 +01:00
$container -> merge ( $config );
2010-07-15 14:11:33 +01:00
////// FIXME
2010-07-16 08:15:22 +01:00
$container -> freeze ();
2010-06-27 17:28:29 +01:00
$this -> assertEquals ( array ( 'bar' => 'foo' , 'foo' => 'foo' , 'baz' => 'foo' ), $container -> getParameterBag () -> all (), '->merge() evaluates the values of the parameters towards already defined ones' );
2010-05-06 12:25:53 +01:00
2010-07-15 14:11:33 +01:00
$container = new ContainerBuilder ();
2010-05-06 12:25:53 +01:00
$container -> register ( 'foo' , 'FooClass' );
$container -> register ( 'bar' , 'BarClass' );
2010-07-15 14:11:33 +01:00
$config = new ContainerBuilder ();
2010-05-06 12:25:53 +01:00
$config -> setDefinition ( 'baz' , new Definition ( 'BazClass' ));
$config -> setAlias ( 'alias_for_foo' , 'foo' );
$container -> merge ( $config );
$this -> assertEquals ( array ( 'foo' , 'bar' , 'baz' ), array_keys ( $container -> getDefinitions ()), '->merge() merges definitions already defined ones' );
$this -> assertEquals ( array ( 'alias_for_foo' => 'foo' ), $container -> getAliases (), '->merge() registers defined aliases' );
2010-07-15 14:11:33 +01:00
$container = new ContainerBuilder ();
2010-05-06 12:25:53 +01:00
$container -> register ( 'foo' , 'FooClass' );
$config -> setDefinition ( 'foo' , new Definition ( 'BazClass' ));
$container -> merge ( $config );
$this -> assertEquals ( 'BazClass' , $container -> getDefinition ( 'foo' ) -> getClass (), '->merge() overrides already defined services' );
2010-03-18 07:48:03 +00:00
}
2010-05-06 12:25:53 +01:00
2010-06-27 17:28:29 +01:00
/**
2010-07-15 14:11:33 +01:00
* @ covers Symfony\Components\DependencyInjection\ContainerBuilder :: findAnnotatedServiceIds
2010-06-27 17:28:29 +01:00
*/
2010-05-06 12:25:53 +01:00
public function testFindAnnotatedServiceIds ()
2010-03-18 07:48:03 +00:00
{
2010-07-15 14:11:33 +01:00
$builder = new ContainerBuilder ();
2010-05-06 12:25:53 +01:00
$builder
-> register ( 'foo' , 'FooClass' )
-> addAnnotation ( 'foo' , array ( 'foo' => 'foo' ))
-> addAnnotation ( 'bar' , array ( 'bar' => 'bar' ))
-> addAnnotation ( 'foo' , array ( 'foofoo' => 'foofoo' ))
;
$this -> assertEquals ( $builder -> findAnnotatedServiceIds ( 'foo' ), array (
'foo' => array (
array ( 'foo' => 'foo' ),
array ( 'foofoo' => 'foofoo' ),
)
), '->findAnnotatedServiceIds() returns an array of service ids and its annotation attributes' );
$this -> assertEquals ( array (), $builder -> findAnnotatedServiceIds ( 'foobar' ), '->findAnnotatedServiceIds() returns an empty array if there is annotated services' );
2010-03-18 07:48:03 +00:00
}
2010-07-15 14:11:33 +01:00
/**
* @ covers Symfony\Components\DependencyInjection\ContainerBuilder :: findDefinition
*/
public function testFindDefinition ()
{
$container = new ContainerBuilder ();
$container -> setDefinition ( 'foo' , $definition = new Definition ( 'FooClass' ));
$container -> setAlias ( 'bar' , 'foo' );
$container -> setAlias ( 'foobar' , 'bar' );
$this -> assertEquals ( $definition , $container -> findDefinition ( 'foobar' ), '->findDefinition() returns a Definition' );
}
/**
* @ covers Symfony\Components\DependencyInjection\ContainerBuilder :: getResources
* @ covers Symfony\Components\DependencyInjection\ContainerBuilder :: addResource
*/
public function testResources ()
{
$container = new ContainerBuilder ();
$container -> addResource ( $a = new FileResource ( __DIR__ . '/Fixtures/xml/services1.xml' ));
$container -> addResource ( $b = new FileResource ( __DIR__ . '/Fixtures/xml/services2.xml' ));
$this -> assertEquals ( array ( $a , $b ), $container -> getResources (), '->getResources() returns an array of resources read for the current configuration' );
}
2010-07-16 08:12:58 +01:00
/**
* @ covers Symfony\Components\DependencyInjection\ContainerBuilder :: registerExtension
* @ covers Symfony\Components\DependencyInjection\ContainerBuilder :: getExtension
*/
public function testExtension ()
{
$container = new ContainerBuilder ();
$container -> registerExtension ( $extension = new \ProjectExtension ());
$this -> assertTrue ( $container -> getExtension ( 'project' ) === $extension , '->registerExtension() registers an extension' );
}
2010-03-18 07:48:03 +00:00
}