bug #17814 [DependencyInjection] fix dumped YAML snytax (xabbuh)

This PR was merged into the 2.3 branch.

Discussion
----------

[DependencyInjection] fix dumped YAML snytax

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets |
| License       | MIT
| Doc PR        |

see the failing tests in #17809

Commits
-------

30388f1 [DependencyInjection] fix dumped YAML snytax
This commit is contained in:
Fabien Potencier 2016-02-16 07:02:27 +01:00
commit 3f749d6559
5 changed files with 17 additions and 14 deletions

View File

@ -64,7 +64,7 @@ class YamlDumper extends Dumper
$class = substr($class, 1);
}
$code .= sprintf(" class: %s\n", $class);
$code .= sprintf(" class: %s\n", $this->dumper->dump($class));
}
if (!$definition->isPublic()) {
@ -100,7 +100,7 @@ class YamlDumper extends Dumper
}
if ($definition->getFactoryClass()) {
$code .= sprintf(" factory_class: %s\n", $definition->getFactoryClass());
$code .= sprintf(" factory_class: %s\n", $this->dumper->dump($definition->getFactoryClass()));
}
if ($definition->isLazy()) {

View File

@ -13,6 +13,7 @@ namespace Symfony\Component\DependencyInjection\Tests\Dumper;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Dumper\YamlDumper;
use Symfony\Component\Yaml\Yaml;
class YamlDumperTest extends \PHPUnit_Framework_TestCase
{
@ -27,24 +28,21 @@ class YamlDumperTest extends \PHPUnit_Framework_TestCase
{
$dumper = new YamlDumper($container = new ContainerBuilder());
$this->assertStringEqualsFile(self::$fixturesPath.'/yaml/services1.yml', $dumper->dump(), '->dump() dumps an empty container as an empty YAML file');
$container = new ContainerBuilder();
$dumper = new YamlDumper($container);
$this->assertEqualYamlStructure(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->assertStringEqualsFile(self::$fixturesPath.'/yaml/services8.yml', $dumper->dump(), '->dump() dumps parameters');
$this->assertEqualYamlStructure(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->assertEquals(str_replace('%path%', self::$fixturesPath.DIRECTORY_SEPARATOR.'includes'.DIRECTORY_SEPARATOR, file_get_contents(self::$fixturesPath.'/yaml/services9.yml')), $dumper->dump(), '->dump() dumps services');
$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());
@ -56,4 +54,9 @@ class YamlDumperTest extends \PHPUnit_Framework_TestCase
$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');
}
}
private function assertEqualYamlStructure($yaml, $expected, $message = '')
{
$this->assertEquals(Yaml::parse($expected), Yaml::parse($yaml), $message);
}
}

View File

@ -6,4 +6,4 @@ services:
class: BAR
project:
test: %project.parameter.foo%
test: '%project.parameter.foo%'

View File

@ -5,7 +5,7 @@ services:
scope.custom: { class: FooClass, scope: custom }
scope.prototype: { class: FooClass, scope: prototype }
constructor: { class: FooClass, factory_method: getInstance }
file: { class: FooClass, file: %path%/foo.php }
file: { class: FooClass, file: '%path%/foo.php' }
arguments: { class: FooClass, arguments: [foo, '@foo', [true, false]] }
configurator1: { class: FooClass, configurator: sc_configure }
configurator2: { class: FooClass, configurator: ['@baz', configure] }

View File

@ -23,16 +23,16 @@ services:
arguments: [foo, '@foo.baz', '%foo_bar%']
configurator: ['@foo.baz', configure]
foo.baz:
class: %baz_class%
factory_class: %baz_class%
class: '%baz_class%'
factory_class: '%baz_class%'
factory_method: getInstance
configurator: ['%baz_class%', configureStatic1]
foo_bar:
class: %foo_class%
class: '%foo_class%'
scope: prototype
method_call1:
class: FooClass
file: %path%foo.php
file: '%path%foo.php'
calls:
- [setBar, ['@foo']]
- [setBar, ['@?foo2']]