YamlDumperTest.php 4.1 KB

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