Skip to content

Commit 19fabbc

Browse files
committed
Add more tests
1 parent 7b5f3da commit 19fabbc

File tree

3 files changed

+151
-5
lines changed

3 files changed

+151
-5
lines changed

tests/data/issue/242/spec2.json

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
{
2+
"openapi": "3.0.0",
3+
"info": {
4+
"title": "API Documentation",
5+
"description": "All API endpoints are presented here.",
6+
"version": "1.0.0"
7+
},
8+
"servers": [
9+
{
10+
"url": "http://127.0.0.1:8080/"
11+
}
12+
],
13+
"paths": {
14+
"/endpoint": {
15+
"get": {
16+
"responses": {
17+
"200": {
18+
"description": "OK"
19+
}
20+
},
21+
"security": [
22+
{
23+
"apiKey": [],
24+
"bearerAuth": []
25+
}
26+
]
27+
}
28+
}
29+
},
30+
"components": {
31+
"securitySchemes": {
32+
"apiKey": {
33+
"type": "apiKey",
34+
"in": "header",
35+
"name": "X-APi-Key"
36+
},
37+
"bearerAuth": {
38+
"type": "http",
39+
"scheme": "bearer",
40+
"bearerFormat": "JWT",
41+
"description": "JWT Authorization header using the Bearer scheme."
42+
}
43+
}
44+
}
45+
}

tests/data/issue/242/spec2.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# https://github.com/cebe/php-openapi/issues/242#issuecomment-2886431173
12
openapi: 3.0.0
23
info:
34
title: API Documentation

tests/issues/242/Issue242Test.php

Lines changed: 105 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
use cebe\openapi\Reader;
44
use cebe\openapi\SpecObjectInterface;
5+
use cebe\openapi\Writer;
56
use PHPUnit\Framework\TestCase;
67

78
// https://github.com/cebe/php-openapi/issues/242
@@ -26,10 +27,11 @@ private function removeCliFormatting($string)
2627
return preg_replace('/\e\[[0-9;]*m/', '', $string);
2728
}
2829

29-
public function test242Case2()
30+
public function test242Case2() # https://github.com/cebe/php-openapi/issues/242#issuecomment-2886431173
3031
{
3132
// read in yml
32-
$openapi = Reader::readFromYamlFile(dirname(__DIR__, 2) . '/data/issue/242/spec2.yml');
33+
$file = dirname(__DIR__, 2) . '/data/issue/242/spec2.yml';
34+
$openapi = Reader::readFromYamlFile($file);
3335
$this->assertInstanceOf(SpecObjectInterface::class, $openapi);
3436
$this->assertSame(json_decode(json_encode($openapi->paths['/endpoint']->get->security->getSerializableData()), true), [
3537
[
@@ -38,8 +40,106 @@ public function test242Case2()
3840
]
3941
]);
4042

41-
// write in yml # TODO
42-
// read in json # TODO
43-
// write in json # TODO
43+
# write back to yml
44+
$json = Writer::writeToYaml($openapi);
45+
$this->assertEquals(preg_replace('~\R~', "\n", <<<YAML
46+
openapi: 3.0.0
47+
info:
48+
title: 'API Documentation'
49+
description: 'All API endpoints are presented here.'
50+
version: 1.0.0
51+
servers:
52+
-
53+
url: 'http://127.0.0.1:8080/'
54+
paths:
55+
/endpoint:
56+
get:
57+
responses:
58+
'200':
59+
description: OK
60+
security:
61+
-
62+
apiKey: []
63+
bearerAuth: []
64+
components:
65+
securitySchemes:
66+
apiKey:
67+
type: apiKey
68+
name: X-APi-Key
69+
in: header
70+
bearerAuth:
71+
type: http
72+
description: 'JWT Authorization header using the Bearer scheme.'
73+
scheme: bearer
74+
bearerFormat: JWT
75+
76+
YAML
77+
),
78+
$json
79+
);
80+
81+
// read in json
82+
$file = dirname(__DIR__, 2) . '/data/issue/242/spec2.json';
83+
$openapi = Reader::readFromJsonFile($file);
84+
$this->assertInstanceOf(SpecObjectInterface::class, $openapi);
85+
$this->assertSame(json_decode(json_encode($openapi->paths['/endpoint']->get->security->getSerializableData()), true), [
86+
[
87+
'apiKey' => [],
88+
'bearerAuth' => []
89+
]
90+
]);
91+
92+
// write back in json
93+
$json = Writer::writeToJson($openapi);
94+
$this->assertEquals(preg_replace('~\R~', "\n", <<<JSON
95+
{
96+
"openapi": "3.0.0",
97+
"info": {
98+
"title": "API Documentation",
99+
"description": "All API endpoints are presented here.",
100+
"version": "1.0.0"
101+
},
102+
"servers": [
103+
{
104+
"url": "http:\/\/127.0.0.1:8080\/"
105+
}
106+
],
107+
"paths": {
108+
"\/endpoint": {
109+
"get": {
110+
"responses": {
111+
"200": {
112+
"description": "OK"
113+
}
114+
},
115+
"security": [
116+
{
117+
"apiKey": [],
118+
"bearerAuth": []
119+
}
120+
]
121+
}
122+
}
123+
},
124+
"components": {
125+
"securitySchemes": {
126+
"apiKey": {
127+
"type": "apiKey",
128+
"name": "X-APi-Key",
129+
"in": "header"
130+
},
131+
"bearerAuth": {
132+
"type": "http",
133+
"description": "JWT Authorization header using the Bearer scheme.",
134+
"scheme": "bearer",
135+
"bearerFormat": "JWT"
136+
}
137+
}
138+
}
139+
}
140+
JSON
141+
),
142+
$json
143+
);
44144
}
45145
}

0 commit comments

Comments
 (0)