2e4ccf1fdbe4cc98f0777fa08a1d00b5331cba9c
[yaffs-website] / vendor / symfony / dependency-injection / Tests / Dumper / XmlDumperTest.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\DependencyInjection\Tests\Dumper;
13
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;
21
22 class XmlDumperTest extends TestCase
23 {
24     protected static $fixturesPath;
25
26     public static function setUpBeforeClass()
27     {
28         self::$fixturesPath = realpath(__DIR__.'/../Fixtures/');
29     }
30
31     public function testDump()
32     {
33         $dumper = new XmlDumper(new ContainerBuilder());
34
35         $this->assertXmlStringEqualsXmlFile(self::$fixturesPath.'/xml/services1.xml', $dumper->dump(), '->dump() dumps an empty container as an empty XML file');
36     }
37
38     public function testExportParameters()
39     {
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');
43     }
44
45     public function testAddParameters()
46     {
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');
50     }
51
52     public function testAddService()
53     {
54         $container = include self::$fixturesPath.'/containers/container9.php';
55         $dumper = new XmlDumper($container);
56
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');
58
59         $dumper = new XmlDumper($container = new ContainerBuilder());
60         $container->register('foo', 'FooClass')->addArgument(new \stdClass())->setPublic(true);
61         try {
62             $dumper->dump();
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');
67         }
68     }
69
70     public function testDumpAnonymousServices()
71     {
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">
76   <services>
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"/>
83           </argument>
84         </service>
85       </argument>
86     </service>
87     <service id="Psr\Container\ContainerInterface" alias="service_container" public="false"/>
88     <service id="Symfony\Component\DependencyInjection\ContainerInterface" alias="service_container" public="false"/>
89   </services>
90 </container>
91 ', $dumper->dump());
92     }
93
94     public function testDumpEntities()
95     {
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\">
100   <services>
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&quot;bar\bar\" foo=\"foo&quot;barřž€\"/>
104       <argument>foo&lt;&gt;&amp;bar</argument>
105     </service>
106     <service id=\"Psr\Container\ContainerInterface\" alias=\"service_container\" public=\"false\"/>
107     <service id=\"Symfony\Component\DependencyInjection\ContainerInterface\" alias=\"service_container\" public=\"false\"/>
108   </services>
109 </container>
110 ", $dumper->dump());
111     }
112
113     /**
114      * @dataProvider provideDecoratedServicesData
115      */
116     public function testDumpDecoratedServices($expectedXmlDump, $container)
117     {
118         $dumper = new XmlDumper($container);
119         $this->assertEquals($expectedXmlDump, $dumper->dump());
120     }
121
122     public function provideDecoratedServicesData()
123     {
124         $fixturesPath = realpath(__DIR__.'/../Fixtures/');
125
126         return array(
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\">
129   <services>
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\"/>
134   </services>
135 </container>
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\">
139   <services>
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\"/>
144   </services>
145 </container>
146 ", include $fixturesPath.'/containers/container16.php'),
147         );
148     }
149
150     /**
151      * @dataProvider provideCompiledContainerData
152      */
153     public function testCompiledContainerCanBeDumped($containerFile)
154     {
155         $fixturesPath = __DIR__.'/../Fixtures';
156         $container = require $fixturesPath.'/containers/'.$containerFile.'.php';
157         $container->compile();
158         $dumper = new XmlDumper($container);
159         $dumper->dump();
160
161         $this->addToAssertionCount(1);
162     }
163
164     public function provideCompiledContainerData()
165     {
166         return array(
167             array('container8'),
168             array('container9'),
169             array('container11'),
170             array('container12'),
171             array('container14'),
172         );
173     }
174
175     public function testDumpInlinedServices()
176     {
177         $container = include self::$fixturesPath.'/containers/container21.php';
178         $dumper = new XmlDumper($container);
179
180         $this->assertEquals(file_get_contents(self::$fixturesPath.'/xml/services21.xml'), $dumper->dump());
181     }
182
183     public function testDumpAutowireData()
184     {
185         $container = include self::$fixturesPath.'/containers/container24.php';
186         $dumper = new XmlDumper($container);
187
188         $this->assertEquals(file_get_contents(self::$fixturesPath.'/xml/services24.xml'), $dumper->dump());
189     }
190
191     public function testDumpLoad()
192     {
193         $container = new ContainerBuilder();
194         $loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
195         $loader->load('services_dump_load.xml');
196
197         $this->assertEquals(array(new Reference('bar', ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE)), $container->getDefinition('foo')->getArguments());
198
199         $dumper = new XmlDumper($container);
200         $this->assertStringEqualsFile(self::$fixturesPath.'/xml/services_dump_load.xml', $dumper->dump());
201     }
202
203     public function testDumpAbstractServices()
204     {
205         $container = include self::$fixturesPath.'/containers/container_abstract.php';
206         $dumper = new XmlDumper($container);
207
208         $this->assertEquals(file_get_contents(self::$fixturesPath.'/xml/services_abstract.xml'), $dumper->dump());
209     }
210 }