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\DependencyInjection\ContainerBuilder;
16 use Symfony\Component\DependencyInjection\Dumper\YamlDumper;
17 use Symfony\Component\Yaml\Yaml;
19 class YamlDumperTest extends TestCase
21 protected static $fixturesPath;
23 public static function setUpBeforeClass()
25 self::$fixturesPath = realpath(__DIR__.'/../Fixtures/');
28 public function testDump()
30 $dumper = new YamlDumper($container = new ContainerBuilder());
32 $this->assertEqualYamlStructure(file_get_contents(self::$fixturesPath.'/yaml/services1.yml'), $dumper->dump(), '->dump() dumps an empty container as an empty YAML file');
35 public function testAddParameters()
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');
42 public function testAddService()
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');
48 $dumper = new YamlDumper($container = new ContainerBuilder());
49 $container->register('foo', 'FooClass')->addArgument(new \stdClass());
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');
59 public function testDumpAutowireData()
61 $container = include self::$fixturesPath.'/containers/container24.php';
62 $dumper = new YamlDumper($container);
63 $this->assertStringEqualsFile(self::$fixturesPath.'/yaml/services24.yml', $dumper->dump());
66 private function assertEqualYamlStructure($yaml, $expected, $message = '')
68 $this->assertEquals(Yaml::parse($expected), Yaml::parse($yaml), $message);