ff2239fb64d66b4c335e2da4e93d6c7e25ba0f80
[yaffs-website] / vendor / symfony / dependency-injection / Tests / Dumper / YamlDumperTest.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\DependencyInjection\Tests\Dumper;
13
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;
24
25 class YamlDumperTest extends TestCase
26 {
27     protected static $fixturesPath;
28
29     public static function setUpBeforeClass()
30     {
31         self::$fixturesPath = realpath(__DIR__.'/../Fixtures/');
32     }
33
34     public function testDump()
35     {
36         $dumper = new YamlDumper($container = new ContainerBuilder());
37
38         $this->assertEqualYamlStructure(file_get_contents(self::$fixturesPath.'/yaml/services1.yml'), $dumper->dump(), '->dump() dumps an empty container as an empty YAML file');
39     }
40
41     public function testAddParameters()
42     {
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');
46     }
47
48     public function testAddService()
49     {
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');
53
54         $dumper = new YamlDumper($container = new ContainerBuilder());
55         $container->register('foo', 'FooClass')->addArgument(new \stdClass())->setPublic(true);
56         try {
57             $dumper->dump();
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');
62         }
63     }
64
65     public function testDumpAutowireData()
66     {
67         $container = include self::$fixturesPath.'/containers/container24.php';
68         $dumper = new YamlDumper($container);
69         $this->assertStringEqualsFile(self::$fixturesPath.'/yaml/services24.yml', $dumper->dump());
70     }
71
72     public function testDumpLoad()
73     {
74         $container = new ContainerBuilder();
75         $loader = new YamlFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml'));
76         $loader->load('services_dump_load.yml');
77
78         $this->assertEquals(array(new Reference('bar', ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE)), $container->getDefinition('foo')->getArguments());
79
80         $dumper = new YamlDumper($container);
81         $this->assertStringEqualsFile(self::$fixturesPath.'/yaml/services_dump_load.yml', $dumper->dump());
82     }
83
84     public function testInlineServices()
85     {
86         $container = new ContainerBuilder();
87         $container->register('foo', 'Class1')
88             ->setPublic(true)
89             ->addArgument((new Definition('Class2'))
90                 ->addArgument(new Definition('Class2'))
91             )
92         ;
93
94         $dumper = new YamlDumper($container);
95         $this->assertStringEqualsFile(self::$fixturesPath.'/yaml/services_inline.yml', $dumper->dump());
96     }
97
98     private function assertEqualYamlStructure($expected, $yaml, $message = '')
99     {
100         $parser = new Parser();
101
102         $this->assertEquals($parser->parse($expected, Yaml::PARSE_CUSTOM_TAGS), $parser->parse($yaml, Yaml::PARSE_CUSTOM_TAGS), $message);
103     }
104 }