00613ba5c1c6ce6afd1b126325ee07133547ce4d
[yaffs-website] / vendor / symfony / dependency-injection / Tests / Compiler / ResolveInvalidReferencesPassTest.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\DependencyInjection\Tests\Compiler;
13
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
16 use Symfony\Component\DependencyInjection\ContainerInterface;
17 use Symfony\Component\DependencyInjection\Reference;
18 use Symfony\Component\DependencyInjection\Compiler\ResolveInvalidReferencesPass;
19 use Symfony\Component\DependencyInjection\ContainerBuilder;
20
21 class ResolveInvalidReferencesPassTest extends TestCase
22 {
23     public function testProcess()
24     {
25         $container = new ContainerBuilder();
26         $def = $container
27             ->register('foo')
28             ->setArguments(array(
29                 new Reference('bar', ContainerInterface::NULL_ON_INVALID_REFERENCE),
30                 new Reference('baz', ContainerInterface::IGNORE_ON_INVALID_REFERENCE),
31             ))
32             ->addMethodCall('foo', array(new Reference('moo', ContainerInterface::IGNORE_ON_INVALID_REFERENCE)))
33         ;
34
35         $this->process($container);
36
37         $arguments = $def->getArguments();
38         $this->assertSame(array(null, null), $arguments);
39         $this->assertCount(0, $def->getMethodCalls());
40     }
41
42     public function testProcessIgnoreInvalidArgumentInCollectionArgument()
43     {
44         $container = new ContainerBuilder();
45         $container->register('baz');
46         $def = $container
47             ->register('foo')
48             ->setArguments(array(
49                 array(
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),
53                 ),
54             ))
55         ;
56
57         $this->process($container);
58
59         $arguments = $def->getArguments();
60         $this->assertSame(array($baz, null), $arguments[0]);
61     }
62
63     public function testProcessKeepMethodCallOnInvalidArgumentInCollectionArgument()
64     {
65         $container = new ContainerBuilder();
66         $container->register('baz');
67         $def = $container
68             ->register('foo')
69             ->addMethodCall('foo', array(
70                 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),
74                 ),
75             ))
76         ;
77
78         $this->process($container);
79
80         $calls = $def->getMethodCalls();
81         $this->assertCount(1, $def->getMethodCalls());
82         $this->assertSame(array($baz, null), $calls[0][1][0]);
83     }
84
85     public function testProcessIgnoreNonExistentServices()
86     {
87         $container = new ContainerBuilder();
88         $def = $container
89             ->register('foo')
90             ->setArguments(array(new Reference('bar')))
91         ;
92
93         $this->process($container);
94
95         $arguments = $def->getArguments();
96         $this->assertEquals('bar', (string) $arguments[0]);
97     }
98
99     public function testProcessRemovesPropertiesOnInvalid()
100     {
101         $container = new ContainerBuilder();
102         $def = $container
103             ->register('foo')
104             ->setProperty('foo', new Reference('bar', ContainerInterface::IGNORE_ON_INVALID_REFERENCE))
105         ;
106
107         $this->process($container);
108
109         $this->assertEquals(array(), $def->getProperties());
110     }
111
112     public function testProcessRemovesArgumentsOnInvalid()
113     {
114         $container = new ContainerBuilder();
115         $def = $container
116             ->register('foo')
117             ->addArgument(array(
118                 array(
119                     new Reference('bar', ContainerInterface::IGNORE_ON_INVALID_REFERENCE),
120                     new ServiceClosureArgument(new Reference('baz', ContainerInterface::IGNORE_ON_INVALID_REFERENCE)),
121                 ),
122             ))
123         ;
124
125         $this->process($container);
126
127         $this->assertSame(array(array(array())), $def->getArguments());
128     }
129
130     protected function process(ContainerBuilder $container)
131     {
132         $pass = new ResolveInvalidReferencesPass();
133         $pass->process($container);
134     }
135 }