9cac9681857f89a5a4dee42f5e437d6ce44da5e2
[yaffs-website] / vendor / symfony / http-kernel / Tests / DependencyInjection / RemoveEmptyControllerArgumentLocatorsPassTest.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\HttpKernel\Tests\DependencyInjection;
13
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\DependencyInjection\Compiler\ResolveInvalidReferencesPass;
16 use Symfony\Component\DependencyInjection\ContainerBuilder;
17 use Symfony\Component\DependencyInjection\Reference;
18 use Symfony\Component\HttpKernel\DependencyInjection\RegisterControllerArgumentLocatorsPass;
19 use Symfony\Component\HttpKernel\DependencyInjection\RemoveEmptyControllerArgumentLocatorsPass;
20
21 class RemoveEmptyControllerArgumentLocatorsPassTest extends TestCase
22 {
23     public function testProcess()
24     {
25         $container = new ContainerBuilder();
26         $resolver = $container->register('argument_resolver.service')->addArgument(array());
27
28         $container->register('stdClass', 'stdClass');
29         $container->register(parent::class, 'stdClass');
30         $container->register('c1', RemoveTestController1::class)->addTag('controller.service_arguments');
31         $container->register('c2', RemoveTestController2::class)->addTag('controller.service_arguments')
32             ->addMethodCall('setTestCase', array(new Reference('c1')));
33
34         $pass = new RegisterControllerArgumentLocatorsPass();
35         $pass->process($container);
36
37         $controllers = $container->getDefinition((string) $resolver->getArgument(0))->getArgument(0);
38
39         $this->assertCount(2, $container->getDefinition((string) $controllers['c1:fooAction']->getValues()[0])->getArgument(0));
40         $this->assertCount(1, $container->getDefinition((string) $controllers['c2:setTestCase']->getValues()[0])->getArgument(0));
41         $this->assertCount(1, $container->getDefinition((string) $controllers['c2:fooAction']->getValues()[0])->getArgument(0));
42
43         (new ResolveInvalidReferencesPass())->process($container);
44
45         $this->assertCount(1, $container->getDefinition((string) $controllers['c2:setTestCase']->getValues()[0])->getArgument(0));
46         $this->assertSame(array(), $container->getDefinition((string) $controllers['c2:fooAction']->getValues()[0])->getArgument(0));
47
48         (new RemoveEmptyControllerArgumentLocatorsPass())->process($container);
49
50         $controllers = $container->getDefinition((string) $resolver->getArgument(0))->getArgument(0);
51
52         $this->assertSame(array('c1:fooAction'), array_keys($controllers));
53         $this->assertSame(array('bar'), array_keys($container->getDefinition((string) $controllers['c1:fooAction']->getValues()[0])->getArgument(0)));
54
55         $expectedLog = array(
56             'Symfony\Component\HttpKernel\DependencyInjection\RemoveEmptyControllerArgumentLocatorsPass: Removing service-argument resolver for controller "c2:fooAction": no corresponding services exist for the referenced types.',
57             'Symfony\Component\HttpKernel\DependencyInjection\RemoveEmptyControllerArgumentLocatorsPass: Removing method "setTestCase" of service "c2" from controller candidates: the method is called at instantiation, thus cannot be an action.',
58         );
59
60         $this->assertSame($expectedLog, $container->getCompiler()->getLog());
61     }
62
63     public function testSameIdClass()
64     {
65         $container = new ContainerBuilder();
66         $resolver = $container->register('argument_resolver.service')->addArgument(array());
67
68         $container->register(RegisterTestController::class, RegisterTestController::class)
69             ->addTag('controller.service_arguments')
70         ;
71
72         (new RegisterControllerArgumentLocatorsPass())->process($container);
73         (new RemoveEmptyControllerArgumentLocatorsPass())->process($container);
74
75         $expected = array(
76             RegisterTestController::class.':fooAction',
77             RegisterTestController::class.'::fooAction',
78         );
79         $this->assertEquals($expected, array_keys($container->getDefinition((string) $resolver->getArgument(0))->getArgument(0)));
80     }
81
82     public function testInvoke()
83     {
84         $container = new ContainerBuilder();
85         $resolver = $container->register('argument_resolver.service')->addArgument(array());
86
87         $container->register('invokable', InvokableRegisterTestController::class)
88             ->addTag('controller.service_arguments')
89         ;
90
91         (new RegisterControllerArgumentLocatorsPass())->process($container);
92         (new RemoveEmptyControllerArgumentLocatorsPass())->process($container);
93
94         $this->assertEquals(
95             array('invokable:__invoke', 'invokable'),
96             array_keys($container->getDefinition((string) $resolver->getArgument(0))->getArgument(0))
97         );
98     }
99
100     public function testInvokeSameIdClass()
101     {
102         $container = new ContainerBuilder();
103         $resolver = $container->register('argument_resolver.service')->addArgument(array());
104
105         $container->register(InvokableRegisterTestController::class, InvokableRegisterTestController::class)
106             ->addTag('controller.service_arguments')
107         ;
108
109         (new RegisterControllerArgumentLocatorsPass())->process($container);
110         (new RemoveEmptyControllerArgumentLocatorsPass())->process($container);
111
112         $expected = array(
113             InvokableRegisterTestController::class.':__invoke',
114             InvokableRegisterTestController::class.'::__invoke',
115             InvokableRegisterTestController::class,
116         );
117         $this->assertEquals($expected, array_keys($container->getDefinition((string) $resolver->getArgument(0))->getArgument(0)));
118     }
119 }
120
121 class RemoveTestController1
122 {
123     public function fooAction(\stdClass $bar, ClassNotInContainer $baz)
124     {
125     }
126 }
127
128 class RemoveTestController2
129 {
130     public function setTestCase(TestCase $test)
131     {
132     }
133
134     public function fooAction(ClassNotInContainer $bar)
135     {
136     }
137 }
138
139 class InvokableRegisterTestController
140 {
141     public function __invoke(\stdClass $bar)
142     {
143     }
144 }
145
146 class ClassNotInContainer
147 {
148 }