/
AnalyzeServiceReferencesPassTest.php
127 lines (99 loc) · 3.66 KB
/
AnalyzeServiceReferencesPassTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\DependencyInjection\Tests\Compiler;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Compiler\Compiler;
use Symfony\Component\DependencyInjection\Compiler\AnalyzeServiceReferencesPass;
use Symfony\Component\DependencyInjection\Compiler\RepeatedPass;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\ContainerBuilder;
class AnalyzeServiceReferencesPassTest extends \PHPUnit_Framework_TestCase
{
public function testProcess()
{
$container = new ContainerBuilder();
$a = $container
->register('a')
->addArgument($ref1 = new Reference('b'))
;
$b = $container
->register('b')
->addMethodCall('setA', array($ref2 = new Reference('a')))
;
$c = $container
->register('c')
->addArgument($ref3 = new Reference('a'))
->addArgument($ref4 = new Reference('b'))
;
$d = $container
->register('d')
->setProperty('foo', $ref5 = new Reference('b'))
;
$e = $container
->register('e')
->setConfigurator(array($ref6 = new Reference('b'), 'methodName'))
;
$graph = $this->process($container);
$this->assertCount(4, $edges = $graph->getNode('b')->getInEdges());
$this->assertSame($ref1, $edges[0]->getValue());
$this->assertSame($ref4, $edges[1]->getValue());
$this->assertSame($ref5, $edges[2]->getValue());
$this->assertSame($ref6, $edges[3]->getValue());
}
public function testProcessDetectsReferencesFromInlinedDefinitions()
{
$container = new ContainerBuilder();
$container
->register('a')
;
$container
->register('b')
->addArgument(new Definition(null, array($ref = new Reference('a'))))
;
$graph = $this->process($container);
$this->assertCount(1, $refs = $graph->getNode('a')->getInEdges());
$this->assertSame($ref, $refs[0]->getValue());
}
public function testProcessDoesNotSaveDuplicateReferences()
{
$container = new ContainerBuilder();
$container
->register('a')
;
$container
->register('b')
->addArgument(new Definition(null, array($ref1 = new Reference('a'))))
->addArgument(new Definition(null, array($ref2 = new Reference('a'))))
;
$graph = $this->process($container);
$this->assertCount(2, $graph->getNode('a')->getInEdges());
}
public function testProcessDetectsFactoryReferences()
{
$container = new ContainerBuilder();
$container
->register('foo', 'stdClass')
->setFactoryClass('stdClass')
->setFactoryMethod('getInstance');
$container
->register('bar', 'stdClass')
->setFactoryService('foo')
->setFactoryMethod('getInstance');
$graph = $this->process($container);
$this->assertTrue($graph->hasNode('foo'));
$this->assertCount(1, $graph->getNode('foo')->getInEdges());
}
protected function process(ContainerBuilder $container)
{
$pass = new RepeatedPass(array(new AnalyzeServiceReferencesPass()));
$pass->process($container);
return $container->getCompiler()->getServiceReferenceGraph();
}
}