ResolveInvalidReferencesPassTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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\Argument\ServiceClosureArgument;
  13. use Symfony\Component\DependencyInjection\ContainerInterface;
  14. use Symfony\Component\DependencyInjection\Reference;
  15. use Symfony\Component\DependencyInjection\Compiler\ResolveInvalidReferencesPass;
  16. use Symfony\Component\DependencyInjection\ContainerBuilder;
  17. class ResolveInvalidReferencesPassTest extends TestCase
  18. {
  19. public function testProcess()
  20. {
  21. $container = new ContainerBuilder();
  22. $def = $container
  23. ->register('foo')
  24. ->setArguments(array(
  25. new Reference('bar', ContainerInterface::NULL_ON_INVALID_REFERENCE),
  26. new Reference('baz', ContainerInterface::IGNORE_ON_INVALID_REFERENCE),
  27. ))
  28. ->addMethodCall('foo', array(new Reference('moo', ContainerInterface::IGNORE_ON_INVALID_REFERENCE)))
  29. ;
  30. $this->process($container);
  31. $arguments = $def->getArguments();
  32. $this->assertSame(array(null, null), $arguments);
  33. $this->assertCount(0, $def->getMethodCalls());
  34. }
  35. public function testProcessIgnoreInvalidArgumentInCollectionArgument()
  36. {
  37. $container = new ContainerBuilder();
  38. $container->register('baz');
  39. $def = $container
  40. ->register('foo')
  41. ->setArguments(array(
  42. array(
  43. new Reference('bar', ContainerInterface::IGNORE_ON_INVALID_REFERENCE),
  44. $baz = new Reference('baz', ContainerInterface::IGNORE_ON_INVALID_REFERENCE),
  45. new Reference('moo', ContainerInterface::NULL_ON_INVALID_REFERENCE),
  46. ),
  47. ))
  48. ;
  49. $this->process($container);
  50. $arguments = $def->getArguments();
  51. $this->assertSame(array($baz, null), $arguments[0]);
  52. }
  53. public function testProcessKeepMethodCallOnInvalidArgumentInCollectionArgument()
  54. {
  55. $container = new ContainerBuilder();
  56. $container->register('baz');
  57. $def = $container
  58. ->register('foo')
  59. ->addMethodCall('foo', array(
  60. array(
  61. new Reference('bar', ContainerInterface::IGNORE_ON_INVALID_REFERENCE),
  62. $baz = new Reference('baz', ContainerInterface::IGNORE_ON_INVALID_REFERENCE),
  63. new Reference('moo', ContainerInterface::NULL_ON_INVALID_REFERENCE),
  64. ),
  65. ))
  66. ;
  67. $this->process($container);
  68. $calls = $def->getMethodCalls();
  69. $this->assertCount(1, $def->getMethodCalls());
  70. $this->assertSame(array($baz, null), $calls[0][1][0]);
  71. }
  72. public function testProcessIgnoreNonExistentServices()
  73. {
  74. $container = new ContainerBuilder();
  75. $def = $container
  76. ->register('foo')
  77. ->setArguments(array(new Reference('bar')))
  78. ;
  79. $this->process($container);
  80. $arguments = $def->getArguments();
  81. $this->assertEquals('bar', (string) $arguments[0]);
  82. }
  83. public function testProcessRemovesPropertiesOnInvalid()
  84. {
  85. $container = new ContainerBuilder();
  86. $def = $container
  87. ->register('foo')
  88. ->setProperty('foo', new Reference('bar', ContainerInterface::IGNORE_ON_INVALID_REFERENCE))
  89. ;
  90. $this->process($container);
  91. $this->assertEquals(array(), $def->getProperties());
  92. }
  93. public function testProcessRemovesArgumentsOnInvalid()
  94. {
  95. $container = new ContainerBuilder();
  96. $def = $container
  97. ->register('foo')
  98. ->addArgument(array(
  99. array(
  100. new Reference('bar', ContainerInterface::IGNORE_ON_INVALID_REFERENCE),
  101. new ServiceClosureArgument(new Reference('baz', ContainerInterface::IGNORE_ON_INVALID_REFERENCE)),
  102. ),
  103. ))
  104. ;
  105. $this->process($container);
  106. $this->assertSame(array(array(array())), $def->getArguments());
  107. }
  108. protected function process(ContainerBuilder $container)
  109. {
  110. $pass = new ResolveInvalidReferencesPass();
  111. $pass->process($container);
  112. }
  113. }