44use cebe \openapi \spec \OpenApi ;
55use cebe \openapi \spec \Operation ;
66use cebe \openapi \spec \PathItem ;
7+ use cebe \openapi \spec \Response ;
8+ use cebe \openapi \spec \Responses ;
79use cebe \openapi \spec \SecurityRequirement ;
810use cebe \openapi \spec \SecurityScheme ;
911
@@ -193,7 +195,7 @@ public function testWriteEmptySecurityPartYaml()
193195 );
194196 }
195197
196- public function testSecurity ()
198+ public function testSecurityAtPathOperationLevel ()
197199 {
198200 $ openapi = $ this ->createOpenAPI ([
199201 'components ' => new Components ([
@@ -209,8 +211,11 @@ public function testSecurity()
209211 '/test ' => new PathItem ([
210212 'get ' => new Operation ([
211213 'security ' => [
212- 'BearerAuth ' => new SecurityRequirement ([ ])
214+ new SecurityRequirement ([ 'BearerAuth ' => [] ])
213215 ],
216+ 'responses ' => new Responses ([
217+ 200 => new Response (['description ' => 'OK ' ]),
218+ ])
214219 ])
215220 ])
216221 ]
@@ -227,15 +232,61 @@ public function testSecurity()
227232paths:
228233 /test:
229234 get:
235+ responses:
236+ '200':
237+ description: OK
230238 security:
231- BearerAuth: { }
239+ -
240+ BearerAuth: []
232241components:
233242 securitySchemes:
234243 BearerAuth:
235244 type: http
236245 scheme: bearer
237246 bearerFormat: 'AuthToken and JWT Format'
238247
248+ YAML
249+ ),
250+ $ yaml
251+ );
252+ }
253+
254+ public function testSecurityAtGlobalLevel ()
255+ {
256+ $ openapi = $ this ->createOpenAPI ([
257+ 'components ' => new Components ([
258+ 'securitySchemes ' => [
259+ 'BearerAuth ' => new SecurityScheme ([
260+ 'type ' => 'http ' ,
261+ 'scheme ' => 'bearer ' ,
262+ 'bearerFormat ' => 'AuthToken and JWT Format ' # optional, arbitrary value for documentation purposes
263+ ])
264+ ],
265+ ]),
266+ 'security ' => [
267+ 'BearerAuth ' => new SecurityRequirement ([])
268+ ],
269+ 'paths ' => [],
270+ ]);
271+
272+ $ yaml = \cebe \openapi \Writer::writeToYaml ($ openapi );
273+
274+
275+ $ this ->assertEquals (preg_replace ('~\R~ ' , "\n" , <<<YAML
276+ openapi: 3.0.0
277+ info:
278+ title: 'Test API'
279+ version: 1.0.0
280+ paths: { }
281+ components:
282+ securitySchemes:
283+ BearerAuth:
284+ type: http
285+ scheme: bearer
286+ bearerFormat: 'AuthToken and JWT Format'
287+ security:
288+ BearerAuth: { }
289+
239290YAML
240291 ),
241292 $ yaml
0 commit comments