CheckCircularReferencesPassTest.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 testProcessWithAliases()
  32. {
  33. $container = new ContainerBuilder();
  34. $container->register('a')->addArgument(new Reference('b'));
  35. $container->setAlias('b', 'c');
  36. $container->setAlias('c', 'a');
  37. $this->process($container);
  38. }
  39. /**
  40. * @expectedException \RuntimeException
  41. */
  42. public function testProcessDetectsIndirectCircularReference()
  43. {
  44. $container = new ContainerBuilder();
  45. $container->register('a')->addArgument(new Reference('b'));
  46. $container->register('b')->addArgument(new Reference('c'));
  47. $container->register('c')->addArgument(new Reference('a'));
  48. $this->process($container);
  49. }
  50. public function testProcessIgnoresMethodCalls()
  51. {
  52. $container = new ContainerBuilder();
  53. $container->register('a')->addArgument(new Reference('b'));
  54. $container->register('b')->addMethodCall('setA', array(new Reference('a')));
  55. $this->process($container);
  56. }
  57. protected function process(ContainerBuilder $container)
  58. {
  59. $compiler = new Compiler();
  60. $passConfig = $compiler->getPassConfig();
  61. $passConfig->setOptimizationPasses(array(
  62. new AnalyzeServiceReferencesPass(true),
  63. new CheckCircularReferencesPass(),
  64. ));
  65. $passConfig->setRemovingPasses(array());
  66. $compiler->compile($container);
  67. }
  68. }