|
| 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\AcceptHeader; |
| 16 | +use Symfony\Component\HttpFoundation\Request; |
| 17 | +use Symfony\Component\HttpKernel\Attribute\MapRequestHeader; |
| 18 | +use Symfony\Component\HttpKernel\Controller\ArgumentResolver\RequestHeaderValueResolver; |
| 19 | +use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata; |
| 20 | +use Symfony\Component\HttpKernel\Exception\HttpException; |
| 21 | + |
| 22 | +class RequestHeaderValueResolverTest extends TestCase |
| 23 | +{ |
| 24 | + public static function provideHeaderValueWithStringType(): iterable |
| 25 | + { |
| 26 | + yield 'with accept' => ['accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8']; |
| 27 | + yield 'with accept-language' => ['accept-language', 'en-us,en;q=0.5']; |
| 28 | + yield 'with host' => ['host', 'localhost']; |
| 29 | + yield 'with user-agent' => ['user-agent', 'Symfony']; |
| 30 | + } |
| 31 | + |
| 32 | + public static function provideHeaderValueWithArrayType(): iterable |
| 33 | + { |
| 34 | + yield 'with accept' => [ |
| 35 | + 'accept', |
| 36 | + 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', |
| 37 | + [ |
| 38 | + [ |
| 39 | + 'text/html', |
| 40 | + 'application/xhtml+xml', |
| 41 | + 'application/xml', |
| 42 | + '*/*', |
| 43 | + ], |
| 44 | + ], |
| 45 | + ]; |
| 46 | + yield 'with accept-language' => [ |
| 47 | + 'accept-language', |
| 48 | + 'en-us,en;q=0.5', |
| 49 | + [ |
| 50 | + [ |
| 51 | + 'en_US', |
| 52 | + 'en', |
| 53 | + ], |
| 54 | + ], |
| 55 | + ]; |
| 56 | + yield 'with host' => [ |
| 57 | + 'host', |
| 58 | + 'localhost', |
| 59 | + [ |
| 60 | + ['localhost'], |
| 61 | + ], |
| 62 | + ]; |
| 63 | + yield 'with user-agent' => [ |
| 64 | + 'user-agent', |
| 65 | + 'Symfony', |
| 66 | + [ |
| 67 | + ['Symfony'], |
| 68 | + ], |
| 69 | + ]; |
| 70 | + } |
| 71 | + |
| 72 | + public static function provideHeaderValueWithAcceptHeaderType(): iterable |
| 73 | + { |
| 74 | + yield 'with accept' => [ |
| 75 | + 'accept', |
| 76 | + 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', |
| 77 | + [AcceptHeader::fromString('text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8')], |
| 78 | + ]; |
| 79 | + yield 'with accept-language' => [ |
| 80 | + 'accept-language', |
| 81 | + 'en-us,en;q=0.5', |
| 82 | + [AcceptHeader::fromString('en-us,en;q=0.5')], |
| 83 | + ]; |
| 84 | + yield 'with host' => [ |
| 85 | + 'host', |
| 86 | + 'localhost', |
| 87 | + [AcceptHeader::fromString('localhost')], |
| 88 | + ]; |
| 89 | + yield 'with user-agent' => [ |
| 90 | + 'user-agent', |
| 91 | + 'Symfony', |
| 92 | + [AcceptHeader::fromString('Symfony')], |
| 93 | + ]; |
| 94 | + } |
| 95 | + |
| 96 | + public static function provideHeaderValueWithDefaultAndNull(): iterable |
| 97 | + { |
| 98 | + yield 'with hasDefaultValue' => [true, 'foo', false, 'foo']; |
| 99 | + yield 'with no isNullable' => [false, null, true, null]; |
| 100 | + } |
| 101 | + |
| 102 | + public function testWrongType() |
| 103 | + { |
| 104 | + self::expectException(\LogicException::class); |
| 105 | + |
| 106 | + $metadata = new ArgumentMetadata('accept', 'int', false, false, null, false, [ |
| 107 | + MapRequestHeader::class => new MapRequestHeader(), |
| 108 | + ]); |
| 109 | + |
| 110 | + $request = Request::create('/'); |
| 111 | + |
| 112 | + $resolver = new RequestHeaderValueResolver(); |
| 113 | + $resolver->resolve($request, $metadata); |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * @dataProvider provideHeaderValueWithStringType |
| 118 | + */ |
| 119 | + public function testWithStringType(string $parameter, string $value) |
| 120 | + { |
| 121 | + $resolver = new RequestHeaderValueResolver(); |
| 122 | + |
| 123 | + $metadata = new ArgumentMetadata('variableName', 'string', false, false, null, false, [ |
| 124 | + MapRequestHeader::class => new MapRequestHeader($parameter), |
| 125 | + ]); |
| 126 | + |
| 127 | + $request = Request::create('/'); |
| 128 | + $request->headers->set($parameter, $value); |
| 129 | + |
| 130 | + $arguments = $resolver->resolve($request, $metadata); |
| 131 | + |
| 132 | + self::assertEquals([$value], $arguments); |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * @dataProvider provideHeaderValueWithArrayType |
| 137 | + */ |
| 138 | + public function testWithArrayType(string $parameter, string $value, array $expected) |
| 139 | + { |
| 140 | + $resolver = new RequestHeaderValueResolver(); |
| 141 | + |
| 142 | + $metadata = new ArgumentMetadata('variableName', 'array', false, false, null, false, [ |
| 143 | + MapRequestHeader::class => new MapRequestHeader($parameter), |
| 144 | + ]); |
| 145 | + |
| 146 | + $request = Request::create('/'); |
| 147 | + $request->headers->set($parameter, $value); |
| 148 | + |
| 149 | + $arguments = $resolver->resolve($request, $metadata); |
| 150 | + |
| 151 | + self::assertEquals($expected, $arguments); |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * @dataProvider provideHeaderValueWithAcceptHeaderType |
| 156 | + */ |
| 157 | + public function testWithAcceptHeaderType(string $parameter, string $value, array $expected) |
| 158 | + { |
| 159 | + $resolver = new RequestHeaderValueResolver(); |
| 160 | + |
| 161 | + $metadata = new ArgumentMetadata('variableName', AcceptHeader::class, false, false, null, false, [ |
| 162 | + MapRequestHeader::class => new MapRequestHeader($parameter), |
| 163 | + ]); |
| 164 | + |
| 165 | + $request = Request::create('/'); |
| 166 | + $request->headers->set($parameter, $value); |
| 167 | + |
| 168 | + $arguments = $resolver->resolve($request, $metadata); |
| 169 | + |
| 170 | + self::assertEquals($expected, $arguments); |
| 171 | + } |
| 172 | + |
| 173 | + /** |
| 174 | + * @dataProvider provideHeaderValueWithDefaultAndNull |
| 175 | + */ |
| 176 | + public function testWithDefaultValueAndNull(bool $hasDefaultValue, ?string $defaultValue, bool $isNullable, ?string $expected) |
| 177 | + { |
| 178 | + $metadata = new ArgumentMetadata('wrong-header', 'string', false, $hasDefaultValue, $defaultValue, $isNullable, [ |
| 179 | + MapRequestHeader::class => new MapRequestHeader(), |
| 180 | + ]); |
| 181 | + |
| 182 | + $request = Request::create('/'); |
| 183 | + |
| 184 | + $resolver = new RequestHeaderValueResolver(); |
| 185 | + $arguments = $resolver->resolve($request, $metadata); |
| 186 | + |
| 187 | + self::assertEquals([$expected], $arguments); |
| 188 | + } |
| 189 | + |
| 190 | + public function testWithNoDefaultAndNotNullable() |
| 191 | + { |
| 192 | + self::expectException(HttpException::class); |
| 193 | + self::expectExceptionMessage('Missing header "variableName".'); |
| 194 | + |
| 195 | + $metadata = new ArgumentMetadata('variableName', 'string', false, false, null, false, [ |
| 196 | + MapRequestHeader::class => new MapRequestHeader(), |
| 197 | + ]); |
| 198 | + |
| 199 | + $resolver = new RequestHeaderValueResolver(); |
| 200 | + $resolver->resolve(Request::create('/'), $metadata); |
| 201 | + } |
| 202 | +} |
0 commit comments