4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\DependencyInjection\Tests\Compiler;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
16 use Symfony\Component\DependencyInjection\Compiler\ResolveInvalidReferencesPass;
17 use Symfony\Component\DependencyInjection\ContainerBuilder;
18 use Symfony\Component\DependencyInjection\ContainerInterface;
19 use Symfony\Component\DependencyInjection\Reference;
21 class ResolveInvalidReferencesPassTest extends TestCase
23 public function testProcess()
25 $container = new ContainerBuilder();
29 new Reference('bar', ContainerInterface::NULL_ON_INVALID_REFERENCE),
30 new Reference('baz', ContainerInterface::IGNORE_ON_INVALID_REFERENCE),
32 ->addMethodCall('foo', array(new Reference('moo', ContainerInterface::IGNORE_ON_INVALID_REFERENCE)))
35 $this->process($container);
37 $arguments = $def->getArguments();
38 $this->assertSame(array(null, null), $arguments);
39 $this->assertCount(0, $def->getMethodCalls());
42 public function testProcessIgnoreInvalidArgumentInCollectionArgument()
44 $container = new ContainerBuilder();
45 $container->register('baz');
50 new Reference('bar', ContainerInterface::IGNORE_ON_INVALID_REFERENCE),
51 $baz = new Reference('baz', ContainerInterface::IGNORE_ON_INVALID_REFERENCE),
52 new Reference('moo', ContainerInterface::NULL_ON_INVALID_REFERENCE),
57 $this->process($container);
59 $arguments = $def->getArguments();
60 $this->assertSame(array($baz, null), $arguments[0]);
63 public function testProcessKeepMethodCallOnInvalidArgumentInCollectionArgument()
65 $container = new ContainerBuilder();
66 $container->register('baz');
69 ->addMethodCall('foo', array(
71 new Reference('bar', ContainerInterface::IGNORE_ON_INVALID_REFERENCE),
72 $baz = new Reference('baz', ContainerInterface::IGNORE_ON_INVALID_REFERENCE),
73 new Reference('moo', ContainerInterface::NULL_ON_INVALID_REFERENCE),
78 $this->process($container);
80 $calls = $def->getMethodCalls();
81 $this->assertCount(1, $def->getMethodCalls());
82 $this->assertSame(array($baz, null), $calls[0][1][0]);
85 public function testProcessIgnoreNonExistentServices()
87 $container = new ContainerBuilder();
90 ->setArguments(array(new Reference('bar')))
93 $this->process($container);
95 $arguments = $def->getArguments();
96 $this->assertEquals('bar', (string) $arguments[0]);
99 public function testProcessRemovesPropertiesOnInvalid()
101 $container = new ContainerBuilder();
104 ->setProperty('foo', new Reference('bar', ContainerInterface::IGNORE_ON_INVALID_REFERENCE))
107 $this->process($container);
109 $this->assertEquals(array(), $def->getProperties());
112 public function testProcessRemovesArgumentsOnInvalid()
114 $container = new ContainerBuilder();
119 new Reference('bar', ContainerInterface::IGNORE_ON_INVALID_REFERENCE),
120 new ServiceClosureArgument(new Reference('baz', ContainerInterface::IGNORE_ON_INVALID_REFERENCE)),
125 $this->process($container);
127 $this->assertSame(array(array(array())), $def->getArguments());
130 protected function process(ContainerBuilder $container)
132 $pass = new ResolveInvalidReferencesPass();
133 $pass->process($container);