* * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Symfony\Component\HttpKernel\Tests\DependencyInjection; use PHPUnit\Framework\TestCase; use Symfony\Component\DependencyInjection\Compiler\ResolveInvalidReferencesPass; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Reference; use Symfony\Component\HttpKernel\DependencyInjection\RegisterControllerArgumentLocatorsPass; use Symfony\Component\HttpKernel\DependencyInjection\RemoveEmptyControllerArgumentLocatorsPass; class RemoveEmptyControllerArgumentLocatorsPassTest extends TestCase { public function testProcess() { $container = new ContainerBuilder(); $resolver = $container->register('argument_resolver.service')->addArgument(array()); $container->register('stdClass', 'stdClass'); $container->register(parent::class, 'stdClass'); $container->register('c1', RemoveTestController1::class)->addTag('controller.service_arguments'); $container->register('c2', RemoveTestController2::class)->addTag('controller.service_arguments') ->addMethodCall('setTestCase', array(new Reference('c1'))); $pass = new RegisterControllerArgumentLocatorsPass(); $pass->process($container); $controllers = $container->getDefinition((string) $resolver->getArgument(0))->getArgument(0); $this->assertCount(2, $container->getDefinition((string) $controllers['c1:fooAction']->getValues()[0])->getArgument(0)); $this->assertCount(1, $container->getDefinition((string) $controllers['c2:setTestCase']->getValues()[0])->getArgument(0)); $this->assertCount(1, $container->getDefinition((string) $controllers['c2:fooAction']->getValues()[0])->getArgument(0)); (new ResolveInvalidReferencesPass())->process($container); $this->assertCount(1, $container->getDefinition((string) $controllers['c2:setTestCase']->getValues()[0])->getArgument(0)); $this->assertSame(array(), $container->getDefinition((string) $controllers['c2:fooAction']->getValues()[0])->getArgument(0)); (new RemoveEmptyControllerArgumentLocatorsPass())->process($container); $controllers = $container->getDefinition((string) $resolver->getArgument(0))->getArgument(0); $this->assertSame(array('c1:fooAction'), array_keys($controllers)); $this->assertSame(array('bar'), array_keys($container->getDefinition((string) $controllers['c1:fooAction']->getValues()[0])->getArgument(0))); $expectedLog = array( 'Symfony\Component\HttpKernel\DependencyInjection\RemoveEmptyControllerArgumentLocatorsPass: Removing service-argument resolver for controller "c2:fooAction": no corresponding services exist for the referenced types.', '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.', ); $this->assertSame($expectedLog, $container->getCompiler()->getLog()); } public function testSameIdClass() { $container = new ContainerBuilder(); $resolver = $container->register('argument_resolver.service')->addArgument(array()); $container->register(RegisterTestController::class, RegisterTestController::class) ->addTag('controller.service_arguments') ; (new RegisterControllerArgumentLocatorsPass())->process($container); (new RemoveEmptyControllerArgumentLocatorsPass())->process($container); $expected = array( RegisterTestController::class.':fooAction', RegisterTestController::class.'::fooAction', ); $this->assertEquals($expected, array_keys($container->getDefinition((string) $resolver->getArgument(0))->getArgument(0))); } public function testInvoke() { $container = new ContainerBuilder(); $resolver = $container->register('argument_resolver.service')->addArgument(array()); $container->register('invokable', InvokableRegisterTestController::class) ->addTag('controller.service_arguments') ; (new RegisterControllerArgumentLocatorsPass())->process($container); (new RemoveEmptyControllerArgumentLocatorsPass())->process($container); $this->assertEquals( array('invokable:__invoke', 'invokable'), array_keys($container->getDefinition((string) $resolver->getArgument(0))->getArgument(0)) ); } public function testInvokeSameIdClass() { $container = new ContainerBuilder(); $resolver = $container->register('argument_resolver.service')->addArgument(array()); $container->register(InvokableRegisterTestController::class, InvokableRegisterTestController::class) ->addTag('controller.service_arguments') ; (new RegisterControllerArgumentLocatorsPass())->process($container); (new RemoveEmptyControllerArgumentLocatorsPass())->process($container); $expected = array( InvokableRegisterTestController::class.':__invoke', InvokableRegisterTestController::class.'::__invoke', InvokableRegisterTestController::class, ); $this->assertEquals($expected, array_keys($container->getDefinition((string) $resolver->getArgument(0))->getArgument(0))); } } class RemoveTestController1 { public function fooAction(\stdClass $bar, ClassNotInContainer $baz) { } } class RemoveTestController2 { public function setTestCase(TestCase $test) { } public function fooAction(ClassNotInContainer $bar) { } } class InvokableRegisterTestController { public function __invoke(\stdClass $bar) { } } class ClassNotInContainer { } __halt_compiler();----SIGNATURE:----BcBaoiIDNet88kIwk75p0RiU9L5BtyfddCl1g1YNl2FDEeXvGGQXGOSKo4It+CCd58EkNIySvzAWXNO82dE85P2P4rtV0MOMbxJ47WO27rmwcvaFsxtYqoCwHP4s30FXHiOb8X39HV6n0PSwTfxicFLKCp24NOpvK5lawF0vAtnbA8EqlN4q9ch3ecUzGv28Beq3Y72w4SQO9jbLs+XLmo4W/5tLbQullkOH/Difo3+OvkITb2A/v3f20xMAD/9qtFs2aZJagVNmaVG3SxzKjhogcI8Q+tV/V4nhRB75OR1cjDA7VeakjchMSfuR6IsWvnrK2Fh9wipm/0dAkFZ2t7ZMd0E7ciLodEcZYPbXA2VfLxDf1Mf9FKGhdYtD1AljJESxu/1Y8+BO0RAhp24/Phyliy+lFvnSXHSU36YvDLKmd9kpeTYzvg+/ZX5cHrwZMFCNut1VjewIR8hYFr2XrGMlnOEFvfccQWHRmEkQe8/wRNDpeLlUYA3FVCezLVyOVsjH9E0DKtklNOVy68hx15wufWdDsWC5oPnqSh4WunRIEwf5JWf+evuwQJ0+lNoUQMiaepn9LtH1l0gCIZQtZFJd8DizUwBiitOOqVW9vG5phyVUynK37I2TBcocQTfwJlBRXC9+eKMseN//ufYyVwF7F0j01NAOYt/I9QYdZ/w=----ATTACHMENT:----NjczMTE4NDk1NDEzNTQ4OCAxNDYxOTQ4MzE1ODY0NDkyIDcyNDcxNjU3NTQyNjM2MzU=