4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\DependencyInjection\Tests\Dumper;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\Config\FileLocator;
16 use Symfony\Component\DependencyInjection\ContainerBuilder;
17 use Symfony\Component\DependencyInjection\ContainerInterface;
18 use Symfony\Component\DependencyInjection\Dumper\XmlDumper;
19 use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
20 use Symfony\Component\DependencyInjection\Reference;
22 class XmlDumperTest extends TestCase
24 protected static $fixturesPath;
26 public static function setUpBeforeClass()
28 self::$fixturesPath = realpath(__DIR__.'/../Fixtures/');
31 public function testDump()
33 $dumper = new XmlDumper(new ContainerBuilder());
35 $this->assertXmlStringEqualsXmlFile(self::$fixturesPath.'/xml/services1.xml', $dumper->dump(), '->dump() dumps an empty container as an empty XML file');
38 public function testExportParameters()
40 $container = include self::$fixturesPath.'//containers/container8.php';
41 $dumper = new XmlDumper($container);
42 $this->assertXmlStringEqualsXmlFile(self::$fixturesPath.'/xml/services8.xml', $dumper->dump(), '->dump() dumps parameters');
45 public function testAddParameters()
47 $container = include self::$fixturesPath.'//containers/container8.php';
48 $dumper = new XmlDumper($container);
49 $this->assertXmlStringEqualsXmlFile(self::$fixturesPath.'/xml/services8.xml', $dumper->dump(), '->dump() dumps parameters');
52 public function testAddService()
54 $container = include self::$fixturesPath.'/containers/container9.php';
55 $dumper = new XmlDumper($container);
57 $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');
59 $dumper = new XmlDumper($container = new ContainerBuilder());
60 $container->register('foo', 'FooClass')->addArgument(new \stdClass())->setPublic(true);
63 $this->fail('->dump() throws a RuntimeException if the container to be dumped has reference to objects or resources');
64 } catch (\Exception $e) {
65 $this->assertInstanceOf('\RuntimeException', $e, '->dump() throws a RuntimeException if the container to be dumped has reference to objects or resources');
66 $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');
70 public function testDumpAnonymousServices()
72 $container = include self::$fixturesPath.'/containers/container11.php';
73 $dumper = new XmlDumper($container);
74 $this->assertEquals('<?xml version="1.0" encoding="utf-8"?>
75 <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">
77 <service id="service_container" class="Symfony\Component\DependencyInjection\ContainerInterface" public="true" synthetic="true"/>
78 <service id="foo" class="FooClass" public="true">
79 <argument type="service">
80 <service class="BarClass">
81 <argument type="service">
82 <service class="BazClass"/>
87 <service id="Psr\Container\ContainerInterface" alias="service_container" public="false"/>
88 <service id="Symfony\Component\DependencyInjection\ContainerInterface" alias="service_container" public="false"/>
94 public function testDumpEntities()
96 $container = include self::$fixturesPath.'/containers/container12.php';
97 $dumper = new XmlDumper($container);
98 $this->assertEquals("<?xml version=\"1.0\" encoding=\"utf-8\"?>
99 <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\">
101 <service id=\"service_container\" class=\"Symfony\Component\DependencyInjection\ContainerInterface\" public=\"true\" synthetic=\"true\"/>
102 <service id=\"foo\" class=\"FooClass\Foo\" public=\"true\">
103 <tag name=\"foo"bar\bar\" foo=\"foo"barřž€\"/>
104 <argument>foo<>&bar</argument>
106 <service id=\"Psr\Container\ContainerInterface\" alias=\"service_container\" public=\"false\"/>
107 <service id=\"Symfony\Component\DependencyInjection\ContainerInterface\" alias=\"service_container\" public=\"false\"/>
114 * @dataProvider provideDecoratedServicesData
116 public function testDumpDecoratedServices($expectedXmlDump, $container)
118 $dumper = new XmlDumper($container);
119 $this->assertEquals($expectedXmlDump, $dumper->dump());
122 public function provideDecoratedServicesData()
124 $fixturesPath = realpath(__DIR__.'/../Fixtures/');
127 array("<?xml version=\"1.0\" encoding=\"utf-8\"?>
128 <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\">
130 <service id=\"service_container\" class=\"Symfony\Component\DependencyInjection\ContainerInterface\" public=\"true\" synthetic=\"true\"/>
131 <service id=\"foo\" class=\"FooClass\Foo\" public=\"true\" decorates=\"bar\" decoration-inner-name=\"bar.woozy\"/>
132 <service id=\"Psr\Container\ContainerInterface\" alias=\"service_container\" public=\"false\"/>
133 <service id=\"Symfony\Component\DependencyInjection\ContainerInterface\" alias=\"service_container\" public=\"false\"/>
136 ", include $fixturesPath.'/containers/container15.php'),
137 array("<?xml version=\"1.0\" encoding=\"utf-8\"?>
138 <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\">
140 <service id=\"service_container\" class=\"Symfony\Component\DependencyInjection\ContainerInterface\" public=\"true\" synthetic=\"true\"/>
141 <service id=\"foo\" class=\"FooClass\Foo\" public=\"true\" decorates=\"bar\"/>
142 <service id=\"Psr\Container\ContainerInterface\" alias=\"service_container\" public=\"false\"/>
143 <service id=\"Symfony\Component\DependencyInjection\ContainerInterface\" alias=\"service_container\" public=\"false\"/>
146 ", include $fixturesPath.'/containers/container16.php'),
151 * @dataProvider provideCompiledContainerData
153 public function testCompiledContainerCanBeDumped($containerFile)
155 $fixturesPath = __DIR__.'/../Fixtures';
156 $container = require $fixturesPath.'/containers/'.$containerFile.'.php';
157 $container->compile();
158 $dumper = new XmlDumper($container);
161 $this->addToAssertionCount(1);
164 public function provideCompiledContainerData()
169 array('container11'),
170 array('container12'),
171 array('container14'),
175 public function testDumpInlinedServices()
177 $container = include self::$fixturesPath.'/containers/container21.php';
178 $dumper = new XmlDumper($container);
180 $this->assertEquals(file_get_contents(self::$fixturesPath.'/xml/services21.xml'), $dumper->dump());
183 public function testDumpAutowireData()
185 $container = include self::$fixturesPath.'/containers/container24.php';
186 $dumper = new XmlDumper($container);
188 $this->assertEquals(file_get_contents(self::$fixturesPath.'/xml/services24.xml'), $dumper->dump());
191 public function testDumpLoad()
193 $container = new ContainerBuilder();
194 $loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
195 $loader->load('services_dump_load.xml');
197 $this->assertEquals(array(new Reference('bar', ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE)), $container->getDefinition('foo')->getArguments());
199 $dumper = new XmlDumper($container);
200 $this->assertStringEqualsFile(self::$fixturesPath.'/xml/services_dump_load.xml', $dumper->dump());
203 public function testDumpAbstractServices()
205 $container = include self::$fixturesPath.'/containers/container_abstract.php';
206 $dumper = new XmlDumper($container);
208 $this->assertEquals(file_get_contents(self::$fixturesPath.'/xml/services_abstract.xml'), $dumper->dump());