11abe3b9235382f38feacf6d54e501b299a4d368
[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     public function testAddService()
43     {
44         $container = include self::$fixturesPath.'/containers/container9.php';
45         $dumper = new YamlDumper($container);
46         $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');
47
48         $dumper = new YamlDumper($container = new ContainerBuilder());
49         $container->register('foo', 'FooClass')->addArgument(new \stdClass());
50         try {
51             $dumper->dump();
52             $this->fail('->dump() throws a RuntimeException if the container to be dumped has reference to objects or resources');
53         } catch (\Exception $e) {
54             $this->assertInstanceOf('\RuntimeException', $e, '->dump() throws a RuntimeException if the container to be dumped has reference to objects or resources');
55             $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');
56         }
57     }
58
59     public function testDumpAutowireData()
60     {
61         $container = include self::$fixturesPath.'/containers/container24.php';
62         $dumper = new YamlDumper($container);
63         $this->assertStringEqualsFile(self::$fixturesPath.'/yaml/services24.yml', $dumper->dump());
64     }
65
66     private function assertEqualYamlStructure($yaml, $expected, $message = '')
67     {
68         $this->assertEquals(Yaml::parse($expected), Yaml::parse($yaml), $message);
69     }
70 }