ResolveInvalidReferencesPassTest.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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\Tests\Component\DependencyInjection\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->assertEquals(0, count($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. protected function process(ContainerBuilder $container)
  52. {
  53. $pass = new ResolveInvalidReferencesPass();
  54. $pass->process($container);
  55. }
  56. }