|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 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\HttpFoundation\Request; |
| 16 | +use Symfony\Component\HttpKernel\Controller\ArgumentResolver\BackedEnumValueResolver; |
| 17 | +use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata; |
| 18 | +use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
| 19 | +use Symfony\Component\HttpKernel\Tests\Fixtures\Suit; |
| 20 | + |
| 21 | +/** |
| 22 | + * @requires PHP 8.1 |
| 23 | + */ |
| 24 | +class BackedEnumValueResolverTest extends TestCase |
| 25 | +{ |
| 26 | + /** |
| 27 | + * @dataProvider provideTestSupportsData |
| 28 | + */ |
| 29 | + public function testSupports(Request $request, ArgumentMetadata $metadata, bool $expectedSupport) |
| 30 | + { |
| 31 | + $resolver = new BackedEnumValueResolver(); |
| 32 | + |
| 33 | + self::assertSame($expectedSupport, $resolver->supports($request, $metadata)); |
| 34 | + } |
| 35 | + |
| 36 | + public function provideTestSupportsData(): iterable |
| 37 | + { |
| 38 | + yield 'unsupported type' => [ |
| 39 | + self::createRequest(['suit' => 'H']), |
| 40 | + self::createArgumentMetadata('suit', \stdClass::class), |
| 41 | + false, |
| 42 | + ]; |
| 43 | + |
| 44 | + yield 'supports from attributes' => [ |
| 45 | + self::createRequest(['suit' => 'H']), |
| 46 | + self::createArgumentMetadata('suit', Suit::class), |
| 47 | + true, |
| 48 | + ]; |
| 49 | + |
| 50 | + yield 'with null attribute value' => [ |
| 51 | + self::createRequest(['suit' => null]), |
| 52 | + self::createArgumentMetadata('suit', Suit::class), |
| 53 | + true, |
| 54 | + ]; |
| 55 | + |
| 56 | + yield 'without matching attribute' => [ |
| 57 | + self::createRequest(), |
| 58 | + self::createArgumentMetadata('suit', Suit::class), |
| 59 | + false, |
| 60 | + ]; |
| 61 | + |
| 62 | + yield 'unsupported variadic' => [ |
| 63 | + self::createRequest(['suit' => ['H', 'S']]), |
| 64 | + self::createArgumentMetadata( |
| 65 | + 'suit', |
| 66 | + Suit::class, |
| 67 | + variadic: true, |
| 68 | + ), |
| 69 | + false, |
| 70 | + ]; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * @dataProvider provideTestResolveData |
| 75 | + */ |
| 76 | + public function testResolve(Request $request, ArgumentMetadata $metadata, $expected) |
| 77 | + { |
| 78 | + $resolver = new BackedEnumValueResolver(); |
| 79 | + /** @var \Generator $results */ |
| 80 | + $results = $resolver->resolve($request, $metadata); |
| 81 | + |
| 82 | + self::assertSame($expected, iterator_to_array($results)); |
| 83 | + } |
| 84 | + |
| 85 | + public function provideTestResolveData(): iterable |
| 86 | + { |
| 87 | + yield 'resolves from attributes' => [ |
| 88 | + self::createRequest(['suit' => 'H']), |
| 89 | + self::createArgumentMetadata('suit', Suit::class), |
| 90 | + [Suit::Hearts], |
| 91 | + ]; |
| 92 | + |
| 93 | + yield 'with null attribute value' => [ |
| 94 | + self::createRequest(['suit' => null]), |
| 95 | + self::createArgumentMetadata( |
| 96 | + 'suit', |
| 97 | + Suit::class, |
| 98 | + ), |
| 99 | + [null], |
| 100 | + ]; |
| 101 | + } |
| 102 | + |
| 103 | + public function testResolveThrowsNotFoundOnInvalidValue() |
| 104 | + { |
| 105 | + $resolver = new BackedEnumValueResolver(); |
| 106 | + $request = self::createRequest(['suit' => 'foo']); |
| 107 | + $metadata = self::createArgumentMetadata('suit', Suit::class); |
| 108 | + |
| 109 | + $this->expectException(NotFoundHttpException::class); |
| 110 | + $this->expectExceptionMessage('Could not resolve the "Symfony\Component\HttpKernel\Tests\Fixtures\Suit $suit" controller argument: "foo" is not a valid backing value for enum'); |
| 111 | + |
| 112 | + /** @var \Generator $results */ |
| 113 | + $results = $resolver->resolve($request, $metadata); |
| 114 | + iterator_to_array($results); |
| 115 | + } |
| 116 | + |
| 117 | + public function testResolveThrowsOnUnexpectedType() |
| 118 | + { |
| 119 | + $resolver = new BackedEnumValueResolver(); |
| 120 | + $request = self::createRequest(['suit' => false]); |
| 121 | + $metadata = self::createArgumentMetadata('suit', Suit::class); |
| 122 | + |
| 123 | + $this->expectException(\LogicException::class); |
| 124 | + $this->expectExceptionMessage('Could not resolve the "Symfony\Component\HttpKernel\Tests\Fixtures\Suit $suit" controller argument: expecting an int or string, got bool.'); |
| 125 | + |
| 126 | + /** @var \Generator $results */ |
| 127 | + $results = $resolver->resolve($request, $metadata); |
| 128 | + iterator_to_array($results); |
| 129 | + } |
| 130 | + |
| 131 | + private static function createRequest(array $attributes = []): Request |
| 132 | + { |
| 133 | + return new Request([], [], $attributes); |
| 134 | + } |
| 135 | + |
| 136 | + private static function createArgumentMetadata(string $name, string $type, bool $variadic = false): ArgumentMetadata |
| 137 | + { |
| 138 | + return new ArgumentMetadata($name, $type, $variadic, false, null); |
| 139 | + } |
| 140 | +} |
0 commit comments