This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Component/DependencyInjection/Tests/Dumper/YamlDumperTest.php

74 lines
3.0 KiB
PHP
Raw Normal View History

<?php
/*
2010-04-25 16:06:54 +01:00
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
2010-04-07 02:07:59 +01:00
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\DependencyInjection\Tests\Dumper;
2017-02-08 07:24:27 +00:00
use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Dumper\YamlDumper;
use Symfony\Component\Yaml\Yaml;
2017-02-06 17:23:56 +00:00
use Symfony\Component\Yaml\Parser;
2017-02-08 07:24:27 +00:00
class YamlDumperTest extends TestCase
{
2012-07-09 13:50:58 +01:00
protected static $fixturesPath;
2012-07-09 13:50:58 +01:00
public static function setUpBeforeClass()
{
2010-06-28 08:31:54 +01:00
self::$fixturesPath = realpath(__DIR__.'/../Fixtures/');
}
public function testDump()
{
$dumper = new YamlDumper($container = new ContainerBuilder());
$this->assertEqualYamlStructure(file_get_contents(self::$fixturesPath.'/yaml/services1.yml'), $dumper->dump(), '->dump() dumps an empty container as an empty YAML file');
}
public function testAddParameters()
{
$container = include self::$fixturesPath.'/containers/container8.php';
$dumper = new YamlDumper($container);
$this->assertEqualYamlStructure(file_get_contents(self::$fixturesPath.'/yaml/services8.yml'), $dumper->dump(), '->dump() dumps parameters');
}
public function testAddService()
{
$container = include self::$fixturesPath.'/containers/container9.php';
$dumper = new YamlDumper($container);
$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');
$dumper = new YamlDumper($container = new ContainerBuilder());
$container->register('foo', 'FooClass')->addArgument(new \stdClass());
try {
$dumper->dump();
$this->fail('->dump() throws a RuntimeException if the container to be dumped has reference to objects or resources');
} catch (\Exception $e) {
$this->assertInstanceOf('\RuntimeException', $e, '->dump() throws a RuntimeException if the container to be dumped has reference to objects or resources');
$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');
}
}
public function testDumpAutowireData()
{
$container = include self::$fixturesPath.'/containers/container24.php';
$dumper = new YamlDumper($container);
$this->assertStringEqualsFile(self::$fixturesPath.'/yaml/services24.yml', $dumper->dump());
}
2017-02-06 17:23:56 +00:00
private function assertEqualYamlStructure($expected, $yaml, $message = '')
{
2017-02-06 17:23:56 +00:00
$parser = new Parser();
$this->assertEquals($parser->parse($expected, Yaml::PARSE_CUSTOM_TAGS), $parser->parse($yaml, Yaml::PARSE_CUSTOM_TAGS), $message);
}
}