|
11 | 11 |
|
12 | 12 | namespace Symfony\Bundle\FrameworkBundle\Tests\Functional; |
13 | 13 |
|
| 14 | +use Symfony\Component\HttpFoundation\File\UploadedFile; |
14 | 15 | use Symfony\Component\HttpFoundation\JsonResponse; |
15 | 16 | use Symfony\Component\HttpFoundation\Request; |
16 | 17 | use Symfony\Component\HttpFoundation\Response; |
17 | 18 | use Symfony\Component\HttpKernel\Attribute\MapQueryString; |
18 | 19 | use Symfony\Component\HttpKernel\Attribute\MapRequestPayload; |
| 20 | +use Symfony\Component\HttpKernel\Attribute\MapUploadedFile; |
19 | 21 | use Symfony\Component\Validator\Constraints as Assert; |
| 22 | +use Symfony\Component\Validator\Constraints\File; |
20 | 23 |
|
21 | 24 | class ApiAttributesTest extends AbstractWebTestCase |
22 | 25 | { |
@@ -346,6 +349,117 @@ public static function mapRequestPayloadProvider(): iterable |
346 | 349 | 'expectedStatusCode' => 422, |
347 | 350 | ]; |
348 | 351 | } |
| 352 | + |
| 353 | + public function testMapUploadedFileDefaults() |
| 354 | + { |
| 355 | + $client = self::createClient(['test_case' => 'ApiAttributesTest']); |
| 356 | + |
| 357 | + $client->request( |
| 358 | + 'POST', |
| 359 | + '/map-uploaded-file-defaults', |
| 360 | + [], |
| 361 | + [ |
| 362 | + 'file' => new UploadedFile(__DIR__.'/Fixtures/file-small.txt', 'file-small.txt', 'text/plain'), |
| 363 | + 'something-else' => new UploadedFile(__DIR__.'/Fixtures/file-big.txt', 'file-big.txt', 'text/plain'), |
| 364 | + ], |
| 365 | + ['HTTP_CONTENT_TYPE' => 'multipart/form-data'], |
| 366 | + ); |
| 367 | + $response = $client->getResponse(); |
| 368 | + |
| 369 | + self::assertStringEqualsFile(__DIR__.'/Fixtures/file-small.txt', $response->getContent()); |
| 370 | + } |
| 371 | + |
| 372 | + public function testMapUploadedFileCustomName() |
| 373 | + { |
| 374 | + $client = self::createClient(['test_case' => 'ApiAttributesTest']); |
| 375 | + |
| 376 | + $client->request( |
| 377 | + 'POST', |
| 378 | + '/map-uploaded-file-custom-name', |
| 379 | + [], |
| 380 | + [ |
| 381 | + 'foo' => new UploadedFile(__DIR__.'/Fixtures/file-small.txt', 'file-small.txt', 'text/plain'), |
| 382 | + 'something-else' => new UploadedFile(__DIR__.'/Fixtures/file-big.txt', 'file-big.txt', 'text/plain'), |
| 383 | + ], |
| 384 | + ['HTTP_CONTENT_TYPE' => 'multipart/form-data'], |
| 385 | + ); |
| 386 | + $response = $client->getResponse(); |
| 387 | + |
| 388 | + self::assertStringEqualsFile(__DIR__.'/Fixtures/file-small.txt', $response->getContent()); |
| 389 | + } |
| 390 | + |
| 391 | + public function testMapUploadedFileNullable() |
| 392 | + { |
| 393 | + $client = self::createClient(['test_case' => 'ApiAttributesTest']); |
| 394 | + $client->request( |
| 395 | + 'POST', |
| 396 | + '/map-uploaded-file-nullable', |
| 397 | + [], |
| 398 | + [], |
| 399 | + ['HTTP_CONTENT_TYPE' => 'multipart/form-data'], |
| 400 | + ); |
| 401 | + $response = $client->getResponse(); |
| 402 | + |
| 403 | + self::assertTrue($response->isSuccessful()); |
| 404 | + self::assertEmpty($response->getContent()); |
| 405 | + } |
| 406 | + |
| 407 | + public function testMapUploadedFileWithConstraints() |
| 408 | + { |
| 409 | + $client = self::createClient(['test_case' => 'ApiAttributesTest']); |
| 410 | + |
| 411 | + $client->request( |
| 412 | + 'POST', |
| 413 | + '/map-uploaded-file-with-constraints', |
| 414 | + [], |
| 415 | + ['file' => new UploadedFile(__DIR__.'/Fixtures/file-small.txt', 'file-small.txt', 'text/plain')], |
| 416 | + ['HTTP_CONTENT_TYPE' => 'multipart/form-data'], |
| 417 | + ); |
| 418 | + $response = $client->getResponse(); |
| 419 | + |
| 420 | + self::assertTrue($response->isSuccessful()); |
| 421 | + self::assertStringEqualsFile(__DIR__.'/Fixtures/file-small.txt', $response->getContent()); |
| 422 | + |
| 423 | + $filePath = __DIR__.'/Fixtures/file-big.txt'; |
| 424 | + $client->request( |
| 425 | + 'POST', |
| 426 | + '/map-uploaded-file-with-constraints', |
| 427 | + [], |
| 428 | + ['file' => new UploadedFile($filePath, 'file-big.txt', 'text/plain')], |
| 429 | + [ |
| 430 | + 'HTTP_ACCEPT' => 'application/json', |
| 431 | + 'HTTP_CONTENT_TYPE' => 'multipart/form-data', |
| 432 | + ], |
| 433 | + ); |
| 434 | + $response = $client->getResponse(); |
| 435 | + |
| 436 | + $content = <<<JSON |
| 437 | + { |
| 438 | + "type": "https://symfony.com/errors/validation", |
| 439 | + "title": "Validation Failed", |
| 440 | + "status": 400, |
| 441 | + "detail": "The file is too large (71 bytes). Allowed maximum size is 50 bytes.", |
| 442 | + "violations": [ |
| 443 | + { |
| 444 | + "propertyPath": "", |
| 445 | + "title": "The file is too large (71 bytes). Allowed maximum size is 50 bytes.", |
| 446 | + "template": "The file is too large ({{ size }} {{ suffix }}). Allowed maximum size is {{ limit }} {{ suffix }}.", |
| 447 | + "parameters": { |
| 448 | + "{{ file }}": "\"$filePath\"", |
| 449 | + "{{ size }}": "71", |
| 450 | + "{{ limit }}": "50", |
| 451 | + "{{ suffix }}": "bytes", |
| 452 | + "{{ name }}": "\"file-big.txt\"" |
| 453 | + }, |
| 454 | + "type": "urn:uuid:df8637af-d466-48c6-a59d-e7126250a654" |
| 455 | + } |
| 456 | + ] |
| 457 | + } |
| 458 | + JSON; |
| 459 | + |
| 460 | + self::assertSame(400, $response->getStatusCode()); |
| 461 | + self::assertJsonStringEqualsJsonString($content, $response->getContent()); |
| 462 | + } |
349 | 463 | } |
350 | 464 |
|
351 | 465 | class WithMapQueryStringController |
@@ -385,6 +499,29 @@ public function __invoke(#[MapRequestPayload] ?RequestBody $body, Request $reque |
385 | 499 | } |
386 | 500 | } |
387 | 501 |
|
| 502 | +class WithMapUploadedFileController |
| 503 | +{ |
| 504 | + public function defaults(#[MapUploadedFile] UploadedFile $file): Response |
| 505 | + { |
| 506 | + return new Response($file->getContent()); |
| 507 | + } |
| 508 | + |
| 509 | + public function customName(#[MapUploadedFile(name: 'foo')] UploadedFile $bar): Response |
| 510 | + { |
| 511 | + return new Response($bar->getContent()); |
| 512 | + } |
| 513 | + |
| 514 | + public function nullable(#[MapUploadedFile] ?UploadedFile $file): Response |
| 515 | + { |
| 516 | + return new Response($file?->getContent()); |
| 517 | + } |
| 518 | + |
| 519 | + public function withConstraints(#[MapUploadedFile(constraints: new File(maxSize: 50))] ?UploadedFile $file): Response |
| 520 | + { |
| 521 | + return new Response($file->getContent()); |
| 522 | + } |
| 523 | +} |
| 524 | + |
388 | 525 | class QueryString |
389 | 526 | { |
390 | 527 | public function __construct( |
|
0 commit comments