1+ <?php
2+
3+ use Astral \Serialize \OpenApi ;
4+ use Astral \Serialize \Serialize ;
5+
6+ beforeAll (static function () {
7+
8+ enum OpenapiEnum
9+ {
10+ case ENUM_1 ;
11+ case ENUM_2 ;
12+ }
13+
14+ enum OpenapiUnionEnum
15+ {
16+ case ENUM_2 ;
17+ case ENUM_3 ;
18+
19+ case ENUM_4 ;
20+
21+ }
22+
23+
24+
25+ class OpenapiEnumRequest extends Serialize
26+ {
27+ public OpenapiEnum $ test_enum ;
28+ public OpenapiEnum |OpenapiUnionEnum $ test_string_enum ;
29+
30+ public OpenapiEnum |OpenapiUnionEnum |string $ test_string_2_enum ;
31+
32+ public OpenapiEnum |OpenapiUnionEnum |string |int $ test_one_of_enum ;
33+
34+ }
35+
36+ #[\Astral \Serialize \OpenApi \Annotations \Tag('接口测试 ' )]
37+ class OpenapiEnumController{
38+
39+ #[\Astral \Serialize \OpenApi \Annotations \Summary('测试方法一 ' )]
40+ #[\Astral \Serialize \OpenApi \Annotations \Route('/test/enum-action ' )]
41+ public function one (OpenapiEnumRequest $ request ): void
42+ {
43+ }
44+
45+ }
46+
47+ });
48+
49+ test ('OpenAPI enums auto create description ' , function () {
50+
51+ $ api = new OpenApi ();
52+ $ api ->buildByClass (OpenapiEnumController::class);
53+
54+ $ openApi = $ api ::$ OpenAPI ;
55+
56+
57+ $ paths = $ openApi ->paths ;
58+ $ post = $ paths ['/test/enum-action ' ]['post ' ];
59+ $ requestBody = $ post ->requestBody ;
60+ $ schema = $ requestBody ['content ' ]['application/json ' ]['schema ' ];
61+
62+ expect ($ schema ['properties ' ])
63+ ->toHaveKeys ([
64+ 'test_enum ' ,
65+ 'test_string_enum ' ,
66+ 'test_string_2_enum ' ,
67+ 'test_one_of_enum ' ,
68+ ])
69+ ->and ($ schema ['properties ' ]['test_enum ' ])->toMatchArray ([
70+ 'type ' => 'string ' ,
71+ 'description ' => 'Optional values:ENUM_1、ENUM_2 ' ,
72+ 'example ' => '' ,
73+ ])
74+ ->and ($ schema ['properties ' ]['test_string_enum ' ])->toMatchArray ([
75+ 'type ' => 'string ' ,
76+ 'description ' => 'Optional values:ENUM_1、ENUM_2、ENUM_3、ENUM_4 ' ,
77+ 'example ' => '' ,
78+ ])
79+ ->and ($ schema ['properties ' ]['test_string_2_enum ' ])->toMatchArray ([
80+ 'type ' => 'string ' ,
81+ 'description ' => 'Optional values:ENUM_1、ENUM_2、ENUM_3、ENUM_4 ' ,
82+ 'example ' => '' ,
83+ ])
84+ ->and ($ schema ['properties ' ]['test_one_of_enum ' ])->toMatchArray ([
85+ 'type ' => 'oneOf ' ,
86+ 'description ' => 'Optional values:ENUM_1、ENUM_2、ENUM_3、ENUM_4 ' ,
87+ 'example ' => '' ,
88+ ])
89+ ->and ($ schema ['properties ' ]['test_one_of_enum ' ]['oneOf ' ])->toBeArray ()->toHaveCount (2 )
90+ ->and ($ schema ['properties ' ]['test_one_of_enum ' ]['oneOf ' ][0 ])->toMatchArray (['type ' => 'string ' ])
91+ ->and ($ schema ['properties ' ]['test_one_of_enum ' ]['oneOf ' ][1 ])->toMatchArray (['type ' => 'integer ' ])
92+ ->and ($ schema ['required ' ])->toBeArray ()->toEqualCanonicalizing ([
93+ 'test_enum ' ,
94+ 'test_string_enum ' ,
95+ 'test_string_2_enum ' ,
96+ 'test_one_of_enum ' ,
97+ ]);
98+
99+ });
0 commit comments