|
11 | 11 | use League\OpenAPIValidation\PSR7\Exception\Validation\InvalidHeaders; |
12 | 12 | use League\OpenAPIValidation\PSR7\ValidatorBuilder; |
13 | 13 | use PHPUnit\Framework\TestCase; |
| 14 | +use Psr\Http\Message\UploadedFileInterface; |
14 | 15 |
|
15 | 16 | use function filesize; |
16 | 17 | use function GuzzleHttp\Psr7\parse_request; |
@@ -259,6 +260,23 @@ public function dataProviderMultipartRed(): array |
259 | 260 | [file content goes there] |
260 | 261 | ------WebKitFormBoundaryWfPNVh4wuWBlyEyQ-- |
261 | 262 | HTTP |
| 263 | +, |
| 264 | + InvalidBody::class, |
| 265 | + ], |
| 266 | + // missing required part |
| 267 | + [ |
| 268 | + <<<HTTP |
| 269 | +POST /multipart/encoding HTTP/1.1 |
| 270 | +Content-Length: 428 |
| 271 | +Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryOmz20xyMCkE27rN7 |
| 272 | +
|
| 273 | +------WebKitFormBoundaryOmz20xyMCkE27rN7 |
| 274 | +Content-Disposition: form-data; name="description" |
| 275 | +Content-Type: text/plain |
| 276 | +
|
| 277 | +123 |
| 278 | +------WebKitFormBoundaryOmz20xyMCkE27rN7-- |
| 279 | +HTTP |
262 | 280 | , |
263 | 281 | InvalidBody::class, |
264 | 282 | ], |
@@ -404,25 +422,56 @@ public function testValidateMultipartRed(string $message, string $expectedExcept |
404 | 422 | $validator->validate($serverRequest); |
405 | 423 | } |
406 | 424 |
|
407 | | - public function testValidateMultipartServerRequestGreen(): void |
| 425 | + /** |
| 426 | + * @return mixed[][] |
| 427 | + */ |
| 428 | + public function dataProviderMultipartServerRequestGreen(): array |
408 | 429 | { |
409 | | - $specFile = __DIR__ . '/../../../stubs/multipart.yaml'; |
410 | | - |
411 | 430 | $imagePath = __DIR__ . '/../../../stubs/image.jpg'; |
412 | 431 | $imageSize = filesize($imagePath); |
413 | 432 |
|
414 | | - $serverRequest = (new ServerRequest('post', new Uri('/multipart'))) |
415 | | - ->withHeader('Content-Type', 'multipart/form-data') |
416 | | - ->withParsedBody([ |
417 | | - 'id' => 'bc8e1430-a963-11e9-a2a3-2a2ae2dbcce4', |
418 | | - 'address' => [ |
419 | | - 'street' => 'Some street', |
420 | | - 'city' => 'some city', |
| 433 | + return [ |
| 434 | + // Normal multipart message |
| 435 | + [ |
| 436 | + 'post', |
| 437 | + '/multipart', |
| 438 | + [ |
| 439 | + 'id' => 'bc8e1430-a963-11e9-a2a3-2a2ae2dbcce4', |
| 440 | + 'address' => [ |
| 441 | + 'street' => 'Some street', |
| 442 | + 'city' => 'some city', |
| 443 | + ], |
| 444 | + ], |
| 445 | + [ |
| 446 | + 'profileImage' => new UploadedFile($imagePath, $imageSize, 0), |
421 | 447 | ], |
422 | | - ]) |
423 | | - ->withUploadedFiles([ |
424 | | - 'profileImage' => new UploadedFile($imagePath, $imageSize, 0), |
425 | | - ]); |
| 448 | + ], |
| 449 | + // Missing optional field with defined encoding |
| 450 | + [ |
| 451 | + 'post', |
| 452 | + '/multipart/encoding', |
| 453 | + [], |
| 454 | + [ |
| 455 | + 'image' => new UploadedFile($imagePath, $imageSize, 0), |
| 456 | + ], |
| 457 | + ], |
| 458 | + ]; |
| 459 | + } |
| 460 | + |
| 461 | + /** |
| 462 | + * @param string[] $body |
| 463 | + * @param array<string, UploadedFileInterface> $files |
| 464 | + * |
| 465 | + * @dataProvider dataProviderMultipartServerRequestGreen |
| 466 | + */ |
| 467 | + public function testValidateMultipartServerRequestGreen(string $method, string $uri, array $body = [], array $files = []): void |
| 468 | + { |
| 469 | + $specFile = __DIR__ . '/../../../stubs/multipart.yaml'; |
| 470 | + |
| 471 | + $serverRequest = (new ServerRequest($method, new Uri($uri))) |
| 472 | + ->withHeader('Content-Type', 'multipart/form-data') |
| 473 | + ->withParsedBody($body) |
| 474 | + ->withUploadedFiles($files); |
426 | 475 |
|
427 | 476 | $validator = (new ValidatorBuilder())->fromYamlFile($specFile)->getServerRequestValidator(); |
428 | 477 | $validator->validate($serverRequest); |
|
0 commit comments