CheckCircularReferencesPassTest.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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\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 \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException
  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 \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException
  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 \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException
  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. /**
  51. * @expectedException \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException
  52. */
  53. public function testDeepCircularReference()
  54. {
  55. $container = new ContainerBuilder();
  56. $container->register('a')->addArgument(new Reference('b'));
  57. $container->register('b')->addArgument(new Reference('c'));
  58. $container->register('c')->addArgument(new Reference('b'));
  59. $this->process($container);
  60. }
  61. public function testProcessIgnoresMethodCalls()
  62. {
  63. $container = new ContainerBuilder();
  64. $container->register('a')->addArgument(new Reference('b'));
  65. $container->register('b')->addMethodCall('setA', array(new Reference('a')));
  66. $this->process($container);
  67. }
  68. protected function process(ContainerBuilder $container)
  69. {
  70. $compiler = new Compiler();
  71. $passConfig = $compiler->getPassConfig();
  72. $passConfig->setOptimizationPasses(array(
  73. new AnalyzeServiceReferencesPass(true),
  74. new CheckCircularReferencesPass(),
  75. ));
  76. $passConfig->setRemovingPasses(array());
  77. $compiler->compile($container);
  78. }
  79. }