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\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;
24 class AutowireExceptionPassTest extends TestCase
26 public function testThrowsException()
28 $autowirePass = $this->getMockBuilder(AutowirePass::class)
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)));
36 $inlinePass = $this->getMockBuilder(InlineServiceDefinitionsPass::class)
38 $inlinePass->expects($this->any())
39 ->method('getInlinedServiceIds')
40 ->will($this->returnValue(array()));
42 $container = new ContainerBuilder();
43 $container->register('foo_service_id');
45 $pass = new AutowireExceptionPass($autowirePass, $inlinePass);
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);
55 public function testThrowExceptionIfServiceInlined()
57 $autowirePass = $this->getMockBuilder(AutowirePass::class)
60 $autowireException = new AutowiringFailedException('a_service', 'An autowiring exception message');
61 $autowirePass->expects($this->any())
62 ->method('getAutowiringExceptions')
63 ->will($this->returnValue(array($autowireException)));
65 $inlinePass = $this->getMockBuilder(InlineServiceDefinitionsPass::class)
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'),
76 $container = new ContainerBuilder();
77 // ONLY register c_service in the final container
78 $container->register('c_service', 'stdClass');
80 $pass = new AutowireExceptionPass($autowirePass, $inlinePass);
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);
90 public function testDoNotThrowExceptionIfServiceInlinedButRemoved()
92 $autowirePass = $this->getMockBuilder(AutowirePass::class)
95 $autowireException = new AutowiringFailedException('a_service', 'An autowiring exception message');
96 $autowirePass->expects($this->any())
97 ->method('getAutowiringExceptions')
98 ->will($this->returnValue(array($autowireException)));
100 $inlinePass = $this->getMockBuilder(InlineServiceDefinitionsPass::class)
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'),
111 // do NOT register c_service in the container
112 $container = new ContainerBuilder();
114 $pass = new AutowireExceptionPass($autowirePass, $inlinePass);
116 $pass->process($container);
117 // mark the test as passed
118 $this->assertTrue(true);
121 public function testNoExceptionIfServiceRemoved()
123 $autowirePass = $this->getMockBuilder(AutowirePass::class)
126 $autowireException = new AutowiringFailedException('non_existent_service');
127 $autowirePass->expects($this->any())
128 ->method('getAutowiringExceptions')
129 ->will($this->returnValue(array($autowireException)));
131 $inlinePass = $this->getMockBuilder(InlineServiceDefinitionsPass::class)
133 $inlinePass->expects($this->any())
134 ->method('getInlinedServiceIds')
135 ->will($this->returnValue(array()));
137 $container = new ContainerBuilder();
139 $pass = new AutowireExceptionPass($autowirePass, $inlinePass);
141 $pass->process($container);
142 // mark the test as passed
143 $this->assertTrue(true);