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