ServiceValueResolverTest.php 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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\ServiceLocator;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpKernel\Controller\ArgumentResolver\ServiceValueResolver;
  15. use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata;
  16. class ServiceValueResolverTest extends TestCase
  17. {
  18. public function testDoNotSupportWhenControllerDoNotExists()
  19. {
  20. $resolver = new ServiceValueResolver(new ServiceLocator(array()));
  21. $argument = new ArgumentMetadata('dummy', DummyService::class, false, false, null);
  22. $request = $this->requestWithAttributes(array('_controller' => 'my_controller'));
  23. $this->assertFalse($resolver->supports($request, $argument));
  24. }
  25. public function testExistingController()
  26. {
  27. $resolver = new ServiceValueResolver(new ServiceLocator(array(
  28. 'App\\Controller\\Mine::method' => function () {
  29. return new ServiceLocator(array(
  30. 'dummy' => function () {
  31. return new DummyService();
  32. },
  33. ));
  34. },
  35. )));
  36. $request = $this->requestWithAttributes(array('_controller' => 'App\\Controller\\Mine::method'));
  37. $argument = new ArgumentMetadata('dummy', DummyService::class, false, false, null);
  38. $this->assertTrue($resolver->supports($request, $argument));
  39. $this->assertYieldEquals(array(new DummyService()), $resolver->resolve($request, $argument));
  40. }
  41. public function testExistingControllerWithATrailingBackSlash()
  42. {
  43. $resolver = new ServiceValueResolver(new ServiceLocator(array(
  44. 'App\\Controller\\Mine::method' => function () {
  45. return new ServiceLocator(array(
  46. 'dummy' => function () {
  47. return new DummyService();
  48. },
  49. ));
  50. },
  51. )));
  52. $request = $this->requestWithAttributes(array('_controller' => '\\App\\Controller\\Mine::method'));
  53. $argument = new ArgumentMetadata('dummy', DummyService::class, false, false, null);
  54. $this->assertTrue($resolver->supports($request, $argument));
  55. $this->assertYieldEquals(array(new DummyService()), $resolver->resolve($request, $argument));
  56. }
  57. public function testControllerNameIsAnArray()
  58. {
  59. $resolver = new ServiceValueResolver(new ServiceLocator(array(
  60. 'App\\Controller\\Mine::method' => function () {
  61. return new ServiceLocator(array(
  62. 'dummy' => function () {
  63. return new DummyService();
  64. },
  65. ));
  66. },
  67. )));
  68. $request = $this->requestWithAttributes(array('_controller' => array('App\\Controller\\Mine', 'method')));
  69. $argument = new ArgumentMetadata('dummy', DummyService::class, false, false, null);
  70. $this->assertTrue($resolver->supports($request, $argument));
  71. $this->assertYieldEquals(array(new DummyService()), $resolver->resolve($request, $argument));
  72. }
  73. private function requestWithAttributes(array $attributes)
  74. {
  75. $request = Request::create('/');
  76. foreach ($attributes as $name => $value) {
  77. $request->attributes->set($name, $value);
  78. }
  79. return $request;
  80. }
  81. private function assertYieldEquals(array $expected, \Generator $generator)
  82. {
  83. $args = array();
  84. foreach ($generator as $arg) {
  85. $args[] = $arg;
  86. }
  87. $this->assertEquals($expected, $args);
  88. }
  89. }
  90. class DummyService
  91. {
  92. }