CheckCircularReferencesPassTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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\CheckCircularReferencesPass;
  14. use Symfony\Component\DependencyInjection\Compiler\Compiler;
  15. use Symfony\Component\DependencyInjection\ContainerBuilder;
  16. use Symfony\Component\DependencyInjection\Reference;
  17. class CheckCircularReferencesPassTest extends TestCase
  18. {
  19. /**
  20. * @expectedException \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException
  21. */
  22. public function testProcess()
  23. {
  24. $container = new ContainerBuilder();
  25. $container->register('a')->addArgument(new Reference('b'));
  26. $container->register('b')->addArgument(new Reference('a'));
  27. $this->process($container);
  28. }
  29. /**
  30. * @expectedException \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException
  31. */
  32. public function testProcessWithAliases()
  33. {
  34. $container = new ContainerBuilder();
  35. $container->register('a')->addArgument(new Reference('b'));
  36. $container->setAlias('b', 'c');
  37. $container->setAlias('c', 'a');
  38. $this->process($container);
  39. }
  40. /**
  41. * @expectedException \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException
  42. */
  43. public function testProcessWithFactory()
  44. {
  45. $container = new ContainerBuilder();
  46. $container
  47. ->register('a', 'stdClass')
  48. ->setFactory(array(new Reference('b'), 'getInstance'));
  49. $container
  50. ->register('b', 'stdClass')
  51. ->setFactory(array(new Reference('a'), 'getInstance'));
  52. $this->process($container);
  53. }
  54. /**
  55. * @expectedException \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException
  56. */
  57. public function testProcessDetectsIndirectCircularReference()
  58. {
  59. $container = new ContainerBuilder();
  60. $container->register('a')->addArgument(new Reference('b'));
  61. $container->register('b')->addArgument(new Reference('c'));
  62. $container->register('c')->addArgument(new Reference('a'));
  63. $this->process($container);
  64. }
  65. /**
  66. * @expectedException \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException
  67. */
  68. public function testProcessDetectsIndirectCircularReferenceWithFactory()
  69. {
  70. $container = new ContainerBuilder();
  71. $container->register('a')->addArgument(new Reference('b'));
  72. $container
  73. ->register('b', 'stdClass')
  74. ->setFactory(array(new Reference('c'), 'getInstance'));
  75. $container->register('c')->addArgument(new Reference('a'));
  76. $this->process($container);
  77. }
  78. /**
  79. * @expectedException \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException
  80. */
  81. public function testDeepCircularReference()
  82. {
  83. $container = new ContainerBuilder();
  84. $container->register('a')->addArgument(new Reference('b'));
  85. $container->register('b')->addArgument(new Reference('c'));
  86. $container->register('c')->addArgument(new Reference('b'));
  87. $this->process($container);
  88. }
  89. public function testProcessIgnoresMethodCalls()
  90. {
  91. $container = new ContainerBuilder();
  92. $container->register('a')->addArgument(new Reference('b'));
  93. $container->register('b')->addMethodCall('setA', array(new Reference('a')));
  94. $this->process($container);
  95. $this->addToAssertionCount(1);
  96. }
  97. protected function process(ContainerBuilder $container)
  98. {
  99. $compiler = new Compiler();
  100. $passConfig = $compiler->getPassConfig();
  101. $passConfig->setOptimizationPasses(array(
  102. new AnalyzeServiceReferencesPass(true),
  103. new CheckCircularReferencesPass(),
  104. ));
  105. $passConfig->setRemovingPasses(array());
  106. $compiler->compile($container);
  107. }
  108. }