4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\DependencyInjection\Tests\Dumper;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\Config\FileLocator;
16 use Symfony\Component\DependencyInjection\ContainerBuilder;
17 use Symfony\Component\DependencyInjection\ContainerInterface;
18 use Symfony\Component\DependencyInjection\Definition;
19 use Symfony\Component\DependencyInjection\Dumper\YamlDumper;
20 use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
21 use Symfony\Component\DependencyInjection\Reference;
22 use Symfony\Component\Yaml\Parser;
23 use Symfony\Component\Yaml\Yaml;
25 class YamlDumperTest extends TestCase
27 protected static $fixturesPath;
29 public static function setUpBeforeClass()
31 self::$fixturesPath = realpath(__DIR__.'/../Fixtures/');
34 public function testDump()
36 $dumper = new YamlDumper($container = new ContainerBuilder());
38 $this->assertEqualYamlStructure(file_get_contents(self::$fixturesPath.'/yaml/services1.yml'), $dumper->dump(), '->dump() dumps an empty container as an empty YAML file');
41 public function testAddParameters()
43 $container = include self::$fixturesPath.'/containers/container8.php';
44 $dumper = new YamlDumper($container);
45 $this->assertEqualYamlStructure(file_get_contents(self::$fixturesPath.'/yaml/services8.yml'), $dumper->dump(), '->dump() dumps parameters');
48 public function testAddService()
50 $container = include self::$fixturesPath.'/containers/container9.php';
51 $dumper = new YamlDumper($container);
52 $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');
54 $dumper = new YamlDumper($container = new ContainerBuilder());
55 $container->register('foo', 'FooClass')->addArgument(new \stdClass())->setPublic(true);
58 $this->fail('->dump() throws a RuntimeException if the container to be dumped has reference to objects or resources');
59 } catch (\Exception $e) {
60 $this->assertInstanceOf('\RuntimeException', $e, '->dump() throws a RuntimeException if the container to be dumped has reference to objects or resources');
61 $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');
65 public function testDumpAutowireData()
67 $container = include self::$fixturesPath.'/containers/container24.php';
68 $dumper = new YamlDumper($container);
69 $this->assertStringEqualsFile(self::$fixturesPath.'/yaml/services24.yml', $dumper->dump());
72 public function testDumpLoad()
74 $container = new ContainerBuilder();
75 $loader = new YamlFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml'));
76 $loader->load('services_dump_load.yml');
78 $this->assertEquals(array(new Reference('bar', ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE)), $container->getDefinition('foo')->getArguments());
80 $dumper = new YamlDumper($container);
81 $this->assertStringEqualsFile(self::$fixturesPath.'/yaml/services_dump_load.yml', $dumper->dump());
84 public function testInlineServices()
86 $container = new ContainerBuilder();
87 $container->register('foo', 'Class1')
89 ->addArgument((new Definition('Class2'))
90 ->addArgument(new Definition('Class2'))
94 $dumper = new YamlDumper($container);
95 $this->assertStringEqualsFile(self::$fixturesPath.'/yaml/services_inline.yml', $dumper->dump());
98 private function assertEqualYamlStructure($expected, $yaml, $message = '')
100 $parser = new Parser();
102 $this->assertEquals($parser->parse($expected, Yaml::PARSE_CUSTOM_TAGS), $parser->parse($yaml, Yaml::PARSE_CUSTOM_TAGS), $message);