ResolveInvalidReferencesPassTest.php 2.5 KB

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