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\ContainerInterface;
16 use Symfony\Component\DependencyInjection\Reference;
17 use Symfony\Component\DependencyInjection\Compiler\ResolveInvalidReferencesPass;
18 use Symfony\Component\DependencyInjection\ContainerBuilder;
20 class ResolveInvalidReferencesPassTest extends TestCase
22 public function testProcess()
24 $container = new ContainerBuilder();
28 new Reference('bar', ContainerInterface::NULL_ON_INVALID_REFERENCE),
29 new Reference('baz', ContainerInterface::IGNORE_ON_INVALID_REFERENCE),
31 ->addMethodCall('foo', array(new Reference('moo', ContainerInterface::IGNORE_ON_INVALID_REFERENCE)))
34 $this->process($container);
36 $arguments = $def->getArguments();
37 $this->assertSame(array(null, null), $arguments);
38 $this->assertCount(0, $def->getMethodCalls());
41 public function testProcessIgnoreInvalidArgumentInCollectionArgument()
43 $container = new ContainerBuilder();
44 $container->register('baz');
49 new Reference('bar', ContainerInterface::IGNORE_ON_INVALID_REFERENCE),
50 $baz = new Reference('baz', ContainerInterface::IGNORE_ON_INVALID_REFERENCE),
51 new Reference('moo', ContainerInterface::NULL_ON_INVALID_REFERENCE),
56 $this->process($container);
58 $arguments = $def->getArguments();
59 $this->assertSame(array($baz, null), $arguments[0]);
62 public function testProcessKeepMethodCallOnInvalidArgumentInCollectionArgument()
64 $container = new ContainerBuilder();
65 $container->register('baz');
68 ->addMethodCall('foo', array(
70 new Reference('bar', ContainerInterface::IGNORE_ON_INVALID_REFERENCE),
71 $baz = new Reference('baz', ContainerInterface::IGNORE_ON_INVALID_REFERENCE),
72 new Reference('moo', ContainerInterface::NULL_ON_INVALID_REFERENCE),
77 $this->process($container);
79 $calls = $def->getMethodCalls();
80 $this->assertCount(1, $def->getMethodCalls());
81 $this->assertSame(array($baz, null), $calls[0][1][0]);
84 public function testProcessIgnoreNonExistentServices()
86 $container = new ContainerBuilder();
89 ->setArguments(array(new Reference('bar')))
92 $this->process($container);
94 $arguments = $def->getArguments();
95 $this->assertEquals('bar', (string) $arguments[0]);
98 public function testProcessRemovesPropertiesOnInvalid()
100 $container = new ContainerBuilder();
103 ->setProperty('foo', new Reference('bar', ContainerInterface::IGNORE_ON_INVALID_REFERENCE))
106 $this->process($container);
108 $this->assertEquals(array(), $def->getProperties());
111 protected function process(ContainerBuilder $container)
113 $pass = new ResolveInvalidReferencesPass();
114 $pass->process($container);