AnalyzeServiceReferencesPassTest.php 4.1 KB

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