Skip to content

Commit 3004221

Browse files
committed
Add more tests
1 parent c9736d8 commit 3004221

File tree

2 files changed

+176
-18
lines changed

2 files changed

+176
-18
lines changed

tests/WriterTest.php

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use cebe\openapi\spec\SecurityRequirement;
99
use cebe\openapi\spec\SecurityRequirements;
1010
use cebe\openapi\spec\SecurityScheme;
11+
use cebe\openapi\Writer;
1112

1213
class WriterTest extends \PHPUnit\Framework\TestCase
1314
{
@@ -27,7 +28,7 @@ public function testWriteJson()
2728
{
2829
$openapi = $this->createOpenAPI();
2930

30-
$json = \cebe\openapi\Writer::writeToJson($openapi);
31+
$json = Writer::writeToJson($openapi);
3132

3233
$this->assertEquals(preg_replace('~\R~', "\n", <<<JSON
3334
{
@@ -52,7 +53,7 @@ public function testWriteJsonMofify()
5253
'description' => 'something'
5354
]);
5455

55-
$json = \cebe\openapi\Writer::writeToJson($openapi);
56+
$json = Writer::writeToJson($openapi);
5657

5758
$this->assertEquals(preg_replace('~\R~', "\n", <<<JSON
5859
{
@@ -77,7 +78,7 @@ public function testWriteYaml()
7778
{
7879
$openapi = $this->createOpenAPI();
7980

80-
$yaml = \cebe\openapi\Writer::writeToYaml($openapi);
81+
$yaml = Writer::writeToYaml($openapi);
8182

8283

8384
$this->assertEquals(preg_replace('~\R~', "\n", <<<YAML
@@ -99,7 +100,7 @@ public function testWriteEmptySecurityJson()
99100
'security' => [],
100101
]);
101102

102-
$json = \cebe\openapi\Writer::writeToJson($openapi);
103+
$json = Writer::writeToJson($openapi);
103104

104105
$this->assertEquals(preg_replace('~\R~', "\n", <<<JSON
105106
{
@@ -124,7 +125,7 @@ public function testWriteEmptySecurityYaml()
124125
'security' => [],
125126
]);
126127

127-
$yaml = \cebe\openapi\Writer::writeToYaml($openapi);
128+
$yaml = Writer::writeToYaml($openapi);
128129

129130

130131
$this->assertEquals(preg_replace('~\R~', "\n", <<<YAML
@@ -149,7 +150,7 @@ public function testWriteEmptySecurityPartJson()
149150
]),
150151
]);
151152

152-
$json = \cebe\openapi\Writer::writeToJson($openapi);
153+
$json = Writer::writeToJson($openapi);
153154

154155
$this->assertEquals(preg_replace('~\R~', "\n", <<<JSON
155156
{
@@ -180,7 +181,7 @@ public function testWriteEmptySecurityPartYaml()
180181
]),
181182
]);
182183

183-
$yaml = \cebe\openapi\Writer::writeToYaml($openapi);
184+
$yaml = Writer::writeToYaml($openapi);
184185

185186

186187
$this->assertEquals(preg_replace('~\R~', "\n", <<<YAML
@@ -225,7 +226,7 @@ public function testSecurityAtPathOperationLevel()
225226
]
226227
]);
227228

228-
$yaml = \cebe\openapi\Writer::writeToYaml($openapi);
229+
$yaml = Writer::writeToYaml($openapi);
229230

230231

231232
$this->assertEquals(preg_replace('~\R~', "\n", <<<YAML
@@ -272,11 +273,30 @@ public function testSecurityAtGlobalLevel()
272273
]),
273274
'paths' => [],
274275
]);
276+
$yaml = Writer::writeToYaml($openapi);
275277

276-
$yaml = \cebe\openapi\Writer::writeToYaml($openapi);
278+
// case 2
279+
$openapi2 = $this->createOpenAPI([
280+
'components' => new Components([
281+
'securitySchemes' => [
282+
'BearerAuth' => new SecurityScheme([
283+
'type' => 'http',
284+
'scheme' => 'bearer',
285+
'bearerFormat' => 'AuthToken and JWT Format' # optional, arbitrary value for documentation purposes
286+
])
287+
],
288+
]),
289+
'security' => new SecurityRequirements([
290+
[
291+
'BearerAuth' => new SecurityRequirement([])
292+
]
293+
]),
294+
'paths' => [],
295+
]);
296+
$yaml2 = Writer::writeToYaml($openapi2);
277297

278298

279-
$this->assertEquals(preg_replace('~\R~', "\n", <<<YAML
299+
$expected = <<<YAML
280300
openapi: 3.0.0
281301
info:
282302
title: 'Test API'
@@ -292,9 +312,9 @@ public function testSecurityAtGlobalLevel()
292312
-
293313
BearerAuth: []
294314
295-
YAML
296-
),
297-
$yaml
298-
);
315+
YAML;
316+
317+
$this->assertEquals(preg_replace('~\R~', "\n", $expected), $yaml);
318+
$this->assertEquals(preg_replace('~\R~', "\n", $expected), $yaml2);
299319
}
300320
}

tests/issues/242/Issue242Test.php

Lines changed: 142 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
<?php
22

33
use cebe\openapi\Reader;
4+
use cebe\openapi\spec\Components;
5+
use cebe\openapi\spec\SecurityRequirement;
6+
use cebe\openapi\spec\SecurityRequirements;
7+
use cebe\openapi\spec\SecurityScheme;
48
use cebe\openapi\SpecObjectInterface;
59
use cebe\openapi\Writer;
610
use PHPUnit\Framework\TestCase;
@@ -41,7 +45,7 @@ public function test242Case2() # https://github.com/cebe/php-openapi/issues/242#
4145
]);
4246

4347
# write back to yml
44-
$json = Writer::writeToYaml($openapi);
48+
$yaml = Writer::writeToYaml($openapi);
4549
$this->assertEquals(preg_replace('~\R~', "\n", <<<YAML
4650
openapi: 3.0.0
4751
info:
@@ -75,7 +79,7 @@ public function test242Case2() # https://github.com/cebe/php-openapi/issues/242#
7579
7680
YAML
7781
),
78-
$json
82+
$yaml
7983
);
8084

8185
// read in json
@@ -90,7 +94,7 @@ public function test242Case2() # https://github.com/cebe/php-openapi/issues/242#
9094
]);
9195

9296
// write back in json
93-
$json = Writer::writeToJson($openapi);
97+
$yaml = Writer::writeToJson($openapi);
9498
$this->assertEquals(preg_replace('~\R~', "\n", <<<JSON
9599
{
96100
"openapi": "3.0.0",
@@ -139,7 +143,141 @@ public function test242Case2() # https://github.com/cebe/php-openapi/issues/242#
139143
}
140144
JSON
141145
),
142-
$json
146+
$yaml
143147
);
144148
}
149+
150+
public function test242Case3MultipleAuth()
151+
{
152+
$file = dirname(__DIR__, 2) . '/data/issue/242/multiple_auth.yml';
153+
$openapi = Reader::readFromYamlFile($file);
154+
$this->assertInstanceOf(SpecObjectInterface::class, $openapi);
155+
$act = json_decode(json_encode($openapi->security->getSerializableData()), true);
156+
$this->assertSame([], $act[0]['BasicAuth']);
157+
158+
# write back to yml
159+
$yaml = Writer::writeToYaml($openapi);
160+
$this->assertEquals(preg_replace('~\R~', "\n", <<<YAML
161+
openapi: 3.0.0
162+
info:
163+
title: 'Multiple auth'
164+
version: 1.0.0
165+
paths: { }
166+
components:
167+
securitySchemes:
168+
BasicAuth:
169+
type: http
170+
scheme: basic
171+
BearerAuth:
172+
type: http
173+
scheme: bearer
174+
ApiKeyAuth:
175+
type: apiKey
176+
name: X-API-Key
177+
in: header
178+
OpenID:
179+
type: openIdConnect
180+
openIdConnectUrl: 'https://example.com/.well-known/openid-configuration'
181+
OAuth2:
182+
type: oauth2
183+
flows:
184+
authorizationCode:
185+
authorizationUrl: 'https://example.com/oauth/authorize'
186+
tokenUrl: 'https://example.com/oauth/token'
187+
scopes:
188+
read: 'Grants read access'
189+
write: 'Grants write access'
190+
admin: 'Grants access to admin operations'
191+
security:
192+
-
193+
BasicAuth: []
194+
BearerAuth: []
195+
-
196+
ApiKeyAuth: []
197+
OAuth2:
198+
- read
199+
200+
YAML
201+
),
202+
$yaml
203+
);
204+
}
205+
206+
public function test242Case4WriteMultipleAuth()
207+
{
208+
$openapi = $this->createOpenAPI([
209+
'components' => new Components([
210+
'securitySchemes' => [
211+
'BearerAuth' => new SecurityScheme([
212+
'type' => 'http',
213+
'scheme' => 'bearer',
214+
]),
215+
'BasicAuth' => new SecurityScheme([
216+
'type' => 'http',
217+
'scheme' => 'basic',
218+
]),
219+
'ApiKeyAuth' => new SecurityScheme([
220+
'type' => 'apiKey',
221+
'name' => 'X-API-Key',
222+
'in' => 'header'
223+
])
224+
],
225+
]),
226+
'security' => new SecurityRequirements([
227+
[
228+
'BearerAuth' => new SecurityRequirement([]),
229+
'BasicAuth' => new SecurityRequirement([])
230+
],
231+
[
232+
'ApiKeyAuth' => new SecurityRequirement([])
233+
]
234+
]),
235+
'paths' => [],
236+
]);
237+
238+
239+
$yaml = \cebe\openapi\Writer::writeToYaml($openapi);
240+
241+
$this->assertEquals(preg_replace('~\R~', "\n", <<<YAML
242+
openapi: 3.0.0
243+
info:
244+
title: 'Test API'
245+
version: 1.0.0
246+
paths: { }
247+
components:
248+
securitySchemes:
249+
BearerAuth:
250+
type: http
251+
scheme: bearer
252+
BasicAuth:
253+
type: http
254+
scheme: basic
255+
ApiKeyAuth:
256+
type: apiKey
257+
name: X-API-Key
258+
in: header
259+
security:
260+
-
261+
BearerAuth: []
262+
BasicAuth: []
263+
-
264+
ApiKeyAuth: []
265+
266+
YAML
267+
),
268+
$yaml
269+
);
270+
}
271+
272+
private function createOpenAPI($merge = [])
273+
{
274+
return new \cebe\openapi\spec\OpenApi(array_merge([
275+
'openapi' => '3.0.0',
276+
'info' => [
277+
'title' => 'Test API',
278+
'version' => '1.0.0',
279+
],
280+
'paths' => [],
281+
], $merge));
282+
}
145283
}

0 commit comments

Comments
 (0)