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 .
*/
2010-08-20 22:09:55 +01:00
namespace Symfony\Tests\Component\DependencyInjection ;
2010-03-18 07:48:03 +00:00
2010-08-20 22:09:55 +01:00
use Symfony\Component\DependencyInjection\Container ;
use Symfony\Component\DependencyInjection\ContainerInterface ;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag ;
2010-03-18 07:48:03 +00:00
class ContainerTest extends \PHPUnit_Framework_TestCase
{
2010-06-27 17:28:29 +01:00
/**
2010-08-20 22:09:55 +01:00
* @ covers Symfony\Component\DependencyInjection\Container :: __construct
2010-06-27 17:28:29 +01: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
2010-06-27 17:28:29 +01:00
/**
2011-01-16 07:12:36 +00:00
* @ covers Symfony\Component\DependencyInjection\Container :: compile
2010-06-27 17:28:29 +01:00
*/
2011-01-16 07:12:36 +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' )));
2011-01-16 07:12:36 +00:00
$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' );
2010-06-27 17:28:29 +01:00
}
2010-03-18 07:48:03 +00:00
2010-06-27 17:28:29 +01:00
/**
2010-08-20 22:09:55 +01:00
* @ covers Symfony\Component\DependencyInjection\Container :: isFrozen
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
/**
2010-08-20 22:09:55 +01:00
* @ covers Symfony\Component\DependencyInjection\Container :: getParameterBag
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-06-27 17:28:29 +01:00
/**
2010-08-20 22:09:55 +01:00
* @ covers Symfony\Component\DependencyInjection\Container :: setParameter
* @ covers Symfony\Component\DependencyInjection\Container :: getParameter
2010-06-27 17:28:29 +01: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' );
$this -> assertEquals ( 'The parameter "baba" must be defined.' , $e -> getMessage (), '->getParameter() thrown an \InvalidArgumentException if the key 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-08-20 22:09:55 +01:00
* @ covers Symfony\Component\DependencyInjection\Container :: getServiceIds
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 ();
$this -> assertEquals ( array ( 'bar' , 'foo_bar' , 'foo.baz' , 'service_container' ), $sc -> getServiceIds (), '->getServiceIds() returns defined service ids by getXXXService() methods' );
2010-03-18 07:48:03 +00:00
}
2010-06-27 17:28:29 +01:00
/**
2010-08-20 22:09:55 +01:00
* @ covers Symfony\Component\DependencyInjection\Container :: set
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 ();
2010-06-27 17:28:29 +01:00
$sc -> set ( 'foo' , $foo = new \stdClass ());
$this -> assertEquals ( $foo , $sc -> get ( 'foo' ), '->set() sets a service' );
}
2010-05-06 12:25:53 +01:00
2010-06-27 17:28:29 +01:00
/**
2010-08-20 22:09:55 +01:00
* @ covers Symfony\Component\DependencyInjection\Container :: get
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 ());
$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' );
2010-05-06 12:25:53 +01:00
2010-06-27 17:28:29 +01:00
$sc -> set ( 'bar' , $bar = new \stdClass ());
2010-11-20 01:09:20 +00:00
$this -> assertSame ( $sc -> get ( 'bar' ), $bar , '->getServiceIds() prefers to return a service defined with a getXXXService() method than one defined with set()' );
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 ) {
2010-06-27 17:28:29 +01:00
$this -> assertInstanceOf ( '\InvalidArgumentException' , $e , '->get() throws a \InvalidArgumentException exception if the service is empty' );
$this -> assertEquals ( 'The service "" does not exist.' , $e -> getMessage (), '->get() throws a \InvalidArgumentException exception if the service is empty' );
2010-05-06 12:25:53 +01:00
}
2010-06-27 17:28:29 +01:00
$this -> assertNull ( $sc -> get ( '' , ContainerInterface :: NULL_ON_INVALID_REFERENCE ));
2010-03-18 07:48:03 +00:00
}
2010-06-27 17:28:29 +01:00
/**
2010-08-20 22:09:55 +01:00
* @ covers Symfony\Component\DependencyInjection\Container :: has
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' );
2010-03-18 07:48:03 +00:00
}
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
{
public $__bar , $__foo_bar , $__foo_baz ;
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 ();
2010-03-18 07:48:03 +00:00
}
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
}
2010-03-18 07:48:03 +00:00
}