RemoveEmptyControllerArgumentLocatorsPassTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\HttpKernel\Tests\DependencyInjection;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\DependencyInjection\Compiler\ResolveInvalidReferencesPass;
  13. use Symfony\Component\DependencyInjection\ContainerBuilder;
  14. use Symfony\Component\DependencyInjection\Reference;
  15. use Symfony\Component\HttpKernel\DependencyInjection\RegisterControllerArgumentLocatorsPass;
  16. use Symfony\Component\HttpKernel\DependencyInjection\RemoveEmptyControllerArgumentLocatorsPass;
  17. class RemoveEmptyControllerArgumentLocatorsPassTest extends TestCase
  18. {
  19. public function testProcess()
  20. {
  21. $container = new ContainerBuilder();
  22. $resolver = $container->register('argument_resolver.service')->addArgument([]);
  23. $container->register('stdClass', 'stdClass');
  24. $container->register(parent::class, 'stdClass');
  25. $container->register('c1', RemoveTestController1::class)->addTag('controller.service_arguments');
  26. $container->register('c2', RemoveTestController2::class)->addTag('controller.service_arguments')
  27. ->addMethodCall('setTestCase', [new Reference('c1')]);
  28. $pass = new RegisterControllerArgumentLocatorsPass();
  29. $pass->process($container);
  30. $controllers = $container->getDefinition((string) $resolver->getArgument(0))->getArgument(0);
  31. $this->assertCount(2, $container->getDefinition((string) $controllers['c1::fooAction']->getValues()[0])->getArgument(0));
  32. $this->assertCount(1, $container->getDefinition((string) $controllers['c2::setTestCase']->getValues()[0])->getArgument(0));
  33. $this->assertCount(1, $container->getDefinition((string) $controllers['c2::fooAction']->getValues()[0])->getArgument(0));
  34. (new ResolveInvalidReferencesPass())->process($container);
  35. $this->assertCount(1, $container->getDefinition((string) $controllers['c2::setTestCase']->getValues()[0])->getArgument(0));
  36. $this->assertSame([], $container->getDefinition((string) $controllers['c2::fooAction']->getValues()[0])->getArgument(0));
  37. (new RemoveEmptyControllerArgumentLocatorsPass())->process($container);
  38. $controllers = $container->getDefinition((string) $resolver->getArgument(0))->getArgument(0);
  39. $this->assertSame(['c1::fooAction', 'c1:fooAction'], array_keys($controllers));
  40. $this->assertSame(['bar'], array_keys($container->getDefinition((string) $controllers['c1::fooAction']->getValues()[0])->getArgument(0)));
  41. $expectedLog = [
  42. 'Symfony\Component\HttpKernel\DependencyInjection\RemoveEmptyControllerArgumentLocatorsPass: Removing service-argument resolver for controller "c2::fooAction": no corresponding services exist for the referenced types.',
  43. '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.',
  44. ];
  45. $this->assertSame($expectedLog, $container->getCompiler()->getLog());
  46. }
  47. public function testInvoke()
  48. {
  49. $container = new ContainerBuilder();
  50. $resolver = $container->register('argument_resolver.service')->addArgument([]);
  51. $container->register('invokable', InvokableRegisterTestController::class)
  52. ->addTag('controller.service_arguments')
  53. ;
  54. (new RegisterControllerArgumentLocatorsPass())->process($container);
  55. (new RemoveEmptyControllerArgumentLocatorsPass())->process($container);
  56. $this->assertEquals(
  57. ['invokable::__invoke', 'invokable:__invoke', 'invokable'],
  58. array_keys($container->getDefinition((string) $resolver->getArgument(0))->getArgument(0))
  59. );
  60. }
  61. }
  62. class RemoveTestController1
  63. {
  64. public function fooAction(\stdClass $bar, ClassNotInContainer $baz = null)
  65. {
  66. }
  67. }
  68. class RemoveTestController2
  69. {
  70. public function setTestCase(TestCase $test)
  71. {
  72. }
  73. public function fooAction(ClassNotInContainer $bar = null)
  74. {
  75. }
  76. }
  77. class InvokableRegisterTestController
  78. {
  79. public function __invoke(\stdClass $bar)
  80. {
  81. }
  82. }
  83. class ClassNotInContainer
  84. {
  85. }