XmlDumperTest.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. namespace Symfony\Tests\Component\DependencyInjection\Dumper;
  10. use Symfony\Component\DependencyInjection\ContainerBuilder;
  11. use Symfony\Component\DependencyInjection\Dumper\XmlDumper;
  12. use Symfony\Component\DependencyInjection\InterfaceInjector;
  13. class XmlDumperTest extends \PHPUnit_Framework_TestCase
  14. {
  15. static protected $fixturesPath;
  16. static public function setUpBeforeClass()
  17. {
  18. self::$fixturesPath = realpath(__DIR__.'/../Fixtures/');
  19. }
  20. public function testDump()
  21. {
  22. $dumper = new XmlDumper($container = new ContainerBuilder());
  23. $this->assertXmlStringEqualsXmlFile(self::$fixturesPath.'/xml/services1.xml', $dumper->dump(), '->dump() dumps an empty container as an empty XML file');
  24. $container = new ContainerBuilder();
  25. $dumper = new XmlDumper($container);
  26. }
  27. public function testExportParameters()
  28. {
  29. $container = include self::$fixturesPath.'//containers/container8.php';
  30. $dumper = new XmlDumper($container);
  31. $this->assertXmlStringEqualsXmlFile(self::$fixturesPath.'/xml/services8.xml', $dumper->dump(), '->dump() dumps parameters');
  32. }
  33. public function testAddParameters()
  34. {
  35. $container = include self::$fixturesPath.'//containers/container8.php';
  36. $dumper = new XmlDumper($container);
  37. $this->assertXmlStringEqualsXmlFile(self::$fixturesPath.'/xml/services8.xml', $dumper->dump(), '->dump() dumps parameters');
  38. }
  39. public function testAddService()
  40. {
  41. $container = include self::$fixturesPath.'/containers/container9.php';
  42. $dumper = new XmlDumper($container);
  43. $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');
  44. $dumper = new XmlDumper($container = new ContainerBuilder());
  45. $container->register('foo', 'FooClass')->addArgument(new \stdClass());
  46. try {
  47. $dumper->dump();
  48. $this->fail('->dump() throws a RuntimeException if the container to be dumped has reference to objects or resources');
  49. } catch (\Exception $e) {
  50. $this->assertInstanceOf('\RuntimeException', $e, '->dump() throws a RuntimeException if the container to be dumped has reference to objects or resources');
  51. $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');
  52. }
  53. }
  54. public function testInterfaceInjectors()
  55. {
  56. $interfaceInjector = new InterfaceInjector('FooClass');
  57. $interfaceInjector->addMethodCall('setBar', array('someValue'));
  58. $container = include self::$fixturesPath.'/containers/interfaces1.php';
  59. $container->addInterfaceInjector($interfaceInjector);
  60. $dumper = new XmlDumper($container);
  61. $classBody = $dumper->dump();
  62. //TODO: find a better way to test dumper
  63. //var_dump($classBody);
  64. $this->assertEquals("<?xml version=\"1.0\" encoding=\"utf-8\"?>
  65. <container xmlns=\"http://www.symfony-project.org/schema/dic/services\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.symfony-project.org/schema/dic/services http://www.symfony-project.org/schema/dic/services/services-1.0.xsd\">
  66. <parameters>
  67. <parameter key=\"cla\">Fo</parameter>
  68. <parameter key=\"ss\">Class</parameter>
  69. </parameters>
  70. <interfaces>
  71. <interface class=\"FooClass\">
  72. <call method=\"setBar\">
  73. <argument>someValue</argument>
  74. </call>
  75. </interface>
  76. </interfaces>
  77. <services>
  78. <service id=\"foo\" class=\"%cla%o%ss%\"/>
  79. </services>
  80. </container>
  81. ", $classBody);
  82. }
  83. public function testDumpAnonymousServices()
  84. {
  85. include self::$fixturesPath.'/containers/container11.php';
  86. $dumper = new XmlDumper($container);
  87. $this->assertEquals("<?xml version=\"1.0\" encoding=\"utf-8\"?>
  88. <container xmlns=\"http://www.symfony-project.org/schema/dic/services\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.symfony-project.org/schema/dic/services http://www.symfony-project.org/schema/dic/services/services-1.0.xsd\">
  89. <services>
  90. <service id=\"foo\" class=\"FooClass\">
  91. <argument type=\"service\">
  92. <service class=\"BarClass\">
  93. <argument type=\"service\">
  94. <service class=\"BazClass\"/>
  95. </argument>
  96. </service>
  97. </argument>
  98. </service>
  99. </services>
  100. </container>
  101. ", $dumper->dump());
  102. }
  103. public function testDumpEntities()
  104. {
  105. include self::$fixturesPath.'/containers/container12.php';
  106. $dumper = new XmlDumper($container);
  107. $this->assertEquals("<?xml version=\"1.0\" encoding=\"utf-8\"?>
  108. <container xmlns=\"http://www.symfony-project.org/schema/dic/services\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.symfony-project.org/schema/dic/services http://www.symfony-project.org/schema/dic/services/services-1.0.xsd\">
  109. <services>
  110. <service id=\"foo\" class=\"FooClass\Foo\">
  111. <tag name=\"foo&quot;bar\bar\" foo=\"foo&quot;barřž€\"/>
  112. <argument>foo&lt;&gt;&amp;bar</argument>
  113. </service>
  114. </services>
  115. </container>
  116. ", $dumper->dump());
  117. }
  118. }