CheckReferenceScopePassTest.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. namespace Symfony\Tests\Component\DependencyInjection\Compiler;
  3. use Symfony\Component\DependencyInjection\Compiler\CheckReferenceScopePass;
  4. use Symfony\Component\DependencyInjection\ContainerInterface;
  5. use Symfony\Component\DependencyInjection\Reference;
  6. use Symfony\Component\DependencyInjection\ContainerBuilder;
  7. class CheckReferenceScopePassTest extends \PHPUnit_Framework_TestCase
  8. {
  9. public function testProcessIgnoresScopeWideningIfNonStrictReference()
  10. {
  11. $container = new ContainerBuilder();
  12. $container->register('a')->addArgument(new Reference('b', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, false));
  13. $container->register('b')->setScope('prototype');
  14. $this->process($container);
  15. }
  16. /**
  17. * @expectedException \RuntimeException
  18. */
  19. public function testProcessDetectsScopeWidening()
  20. {
  21. $container = new ContainerBuilder();
  22. $container->register('a')->addArgument(new Reference('b'));
  23. $container->register('b')->setScope('prototype');
  24. $this->process($container);
  25. }
  26. public function testProcessIgnoresCrossScopeHierarchyReferenceIfNotStrict()
  27. {
  28. $container = new ContainerBuilder();
  29. $container->addScope('a');
  30. $container->addScope('b');
  31. $container->register('a')->setScope('a')->addArgument(new Reference('b', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, false));
  32. $container->register('b')->setScope('b');
  33. $this->process($container);
  34. }
  35. /**
  36. * @expectedException \RuntimeException
  37. */
  38. public function testProcessDetectsCrossScopeHierarchyReference()
  39. {
  40. $container = new ContainerBuilder();
  41. $container->addScope('a');
  42. $container->addScope('b');
  43. $container->register('a')->setScope('a')->addArgument(new Reference('b'));
  44. $container->register('b')->setScope('b');
  45. $this->process($container);
  46. }
  47. public function testProcess()
  48. {
  49. $container = new ContainerBuilder();
  50. $container->register('a')->addArgument(new Reference('b'));
  51. $container->register('b');
  52. $this->process($container);
  53. }
  54. protected function process(ContainerBuilder $container)
  55. {
  56. $pass = new CheckReferenceScopePass();
  57. $pass->process($container);
  58. }
  59. }