AnalyzeServiceReferencesPassTest.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.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\Tests\Component\DependencyInjection\Compiler;
  11. use Symfony\Component\DependencyInjection\Definition;
  12. use Symfony\Component\DependencyInjection\Compiler\Compiler;
  13. use Symfony\Component\DependencyInjection\Compiler\AnalyzeServiceReferencesPass;
  14. use Symfony\Component\DependencyInjection\Compiler\RepeatedPass;
  15. use Symfony\Component\DependencyInjection\Reference;
  16. use Symfony\Component\DependencyInjection\ContainerBuilder;
  17. class AnalyzeServiceReferencesPassTest extends \PHPUnit_Framework_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. $graph = $this->process($container);
  40. $this->assertEquals(3, count($edges = $graph->getNode('b')->getInEdges()));
  41. $this->assertSame($ref1, $edges[0]->getValue());
  42. $this->assertSame($ref4, $edges[1]->getValue());
  43. $this->assertSame($ref5, $edges[2]->getValue());
  44. }
  45. public function testProcessDetectsReferencesFromInlinedDefinitions()
  46. {
  47. $container = new ContainerBuilder();
  48. $container
  49. ->register('a')
  50. ;
  51. $container
  52. ->register('b')
  53. ->addArgument(new Definition(null, array($ref = new Reference('a'))))
  54. ;
  55. $graph = $this->process($container);
  56. $this->assertEquals(1, count($refs = $graph->getNode('a')->getInEdges()));
  57. $this->assertSame($ref, $refs[0]->getValue());
  58. }
  59. public function testProcessDoesNotSaveDuplicateReferences()
  60. {
  61. $container = new ContainerBuilder();
  62. $container
  63. ->register('a')
  64. ;
  65. $container
  66. ->register('b')
  67. ->addArgument(new Definition(null, array($ref1 = new Reference('a'))))
  68. ->addArgument(new Definition(null, array($ref2 = new Reference('a'))))
  69. ;
  70. $graph = $this->process($container);
  71. $this->assertEquals(2, count($graph->getNode('a')->getInEdges()));
  72. }
  73. protected function process(ContainerBuilder $container)
  74. {
  75. $pass = new RepeatedPass(array(new AnalyzeServiceReferencesPass()));
  76. $pass->process($container);
  77. return $container->getCompiler()->getServiceReferenceGraph();
  78. }
  79. }