ResolveInvalidReferencesPassTest.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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\ResolveInvalidReferencesPass;
  13. use Symfony\Component\DependencyInjection\ContainerBuilder;
  14. use Symfony\Component\DependencyInjection\ContainerInterface;
  15. use Symfony\Component\DependencyInjection\Reference;
  16. class ResolveInvalidReferencesPassTest extends TestCase
  17. {
  18. public function testProcess()
  19. {
  20. $container = new ContainerBuilder();
  21. $def = $container
  22. ->register('foo')
  23. ->setArguments(array(new Reference('bar', ContainerInterface::NULL_ON_INVALID_REFERENCE)))
  24. ->addMethodCall('foo', array(new Reference('moo', ContainerInterface::IGNORE_ON_INVALID_REFERENCE)))
  25. ;
  26. $this->process($container);
  27. $arguments = $def->getArguments();
  28. $this->assertNull($arguments[0]);
  29. $this->assertCount(0, $def->getMethodCalls());
  30. }
  31. public function testProcessIgnoreNonExistentServices()
  32. {
  33. $container = new ContainerBuilder();
  34. $def = $container
  35. ->register('foo')
  36. ->setArguments(array(new Reference('bar')))
  37. ;
  38. $this->process($container);
  39. $arguments = $def->getArguments();
  40. $this->assertEquals('bar', (string) $arguments[0]);
  41. }
  42. public function testProcessRemovesPropertiesOnInvalid()
  43. {
  44. $container = new ContainerBuilder();
  45. $def = $container
  46. ->register('foo')
  47. ->setProperty('foo', new Reference('bar', ContainerInterface::IGNORE_ON_INVALID_REFERENCE))
  48. ;
  49. $this->process($container);
  50. $this->assertEquals(array(), $def->getProperties());
  51. }
  52. /**
  53. * @group legacy
  54. */
  55. public function testStrictFlagIsPreserved()
  56. {
  57. $container = new ContainerBuilder();
  58. $container->register('bar');
  59. $def = $container
  60. ->register('foo')
  61. ->addArgument(new Reference('bar', ContainerInterface::NULL_ON_INVALID_REFERENCE, false))
  62. ;
  63. $this->process($container);
  64. $this->assertFalse($def->getArgument(0)->isStrict());
  65. }
  66. protected function process(ContainerBuilder $container)
  67. {
  68. $pass = new ResolveInvalidReferencesPass();
  69. $pass->process($container);
  70. }
  71. }