XmlDumperTest.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\DependencyInjection\Tests\Dumper;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\DependencyInjection\ContainerBuilder;
  13. use Symfony\Component\DependencyInjection\Dumper\XmlDumper;
  14. class XmlDumperTest extends TestCase
  15. {
  16. protected static $fixturesPath;
  17. public static function setUpBeforeClass()
  18. {
  19. self::$fixturesPath = realpath(__DIR__.'/../Fixtures/');
  20. }
  21. public function testDump()
  22. {
  23. $dumper = new XmlDumper(new ContainerBuilder());
  24. $this->assertXmlStringEqualsXmlFile(self::$fixturesPath.'/xml/services1.xml', $dumper->dump(), '->dump() dumps an empty container as an empty XML file');
  25. }
  26. public function testExportParameters()
  27. {
  28. $container = include self::$fixturesPath.'//containers/container8.php';
  29. $dumper = new XmlDumper($container);
  30. $this->assertXmlStringEqualsXmlFile(self::$fixturesPath.'/xml/services8.xml', $dumper->dump(), '->dump() dumps parameters');
  31. }
  32. public function testAddParameters()
  33. {
  34. $container = include self::$fixturesPath.'//containers/container8.php';
  35. $dumper = new XmlDumper($container);
  36. $this->assertXmlStringEqualsXmlFile(self::$fixturesPath.'/xml/services8.xml', $dumper->dump(), '->dump() dumps parameters');
  37. }
  38. /**
  39. * @group legacy
  40. */
  41. public function testLegacyAddService()
  42. {
  43. $container = include self::$fixturesPath.'/containers/legacy-container9.php';
  44. $dumper = new XmlDumper($container);
  45. $this->assertEquals(str_replace('%path%', self::$fixturesPath.\DIRECTORY_SEPARATOR.'includes'.\DIRECTORY_SEPARATOR, file_get_contents(self::$fixturesPath.'/xml/legacy-services9.xml')), $dumper->dump(), '->dump() dumps services');
  46. $dumper = new XmlDumper($container = new ContainerBuilder());
  47. $container->register('foo', 'FooClass')->addArgument(new \stdClass());
  48. try {
  49. $dumper->dump();
  50. $this->fail('->dump() throws a RuntimeException if the container to be dumped has reference to objects or resources');
  51. } catch (\Exception $e) {
  52. $this->assertInstanceOf('\RuntimeException', $e, '->dump() throws a RuntimeException if the container to be dumped has reference to objects or resources');
  53. $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');
  54. }
  55. }
  56. public function testAddService()
  57. {
  58. $container = include self::$fixturesPath.'/containers/container9.php';
  59. $dumper = new XmlDumper($container);
  60. $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');
  61. $dumper = new XmlDumper($container = new ContainerBuilder());
  62. $container->register('foo', 'FooClass')->addArgument(new \stdClass());
  63. try {
  64. $dumper->dump();
  65. $this->fail('->dump() throws a RuntimeException if the container to be dumped has reference to objects or resources');
  66. } catch (\Exception $e) {
  67. $this->assertInstanceOf('\RuntimeException', $e, '->dump() throws a RuntimeException if the container to be dumped has reference to objects or resources');
  68. $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');
  69. }
  70. }
  71. public function testDumpAnonymousServices()
  72. {
  73. $container = include self::$fixturesPath.'/containers/container11.php';
  74. $dumper = new XmlDumper($container);
  75. $this->assertEquals('<?xml version="1.0" encoding="utf-8"?>
  76. <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. <services>
  78. <service id="foo" class="FooClass">
  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. </services>
  88. </container>
  89. ', $dumper->dump());
  90. }
  91. public function testDumpEntities()
  92. {
  93. $container = include self::$fixturesPath.'/containers/container12.php';
  94. $dumper = new XmlDumper($container);
  95. $this->assertEquals("<?xml version=\"1.0\" encoding=\"utf-8\"?>
  96. <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\">
  97. <services>
  98. <service id=\"foo\" class=\"FooClass\Foo\">
  99. <tag name=\"foo&quot;bar\bar\" foo=\"foo&quot;barřž€\"/>
  100. <argument>foo&lt;&gt;&amp;bar</argument>
  101. </service>
  102. </services>
  103. </container>
  104. ", $dumper->dump());
  105. }
  106. /**
  107. * @dataProvider provideDecoratedServicesData
  108. */
  109. public function testDumpDecoratedServices($expectedXmlDump, $container)
  110. {
  111. $dumper = new XmlDumper($container);
  112. $this->assertEquals($expectedXmlDump, $dumper->dump());
  113. }
  114. public function provideDecoratedServicesData()
  115. {
  116. $fixturesPath = realpath(__DIR__.'/../Fixtures/');
  117. return array(
  118. array("<?xml version=\"1.0\" encoding=\"utf-8\"?>
  119. <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\">
  120. <services>
  121. <service id=\"foo\" class=\"FooClass\Foo\" decorates=\"bar\" decoration-inner-name=\"bar.woozy\"/>
  122. </services>
  123. </container>
  124. ", include $fixturesPath.'/containers/container15.php'),
  125. array("<?xml version=\"1.0\" encoding=\"utf-8\"?>
  126. <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\">
  127. <services>
  128. <service id=\"foo\" class=\"FooClass\Foo\" decorates=\"bar\"/>
  129. </services>
  130. </container>
  131. ", include $fixturesPath.'/containers/container16.php'),
  132. );
  133. }
  134. /**
  135. * @dataProvider provideCompiledContainerData
  136. */
  137. public function testCompiledContainerCanBeDumped($containerFile)
  138. {
  139. $fixturesPath = __DIR__.'/../Fixtures';
  140. $container = require $fixturesPath.'/containers/'.$containerFile.'.php';
  141. $container->compile();
  142. $dumper = new XmlDumper($container);
  143. $dumper->dump();
  144. $this->addToAssertionCount(1);
  145. }
  146. public function provideCompiledContainerData()
  147. {
  148. return array(
  149. array('container8'),
  150. array('container9'),
  151. array('container11'),
  152. array('container12'),
  153. array('container14'),
  154. );
  155. }
  156. public function testDumpInlinedServices()
  157. {
  158. $container = include self::$fixturesPath.'/containers/container21.php';
  159. $dumper = new XmlDumper($container);
  160. $this->assertEquals(file_get_contents(self::$fixturesPath.'/xml/services21.xml'), $dumper->dump());
  161. }
  162. public function testDumpAutowireData()
  163. {
  164. $container = include self::$fixturesPath.'/containers/container24.php';
  165. $dumper = new XmlDumper($container);
  166. $this->assertEquals(file_get_contents(self::$fixturesPath.'/xml/services24.xml'), $dumper->dump());
  167. }
  168. public function testDumpAbstractServices()
  169. {
  170. $container = include self::$fixturesPath.'/containers/container_abstract.php';
  171. $dumper = new XmlDumper($container);
  172. $this->assertEquals(file_get_contents(self::$fixturesPath.'/xml/services_abstract.xml'), $dumper->dump());
  173. }
  174. }