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\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 ;
2011-10-18 13:52:35 +01:00
use Symfony\Component\DependencyInjection\Definition ;
2010-03-18 07:48:03 +00:00
class PhpDumperTest extends \PHPUnit_Framework_TestCase
{
2012-07-09 13:50:58 +01:00
protected static $fixturesPath ;
2010-03-18 07:48:03 +00:00
2012-07-09 13:50:58 +01:00
public static function setUpBeforeClass ()
2010-05-06 12:25:53 +01:00
{
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 ();
2011-10-18 13:52:35 +01:00
new PhpDumper ( $container );
}
public function testDumpOptimizationString ()
{
$definition = new Definition ();
$definition -> setClass ( 'stdClass' );
$definition -> addArgument ( array (
'only dot' => '.' ,
'concatenation as value' => '.\'\'.' ,
'concatenation from the start value' => '\'\'.' ,
'.' => 'dot as a key' ,
'.\'\'.' => 'concatenation as a key' ,
2014-09-21 19:53:12 +01:00
'\'\'.' => 'concatenation from the start key' ,
2011-10-18 13:52:35 +01:00
'optimize concatenation' => " string1%some_string%string2 " ,
'optimize concatenation with empty string' => " string1%empty_value%string2 " ,
'optimize concatenation from the start' => '%empty_value%start' ,
'optimize concatenation at the end' => 'end%empty_value%' ,
));
$container = new ContainerBuilder ();
2012-12-28 09:40:13 +00:00
$container -> setResourceTracking ( false );
2011-10-18 13:52:35 +01:00
$container -> setDefinition ( 'test' , $definition );
$container -> setParameter ( 'empty_value' , '' );
$container -> setParameter ( 'some_string' , '-' );
$container -> compile ();
2010-05-06 12:25:53 +01:00
$dumper = new PhpDumper ( $container );
2011-10-18 13:52:35 +01:00
$this -> assertStringEqualsFile ( self :: $fixturesPath . '/php/services10.php' , $dumper -> dump (), '->dump() dumps an empty container as an empty PHP class' );
2010-05-06 12:25:53 +01:00
}
2010-03-18 07:48:03 +00:00
2014-11-30 15:07:59 +00:00
public function testDumpRelativeDir ()
{
$definition = new Definition ();
$definition -> setClass ( 'stdClass' );
$definition -> addArgument ( '%foo%' );
$definition -> addArgument ( array ( '%foo%' => '%foo%' ));
$container = new ContainerBuilder ();
$container -> setDefinition ( 'test' , $definition );
$container -> setParameter ( 'foo' , 'wiz' . dirname ( dirname ( __FILE__ )));
$container -> setParameter ( 'bar' , dirname ( __FILE__ ));
$container -> setParameter ( 'baz' , '%bar%/PhpDumperTest.php' );
$container -> compile ();
$dumper = new PhpDumper ( $container );
$this -> assertStringEqualsFile ( self :: $fixturesPath . '/php/services12.php' , $dumper -> dump ( array ( 'file' => __FILE__ )), '->dump() dumps __DIR__ relative strings' );
}
2010-06-27 17:28:29 +01:00
/**
2013-11-25 08:44:14 +00:00
* @ expectedException \InvalidArgumentException
2010-06-27 17:28:29 +01:00
*/
public function testExportParameters ()
{
2011-10-18 13:52:35 +01:00
$dumper = new PhpDumper ( 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
{
2013-01-05 08:50:11 +00:00
// without compilation
2010-05-06 12:25:53 +01:00
$container = include self :: $fixturesPath . '/containers/container9.php' ;
$dumper = new PhpDumper ( $container );
2014-11-30 15:07:59 +00:00
$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-05-06 12:25:53 +01:00
2013-01-05 08:50:11 +00:00
// with compilation
$container = include self :: $fixturesPath . '/containers/container9.php' ;
$container -> compile ();
$dumper = new PhpDumper ( $container );
2014-11-30 15:07:59 +00:00
$this -> assertEquals ( str_replace ( '%path%' , str_replace ( '\\' , '\\\\' , self :: $fixturesPath . DIRECTORY_SEPARATOR . 'includes' . DIRECTORY_SEPARATOR ), file_get_contents ( self :: $fixturesPath . '/php/services9_compiled.php' )), $dumper -> dump (), '->dump() dumps services' );
2013-01-05 08:50:11 +00:00
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 ) {
2011-12-05 18:05:21 +00:00
$this -> assertInstanceOf ( '\Symfony\Component\DependencyInjection\Exception\RuntimeException' , $e , '->dump() throws a RuntimeException if the container to be dumped has reference to objects or resources' );
$this -> assertEquals ( 'Unable to dump a service container if a parameter is an object or a resource.' , $e -> getMessage (), '->dump() throws a RuntimeException if the container to be dumped has reference to objects or resources' );
2010-05-06 12:25:53 +01:00
}
2010-03-18 07:48:03 +00:00
}
2010-04-08 09:43:42 +01:00
2013-07-19 08:14:04 +01:00
/**
2013-11-25 08:44:14 +00:00
* @ expectedException \InvalidArgumentException
2013-07-19 08:14:04 +01:00
* @ expectedExceptionMessage Service id " bar $ " cannot be converted to a valid PHP method name .
*/
public function testAddServiceInvalidServiceId ()
{
$container = new ContainerBuilder ();
$container -> register ( 'bar$' , 'FooClass' );
$dumper = new PhpDumper ( $container );
$dumper -> dump ();
}
2013-06-05 10:51:05 +01:00
public function testAliases ()
{
$container = include self :: $fixturesPath . '/containers/container9.php' ;
$container -> compile ();
$dumper = new PhpDumper ( $container );
eval ( '?>' . $dumper -> dump ( array ( 'class' => 'Symfony_DI_PhpDumper_Test_Aliases' )));
$container = new \Symfony_DI_PhpDumper_Test_Aliases ();
2014-07-29 19:09:11 +01:00
$container -> set ( 'foo' , $foo = new \stdClass ());
2013-06-05 10:51:05 +01:00
$this -> assertSame ( $foo , $container -> get ( 'foo' ));
$this -> assertSame ( $foo , $container -> get ( 'alias_for_foo' ));
$this -> assertSame ( $foo , $container -> get ( 'alias_for_alias' ));
}
2013-07-07 13:58:42 +01:00
public function testFrozenContainerWithoutAliases ()
{
$container = new ContainerBuilder ();
$container -> compile ();
$dumper = new PhpDumper ( $container );
eval ( '?>' . $dumper -> dump ( array ( 'class' => 'Symfony_DI_PhpDumper_Test_Frozen_No_Aliases' )));
$container = new \Symfony_DI_PhpDumper_Test_Frozen_No_Aliases ();
$this -> assertFalse ( $container -> has ( 'foo' ));
}
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
}
2013-09-12 07:52:33 +01:00
/**
* @ expectedException \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException
*/
public function testCircularReference ()
{
$container = new ContainerBuilder ();
$container -> register ( 'foo' , 'stdClass' ) -> addArgument ( new Reference ( 'bar' ));
$container -> register ( 'bar' , 'stdClass' ) -> setPublic ( false ) -> addMethodCall ( 'setA' , array ( new Reference ( 'baz' )));
$container -> register ( 'baz' , 'stdClass' ) -> addMethodCall ( 'setA' , array ( new Reference ( 'foo' )));
$container -> compile ();
$dumper = new PhpDumper ( $container );
$dumper -> dump ();
}
2010-03-18 07:48:03 +00:00
}