CheckReferenceValidityPassTest.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /*
  3. * This file is part of the Symfony framework.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. namespace Symfony\Tests\Component\DependencyInjection\Compiler;
  11. use Symfony\Component\DependencyInjection\Scope;
  12. use Symfony\Component\DependencyInjection\Compiler\CheckReferenceValidityPass;
  13. use Symfony\Component\DependencyInjection\ContainerInterface;
  14. use Symfony\Component\DependencyInjection\Reference;
  15. use Symfony\Component\DependencyInjection\ContainerBuilder;
  16. class CheckReferenceValidityPassTest extends \PHPUnit_Framework_TestCase
  17. {
  18. public function testProcessIgnoresScopeWideningIfNonStrictReference()
  19. {
  20. $container = new ContainerBuilder();
  21. $container->register('a')->addArgument(new Reference('b', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, false));
  22. $container->register('b')->setScope('prototype');
  23. $this->process($container);
  24. }
  25. /**
  26. * @expectedException \RuntimeException
  27. */
  28. public function testProcessDetectsScopeWidening()
  29. {
  30. $container = new ContainerBuilder();
  31. $container->register('a')->addArgument(new Reference('b'));
  32. $container->register('b')->setScope('prototype');
  33. $this->process($container);
  34. }
  35. public function testProcessIgnoresCrossScopeHierarchyReferenceIfNotStrict()
  36. {
  37. $container = new ContainerBuilder();
  38. $container->addScope(new Scope('a'));
  39. $container->addScope(new Scope('b'));
  40. $container->register('a')->setScope('a')->addArgument(new Reference('b', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, false));
  41. $container->register('b')->setScope('b');
  42. $this->process($container);
  43. }
  44. /**
  45. * @expectedException \RuntimeException
  46. */
  47. public function testProcessDetectsCrossScopeHierarchyReference()
  48. {
  49. $container = new ContainerBuilder();
  50. $container->addScope(new Scope('a'));
  51. $container->addScope(new Scope('b'));
  52. $container->register('a')->setScope('a')->addArgument(new Reference('b'));
  53. $container->register('b')->setScope('b');
  54. $this->process($container);
  55. }
  56. /**
  57. * @expectedException \RuntimeException
  58. */
  59. public function testProcessDetectsReferenceToAbstractDefinition()
  60. {
  61. $container = new ContainerBuilder();
  62. $container->register('a')->setAbstract(true);
  63. $container->register('b')->addArgument(new Reference('a'));
  64. $this->process($container);
  65. }
  66. public function testProcess()
  67. {
  68. $container = new ContainerBuilder();
  69. $container->register('a')->addArgument(new Reference('b'));
  70. $container->register('b');
  71. $this->process($container);
  72. }
  73. protected function process(ContainerBuilder $container)
  74. {
  75. $pass = new CheckReferenceValidityPass();
  76. $pass->process($container);
  77. }
  78. }