RemoveEmptyControllerArgumentLocatorsPassTest.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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(array());
  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', array(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(array(), $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(array('c1:fooAction'), array_keys($controllers));
  40. $this->assertSame(array('bar'), array_keys($container->getDefinition((string) $controllers['c1:fooAction']->getValues()[0])->getArgument(0)));
  41. $expectedLog = array(
  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 testSameIdClass()
  48. {
  49. $container = new ContainerBuilder();
  50. $resolver = $container->register('argument_resolver.service')->addArgument(array());
  51. $container->register(RegisterTestController::class, RegisterTestController::class)
  52. ->addTag('controller.service_arguments')
  53. ;
  54. (new RegisterControllerArgumentLocatorsPass())->process($container);
  55. (new RemoveEmptyControllerArgumentLocatorsPass())->process($container);
  56. $expected = array(
  57. RegisterTestController::class.':fooAction',
  58. RegisterTestController::class.'::fooAction',
  59. );
  60. $this->assertEquals($expected, array_keys($container->getDefinition((string) $resolver->getArgument(0))->getArgument(0)));
  61. }
  62. public function testInvoke()
  63. {
  64. $container = new ContainerBuilder();
  65. $resolver = $container->register('argument_resolver.service')->addArgument(array());
  66. $container->register('invokable', InvokableRegisterTestController::class)
  67. ->addTag('controller.service_arguments')
  68. ;
  69. (new RegisterControllerArgumentLocatorsPass())->process($container);
  70. (new RemoveEmptyControllerArgumentLocatorsPass())->process($container);
  71. $this->assertEquals(
  72. array('invokable:__invoke', 'invokable'),
  73. array_keys($container->getDefinition((string) $resolver->getArgument(0))->getArgument(0))
  74. );
  75. }
  76. public function testInvokeSameIdClass()
  77. {
  78. $container = new ContainerBuilder();
  79. $resolver = $container->register('argument_resolver.service')->addArgument(array());
  80. $container->register(InvokableRegisterTestController::class, InvokableRegisterTestController::class)
  81. ->addTag('controller.service_arguments')
  82. ;
  83. (new RegisterControllerArgumentLocatorsPass())->process($container);
  84. (new RemoveEmptyControllerArgumentLocatorsPass())->process($container);
  85. $expected = array(
  86. InvokableRegisterTestController::class.':__invoke',
  87. InvokableRegisterTestController::class.'::__invoke',
  88. InvokableRegisterTestController::class,
  89. );
  90. $this->assertEquals($expected, array_keys($container->getDefinition((string) $resolver->getArgument(0))->getArgument(0)));
  91. }
  92. }
  93. class RemoveTestController1
  94. {
  95. public function fooAction(\stdClass $bar, ClassNotInContainer $baz)
  96. {
  97. }
  98. }
  99. class RemoveTestController2
  100. {
  101. public function setTestCase(TestCase $test)
  102. {
  103. }
  104. public function fooAction(ClassNotInContainer $bar)
  105. {
  106. }
  107. }
  108. class InvokableRegisterTestController
  109. {
  110. public function __invoke(\stdClass $bar)
  111. {
  112. }
  113. }
  114. class ClassNotInContainer
  115. {
  116. }