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');
45 public function testLegacyAddService()
47 $container = include self::$fixturesPath.'/containers/legacy-container9.php';
48 $dumper = new YamlDumper($container);
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');
52 $dumper = new YamlDumper($container = new ContainerBuilder());
53 $container->register('foo', 'FooClass')->addArgument(new \stdClass());
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');
63 public function testAddService()
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');
69 $dumper = new YamlDumper($container = new ContainerBuilder());
70 $container->register('foo', 'FooClass')->addArgument(new \stdClass());
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');
80 public function testDumpAutowireData()
82 $container = include self::$fixturesPath.'/containers/container24.php';
83 $dumper = new YamlDumper($container);
84 $this->assertStringEqualsFile(self::$fixturesPath.'/yaml/services24.yml', $dumper->dump());
87 private function assertEqualYamlStructure($yaml, $expected, $message = '')
89 $this->assertEquals(Yaml::parse($expected), Yaml::parse($yaml), $message);