Skip to content

Commit

Permalink
bug #29102 [DI] fix GraphvizDumper ignoring inline definitions (nicol…
Browse files Browse the repository at this point in the history
…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 69ae468
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 6 deletions.
Expand Up @@ -149,6 +149,14 @@ private function findEdges($id, array $arguments, $required, $name, $lazy = fals
$edges[] = array('name' => $name, 'required' => $required, 'to' => $argument, 'lazy' => $lazyEdge); $edges[] = array('name' => $name, 'required' => $required, 'to' => $argument, 'lazy' => $lazyEdge);
} elseif ($argument instanceof ArgumentInterface) { } elseif ($argument instanceof ArgumentInterface) {
$edges = array_merge($edges, $this->findEdges($id, $argument->getValues(), $required, $name, true)); $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)) { } elseif (\is_array($argument)) {
$edges = array_merge($edges, $this->findEdges($id, $argument, $required, $name, $lazy)); $edges = array_merge($edges, $this->findEdges($id, $argument, $required, $name, $lazy));
} }
Expand Down
Expand Up @@ -13,7 +13,9 @@


use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Dumper\GraphvizDumper; use Symfony\Component\DependencyInjection\Dumper\GraphvizDumper;
use Symfony\Component\DependencyInjection\Reference;


class GraphvizDumperTest extends TestCase class GraphvizDumperTest extends TestCase
{ {
Expand All @@ -32,11 +34,11 @@ public function testDump()


$container = include self::$fixturesPath.'/containers/container9.php'; $container = include self::$fixturesPath.'/containers/container9.php';
$dumper = new GraphvizDumper($container); $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'; $container = include self::$fixturesPath.'/containers/container10.php';
$dumper = new GraphvizDumper($container); $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'; $container = include self::$fixturesPath.'/containers/container10.php';
$dumper = new GraphvizDumper($container); $dumper = new GraphvizDumper($container);
Expand All @@ -47,28 +49,40 @@ public function testDump()
'node.instance' => array('fillcolor' => 'green', 'style' => 'empty'), 'node.instance' => array('fillcolor' => 'green', 'style' => 'empty'),
'node.definition' => array('fillcolor' => 'grey'), 'node.definition' => array('fillcolor' => 'grey'),
'node.missing' => array('fillcolor' => 'red', 'style' => 'empty'), '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() public function testDumpWithFrozenContainer()
{ {
$container = include self::$fixturesPath.'/containers/container13.php'; $container = include self::$fixturesPath.'/containers/container13.php';
$dumper = new GraphvizDumper($container); $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() public function testDumpWithFrozenCustomClassContainer()
{ {
$container = include self::$fixturesPath.'/containers/container14.php'; $container = include self::$fixturesPath.'/containers/container14.php';
$dumper = new GraphvizDumper($container); $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() public function testDumpWithUnresolvedParameter()
{ {
$container = include self::$fixturesPath.'/containers/container17.php'; $container = include self::$fixturesPath.'/containers/container17.php';
$dumper = new GraphvizDumper($container); $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.