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\XmlDumper ;
2010-10-02 15:47:35 +01:00
use Symfony\Component\DependencyInjection\InterfaceInjector ;
2010-03-18 07:48:03 +00:00
class XmlDumperTest extends \PHPUnit_Framework_TestCase
{
2010-05-06 12:25:53 +01:00
static protected $fixturesPath ;
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
}
public function testDump ()
{
2010-07-15 14:11:33 +01:00
$dumper = new XmlDumper ( $container = new ContainerBuilder ());
2010-05-06 12:25:53 +01:00
$this -> assertXmlStringEqualsXmlFile ( self :: $fixturesPath . '/xml/services1.xml' , $dumper -> dump (), '->dump() dumps an empty container as an empty XML file' );
2010-07-15 14:11:33 +01:00
$container = new ContainerBuilder ();
2010-05-06 12:25:53 +01:00
$dumper = new XmlDumper ( $container );
}
2010-06-27 17:28:29 +01:00
public function testExportParameters ()
{
$container = include self :: $fixturesPath . '//containers/container8.php' ;
$dumper = new XmlDumper ( $container );
$this -> assertXmlStringEqualsXmlFile ( self :: $fixturesPath . '/xml/services8.xml' , $dumper -> dump (), '->dump() dumps parameters' );
}
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 XmlDumper ( $container );
$this -> assertXmlStringEqualsXmlFile ( self :: $fixturesPath . '/xml/services8.xml' , $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 XmlDumper ( $container );
$this -> assertEquals ( str_replace ( '%path%' , self :: $fixturesPath . DIRECTORY_SEPARATOR . 'includes' . DIRECTORY_SEPARATOR , file_get_contents ( self :: $fixturesPath . '/xml/services9.xml' )), $dumper -> dump (), '->dump() dumps services' );
2010-07-15 14:11:33 +01:00
$dumper = new XmlDumper ( $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() 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-03-18 07:48:03 +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 XmlDumper ( $container );
$classBody = $dumper -> dump ();
//TODO: find a better way to test dumper
//var_dump($classBody);
2011-01-14 14:07:09 +00:00
$this -> assertEquals ( " <?xml version= \" 1.0 \" encoding= \" utf-8 \" ?>
2011-03-06 11:40:06 +00:00
< container xmlns = \ " http://symfony.com/schema/dic/services \" xmlns:xsi= \" http://www.w3.org/2001/XMLSchema-instance \" xsi:schemaLocation= \" http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd \" >
2010-10-02 15:47:35 +01:00
< parameters >
< parameter key = \ " cla \" >Fo</parameter>
< parameter key = \ " ss \" >Class</parameter>
</ parameters >
2011-01-14 17:06:22 +00:00
< services >
< service id = \ " foo \" class= \" %cla%o%ss% \" />
</ services >
2010-10-02 15:47:35 +01:00
< interfaces >
< interface class = \ " FooClass \" >
< call method = \ " setBar \" >
< argument > someValue </ argument >
</ call >
</ interface >
</ interfaces >
</ container >
" , $classBody );
2011-01-14 17:06:22 +00:00
$dom = new \DOMDocument ();
$dom -> loadXML ( $classBody );
$this -> assertTrue ( $dom -> schemaValidate ( __DIR__ . '/../../../../../../src/Symfony/Component/DependencyInjection/Loader/schema/dic/services/services-1.0.xsd' ));
2010-10-02 15:47:35 +01:00
}
2011-01-13 15:06:14 +00:00
public function testDumpAnonymousServices ()
{
include self :: $fixturesPath . '/containers/container11.php' ;
$dumper = new XmlDumper ( $container );
2011-01-14 14:07:09 +00:00
$this -> assertEquals ( " <?xml version= \" 1.0 \" encoding= \" utf-8 \" ?>
2011-03-06 11:40:06 +00:00
< container xmlns = \ " http://symfony.com/schema/dic/services \" xmlns:xsi= \" http://www.w3.org/2001/XMLSchema-instance \" xsi:schemaLocation= \" http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd \" >
2011-01-13 15:06:14 +00:00
< services >
< service id = \ " foo \" class= \" FooClass \" >
< argument type = \ " service \" >
< service class = \ " BarClass \" >
< argument type = \ " service \" >
2011-01-14 14:07:09 +00:00
< service class = \ " BazClass \" />
2011-01-13 15:06:14 +00:00
</ argument >
</ service >
</ argument >
</ service >
</ services >
</ container >
2011-01-14 14:07:09 +00:00
" , $dumper->dump ());
}
public function testDumpEntities ()
{
include self :: $fixturesPath . '/containers/container12.php' ;
$dumper = new XmlDumper ( $container );
$this -> assertEquals ( " <?xml version= \" 1.0 \" encoding= \" utf-8 \" ?>
2011-03-06 11:40:06 +00:00
< container xmlns = \ " http://symfony.com/schema/dic/services \" xmlns:xsi= \" http://www.w3.org/2001/XMLSchema-instance \" xsi:schemaLocation= \" http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd \" >
2011-01-14 14:07:09 +00:00
< services >
< service id = \ " foo \" class= \" FooClass \ Foo \" >
< tag name = \ " foo"bar \ bar \" foo= \" foo"barřž€ \" />
< argument > foo & lt ; & gt ; & amp ; bar </ argument >
</ service >
</ services >
</ container >
2011-01-13 15:06:14 +00:00
" , $dumper->dump ());
}
2010-03-18 07:48:03 +00:00
}