YamlDumperTest.php 4.6 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 PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Config\FileLocator;
  13. use Symfony\Component\DependencyInjection\ContainerBuilder;
  14. use Symfony\Component\DependencyInjection\Dumper\YamlDumper;
  15. use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
  16. use Symfony\Component\Yaml\Yaml;
  17. class YamlDumperTest extends TestCase
  18. {
  19. protected static $fixturesPath;
  20. public static function setUpBeforeClass()
  21. {
  22. self::$fixturesPath = realpath(__DIR__.'/../Fixtures/');
  23. }
  24. public function testDump()
  25. {
  26. $dumper = new YamlDumper($container = new ContainerBuilder());
  27. $this->assertEqualYamlStructure(file_get_contents(self::$fixturesPath.'/yaml/services1.yml'), $dumper->dump(), '->dump() dumps an empty container as an empty YAML file');
  28. }
  29. public function testAddParameters()
  30. {
  31. $container = include self::$fixturesPath.'/containers/container8.php';
  32. $dumper = new YamlDumper($container);
  33. $this->assertEqualYamlStructure(file_get_contents(self::$fixturesPath.'/yaml/services8.yml'), $dumper->dump(), '->dump() dumps parameters');
  34. }
  35. /**
  36. * @group legacy
  37. */
  38. public function testLegacyAddService()
  39. {
  40. $container = include self::$fixturesPath.'/containers/legacy-container9.php';
  41. $dumper = new YamlDumper($container);
  42. $this->assertEquals(str_replace('%path%', self::$fixturesPath.\DIRECTORY_SEPARATOR.'includes'.\DIRECTORY_SEPARATOR, file_get_contents(self::$fixturesPath.'/yaml/legacy-services9.yml')), $dumper->dump(), '->dump() dumps services');
  43. $dumper = new YamlDumper($container = new ContainerBuilder());
  44. $container->register('foo', 'FooClass')->addArgument(new \stdClass());
  45. try {
  46. $dumper->dump();
  47. $this->fail('->dump() throws a RuntimeException if the container to be dumped has reference to objects or resources');
  48. } catch (\Exception $e) {
  49. $this->assertInstanceOf('\RuntimeException', $e, '->dump() throws a RuntimeException if the container to be dumped has reference to objects or resources');
  50. $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');
  51. }
  52. }
  53. public function testAddService()
  54. {
  55. $container = include self::$fixturesPath.'/containers/container9.php';
  56. $dumper = new YamlDumper($container);
  57. $this->assertEqualYamlStructure(str_replace('%path%', self::$fixturesPath.\DIRECTORY_SEPARATOR.'includes'.\DIRECTORY_SEPARATOR, file_get_contents(self::$fixturesPath.'/yaml/services9.yml')), $dumper->dump(), '->dump() dumps services');
  58. $dumper = new YamlDumper($container = new ContainerBuilder());
  59. $container->register('foo', 'FooClass')->addArgument(new \stdClass());
  60. try {
  61. $dumper->dump();
  62. $this->fail('->dump() throws a RuntimeException if the container to be dumped has reference to objects or resources');
  63. } catch (\Exception $e) {
  64. $this->assertInstanceOf('\RuntimeException', $e, '->dump() throws a RuntimeException if the container to be dumped has reference to objects or resources');
  65. $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');
  66. }
  67. }
  68. public function testDumpAutowireData()
  69. {
  70. $container = include self::$fixturesPath.'/containers/container24.php';
  71. $dumper = new YamlDumper($container);
  72. $this->assertStringEqualsFile(self::$fixturesPath.'/yaml/services24.yml', $dumper->dump());
  73. }
  74. public function testDumpLoad()
  75. {
  76. $container = new ContainerBuilder();
  77. $loader = new YamlFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml'));
  78. $loader->load('services_dump_load.yml');
  79. $dumper = new YamlDumper($container);
  80. $this->assertStringEqualsFile(self::$fixturesPath.'/yaml/services_dump_load.yml', $dumper->dump());
  81. }
  82. private function assertEqualYamlStructure($yaml, $expected, $message = '')
  83. {
  84. $this->assertEquals(Yaml::parse($expected), Yaml::parse($yaml), $message);
  85. }
  86. }