|
| 1 | +<?php |
| 2 | + |
| 3 | +use Astral\Serialize\OpenApi; |
| 4 | +use Astral\Serialize\Serialize; |
| 5 | + |
| 6 | +beforeAll(static function () { |
| 7 | + |
| 8 | + enum AnnotationOpenApiEnum |
| 9 | + { |
| 10 | + case ENUM_1; |
| 11 | + case ENUM_2; |
| 12 | + } |
| 13 | + |
| 14 | + |
| 15 | + class AnnotationOpenApiRequest extends Serialize |
| 16 | + { |
| 17 | + #[OpenApi\Annotations\OpenApi('description test enum')] |
| 18 | + public AnnotationOpenApiEnum $test_enum; |
| 19 | + |
| 20 | + #[OpenApi\Annotations\OpenApi(description:'this is object description', example:'1')] |
| 21 | + public object $test_object; |
| 22 | + |
| 23 | + #[OpenApi\Annotations\OpenApi(example:'abc')] |
| 24 | + public string $test_example; |
| 25 | + |
| 26 | + } |
| 27 | + |
| 28 | + #[\Astral\Serialize\OpenApi\Annotations\Tag('接口测试')] |
| 29 | + class AnnotationOpenApiController{ |
| 30 | + |
| 31 | + #[\Astral\Serialize\OpenApi\Annotations\Summary('测试方法一')] |
| 32 | + #[\Astral\Serialize\OpenApi\Annotations\Route('/test/description-action')] |
| 33 | + public function one(AnnotationOpenApiRequest $request): void |
| 34 | + { |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | +}); |
| 39 | + |
| 40 | +test('OpenAPI build description', function () { |
| 41 | + $api = new OpenApi(); |
| 42 | + $api->buildByClass(AnnotationOpenApiController::class); |
| 43 | + |
| 44 | + $openApi = $api::$OpenAPI; |
| 45 | + $paths = $openApi->paths; |
| 46 | + $post = $paths['/test/description-action']['post']; |
| 47 | + $requestBody = $post->requestBody; |
| 48 | + $schema = $requestBody['content']['application/json']['schema']; |
| 49 | + |
| 50 | + expect(array_keys($schema['properties']))->toMatchArray([ |
| 51 | + 'test_enum', |
| 52 | + 'test_object', |
| 53 | + 'test_example', |
| 54 | + ]); |
| 55 | + |
| 56 | + $enumProp = $schema['properties']['test_enum']; |
| 57 | + expect($enumProp['type'])->toBe('string') |
| 58 | + ->and($enumProp['description'])->toBe('description test enum optional values:ENUM_1、ENUM_2') |
| 59 | + ->and($enumProp['example'])->toBe(''); |
| 60 | + |
| 61 | + $objProp = $schema['properties']['test_object']; |
| 62 | + expect($objProp['type'])->toBe('object') |
| 63 | + ->and($objProp['description'])->toBe('this is object description') |
| 64 | + ->and($objProp['example'])->toBe('1'); |
| 65 | + |
| 66 | + $exProp = $schema['properties']['test_example']; |
| 67 | + expect($exProp['type'])->toBe('string') |
| 68 | + ->and($exProp['description'])->toBe('') |
| 69 | + ->and($exProp['example'])->toBe('abc') |
| 70 | + ->and($schema['required'])->toMatchArray([ |
| 71 | + 'test_enum', |
| 72 | + 'test_object', |
| 73 | + 'test_example', |
| 74 | + ]); |
| 75 | +}); |
0 commit comments