-
Notifications
You must be signed in to change notification settings - Fork 97
Expand file tree
/
Copy pathOpenApiTest.php
More file actions
235 lines (199 loc) · 11.2 KB
/
OpenApiTest.php
File metadata and controls
235 lines (199 loc) · 11.2 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
<?php
use cebe\openapi\spec\OpenApi;
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/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/examples/v3.0/api-with-examples.yaml',
__DIR__ . '/../../vendor/oai/openapi-specification/examples/v3.0/callback-example.yaml',
__DIR__ . '/../../vendor/oai/openapi-specification/examples/v3.0/link-example.yaml',
__DIR__ . '/../../vendor/oai/openapi-specification/examples/v3.0/petstore.yaml',
__DIR__ . '/../../vendor/oai/openapi-specification/examples/v3.0/petstore-expanded.yaml',
__DIR__ . '/../../vendor/oai/openapi-specification/examples/v3.0/uspto.yaml',
];
// examples from https://github.com/Mermade/openapi3-examples
$mermadeExamples = [
__DIR__ . '/../../vendor/mermade/openapi3-examples/pass/externalPathItemRef.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/pass/deprecated.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/pass/swagger2openapi/openapi.json',
__DIR__ . '/../../vendor/mermade/openapi3-examples/pass/gluecon/example1_from_._Different_parameters.md.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/pass/gluecon/example1_from_._Fixed_file.md.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/pass/gluecon/example1_from_._Different_parameters.md.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/pass/gluecon/example1_from_._Fixed_file.md.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/pass/gluecon/example1_from_._Fixed_multipart.md.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/pass/gluecon/example1_from_._Improved_examples.md.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/pass/gluecon/example1_from_._Improved_pathdescriptions.md.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/pass/gluecon/example1_from_._Improved_securityschemes.md.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/pass/gluecon/example1_from_._Improved_serverseverywhere.md.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/pass/gluecon/example1_from_._New_callbacks.md.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/pass/gluecon/example1_from_._New_links.md.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/pass/gluecon/example2_from_._Different_parameters.md.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/pass/gluecon/example2_from_._Different_requestbody.md.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/pass/gluecon/example2_from_._Different_servers.md.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/pass/gluecon/example2_from_._Fixed_multipart.md.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/pass/gluecon/example2_from_._Improved_securityschemes.md.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/pass/gluecon/example2_from_._New_callbacks.md.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/pass/gluecon/example2_from_._New_links.md.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/pass/gluecon/example3_from_._Different_parameters.md.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/pass/gluecon/example3_from_._Different_servers.md.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/pass/gluecon/example4_from_._Different_parameters.md.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/pass/gluecon/example5_from_._Different_parameters.md.yaml',
// TODO symfony/yaml can not read this file!?
// __DIR__ . '/../../vendor/mermade/openapi3-examples/pass/OAI/api-with-examples.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/pass/OAI/petstore-expanded.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/pass/OAI/petstore.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/pass/OAI/uber.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/malicious/rapid7-html.json',
__DIR__ . '/../../vendor/mermade/openapi3-examples/malicious/rapid7-java.json',
__DIR__ . '/../../vendor/mermade/openapi3-examples/malicious/rapid7-js.json',
__DIR__ . '/../../vendor/mermade/openapi3-examples/malicious/rapid7-php.json',
__DIR__ . '/../../vendor/mermade/openapi3-examples/malicious/rapid7-ruby.json',
// __DIR__ . '/../../vendor/mermade/openapi3-examples/malicious/yamlbomb.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) {
yield [
substr($path, strlen(__DIR__ . '/../../vendor/')),
basename(dirname($path, 2)) . DIRECTORY_SEPARATOR . basename(dirname($path, 1)) . DIRECTORY_SEPARATOR . basename($path)
];
}
}
/**
* @dataProvider specProvider
*/
public function testSpecs($openApiFile)
{
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->assertStringStartsWith('3.0.', $openapi->openapi);
// 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);
}
}
}