XmlDumperTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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\" ?>
  65. <container xmlns=\"http://www.symfony-project.org/schema/dic/services\"
  66. xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
  67. xsi:schemaLocation=\"http://www.symfony-project.org/schema/dic/services http://www.symfony-project.org/schema/dic/services/services-1.0.xsd\">
  68. <parameters>
  69. <parameter key=\"cla\">Fo</parameter>
  70. <parameter key=\"ss\">Class</parameter>
  71. </parameters>
  72. <interfaces>
  73. <interface class=\"FooClass\">
  74. <call method=\"setBar\">
  75. <argument>someValue</argument>
  76. </call>
  77. </interface>
  78. </interfaces>
  79. <services>
  80. <service id=\"foo\" class=\"%cla%o%ss%\">
  81. </service>
  82. </services>
  83. </container>
  84. ", $classBody);
  85. }
  86. }