CheckReferenceValidityPassTest.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. namespace Symfony\Tests\Component\DependencyInjection\Compiler;
  3. use Symfony\Component\DependencyInjection\Compiler\CheckReferenceValidityPass;
  4. use Symfony\Component\DependencyInjection\ContainerInterface;
  5. use Symfony\Component\DependencyInjection\Reference;
  6. use Symfony\Component\DependencyInjection\ContainerBuilder;
  7. class CheckReferenceValidityPassTest 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. /**
  48. * @expectedException \RuntimeException
  49. */
  50. public function testProcessDetectsReferenceToAbstractDefinition()
  51. {
  52. $container = new ContainerBuilder();
  53. $container->register('a')->setAbstract(true);
  54. $container->register('b')->addArgument(new Reference('a'));
  55. $this->process($container);
  56. }
  57. public function testProcess()
  58. {
  59. $container = new ContainerBuilder();
  60. $container->register('a')->addArgument(new Reference('b'));
  61. $container->register('b');
  62. $this->process($container);
  63. }
  64. protected function process(ContainerBuilder $container)
  65. {
  66. $pass = new CheckReferenceValidityPass();
  67. $pass->process($container);
  68. }
  69. }