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 .
*/
2010-08-20 22:09:55 +01:00
namespace Symfony\Tests\Component\DependencyInjection\Dumper ;
2010-03-18 07:48:03 +00:00
2010-08-20 22:09:55 +01:00
use Symfony\Component\DependencyInjection\ContainerBuilder ;
use Symfony\Component\DependencyInjection\Dumper\PhpDumper ;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag ;
use Symfony\Component\DependencyInjection\Reference ;
2010-10-02 15:47:35 +01:00
use Symfony\Component\DependencyInjection\InterfaceInjector ;
2010-03-18 07:48:03 +00:00
class PhpDumperTest extends \PHPUnit_Framework_TestCase
{
2010-05-06 12:25:53 +01:00
static protected $fixturesPath ;
2010-03-18 07:48:03 +00:00
2010-05-06 12:25:53 +01:00
static public function setUpBeforeClass ()
{
2010-06-28 08:31:54 +01:00
self :: $fixturesPath = realpath ( __DIR__ . '/../Fixtures/' );
2010-05-06 12:25:53 +01:00
}
2010-03-18 07:48:03 +00:00
2010-05-06 12:25:53 +01:00
public function testDump ()
{
2010-07-15 14:11:33 +01:00
$dumper = new PhpDumper ( $container = new ContainerBuilder ());
2010-03-18 07:48:03 +00:00
2010-05-06 12:25:53 +01:00
$this -> assertStringEqualsFile ( self :: $fixturesPath . '/php/services1.php' , $dumper -> dump (), '->dump() dumps an empty container as an empty PHP class' );
$this -> assertStringEqualsFile ( self :: $fixturesPath . '/php/services1-1.php' , $dumper -> dump ( array ( 'class' => 'Container' , 'base_class' => 'AbstractContainer' )), '->dump() takes a class and a base_class options' );
2010-03-18 07:48:03 +00:00
2010-07-15 14:11:33 +01:00
$container = new ContainerBuilder ();
2010-05-06 12:25:53 +01:00
$dumper = new PhpDumper ( $container );
}
2010-03-18 07:48:03 +00:00
2010-06-27 17:28:29 +01:00
/**
* @ expectedException InvalidArgumentException
*/
public function testExportParameters ()
{
2010-07-15 14:11:33 +01:00
$dumper = new PhpDumper ( $container = new ContainerBuilder ( new ParameterBag ( array ( 'foo' => new Reference ( 'foo' )))));
2010-06-27 17:28:29 +01:00
$dumper -> dump ();
}
2010-05-06 12:25:53 +01:00
public function testAddParameters ()
2010-03-18 07:48:03 +00:00
{
2010-05-06 12:25:53 +01:00
$container = include self :: $fixturesPath . '/containers/container8.php' ;
$dumper = new PhpDumper ( $container );
$this -> assertStringEqualsFile ( self :: $fixturesPath . '/php/services8.php' , $dumper -> dump (), '->dump() dumps parameters' );
2010-03-18 07:48:03 +00:00
}
2010-05-06 12:25:53 +01:00
public function testAddService ()
2010-03-18 07:48:03 +00:00
{
2010-05-06 12:25:53 +01:00
$container = include self :: $fixturesPath . '/containers/container9.php' ;
$dumper = new PhpDumper ( $container );
$this -> assertEquals ( str_replace ( '%path%' , str_replace ( '\\' , '\\\\' , self :: $fixturesPath . DIRECTORY_SEPARATOR . 'includes' . DIRECTORY_SEPARATOR ), file_get_contents ( self :: $fixturesPath . '/php/services9.php' )), $dumper -> dump (), '->dump() dumps services' );
2010-07-15 14:11:33 +01:00
$dumper = new PhpDumper ( $container = new ContainerBuilder ());
2010-05-06 12:25:53 +01:00
$container -> register ( 'foo' , 'FooClass' ) -> addArgument ( new \stdClass ());
2010-05-07 15:09:11 +01:00
try {
2010-05-06 12:25:53 +01:00
$dumper -> dump ();
$this -> fail ( '->dump() throws a RuntimeException if the container to be dumped has reference to objects or resources' );
2010-05-07 15:09:11 +01:00
} catch ( \Exception $e ) {
2010-05-06 12:25:53 +01:00
$this -> assertInstanceOf ( '\RuntimeException' , $e , '->dump() returns a LogicException if the dump() method has not been overriden by a children class' );
2011-03-01 17:56:35 +00:00
$this -> assertEquals ( 'Unable to dump a service container if a parameter is an object or a resource.' , $e -> getMessage (), '->dump() returns a LogicException if the dump() method has not been overridden by a children class' );
2010-05-06 12:25:53 +01:00
}
2010-03-18 07:48:03 +00:00
}
2010-04-08 09:43:42 +01:00
2010-05-06 12:25:53 +01:00
public function testOverrideServiceWhenUsingADumpedContainer ()
{
require_once self :: $fixturesPath . '/php/services9.php' ;
require_once self :: $fixturesPath . '/includes/foo.php' ;
2010-04-08 09:43:42 +01:00
2010-05-06 12:25:53 +01:00
$container = new \ProjectServiceContainer ();
2010-06-27 17:28:29 +01:00
$container -> set ( 'bar' , $bar = new \stdClass ());
2010-05-06 12:25:53 +01:00
$container -> setParameter ( 'foo_bar' , 'foo_bar' );
2010-04-08 09:43:42 +01:00
2010-06-27 17:28:29 +01:00
$this -> assertEquals ( $bar , $container -> get ( 'bar' ), '->set() overrides an already defined service' );
2010-05-06 12:25:53 +01:00
}
2010-11-12 08:49:59 +00:00
public function testOverrideServiceWhenUsingADumpedContainerAndServiceIsUsedFromAnotherOne ()
{
require_once self :: $fixturesPath . '/php/services9.php' ;
require_once self :: $fixturesPath . '/includes/foo.php' ;
require_once self :: $fixturesPath . '/includes/classes.php' ;
$container = new \ProjectServiceContainer ();
$container -> set ( 'bar' , $bar = new \stdClass ());
2010-11-23 21:43:09 +00:00
$this -> assertSame ( $bar , $container -> get ( 'foo' ) -> bar , '->set() overrides an already defined service' );
2010-11-12 08:49:59 +00:00
}
2010-10-02 15:47:35 +01:00
public function testInterfaceInjectors ()
{
$interfaceInjector = new InterfaceInjector ( 'FooClass' );
$interfaceInjector -> addMethodCall ( 'setBar' , array ( 'someValue' ));
$container = include self :: $fixturesPath . '/containers/interfaces1.php' ;
$container -> addInterfaceInjector ( $interfaceInjector );
$dumper = new PhpDumper ( $container );
$this -> assertStringEqualsFile ( self :: $fixturesPath . '/php/services_interfaces-1.php' , $dumper -> dump (), '->dump() dumps interface injectors' );
}
public function testInterfaceInjectorsAndServiceFactories ()
{
$interfaceInjector = new InterfaceInjector ( 'BarClass' );
$interfaceInjector -> addMethodCall ( 'setFoo' , array ( 'someValue' ));
$container = include self :: $fixturesPath . '/containers/interfaces2.php' ;
$container -> addInterfaceInjector ( $interfaceInjector );
$dumper = new PhpDumper ( $container );
$this -> assertStringEqualsFile ( self :: $fixturesPath . '/php/services_interfaces-2.php' , $dumper -> dump (), '->dump() dumps interface injectors' );
}
public function testFrozenContainerInterfaceInjectors ()
{
$interfaceInjector = new InterfaceInjector ( 'FooClass' );
$interfaceInjector -> addMethodCall ( 'setBar' , array ( 'someValue' ));
$container = include self :: $fixturesPath . '/containers/interfaces1.php' ;
$container -> addInterfaceInjector ( $interfaceInjector );
2011-01-16 07:12:36 +00:00
$container -> compile ();
2010-10-02 15:47:35 +01:00
$dumper = new PhpDumper ( $container );
$this -> assertStringEqualsFile ( self :: $fixturesPath . '/php/services_interfaces-1-1.php' , $dumper -> dump (), '->dump() dumps interface injectors' );
}
2010-03-18 07:48:03 +00:00
}