|
11 | 11 |
|
12 | 12 | namespaceSymfony\Bundle\FrameworkBundle\Tests\Functional; |
13 | 13 |
|
| 14 | +useSymfony\Component\HttpFoundation\File\UploadedFile; |
14 | 15 | useSymfony\Component\HttpFoundation\JsonResponse; |
15 | 16 | useSymfony\Component\HttpFoundation\Request; |
16 | 17 | useSymfony\Component\HttpFoundation\Response; |
17 | 18 | useSymfony\Component\HttpKernel\Attribute\MapQueryString; |
18 | 19 | useSymfony\Component\HttpKernel\Attribute\MapRequestPayload; |
| 20 | +useSymfony\Component\HttpKernel\Attribute\MapUploadedFile; |
19 | 21 | useSymfony\Component\Validator\ConstraintsasAssert; |
20 | 22 |
|
21 | 23 | class ApiAttributesTestextends AbstractWebTestCase |
@@ -346,6 +348,172 @@ public static function mapRequestPayloadProvider(): iterable |
346 | 348 | 'expectedStatusCode' =>422, |
347 | 349 | ]; |
348 | 350 | } |
| 351 | + |
| 352 | +publicfunctiontestMapUploadedFileDefaults() |
| 353 | + { |
| 354 | +$client =self::createClient(['test_case' =>'ApiAttributesTest']); |
| 355 | + |
| 356 | +$client->request( |
| 357 | +'POST', |
| 358 | +'/map-uploaded-file-defaults', |
| 359 | + [], |
| 360 | + [ |
| 361 | +'file' =>newUploadedFile(__DIR__.'/Fixtures/file-small.txt','file-small.txt','text/plain'), |
| 362 | +'something-else' =>newUploadedFile(__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 | +publicfunctiontestMapUploadedFileCustomName() |
| 372 | + { |
| 373 | +$client =self::createClient(['test_case' =>'ApiAttributesTest']); |
| 374 | + |
| 375 | +$client->request( |
| 376 | +'POST', |
| 377 | +'/map-uploaded-file-custom-name', |
| 378 | + [], |
| 379 | + [ |
| 380 | +'foo' =>newUploadedFile(__DIR__.'/Fixtures/file-small.txt','file-small.txt','text/plain'), |
| 381 | +'something-else' =>newUploadedFile(__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 | +publicfunctiontestMapUploadedFileNullable() |
| 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 | +publicfunctiontestMapUploadedFileWithConstraints() |
| 407 | + { |
| 408 | +$client =self::createClient(['test_case' =>'ApiAttributesTest']); |
| 409 | + |
| 410 | +$client->request( |
| 411 | +'POST', |
| 412 | +'/map-uploaded-file-with-constraints', |
| 413 | + [], |
| 414 | + ['file' =>newUploadedFile(__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' =>newUploadedFile($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 | +publicfunctiontestMapUploadedFileWithMultipleFilesArray() |
| 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 | +newUploadedFile(__DIR__.'/Fixtures/file-small.txt','file-small.txt','text/plain'), |
| 474 | +newUploadedFile(__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 | +publicfunctiontestMapUploadedFileWithMultipleFilesVariadic() |
| 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 | +newUploadedFile(__DIR__.'/Fixtures/file-small.txt','first.txt','text/plain'), |
| 499 | +newUploadedFile(__DIR__.'/Fixtures/file-small.txt','second.txt','text/plain'), |
| 500 | +newUploadedFile(__DIR__.'/Fixtures/file-small.txt','third.txt','text/plain'), |
| 501 | + ], |
| 502 | +'bar' => [ |
| 503 | +newUploadedFile(__DIR__.'/Fixtures/file-big.txt','big.txt','text/plain'), |
| 504 | +newUploadedFile(__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 | +publicfunctiondefaults(#[MapUploadedFile]UploadedFile$file):Response |
| 559 | + { |
| 560 | +returnnewResponse($file->getContent()); |
| 561 | + } |
| 562 | + |
| 563 | +publicfunctioncustomName(#[MapUploadedFile(name:'foo')]UploadedFile$bar):Response |
| 564 | + { |
| 565 | +returnnewResponse($bar->getContent()); |
| 566 | + } |
| 567 | + |
| 568 | +publicfunctionnullable(#[MapUploadedFile] ?UploadedFile$file):Response |
| 569 | + { |
| 570 | +returnnewResponse($file?->getContent()); |
| 571 | + } |
| 572 | + |
| 573 | +publicfunctionwithConstraints(#[MapUploadedFile(constraints:newAssert\File(maxSize:50))] ?UploadedFile$file):Response |
| 574 | + { |
| 575 | +returnnewResponse($file->getContent()); |
| 576 | + } |
| 577 | + |
| 578 | +publicfunctionwithMultipleFilesArray(#[MapUploadedFile(constraints:newAssert\All([newAssert\File(maxSize:100)]))] ?array$files):JsonResponse |
| 579 | + { |
| 580 | +returnnewJsonResponse([\count($files),\get_class($files[0]),\get_class($files[1])]); |
| 581 | + } |
| 582 | + |
| 583 | +publicfunctionwithMultipleFilesVariadic(#[MapUploadedFile(constraints:newAssert\All([newAssert\File(maxSize:100)]))]UploadedFile ...$foo):JsonResponse |
| 584 | + { |
| 585 | +returnnewJsonResponse([\count($foo), ...array_map(staticfn($current) =>$current->getClientOriginalName(),$foo)]); |
| 586 | + } |
| 587 | +} |
| 588 | + |
388 | 589 | class QueryString |
389 | 590 | { |
390 | 591 | publicfunction__construct( |
|