8cf4e5186cd1c7f719e68145ac8e094df2cb89c4
[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\ContainerInterface;
16 use Symfony\Component\DependencyInjection\Reference;
17 use Symfony\Component\DependencyInjection\Compiler\ResolveInvalidReferencesPass;
18 use Symfony\Component\DependencyInjection\ContainerBuilder;
19
20 class ResolveInvalidReferencesPassTest extends TestCase
21 {
22     public function testProcess()
23     {
24         $container = new ContainerBuilder();
25         $def = $container
26             ->register('foo')
27             ->setArguments(array(
28                 new Reference('bar', ContainerInterface::NULL_ON_INVALID_REFERENCE),
29                 new Reference('baz', ContainerInterface::IGNORE_ON_INVALID_REFERENCE),
30             ))
31             ->addMethodCall('foo', array(new Reference('moo', ContainerInterface::IGNORE_ON_INVALID_REFERENCE)))
32         ;
33
34         $this->process($container);
35
36         $arguments = $def->getArguments();
37         $this->assertSame(array(null, null), $arguments);
38         $this->assertCount(0, $def->getMethodCalls());
39     }
40
41     public function testProcessIgnoreInvalidArgumentInCollectionArgument()
42     {
43         $container = new ContainerBuilder();
44         $container->register('baz');
45         $def = $container
46             ->register('foo')
47             ->setArguments(array(
48                 array(
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),
52                 ),
53             ))
54         ;
55
56         $this->process($container);
57
58         $arguments = $def->getArguments();
59         $this->assertSame(array($baz, null), $arguments[0]);
60     }
61
62     public function testProcessKeepMethodCallOnInvalidArgumentInCollectionArgument()
63     {
64         $container = new ContainerBuilder();
65         $container->register('baz');
66         $def = $container
67             ->register('foo')
68             ->addMethodCall('foo', array(
69                 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),
73                 ),
74             ))
75         ;
76
77         $this->process($container);
78
79         $calls = $def->getMethodCalls();
80         $this->assertCount(1, $def->getMethodCalls());
81         $this->assertSame(array($baz, null), $calls[0][1][0]);
82     }
83
84     public function testProcessIgnoreNonExistentServices()
85     {
86         $container = new ContainerBuilder();
87         $def = $container
88             ->register('foo')
89             ->setArguments(array(new Reference('bar')))
90         ;
91
92         $this->process($container);
93
94         $arguments = $def->getArguments();
95         $this->assertEquals('bar', (string) $arguments[0]);
96     }
97
98     public function testProcessRemovesPropertiesOnInvalid()
99     {
100         $container = new ContainerBuilder();
101         $def = $container
102             ->register('foo')
103             ->setProperty('foo', new Reference('bar', ContainerInterface::IGNORE_ON_INVALID_REFERENCE))
104         ;
105
106         $this->process($container);
107
108         $this->assertEquals(array(), $def->getProperties());
109     }
110
111     protected function process(ContainerBuilder $container)
112     {
113         $pass = new ResolveInvalidReferencesPass();
114         $pass->process($container);
115     }
116 }