CheckReferenceValidityPassTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 PHPUnit\Framework\TestCase;
  12. use Symfony\Component\DependencyInjection\Compiler\CheckReferenceValidityPass;
  13. use Symfony\Component\DependencyInjection\ContainerBuilder;
  14. use Symfony\Component\DependencyInjection\ContainerInterface;
  15. use Symfony\Component\DependencyInjection\Reference;
  16. use Symfony\Component\DependencyInjection\Scope;
  17. class CheckReferenceValidityPassTest extends TestCase
  18. {
  19. /**
  20. * @group legacy
  21. */
  22. public function testProcessIgnoresScopeWideningIfNonStrictReference()
  23. {
  24. $container = new ContainerBuilder();
  25. $container->register('a')->addArgument(new Reference('b', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, false));
  26. $container->register('b')->setScope('prototype');
  27. $this->process($container);
  28. $this->addToAssertionCount(1);
  29. }
  30. /**
  31. * @expectedException \RuntimeException
  32. * @group legacy
  33. */
  34. public function testProcessDetectsScopeWidening()
  35. {
  36. $container = new ContainerBuilder();
  37. $container->register('a')->addArgument(new Reference('b'));
  38. $container->register('b')->setScope('prototype');
  39. $this->process($container);
  40. $this->addToAssertionCount(1);
  41. }
  42. /**
  43. * @group legacy
  44. */
  45. public function testProcessIgnoresCrossScopeHierarchyReferenceIfNotStrict()
  46. {
  47. $container = new ContainerBuilder();
  48. $container->addScope(new Scope('a'));
  49. $container->addScope(new Scope('b'));
  50. $container->register('a')->setScope('a')->addArgument(new Reference('b', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, false));
  51. $container->register('b')->setScope('b');
  52. $this->process($container);
  53. $this->addToAssertionCount(1);
  54. }
  55. /**
  56. * @expectedException \RuntimeException
  57. * @group legacy
  58. */
  59. public function testProcessDetectsCrossScopeHierarchyReference()
  60. {
  61. $container = new ContainerBuilder();
  62. $container->addScope(new Scope('a'));
  63. $container->addScope(new Scope('b'));
  64. $container->register('a')->setScope('a')->addArgument(new Reference('b'));
  65. $container->register('b')->setScope('b');
  66. $this->process($container);
  67. }
  68. /**
  69. * @expectedException \RuntimeException
  70. */
  71. public function testProcessDetectsReferenceToAbstractDefinition()
  72. {
  73. $container = new ContainerBuilder();
  74. $container->register('a')->setAbstract(true);
  75. $container->register('b')->addArgument(new Reference('a'));
  76. $this->process($container);
  77. }
  78. public function testProcess()
  79. {
  80. $container = new ContainerBuilder();
  81. $container->register('a')->addArgument(new Reference('b'));
  82. $container->register('b');
  83. $this->process($container);
  84. $this->addToAssertionCount(1);
  85. }
  86. protected function process(ContainerBuilder $container)
  87. {
  88. $pass = new CheckReferenceValidityPass();
  89. $pass->process($container);
  90. }
  91. }