CheckCircularReferencesPassTest.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. /*
  3. * This file is part of the Symfony framework.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. namespace Symfony\Tests\Component\DependencyInjection\Compiler;
  11. use Symfony\Component\DependencyInjection\Reference;
  12. use Symfony\Component\DependencyInjection\Compiler\CheckCircularReferencesPass;
  13. use Symfony\Component\DependencyInjection\Compiler\AnalyzeServiceReferencesPass;
  14. use Symfony\Component\DependencyInjection\Compiler\Compiler;
  15. use Symfony\Component\DependencyInjection\ContainerBuilder;
  16. class CheckCircularReferencesPassTest extends \PHPUnit_Framework_TestCase
  17. {
  18. /**
  19. * @expectedException \RuntimeException
  20. */
  21. public function testProcess()
  22. {
  23. $container = new ContainerBuilder();
  24. $container->register('a')->addArgument(new Reference('b'));
  25. $container->register('b')->addArgument(new Reference('a'));
  26. $this->process($container);
  27. }
  28. /**
  29. * @expectedException \RuntimeException
  30. */
  31. public function testProcessDetectsIndirectCircularReference()
  32. {
  33. $container = new ContainerBuilder();
  34. $container->register('a')->addArgument(new Reference('b'));
  35. $container->register('b')->addArgument(new Reference('c'));
  36. $container->register('c')->addArgument(new Reference('a'));
  37. $this->process($container);
  38. }
  39. public function testProcessIgnoresMethodCalls()
  40. {
  41. $container = new ContainerBuilder();
  42. $container->register('a')->addArgument(new Reference('b'));
  43. $container->register('b')->addMethodCall('setA', array(new Reference('a')));
  44. $this->process($container);
  45. }
  46. protected function process(ContainerBuilder $container)
  47. {
  48. $compiler = new Compiler();
  49. $passConfig = $compiler->getPassConfig();
  50. $passConfig->setOptimizationPasses(array(
  51. new AnalyzeServiceReferencesPass(true),
  52. new CheckCircularReferencesPass(),
  53. ));
  54. $passConfig->setRemovingPasses(array());
  55. $compiler->compile($container);
  56. }
  57. }