forked from cebe/php-openapi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpenApiTest.php
More file actions
283 lines (241 loc) · 13 KB
/
OpenApiTest.php
File metadata and controls
283 lines (241 loc) · 13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
<?php
use cebe\openapi\spec\OpenApi;
use cebe\openapi\Reader;
use Symfony\Component\Yaml\Yaml;
/**
* @covers \cebe\openapi\spec\OpenApi
*/
class OpenApiTest extends \PHPUnit\Framework\TestCase
{
public function testEmpty()
{
$openapi = new OpenApi([]);
$this->assertFalse($openapi->validate());
$this->assertEquals([
'OpenApi is missing required property: openapi',
'OpenApi is missing required property: info',
'OpenApi is missing required property: paths',
], $openapi->getErrors());
// check default value of servers
// https://github.com/OAI/OpenAPI-Specification/blob/3.0.2/versions/3.0.2.md#openapiObject
// If the servers property is not provided, or is an empty array, the default value would be a Server Object with a url value of /.
$this->assertCount(1, $openapi->servers);
$this->assertEquals('/', $openapi->servers[0]->url);
}
public function testReadPetStore()
{
$openApiFile = __DIR__ . '/../../vendor/oai/openapi-specification-3.0/examples/v3.0/petstore.yaml';
$yaml = Yaml::parse(file_get_contents($openApiFile));
$openapi = new OpenApi($yaml);
$result = $openapi->validate();
$this->assertEquals([], $openapi->getErrors());
$this->assertTrue($result);
// openapi
$this->assertEquals('3.0.0', $openapi->openapi);
// info
$this->assertInstanceOf(\cebe\openapi\spec\Info::class, $openapi->info);
$this->assertEquals('1.0.0', $openapi->info->version);
$this->assertEquals('Swagger Petstore', $openapi->info->title);
// info.license
$this->assertInstanceOf(\cebe\openapi\spec\License::class, $openapi->info->license);
$this->assertEquals('MIT', $openapi->info->license->name);
// info.contact
$this->assertNull($openapi->info->contact);
// servers
if (method_exists($this, 'assertIsArray')) {
$this->assertIsArray($openapi->servers);
} else {
$this->assertInternalType('array', $openapi->servers);
}
$this->assertCount(1, $openapi->servers);
foreach ($openapi->servers as $server) {
$this->assertInstanceOf(\cebe\openapi\spec\Server::class, $server);
$this->assertEquals('http://petstore.swagger.io/v1', $server->url);
}
// paths
$this->assertInstanceOf(\cebe\openapi\spec\Paths::class, $openapi->paths);
// components
$this->assertInstanceOf(\cebe\openapi\spec\Components::class, $openapi->components);
// security
$this->assertAllInstanceOf(\cebe\openapi\spec\SecurityRequirement::class, $openapi->security);
// tags
$this->assertAllInstanceOf(\cebe\openapi\spec\Tag::class, $openapi->tags);
// externalDocs
$this->assertNull($openapi->externalDocs);
}
public function assertAllInstanceOf($className, $array)
{
foreach($array as $k => $v) {
$this->assertInstanceOf($className, $v, "Asserting that item with key '$k' is instance of $className");
}
}
public function specProvider()
{
// examples from https://github.com/OAI/OpenAPI-Specification/tree/master/examples/v3.0
$oaiExamples = [
// TODO symfony/yaml can not read this file!?
// __DIR__ . '/../../vendor/oai/openapi-specification-3.0/examples/v3.0/api-with-examples.yaml',
__DIR__ . '/../../vendor/oai/openapi-specification-3.0/examples/v3.0/callback-example.yaml',
__DIR__ . '/../../vendor/oai/openapi-specification-3.0/examples/v3.0/link-example.yaml',
__DIR__ . '/../../vendor/oai/openapi-specification-3.0/examples/v3.0/petstore.yaml',
__DIR__ . '/../../vendor/oai/openapi-specification-3.0/examples/v3.0/petstore-expanded.yaml',
__DIR__ . '/../../vendor/oai/openapi-specification-3.0/examples/v3.0/uspto.yaml',
];
// examples from https://github.com/Mermade/openapi3-examples
$mermadeExamples = [
__DIR__ . '/../../vendor/mermade/openapi3-examples/3.0/pass/externalPathItemRef.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/3.0/pass/deprecated.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/3.0/pass/swagger2openapi/openapi.json',
__DIR__ . '/../../vendor/mermade/openapi3-examples/3.0/pass/gluecon/example1_from_._Different_parameters.md.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/3.0/pass/gluecon/example1_from_._Fixed_file.md.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/3.0/pass/gluecon/example1_from_._Fixed_multipart.md.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/3.0/pass/gluecon/example1_from_._Improved_examples.md.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/3.0/pass/gluecon/example1_from_._Improved_pathdescriptions.md.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/3.0/pass/gluecon/example1_from_._Improved_securityschemes.md.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/3.0/pass/gluecon/example1_from_._Improved_serverseverywhere.md.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/3.0/pass/gluecon/example1_from_._New_callbacks.md.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/3.0/pass/gluecon/example1_from_._New_links.md.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/3.0/pass/gluecon/example2_from_._Different_parameters.md.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/3.0/pass/gluecon/example2_from_._Different_requestbody.md.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/3.0/pass/gluecon/example2_from_._Different_servers.md.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/3.0/pass/gluecon/example2_from_._Fixed_multipart.md.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/3.0/pass/gluecon/example2_from_._Improved_securityschemes.md.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/3.0/pass/gluecon/example2_from_._New_callbacks.md.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/3.0/pass/gluecon/example2_from_._New_links.md.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/3.0/pass/gluecon/example3_from_._Different_parameters.md.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/3.0/pass/gluecon/example3_from_._Different_servers.md.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/3.0/pass/gluecon/example4_from_._Different_parameters.md.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/3.0/pass/gluecon/example5_from_._Different_parameters.md.yaml',
// TODO symfony/yaml can not read this file!?
// __DIR__ . '/../../vendor/mermade/openapi3-examples/3.0/pass/OAI/api-with-examples.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/3.0/pass/OAI/petstore-expanded.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/3.0/pass/OAI/petstore.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/3.0/pass/OAI/uber.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/3.0/malicious/rapid7-html.json',
__DIR__ . '/../../vendor/mermade/openapi3-examples/3.0/malicious/rapid7-java.json',
__DIR__ . '/../../vendor/mermade/openapi3-examples/3.0/malicious/rapid7-js.json',
__DIR__ . '/../../vendor/mermade/openapi3-examples/3.0/malicious/rapid7-php.json',
__DIR__ . '/../../vendor/mermade/openapi3-examples/3.0/malicious/rapid7-ruby.json',
// __DIR__ . '/../../vendor/mermade/openapi3-examples/3.0/malicious/yamlbomb.yaml',
// OpenAPI 3.1 examples
__DIR__ . '/../../vendor/mermade/openapi3-examples/3.1/pass/minimal_comp.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/3.1/pass/minimal_hooks.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/3.1/pass/minimal_paths.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/3.1/pass/path_var_empty_pathitem.yaml',
];
// examples from https://github.com/APIs-guru/openapi-directory/tree/openapi3.0.0/APIs
$apisGuruExamples = [];
/** @var $it RecursiveDirectoryIterator|RecursiveIteratorIterator */
$it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator(__DIR__ . '/../../vendor/apis-guru/openapi-directory/APIs'));
$it->rewind();
while($it->valid()) {
if ($it->getBasename() === 'openapi.yaml') {
$apisGuruExamples[] = $it->key();
}
$it->next();
}
// examples from https://github.com/Nexmo/api-specification/tree/master/definitions
$nexmoExamples = [];
/** @var $it RecursiveDirectoryIterator|RecursiveIteratorIterator */
$it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator(__DIR__ . '/../../vendor/nexmo/api-specification/definitions'));
$it->rewind();
while($it->valid()) {
if ($it->getExtension() === 'yml'
&& strpos($it->getSubPath(), 'common') === false
&& $it->getBasename() !== 'voice.v2.yml' // contains invalid references
) {
$nexmoExamples[] = $it->key();
}
$it->next();
}
$all = array_merge(
$oaiExamples,
$mermadeExamples,
$apisGuruExamples,
$nexmoExamples
);
foreach($all as $path) {
$pathWithoutVendorPrefix = substr($path, strlen(__DIR__ . '/../../vendor/'));
yield $pathWithoutVendorPrefix => [
$pathWithoutVendorPrefix
];
}
}
/**
* @dataProvider specProvider
*/
public function testSpecs($openApiFile)
{
// skip test on symfony/yaml 5.0 due to bug https://github.com/symfony/symfony/issues/34805
if ($openApiFile === 'oai/openapi-specification/examples/v3.0/uspto.yaml') {
$installed = json_decode(file_get_contents(__DIR__ . '/../../vendor/composer/installed.json'), true);
// Check for composer 2.0 structure
if (array_key_exists('packages', $installed)) {
$installed = $installed['packages'];
}
foreach ($installed as $pkg) {
if ($pkg['name'] === 'symfony/yaml' && version_compare($pkg['version'], 'v4.4', '>=')) {
$this->markTestSkipped(
'This test is incompatible with symfony/yaml 4.4 and 5.0, see symfony bug https://github.com/symfony/symfony/issues/34805'
);
}
}
}
if (strtolower(substr($openApiFile, -5, 5)) === '.json') {
$json = json_decode(file_get_contents(__DIR__ . '/../../vendor/' . $openApiFile), true);
$openapi = new OpenApi($json);
} else {
$yaml = Yaml::parse(file_get_contents(__DIR__ . '/../../vendor/' . $openApiFile));
$openapi = new OpenApi($yaml);
}
$openapi->setDocumentContext($openapi, new \cebe\openapi\json\JsonPointer(''));
$result = $openapi->validate();
$this->assertEquals([], $openapi->getErrors(), print_r($openapi->getErrors(), true));
$this->assertTrue($result);
// openapi
$this->assertNotSame(OpenApi::VERSION_UNSUPPORTED, $openapi->getMajorVersion());
// info
$this->assertInstanceOf(\cebe\openapi\spec\Info::class, $openapi->info);
// servers
$this->assertAllInstanceOf(\cebe\openapi\spec\Server::class, $openapi->servers);
// paths
if ($openapi->components !== null) {
$this->assertInstanceOf(\cebe\openapi\spec\Paths::class, $openapi->paths);
}
// components
if ($openapi->components !== null) {
$this->assertInstanceOf(\cebe\openapi\spec\Components::class, $openapi->components);
}
// security
$this->assertAllInstanceOf(\cebe\openapi\spec\SecurityRequirement::class, $openapi->security);
// tags
$this->assertAllInstanceOf(\cebe\openapi\spec\Tag::class, $openapi->tags);
// externalDocs
if ($openapi->externalDocs !== null) {
$this->assertInstanceOf(\cebe\openapi\spec\ExternalDocumentation::class, $openapi->externalDocs);
}
}
public function testVersions()
{
$yaml = <<<YAML
openapi: 3.0.2
info:
title: Test API
version: 1
paths: []
YAML;
$openapi = Reader::readFromYaml($yaml);
$this->assertTrue($openapi->validate(), print_r($openapi->getErrors(), true));
$this->assertEquals('3.0', $openapi->getMajorVersion());
$yaml = <<<YAML
openapi: 3.1.0
info:
title: Test API
version: 1
paths: []
YAML;
$openapi = Reader::readFromYaml($yaml);
$this->assertTrue($openapi->validate(), print_r($openapi->getErrors(), true));
$this->assertEquals('3.1', $openapi->getMajorVersion());
}
}