AnalyzeServiceReferencesPassTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 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. $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 testProcessDoesNotSaveDuplicateReferences()
  65. {
  66. $container = new ContainerBuilder();
  67. $container
  68. ->register('a')
  69. ;
  70. $container
  71. ->register('b')
  72. ->addArgument(new Definition(null, array($ref1 = new Reference('a'))))
  73. ->addArgument(new Definition(null, array($ref2 = new Reference('a'))))
  74. ;
  75. $graph = $this->process($container);
  76. $this->assertCount(2, $graph->getNode('a')->getInEdges());
  77. }
  78. protected function process(ContainerBuilder $container)
  79. {
  80. $pass = new RepeatedPass(array(new AnalyzeServiceReferencesPass()));
  81. $pass->process($container);
  82. return $container->getCompiler()->getServiceReferenceGraph();
  83. }
  84. }