|
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;
|
20 | 22 |
|
21 | 23 | class ApiAttributesTest extends AbstractWebTestCase
|
@@ -346,6 +348,172 @@ public static function mapRequestPayloadProvider(): iterable
|
346 | 348 | 'expectedStatusCode' => 422,
|
347 | 349 | ];
|
348 | 350 | }
|
| 351 | + |
| 352 | + public function testMapUploadedFileDefaults() |
| 353 | + { |
| 354 | + $client = self::createClient(['test_case' => 'ApiAttributesTest']); |
| 355 | + |
| 356 | + $client->request( |
| 357 | + 'POST', |
| 358 | + '/map-uploaded-file-defaults', |
| 359 | + [], |
| 360 | + [ |
| 361 | + 'file' => new UploadedFile(__DIR__.'/Fixtures/file-small.txt', 'file-small.txt', 'text/plain'), |
| 362 | + 'something-else' => new UploadedFile(__DIR__.'/Fixtures/file-big.txt', 'file-big.txt', 'text/plain'), |
| 363 | + ], |
| 364 | + ['HTTP_CONTENT_TYPE' => 'multipart/form-data'], |
| 365 | + ); |
| 366 | + $response = $client->getResponse(); |
| 367 | + |
| 368 | + self::assertStringEqualsFile(__DIR__.'/Fixtures/file-small.txt', $response->getContent()); |
| 369 | + } |
| 370 | + |
| 371 | + public function testMapUploadedFileCustomName() |
| 372 | + { |
| 373 | + $client = self::createClient(['test_case' => 'ApiAttributesTest']); |
| 374 | + |
| 375 | + $client->request( |
| 376 | + 'POST', |
| 377 | + '/map-uploaded-file-custom-name', |
| 378 | + [], |
| 379 | + [ |
| 380 | + 'foo' => new UploadedFile(__DIR__.'/Fixtures/file-small.txt', 'file-small.txt', 'text/plain'), |
| 381 | + 'something-else' => new UploadedFile(__DIR__.'/Fixtures/file-big.txt', 'file-big.txt', 'text/plain'), |
| 382 | + ], |
| 383 | + ['HTTP_CONTENT_TYPE' => 'multipart/form-data'], |
| 384 | + ); |
| 385 | + $response = $client->getResponse(); |
| 386 | + |
| 387 | + self::assertStringEqualsFile(__DIR__.'/Fixtures/file-small.txt', $response->getContent()); |
| 388 | + } |
| 389 | + |
| 390 | + public function testMapUploadedFileNullable() |
| 391 | + { |
| 392 | + $client = self::createClient(['test_case' => 'ApiAttributesTest']); |
| 393 | + $client->request( |
| 394 | + 'POST', |
| 395 | + '/map-uploaded-file-nullable', |
| 396 | + [], |
| 397 | + [], |
| 398 | + ['HTTP_CONTENT_TYPE' => 'multipart/form-data'], |
| 399 | + ); |
| 400 | + $response = $client->getResponse(); |
| 401 | + |
| 402 | + self::assertTrue($response->isSuccessful()); |
| 403 | + self::assertEmpty($response->getContent()); |
| 404 | + } |
| 405 | + |
| 406 | + public function testMapUploadedFileWithConstraints() |
| 407 | + { |
| 408 | + $client = self::createClient(['test_case' => 'ApiAttributesTest']); |
| 409 | + |
| 410 | + $client->request( |
| 411 | + 'POST', |
| 412 | + '/map-uploaded-file-with-constraints', |
| 413 | + [], |
| 414 | + ['file' => new UploadedFile(__DIR__.'/Fixtures/file-small.txt', 'file-small.txt', 'text/plain')], |
| 415 | + ['HTTP_CONTENT_TYPE' => 'multipart/form-data'], |
| 416 | + ); |
| 417 | + $response = $client->getResponse(); |
| 418 | + |
| 419 | + self::assertTrue($response->isSuccessful()); |
| 420 | + self::assertStringEqualsFile(__DIR__.'/Fixtures/file-small.txt', $response->getContent()); |
| 421 | + |
| 422 | + $filePath = __DIR__.'/Fixtures/file-big.txt'; |
| 423 | + $client->request( |
| 424 | + 'POST', |
| 425 | + '/map-uploaded-file-with-constraints', |
| 426 | + [], |
| 427 | + ['file' => new UploadedFile($filePath, 'file-big.txt', 'text/plain')], |
| 428 | + [ |
| 429 | + 'HTTP_ACCEPT' => 'application/json', |
| 430 | + 'HTTP_CONTENT_TYPE' => 'multipart/form-data', |
| 431 | + ], |
| 432 | + ); |
| 433 | + $response = $client->getResponse(); |
| 434 | + |
| 435 | + $content = <<<JSON |
| 436 | + { |
| 437 | + "type": "https://symfony.com/errors/validation", |
| 438 | + "title": "Validation Failed", |
| 439 | + "status": 422, |
| 440 | + "detail": "The file is too large (71 bytes). Allowed maximum size is 50 bytes.", |
| 441 | + "violations": [ |
| 442 | + { |
| 443 | + "propertyPath": "", |
| 444 | + "title": "The file is too large (71 bytes). Allowed maximum size is 50 bytes.", |
| 445 | + "template": "The file is too large ({{ size }} {{ suffix }}). Allowed maximum size is {{ limit }} {{ suffix }}.", |
| 446 | + "parameters": { |
| 447 | + "{{ file }}": "\"$filePath\"", |
| 448 | + "{{ size }}": "71", |
| 449 | + "{{ limit }}": "50", |
| 450 | + "{{ suffix }}": "bytes", |
| 451 | + "{{ name }}": "\"file-big.txt\"" |
| 452 | + }, |
| 453 | + "type": "urn:uuid:df8637af-d466-48c6-a59d-e7126250a654" |
| 454 | + } |
| 455 | + ] |
| 456 | + } |
| 457 | + JSON; |
| 458 | + |
| 459 | + self::assertSame(422, $response->getStatusCode()); |
| 460 | + self::assertJsonStringEqualsJsonString($content, $response->getContent()); |
| 461 | + } |
| 462 | + |
| 463 | + public function testMapUploadedFileWithMultipleFilesArray() |
| 464 | + { |
| 465 | + $client = self::createClient(['test_case' => 'ApiAttributesTest']); |
| 466 | + |
| 467 | + $client->request( |
| 468 | + 'POST', |
| 469 | + '/map-uploaded-file-with-multiple-array', |
| 470 | + [], |
| 471 | + [ |
| 472 | + 'files' => [ |
| 473 | + new UploadedFile(__DIR__.'/Fixtures/file-small.txt', 'file-small.txt', 'text/plain'), |
| 474 | + new UploadedFile(__DIR__.'/Fixtures/file-big.txt', 'file-small.txt', 'text/plain'), |
| 475 | + ], |
| 476 | + ], |
| 477 | + ['HTTP_CONTENT_TYPE' => 'multipart/form-data'], |
| 478 | + ); |
| 479 | + $response = $client->getResponse(); |
| 480 | + |
| 481 | + self::assertTrue($response->isSuccessful()); |
| 482 | + self::assertJsonStringEqualsJsonString( |
| 483 | + json_encode([2, UploadedFile::class, UploadedFile::class], \JSON_THROW_ON_ERROR), |
| 484 | + $response->getContent() |
| 485 | + ); |
| 486 | + } |
| 487 | + |
| 488 | + public function testMapUploadedFileWithMultipleFilesVariadic() |
| 489 | + { |
| 490 | + $client = self::createClient(['test_case' => 'ApiAttributesTest']); |
| 491 | + |
| 492 | + $client->request( |
| 493 | + 'POST', |
| 494 | + '/map-uploaded-file-with-multiple-variadic', |
| 495 | + [], |
| 496 | + [ |
| 497 | + 'foo' => [ |
| 498 | + new UploadedFile(__DIR__.'/Fixtures/file-small.txt', 'first.txt', 'text/plain'), |
| 499 | + new UploadedFile(__DIR__.'/Fixtures/file-small.txt', 'second.txt', 'text/plain'), |
| 500 | + new UploadedFile(__DIR__.'/Fixtures/file-small.txt', 'third.txt', 'text/plain'), |
| 501 | + ], |
| 502 | + 'bar' => [ |
| 503 | + new UploadedFile(__DIR__.'/Fixtures/file-big.txt', 'big.txt', 'text/plain'), |
| 504 | + new UploadedFile(__DIR__.'/Fixtures/file-big.txt', 'huge.txt', 'text/plain'), |
| 505 | + ], |
| 506 | + ], |
| 507 | + ['HTTP_CONTENT_TYPE' => 'multipart/form-data'], |
| 508 | + ); |
| 509 | + $response = $client->getResponse(); |
| 510 | + |
| 511 | + self::assertTrue($response->isSuccessful()); |
| 512 | + self::assertJsonStringEqualsJsonString( |
| 513 | + json_encode([3, 'first.txt', 'second.txt', 'third.txt'], \JSON_THROW_ON_ERROR), |
| 514 | + $response->getContent() |
| 515 | + ); |
| 516 | + } |
349 | 517 | }
|
350 | 518 |
|
351 | 519 | class WithMapQueryStringController
|
@@ -385,6 +553,39 @@ public function __invoke(#[MapRequestPayload] ?RequestBody $body, Request $reque
|
385 | 553 | }
|
386 | 554 | }
|
387 | 555 |
|
| 556 | +class WithMapUploadedFileController |
| 557 | +{ |
| 558 | + public function defaults(#[MapUploadedFile] UploadedFile $file): Response |
| 559 | + { |
| 560 | + return new Response($file->getContent()); |
| 561 | + } |
| 562 | + |
| 563 | + public function customName(#[MapUploadedFile(name: 'foo')] UploadedFile $bar): Response |
| 564 | + { |
| 565 | + return new Response($bar->getContent()); |
| 566 | + } |
| 567 | + |
| 568 | + public function nullable(#[MapUploadedFile] ?UploadedFile $file): Response |
| 569 | + { |
| 570 | + return new Response($file?->getContent()); |
| 571 | + } |
| 572 | + |
| 573 | + public function withConstraints(#[MapUploadedFile(constraints: new Assert\File(maxSize: 50))] ?UploadedFile $file): Response |
| 574 | + { |
| 575 | + return new Response($file->getContent()); |
| 576 | + } |
| 577 | + |
| 578 | + public function withMultipleFilesArray(#[MapUploadedFile(constraints: new Assert\All([new Assert\File(maxSize: 100)]))] ?array $files): JsonResponse |
| 579 | + { |
| 580 | + return new JsonResponse([\count($files), \get_class($files[0]), \get_class($files[1])]); |
| 581 | + } |
| 582 | + |
| 583 | + public function withMultipleFilesVariadic(#[MapUploadedFile(constraints: new Assert\All([new Assert\File(maxSize: 100)]))] UploadedFile ...$foo): JsonResponse |
| 584 | + { |
| 585 | + return new JsonResponse([\count($foo), ...array_map(static fn($current) => $current->getClientOriginalName(), $foo)]); |
| 586 | + } |
| 587 | +} |
| 588 | + |
388 | 589 | class QueryString
|
389 | 590 | {
|
390 | 591 | public function __construct(
|
|
0 commit comments