YamlDumperTest.php 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. use Symfony\Component\Yaml\Parser;
  18. class YamlDumperTest extends TestCase
  19. {
  20. protected static $fixturesPath;
  21. public static function setUpBeforeClass()
  22. {
  23. self::$fixturesPath = realpath(__DIR__.'/../Fixtures/');
  24. }
  25. public function testDump()
  26. {
  27. $dumper = new YamlDumper($container = new ContainerBuilder());
  28. $this->assertEqualYamlStructure(file_get_contents(self::$fixturesPath.'/yaml/services1.yml'), $dumper->dump(), '->dump() dumps an empty container as an empty YAML file');
  29. }
  30. public function testAddParameters()
  31. {
  32. $container = include self::$fixturesPath.'/containers/container8.php';
  33. $dumper = new YamlDumper($container);
  34. $this->assertEqualYamlStructure(file_get_contents(self::$fixturesPath.'/yaml/services8.yml'), $dumper->dump(), '->dump() dumps parameters');
  35. }
  36. public function testAddService()
  37. {
  38. $container = include self::$fixturesPath.'/containers/container9.php';
  39. $dumper = new YamlDumper($container);
  40. $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');
  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 testDumpAutowireData()
  52. {
  53. $container = include self::$fixturesPath.'/containers/container24.php';
  54. $dumper = new YamlDumper($container);
  55. $this->assertStringEqualsFile(self::$fixturesPath.'/yaml/services24.yml', $dumper->dump());
  56. }
  57. public function testDumpLoad()
  58. {
  59. $container = new ContainerBuilder();
  60. $loader = new YamlFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml'));
  61. $loader->load('services_dump_load.yml');
  62. $dumper = new YamlDumper($container);
  63. $this->assertStringEqualsFile(self::$fixturesPath.'/yaml/services_dump_load.yml', $dumper->dump());
  64. }
  65. private function assertEqualYamlStructure($expected, $yaml, $message = '')
  66. {
  67. $parser = new Parser();
  68. $this->assertEquals($parser->parse($expected, Yaml::PARSE_CUSTOM_TAGS), $parser->parse($yaml, Yaml::PARSE_CUSTOM_TAGS), $message);
  69. }
  70. }