11<?php
22
3+ use cebe \openapi \spec \Components ;
4+ use cebe \openapi \spec \Operation ;
5+ use cebe \openapi \spec \PathItem ;
6+ use cebe \openapi \spec \Response ;
7+ use cebe \openapi \spec \Responses ;
38use cebe \openapi \spec \SecurityRequirement ;
9+ use cebe \openapi \spec \SecurityRequirements ;
10+ use cebe \openapi \spec \SecurityScheme ;
411
512class WriterTest extends \PHPUnit \Framework \TestCase
613{
@@ -137,7 +144,9 @@ public function testWriteEmptySecurityYaml()
137144 public function testWriteEmptySecurityPartJson ()
138145 {
139146 $ openapi = $ this ->createOpenAPI ([
140- 'security ' => [new SecurityRequirement (['Bearer ' => []])],
147+ 'security ' => new SecurityRequirements ([
148+ 'Bearer ' => new SecurityRequirement ([])
149+ ]),
141150 ]);
142151
143152 $ json = \cebe \openapi \Writer::writeToJson ($ openapi );
@@ -166,7 +175,9 @@ public function testWriteEmptySecurityPartJson()
166175 public function testWriteEmptySecurityPartYaml ()
167176 {
168177 $ openapi = $ this ->createOpenAPI ([
169- 'security ' => [new SecurityRequirement (['Bearer ' => []])],
178+ 'security ' => new SecurityRequirements ([
179+ 'Bearer ' => new SecurityRequirement ([])
180+ ]),
170181 ]);
171182
172183 $ yaml = \cebe \openapi \Writer::writeToYaml ($ openapi );
@@ -182,6 +193,105 @@ public function testWriteEmptySecurityPartYaml()
182193 -
183194 Bearer: []
184195
196+ YAML
197+ ),
198+ $ yaml
199+ );
200+ }
201+
202+ public function testSecurityAtPathOperationLevel ()
203+ {
204+ $ openapi = $ this ->createOpenAPI ([
205+ 'components ' => new Components ([
206+ 'securitySchemes ' => [
207+ 'BearerAuth ' => new SecurityScheme ([
208+ 'type ' => 'http ' ,
209+ 'scheme ' => 'bearer ' ,
210+ 'bearerFormat ' => 'AuthToken and JWT Format ' # optional, arbitrary value for documentation purposes
211+ ]),
212+ ],
213+ ]),
214+ 'paths ' => [
215+ '/test ' => new PathItem ([
216+ 'get ' => new Operation ([
217+ 'security ' => new SecurityRequirements ([
218+ 'BearerAuth ' => new SecurityRequirement ([]),
219+ ]),
220+ 'responses ' => new Responses ([
221+ 200 => new Response (['description ' => 'OK ' ]),
222+ ])
223+ ])
224+ ])
225+ ]
226+ ]);
227+
228+ $ yaml = \cebe \openapi \Writer::writeToYaml ($ openapi );
229+
230+
231+ $ this ->assertEquals (preg_replace ('~\R~ ' , "\n" , <<<YAML
232+ openapi: 3.0.0
233+ info:
234+ title: 'Test API'
235+ version: 1.0.0
236+ paths:
237+ /test:
238+ get:
239+ responses:
240+ '200':
241+ description: OK
242+ security:
243+ -
244+ BearerAuth: []
245+ components:
246+ securitySchemes:
247+ BearerAuth:
248+ type: http
249+ scheme: bearer
250+ bearerFormat: 'AuthToken and JWT Format'
251+
252+ YAML
253+ ),
254+ $ yaml
255+ );
256+ }
257+
258+ public function testSecurityAtGlobalLevel ()
259+ {
260+ $ openapi = $ this ->createOpenAPI ([
261+ 'components ' => new Components ([
262+ 'securitySchemes ' => [
263+ 'BearerAuth ' => new SecurityScheme ([
264+ 'type ' => 'http ' ,
265+ 'scheme ' => 'bearer ' ,
266+ 'bearerFormat ' => 'AuthToken and JWT Format ' # optional, arbitrary value for documentation purposes
267+ ])
268+ ],
269+ ]),
270+ 'security ' => new SecurityRequirements ([
271+ 'BearerAuth ' => new SecurityRequirement ([])
272+ ]),
273+ 'paths ' => [],
274+ ]);
275+
276+ $ yaml = \cebe \openapi \Writer::writeToYaml ($ openapi );
277+
278+
279+ $ this ->assertEquals (preg_replace ('~\R~ ' , "\n" , <<<YAML
280+ openapi: 3.0.0
281+ info:
282+ title: 'Test API'
283+ version: 1.0.0
284+ paths: { }
285+ components:
286+ securitySchemes:
287+ BearerAuth:
288+ type: http
289+ scheme: bearer
290+ bearerFormat: 'AuthToken and JWT Format'
291+ security:
292+ -
293+ BearerAuth: []
294+
185295YAML
186296 ),
187297 $ yaml
0 commit comments