22
33use cebe \openapi \Reader ;
44use cebe \openapi \SpecObjectInterface ;
5+ use cebe \openapi \Writer ;
56use 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