CheckReferenceValidityPassTest.php 2.7 KB

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