XmlDumperTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 Symfony\Component\DependencyInjection\ContainerBuilder;
  12. use Symfony\Component\DependencyInjection\Dumper\XmlDumper;
  13. class XmlDumperTest extends \PHPUnit_Framework_TestCase
  14. {
  15. protected static $fixturesPath;
  16. public static 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 testDumpAnonymousServices()
  55. {
  56. include self::$fixturesPath.'/containers/container11.php';
  57. $dumper = new XmlDumper($container);
  58. $this->assertEquals("<?xml version=\"1.0\" encoding=\"utf-8\"?>
  59. <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\">
  60. <services>
  61. <service id=\"foo\" class=\"FooClass\">
  62. <argument type=\"service\">
  63. <service class=\"BarClass\">
  64. <argument type=\"service\">
  65. <service class=\"BazClass\"/>
  66. </argument>
  67. </service>
  68. </argument>
  69. </service>
  70. </services>
  71. </container>
  72. ", $dumper->dump());
  73. }
  74. public function testDumpEntities()
  75. {
  76. include self::$fixturesPath.'/containers/container12.php';
  77. $dumper = new XmlDumper($container);
  78. $this->assertEquals("<?xml version=\"1.0\" encoding=\"utf-8\"?>
  79. <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\">
  80. <services>
  81. <service id=\"foo\" class=\"FooClass\Foo\">
  82. <tag name=\"foo&quot;bar\bar\" foo=\"foo&quot;barřž€\"/>
  83. <argument>foo&lt;&gt;&amp;bar</argument>
  84. </service>
  85. </services>
  86. </container>
  87. ", $dumper->dump());
  88. }
  89. }