Permalink
Browse files

bug #29102 [DI] fix GraphvizDumper ignoring inline definitions (nicol…

…as-grekas)

This PR was merged into the 3.4 branch.

Discussion
----------

[DI] fix GraphvizDumper ignoring inline definitions

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

Since SF3, inline definitions are first class citizen. I noticed that the GraphvizDumper does not inspect them. Here is the fix.

Commits
-------

ebe6265 [DI] fix GraphvizDumper ignoring inline definitions
  • Loading branch information...
nicolas-grekas committed Nov 6, 2018
2 parents 6cfd3de + ebe6265 commit 69ae46835187085147f9e0c3f28de5fc412fc00d
@@ -149,6 +149,14 @@ private function findEdges($id, array $arguments, $required, $name, $lazy = fals
$edges[] = array('name' => $name, 'required' => $required, 'to' => $argument, 'lazy' => $lazyEdge);
} elseif ($argument instanceof ArgumentInterface) {
$edges = array_merge($edges, $this->findEdges($id, $argument->getValues(), $required, $name, true));
} elseif ($argument instanceof Definition) {
$edges = array_merge($edges,
$this->findEdges($id, $argument->getArguments(), $required, ''),
$this->findEdges($id, $argument->getProperties(), false, '')
);
foreach ($argument->getMethodCalls() as $call) {
$edges = array_merge($edges, $this->findEdges($id, $call[1], false, $call[0].'()'));
}
} elseif (\is_array($argument)) {
$edges = array_merge($edges, $this->findEdges($id, $argument, $required, $name, $lazy));
}
@@ -13,7 +13,9 @@
use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Dumper\GraphvizDumper;
use Symfony\Component\DependencyInjection\Reference;
class GraphvizDumperTest extends TestCase
{
@@ -32,11 +34,11 @@ public function testDump()
$container = include self::$fixturesPath.'/containers/container9.php';
$dumper = new GraphvizDumper($container);
$this->assertEquals(str_replace('%path%', __DIR__, file_get_contents(self::$fixturesPath.'/graphviz/services9.dot')), $dumper->dump(), '->dump() dumps services');
$this->assertStringEqualsFile(self::$fixturesPath.'/graphviz/services9.dot', $dumper->dump(), '->dump() dumps services');
$container = include self::$fixturesPath.'/containers/container10.php';
$dumper = new GraphvizDumper($container);
$this->assertEquals(str_replace('%path%', __DIR__, file_get_contents(self::$fixturesPath.'/graphviz/services10.dot')), $dumper->dump(), '->dump() dumps services');
$this->assertStringEqualsFile(self::$fixturesPath.'/graphviz/services10.dot', $dumper->dump(), '->dump() dumps services');
$container = include self::$fixturesPath.'/containers/container10.php';
$dumper = new GraphvizDumper($container);
@@ -47,28 +49,40 @@ public function testDump()
'node.instance' => array('fillcolor' => 'green', 'style' => 'empty'),
'node.definition' => array('fillcolor' => 'grey'),
'node.missing' => array('fillcolor' => 'red', 'style' => 'empty'),
)), str_replace('%path%', __DIR__, file_get_contents(self::$fixturesPath.'/graphviz/services10-1.dot')), '->dump() dumps services');
)), file_get_contents(self::$fixturesPath.'/graphviz/services10-1.dot'), '->dump() dumps services');
}
public function testDumpWithFrozenContainer()
{
$container = include self::$fixturesPath.'/containers/container13.php';
$dumper = new GraphvizDumper($container);
$this->assertEquals(str_replace('%path%', __DIR__, file_get_contents(self::$fixturesPath.'/graphviz/services13.dot')), $dumper->dump(), '->dump() dumps services');
$this->assertStringEqualsFile(self::$fixturesPath.'/graphviz/services13.dot', $dumper->dump(), '->dump() dumps services');
}
public function testDumpWithFrozenCustomClassContainer()
{
$container = include self::$fixturesPath.'/containers/container14.php';
$dumper = new GraphvizDumper($container);
$this->assertEquals(str_replace('%path%', __DIR__, file_get_contents(self::$fixturesPath.'/graphviz/services14.dot')), $dumper->dump(), '->dump() dumps services');
$this->assertStringEqualsFile(self::$fixturesPath.'/graphviz/services14.dot', $dumper->dump(), '->dump() dumps services');
}
public function testDumpWithUnresolvedParameter()
{
$container = include self::$fixturesPath.'/containers/container17.php';
$dumper = new GraphvizDumper($container);
$this->assertEquals(str_replace('%path%', __DIR__, file_get_contents(self::$fixturesPath.'/graphviz/services17.dot')), $dumper->dump(), '->dump() dumps services');
$this->assertStringEqualsFile(self::$fixturesPath.'/graphviz/services17.dot', $dumper->dump(), '->dump() dumps services');
}
public function testDumpWithInlineDefinition()
{
$container = new ContainerBuilder();
$container->register('foo', 'stdClass')->addArgument(
(new Definition('stdClass'))->addArgument(new Reference('bar'))
);
$container->register('bar', 'stdClass');
$dumper = new GraphvizDumper($container);
$this->assertStringEqualsFile(self::$fixturesPath.'/graphviz/services_inline.dot', $dumper->dump(), '->dump() dumps nested references');
}
}
@@ -0,0 +1,10 @@
digraph sc {
ratio="compress"
node [fontsize="11" fontname="Arial" shape="record"];
edge [fontsize="9" fontname="Arial" color="grey" arrowhead="open" arrowsize="0.5"];
node_service_container [label="service_container (Psr\Container\ContainerInterface, Symfony\Component\DependencyInjection\ContainerInterface)\nSymfony\\Component\\DependencyInjection\\ContainerInterface\n", shape=record, fillcolor="#eeeeee", style="filled"];
node_foo [label="foo\nstdClass\n", shape=record, fillcolor="#eeeeee", style="filled"];
node_bar [label="bar\nstdClass\n", shape=record, fillcolor="#eeeeee", style="filled"];
node_foo -> node_bar [label="" style="filled"];
}

0 comments on commit 69ae468

Please sign in to comment.