|
18 | 18 | use Symfony\Component\HttpKernel\Controller\ValueResolverInterface;
|
19 | 19 | use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata;
|
20 | 20 | use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
| 21 | +use Symfony\Component\HttpKernel\Tests\Fixtures\Suit; |
21 | 22 |
|
22 | 23 | class QueryParameterValueResolverTest extends TestCase
|
23 | 24 | {
|
@@ -64,6 +65,13 @@ public static function provideTestResolve(): iterable
|
64 | 65 |
F438
[['1', '2'], ['2']], |
65 | 66 | null,
|
66 | 67 | ];
|
| 68 | + yield 'parameter found and array variadic with parameter not array failure' => [ |
| 69 | + Request::create('/', 'GET', ['ids' => [['1', '2'], 1]]), |
| 70 | + new ArgumentMetadata('ids', 'array', true, false, false, attributes: [new MapQueryParameter()]), |
| 71 | + [], |
| 72 | + NotFoundHttpException::class, |
| 73 | + 'Invalid query parameter "ids".', |
| 74 | + ]; |
67 | 75 | yield 'parameter found and string' => [
|
68 | 76 | Request::create('/', 'GET', ['firstName' => 'John']),
|
69 | 77 | new ArgumentMetadata('firstName', 'string', false, false, false, attributes: [new MapQueryParameter()]),
|
@@ -176,6 +184,71 @@ public static function provideTestResolve(): iterable
|
176 | 184 | 'Invalid query parameter "isVerified".',
|
177 | 185 | ];
|
178 | 186 |
|
| 187 | + yield 'parameter found and backing value' => [ |
| 188 | + Request::create('/', 'GET', ['suit' => 'H']), |
| 189 | + new ArgumentMetadata('suit', Suit::class, false, false, false, attributes: [new MapQueryParameter()]), |
| 190 | + [Suit::Hearts], |
| 191 | + null, |
| 192 | + ]; |
| 193 | + yield 'parameter found and backing value variadic' => [ |
| 194 | + Request::create('/', 'GET', ['suits' => ['H', 'D']]), |
| 195 | + new ArgumentMetadata('suits', Suit::class, true, false, false, attributes: [new MapQueryParameter()]), |
| 196 | + [Suit::Hearts, Suit::Diamonds], |
| 197 | + null, |
| 198 | + ]; |
| 199 | + yield 'parameter found and backing value not int nor string' => [ |
| 200 | + Request::create('/', 'GET', ['suit' => 1]), |
| 201 | + new ArgumentMetadata('suit', Suit::class, false, false, false, attributes: [new MapQueryParameter(filter: \FILTER_VALIDATE_BOOL)]), |
| 202 | + [], |
| 203 | + NotFoundHttpException::class, |
| 204 | + 'Invalid query parameter "suit".', |
| 205 | + ]; |
| 206 | + yield 'parameter found and backing value not int nor string that fallbacks to null on failure' => [ |
| 207 | + Request::create('/', 'GET', ['suit' => 1]), |
| 208 | + new ArgumentMetadata('suit', Suit::class, false, false, false, attributes: [new MapQueryParameter(filter: \FILTER_VALIDATE_BOOL, flags: \FILTER_NULL_ON_FAILURE)]), |
| 209 | + [null], |
| 210 | + null, |
| 211 | + ]; |
| 212 | + yield 'parameter found and value not valid backing value' => [ |
| 213 | + Request::create('/', 'GET', ['suit' => 'B']), |
| 214 | + new ArgumentMetadata('suit', Suit::class, false, false, false, attributes: [new MapQueryParameter()]), |
| 215 | + [], |
| 216 | + NotFoundHttpException::class, |
| 217 | + 'Invalid query parameter "suit".', |
| 218 | + ]; |
| 219 | + yield 'parameter found and value not valid backing value that falls back to null on failure' => [ |
| 220 | + Request::create('/', 'GET', ['suit' => 'B']), |
| 221 | + new ArgumentMetadata('suit', Suit::class, false, false, false, attributes: [new MapQueryParameter(flags: \FILTER_NULL_ON_FAILURE)]), |
| 222 | + [null], |
| 223 | + null, |
| 224 | + ]; |
| 225 | + yield 'parameter found and backing type variadic and at least one backing value not int nor string' => [ |
| 226 | + Request::create('/', 'GET', ['suits' => [1, 'D']]), |
| 227 | + new ArgumentMetadata('suits', Suit::class, false, false, false, attributes: [new MapQueryParameter(filter: \FILTER_VALIDATE_BOOL)]), |
| 228 | + [], |
| 229 | + NotFoundHttpException::class, |
| 230 | + 'Invalid query parameter "suits".', |
| 231 | + ]; |
| 232 | + yield 'parameter found and backing type variadic and at least one backing value not int nor string that fallbacks to null on failure' => [ |
| 233 | + Request::create('/', 'GET', ['suits' => [1, 'D']]), |
| 234 | + new ArgumentMetadata('suits', Suit::class, false, false, false, attributes: [new MapQueryParameter(flags: \FILTER_NULL_ON_FAILURE)]), |
| 235 | + [null], |
| 236 | + null, |
| 237 | + ]; |
| 238 | + yield 'parameter found and backing type variadic and at least one value not valid backing value' => [ |
| 239 | + Request::create('/', 'GET', ['suits' => ['B', 'D
F987
']]), |
| 240 | + new ArgumentMetadata('suits', Suit::class, false, false, false, attributes: [new MapQueryParameter()]), |
| 241 | + [], |
| 242 | + NotFoundHttpException::class, |
| 243 | + 'Invalid query parameter "suits".', |
| 244 | + ]; |
| 245 | + yield 'parameter found and backing type variadic and at least one value not valid backing value that falls back to null on failure' => [ |
| 246 | + Request::create('/', 'GET', ['suits' => ['B', 'D']]), |
| 247 | + new ArgumentMetadata('suits', Suit::class, false, false, false, attributes: [new MapQueryParameter(flags: \FILTER_NULL_ON_FAILURE)]), |
| 248 | + [null], |
| 249 | + null, |
| 250 | + ]; |
| 251 | + |
179 | 252 | yield 'parameter not found but nullable' => [
|
180 | 253 | Request::create('/', 'GET'),
|
181 | 254 | new ArgumentMetadata('firstName', 'string', false, false, false, true, [new MapQueryParameter()]),
|
@@ -203,14 +276,14 @@ public static function provideTestResolve(): iterable
|
203 | 276 | new ArgumentMetadata('standardClass', \stdClass::class, false, false, false, attributes: [new MapQueryParameter()]),
|
204 | 277 | [],
|
205 | 278 | \LogicException::class,
|
206 |
| - '#[MapQueryParameter] cannot be used on controller argument "$standardClass" of type "stdClass"; one of array, string, int, float or bool should be used.', |
| 279 | + '#[MapQueryParameter] cannot be used on controller argument "$standardClass" of type "stdClass"; one of array, string, int, float, bool or \BackedEnum should be used.', |
207 | 280 | ];
|
208 | 281 | yield 'unsupported type variadic' => [
|
209 | 282 | Request::create('/', 'GET', ['standardClass' => 'test']),
|
210 | 283 | new ArgumentMetadata('standardClass', \stdClass::class, true, false, false, attributes: [new MapQueryParameter()]),
|
211 | 284 | [],
|
212 | 285 | \LogicException::class,
|
213 |
| - '#[MapQueryParameter] cannot be used on controller argument "...$standardClass" of type "stdClass"; one of array, string, int, float or bool should be used.', |
| 286 | + '#[MapQueryParameter] cannot be used on controller argument "...$standardClass" of type "stdClass"; one of array, string, int, float, bool or \BackedEnum should be used.', |
214 | 287 | ];
|
215 | 288 | }
|
216 | 289 |
|
|
0 commit comments