Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / vendor / symfony / dependency-injection / Tests / Compiler / AutowireExceptionPassTest.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\Compiler\AutowireExceptionPass;
16 use Symfony\Component\DependencyInjection\Compiler\AutowirePass;
17 use Symfony\Component\DependencyInjection\Compiler\InlineServiceDefinitionsPass;
18 use Symfony\Component\DependencyInjection\ContainerBuilder;
19 use Symfony\Component\DependencyInjection\Exception\AutowiringFailedException;
20
21 /**
22  * @group legacy
23  */
24 class AutowireExceptionPassTest extends TestCase
25 {
26     public function testThrowsException()
27     {
28         $autowirePass = $this->getMockBuilder(AutowirePass::class)
29             ->getMock();
30
31         $autowireException = new AutowiringFailedException('foo_service_id', 'An autowiring exception message');
32         $autowirePass->expects($this->any())
33             ->method('getAutowiringExceptions')
34             ->will($this->returnValue(array($autowireException)));
35
36         $inlinePass = $this->getMockBuilder(InlineServiceDefinitionsPass::class)
37             ->getMock();
38         $inlinePass->expects($this->any())
39             ->method('getInlinedServiceIds')
40             ->will($this->returnValue(array()));
41
42         $container = new ContainerBuilder();
43         $container->register('foo_service_id');
44
45         $pass = new AutowireExceptionPass($autowirePass, $inlinePass);
46
47         try {
48             $pass->process($container);
49             $this->fail('->process() should throw the exception if the service id exists');
50         } catch (\Exception $e) {
51             $this->assertSame($autowireException, $e);
52         }
53     }
54
55     public function testThrowExceptionIfServiceInlined()
56     {
57         $autowirePass = $this->getMockBuilder(AutowirePass::class)
58             ->getMock();
59
60         $autowireException = new AutowiringFailedException('a_service', 'An autowiring exception message');
61         $autowirePass->expects($this->any())
62             ->method('getAutowiringExceptions')
63             ->will($this->returnValue(array($autowireException)));
64
65         $inlinePass = $this->getMockBuilder(InlineServiceDefinitionsPass::class)
66             ->getMock();
67         $inlinePass->expects($this->any())
68             ->method('getInlinedServiceIds')
69             ->will($this->returnValue(array(
70                 // a_service inlined into b_service
71                 'a_service' => array('b_service'),
72                 // b_service inlined into c_service
73                 'b_service' => array('c_service'),
74             )));
75
76         $container = new ContainerBuilder();
77         // ONLY register c_service in the final container
78         $container->register('c_service', 'stdClass');
79
80         $pass = new AutowireExceptionPass($autowirePass, $inlinePass);
81
82         try {
83             $pass->process($container);
84             $this->fail('->process() should throw the exception if the service id exists');
85         } catch (\Exception $e) {
86             $this->assertSame($autowireException, $e);
87         }
88     }
89
90     public function testDoNotThrowExceptionIfServiceInlinedButRemoved()
91     {
92         $autowirePass = $this->getMockBuilder(AutowirePass::class)
93             ->getMock();
94
95         $autowireException = new AutowiringFailedException('a_service', 'An autowiring exception message');
96         $autowirePass->expects($this->any())
97             ->method('getAutowiringExceptions')
98             ->will($this->returnValue(array($autowireException)));
99
100         $inlinePass = $this->getMockBuilder(InlineServiceDefinitionsPass::class)
101             ->getMock();
102         $inlinePass->expects($this->any())
103             ->method('getInlinedServiceIds')
104             ->will($this->returnValue(array(
105                 // a_service inlined into b_service
106                 'a_service' => array('b_service'),
107                 // b_service inlined into c_service
108                 'b_service' => array('c_service'),
109             )));
110
111         // do NOT register c_service in the container
112         $container = new ContainerBuilder();
113
114         $pass = new AutowireExceptionPass($autowirePass, $inlinePass);
115
116         $pass->process($container);
117         // mark the test as passed
118         $this->assertTrue(true);
119     }
120
121     public function testNoExceptionIfServiceRemoved()
122     {
123         $autowirePass = $this->getMockBuilder(AutowirePass::class)
124             ->getMock();
125
126         $autowireException = new AutowiringFailedException('non_existent_service');
127         $autowirePass->expects($this->any())
128             ->method('getAutowiringExceptions')
129             ->will($this->returnValue(array($autowireException)));
130
131         $inlinePass = $this->getMockBuilder(InlineServiceDefinitionsPass::class)
132             ->getMock();
133         $inlinePass->expects($this->any())
134             ->method('getInlinedServiceIds')
135             ->will($this->returnValue(array()));
136
137         $container = new ContainerBuilder();
138
139         $pass = new AutowireExceptionPass($autowirePass, $inlinePass);
140
141         $pass->process($container);
142         // mark the test as passed
143         $this->assertTrue(true);
144     }
145 }