AnalyzeServiceReferencesPassTest.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. $graph = $this->process($container);
  36. $this->assertEquals(2, count($edges = $graph->getNode('b')->getInEdges()));
  37. $this->assertSame($ref1, $edges[0]->getValue());
  38. $this->assertSame($ref4, $edges[1]->getValue());
  39. }
  40. public function testProcessDetectsReferencesFromInlinedDefinitions()
  41. {
  42. $container = new ContainerBuilder();
  43. $container
  44. ->register('a')
  45. ;
  46. $container
  47. ->register('b')
  48. ->addArgument(new Definition(null, array($ref = new Reference('a'))))
  49. ;
  50. $graph = $this->process($container);
  51. $this->assertEquals(1, count($refs = $graph->getNode('a')->getInEdges()));
  52. $this->assertSame($ref, $refs[0]->getValue());
  53. }
  54. public function testProcessDoesNotSaveDuplicateReferences()
  55. {
  56. $container = new ContainerBuilder();
  57. $container
  58. ->register('a')
  59. ;
  60. $container
  61. ->register('b')
  62. ->addArgument(new Definition(null, array($ref1 = new Reference('a'))))
  63. ->addArgument(new Definition(null, array($ref2 = new Reference('a'))))
  64. ;
  65. $graph = $this->process($container);
  66. $this->assertEquals(2, count($graph->getNode('a')->getInEdges()));
  67. }
  68. protected function process(ContainerBuilder $container)
  69. {
  70. $pass = new RepeatedPass(array(new AnalyzeServiceReferencesPass()));
  71. $pass->process($container);
  72. return $container->getCompiler()->getServiceReferenceGraph();
  73. }
  74. }