ServiceValueResolverTest.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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\Controller\ArgumentResolver;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\DependencyInjection\ContainerBuilder;
  13. use Symfony\Component\DependencyInjection\ServiceLocator;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\HttpKernel\Controller\ArgumentResolver\ServiceValueResolver;
  16. use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata;
  17. use Symfony\Component\HttpKernel\DependencyInjection\RegisterControllerArgumentLocatorsPass;
  18. class ServiceValueResolverTest extends TestCase
  19. {
  20. public function testDoNotSupportWhenControllerDoNotExists()
  21. {
  22. $resolver = new ServiceValueResolver(new ServiceLocator([]));
  23. $argument = new ArgumentMetadata('dummy', DummyService::class, false, false, null);
  24. $request = $this->requestWithAttributes(['_controller' => 'my_controller']);
  25. $this->assertFalse($resolver->supports($request, $argument));
  26. }
  27. public function testExistingController()
  28. {
  29. $resolver = new ServiceValueResolver(new ServiceLocator([
  30. 'App\\Controller\\Mine::method' => function () {
  31. return new ServiceLocator([
  32. 'dummy' => function () {
  33. return new DummyService();
  34. },
  35. ]);
  36. },
  37. ]));
  38. $request = $this->requestWithAttributes(['_controller' => 'App\\Controller\\Mine::method']);
  39. $argument = new ArgumentMetadata('dummy', DummyService::class, false, false, null);
  40. $this->assertTrue($resolver->supports($request, $argument));
  41. $this->assertYieldEquals([new DummyService()], $resolver->resolve($request, $argument));
  42. }
  43. public function testExistingControllerWithATrailingBackSlash()
  44. {
  45. $resolver = new ServiceValueResolver(new ServiceLocator([
  46. 'App\\Controller\\Mine::method' => function () {
  47. return new ServiceLocator([
  48. 'dummy' => function () {
  49. return new DummyService();
  50. },
  51. ]);
  52. },
  53. ]));
  54. $request = $this->requestWithAttributes(['_controller' => '\\App\\Controller\\Mine::method']);
  55. $argument = new ArgumentMetadata('dummy', DummyService::class, false, false, null);
  56. $this->assertTrue($resolver->supports($request, $argument));
  57. $this->assertYieldEquals([new DummyService()], $resolver->resolve($request, $argument));
  58. }
  59. public function testExistingControllerWithMethodNameStartUppercase()
  60. {
  61. $resolver = new ServiceValueResolver(new ServiceLocator([
  62. 'App\\Controller\\Mine::method' => function () {
  63. return new ServiceLocator([
  64. 'dummy' => function () {
  65. return new DummyService();
  66. },
  67. ]);
  68. },
  69. ]));
  70. $request = $this->requestWithAttributes(['_controller' => 'App\\Controller\\Mine::Method']);
  71. $argument = new ArgumentMetadata('dummy', DummyService::class, false, false, null);
  72. $this->assertTrue($resolver->supports($request, $argument));
  73. $this->assertYieldEquals([new DummyService()], $resolver->resolve($request, $argument));
  74. }
  75. public function testControllerNameIsAnArray()
  76. {
  77. $resolver = new ServiceValueResolver(new ServiceLocator([
  78. 'App\\Controller\\Mine::method' => function () {
  79. return new ServiceLocator([
  80. 'dummy' => function () {
  81. return new DummyService();
  82. },
  83. ]);
  84. },
  85. ]));
  86. $request = $this->requestWithAttributes(['_controller' => ['App\\Controller\\Mine', 'method']]);
  87. $argument = new ArgumentMetadata('dummy', DummyService::class, false, false, null);
  88. $this->assertTrue($resolver->supports($request, $argument));
  89. $this->assertYieldEquals([new DummyService()], $resolver->resolve($request, $argument));
  90. }
  91. /**
  92. * @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
  93. * @expectedExceptionMessage Cannot autowire argument $dummy of "Symfony\Component\HttpKernel\Tests\Controller\ArgumentResolver\DummyController::index()": it references class "Symfony\Component\HttpKernel\Tests\Controller\ArgumentResolver\DummyService" but no such service exists.
  94. */
  95. public function testErrorIsTruncated()
  96. {
  97. $container = new ContainerBuilder();
  98. $container->addCompilerPass(new RegisterControllerArgumentLocatorsPass());
  99. $container->register('argument_resolver.service', ServiceValueResolver::class)->addArgument(null)->setPublic(true);
  100. $container->register(DummyController::class)->addTag('controller.service_arguments')->setPublic(true);
  101. $container->compile();
  102. $request = $this->requestWithAttributes(['_controller' => [DummyController::class, 'index']]);
  103. $argument = new ArgumentMetadata('dummy', DummyService::class, false, false, null);
  104. $container->get('argument_resolver.service')->resolve($request, $argument)->current();
  105. }
  106. private function requestWithAttributes(array $attributes)
  107. {
  108. $request = Request::create('/');
  109. foreach ($attributes as $name => $value) {
  110. $request->attributes->set($name, $value);
  111. }
  112. return $request;
  113. }
  114. private function assertYieldEquals(array $expected, \Generator $generator)
  115. {
  116. $args = [];
  117. foreach ($generator as $arg) {
  118. $args[] = $arg;
  119. }
  120. $this->assertEquals($expected, $args);
  121. }
  122. }
  123. class DummyService
  124. {
  125. }
  126. class DummyController
  127. {
  128. public function index(DummyService $dummy)
  129. {
  130. }
  131. }