|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\HttpKernel\Tests\Controller\ArgumentResolver; |
| 13 | + |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | +use Symfony\Component\DependencyInjection\ServiceLocator; |
| 16 | +use Symfony\Component\HttpFoundation\Request; |
| 17 | +use Symfony\Component\HttpKernel\Controller\ArgumentResolver\ServiceValueResolver; |
| 18 | +use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata; |
| 19 | + |
| 20 | +class ServiceValueResolverTest extends TestCase |
| 21 | +{ |
| 22 | + public function testDoNotSupportWhenControllerDoNotExists() |
| 23 | + { |
| 24 | + $resolver = new ServiceValueResolver(new ServiceLocator(array())); |
| 25 | + $argument = new ArgumentMetadata('dummy', DummyService::class, false, false, null); |
| 26 | + $request = $this->requestWithAttributes(array('_controller' => 'my_controller')); |
| 27 | + |
| 28 | + $this->assertFalse($resolver->supports($request, $argument)); |
| 29 | + } |
| 30 | + |
| 31 | + public function testExistingController() |
| 32 | + { |
| 33 | + $resolver = new ServiceValueResolver(new ServiceLocator(array( |
| 34 | + 'App\\Controller\\Mine::method' => function () { |
| 35 | + return new ServiceLocator(array( |
| 36 | + 'dummy' => function () { |
| 37 | + return new DummyService(); |
| 38 | + }, |
| 39 | + )); |
| 40 | + }, |
| 41 | + ))); |
| 42 | + |
| 43 | + $request = $this->requestWithAttributes(array('_controller' => 'App\\Controller\\Mine::method')); |
| 44 | + $argument = new ArgumentMetadata('dummy', DummyService::class, false, false, null); |
| 45 | + |
| 46 | + $this->assertTrue($resolver->supports($request, $argument)); |
| 47 | + $this->assertYieldEquals(array(new DummyService()), $resolver->resolve($request, $argument)); |
| 48 | + } |
| 49 | + |
| 50 | + public function testControllerNameIsAnArray() |
| 51 | + { |
| 52 | + $resolver = new ServiceValueResolver(new ServiceLocator(array( |
| 53 | + 'App\\Controller\\Mine::method' => function () { |
| 54 | + return new ServiceLocator(array( |
| 55 | + 'dummy' => function () { |
| 56 | + return new DummyService(); |
| 57 | + }, |
| 58 | + )); |
| 59 | + }, |
| 60 | + ))); |
| 61 | + |
| 62 | + $request = $this->requestWithAttributes(array('_controller' => array('App\\Controller\\Mine', 'method'))); |
| 63 | + $argument = new ArgumentMetadata('dummy', DummyService::class, false, false, null); |
| 64 | + |
| 65 | + $this->assertTrue($resolver->supports($request, $argument)); |
| 66 | + $this->assertYieldEquals(array(new DummyService()), $resolver->resolve($request, $argument)); |
| 67 | + } |
| 68 | + |
| 69 | + private function requestWithAttributes(array $attributes) |
| 70 | + { |
| 71 | + $request = Request::create('/'); |
| 72 | + |
| 73 | + foreach ($attributes as $name => $value) { |
| 74 | + $request->attributes->set($name, $value); |
| 75 | + } |
| 76 | + |
| 77 | + return $request; |
| 78 | + } |
| 79 | + |
| 80 | + private function assertYieldEquals(array $expected, \Generator $generator) |
| 81 | + { |
| 82 | + $args = array(); |
| 83 | + foreach ($generator as $arg) { |
| 84 | + $args[] = $arg; |
| 85 | + } |
| 86 | + |
| 87 | + $this->assertEquals($expected, $args); |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +class DummyService |
| 92 | +{ |
| 93 | +} |
0 commit comments