forked from cebe/php-openapi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPathTest.php
More file actions
195 lines (167 loc) · 7.19 KB
/
Copy pathPathTest.php
File metadata and controls
195 lines (167 loc) · 7.19 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
<?php
use cebe\openapi\Reader;
use cebe\openapi\spec\Operation;
use cebe\openapi\spec\PathItem;
use cebe\openapi\spec\Paths;
use cebe\openapi\spec\Reference;
use cebe\openapi\spec\Response;
use cebe\openapi\spec\Responses;
/**
* @covers \cebe\openapi\spec\Paths
* @covers \cebe\openapi\spec\PathItem
*/
class PathTest extends \PHPUnit\Framework\TestCase
{
public function testRead()
{
/** @var $paths Paths */
$paths = Reader::readFromJson(<<<'JSON'
{
"/pets": {
"get": {
"description": "Returns all pets from the system that the user has access to",
"responses": {
"200": {
"description": "A list of pets.",
"content": {
"application/json": {
"schema": {
"type": "array",
"items": {
"$ref": "#/components/schemas/pet"
}
}
}
}
}
}
}
}
}
JSON
, Paths::class);
$result = $paths->validate();
$this->assertEquals([], $paths->getErrors());
$this->assertTrue($result);
$this->assertTrue($paths->hasPath('/pets'));
$this->assertTrue(isset($paths['/pets']));
$this->assertFalse($paths->hasPath('/dog'));
$this->assertFalse(isset($paths['/dog']));
$this->assertInstanceOf(PathItem::class, $paths->getPath('/pets'));
$this->assertInstanceOf(PathItem::class, $paths['/pets']);
$this->assertInstanceOf(Operation::class, $paths->getPath('/pets')->get);
$this->assertNull($paths->getPath('/dog'));
$this->assertNull($paths['/dog']);
$this->assertCount(1, $paths->getPaths());
$this->assertCount(1, $paths);
foreach($paths as $path => $pathItem) {
$this->assertEquals('/pets', $path);
$this->assertInstanceOf(PathItem::class, $pathItem);
}
}
public function testCreationFromObjects()
{
$paths = new Paths([
'/pets' => new PathItem([
'get' => new Operation([
'responses' => new Responses([
200 => new Response(['description' => 'A list of pets.']),
404 => ['description' => 'The pets list is gone 🙀'],
])
])
])
]);
$this->assertTrue($paths->hasPath('/pets'));
$this->assertInstanceOf(PathItem::class, $paths->getPath('/pets'));
$this->assertInstanceOf(PathItem::class, $paths['/pets']);
$this->assertInstanceOf(Operation::class, $paths->getPath('/pets')->get);
$this->assertSame('A list of pets.', $paths->getPath('/pets')->get->responses->getResponse(200)->description);
$this->assertSame('The pets list is gone 🙀', $paths->getPath('/pets')->get->responses->getResponse(404)->description);
}
public function badPathsConfigProvider()
{
yield [['/pets' => 'foo'], 'Path MUST be either array or PathItem object, "string" given'];
yield [['/pets' => 42], 'Path MUST be either array or PathItem object, "integer" given'];
yield [['/pets' => false], 'Path MUST be either array or PathItem object, "boolean" given'];
yield [['/pets' => new stdClass()], 'Path MUST be either array or PathItem object, "stdClass" given'];
// The last one can be supported in future, but now SpecBaseObjects::__construct() requires array explicitly
}
/**
* @dataProvider badPathsConfigProvider
*/
public function testPathsCanNotBeCreatedFromBullshit($config, $expectedException)
{
$this->expectException(\cebe\openapi\exceptions\TypeErrorException::class);
$this->expectExceptionMessage($expectedException);
new Paths($config);
}
public function testInvalidPath()
{
/** @var $paths Paths */
$paths = Reader::readFromJson(<<<'JSON'
{
"pets": {
"get": {
"description": "Returns all pets from the system that the user has access to",
"responses": {
"200": {
"description": "A list of pets."
}
}
}
}
}
JSON
, Paths::class);
$result = $paths->validate();
$this->assertEquals([
'Path must begin with /: pets'
], $paths->getErrors());
$this->assertFalse($result);
}
public function testPathItemReference()
{
$file = __DIR__ . '/data/paths/openapi.yaml';
/** @var $openapi \cebe\openapi\spec\OpenApi */
$openapi = Reader::readFromYamlFile($file, \cebe\openapi\spec\OpenApi::class, false);
$result = $openapi->validate();
$this->assertEquals([], $openapi->getErrors(), print_r($openapi->getErrors(), true));
$this->assertTrue($result);
$this->assertInstanceOf(Paths::class, $openapi->paths);
$this->assertInstanceOf(PathItem::class, $fooPath = $openapi->paths['/foo']);
$this->assertInstanceOf(PathItem::class, $barPath = $openapi->paths['/bar']);
$this->assertSame([
'x-extension-1' => 'Extension1',
'x-extension-2' => 'Extension2'
], $openapi->getExtensions());
$this->assertEmpty($fooPath->getOperations());
$this->assertEmpty($barPath->getOperations());
$this->assertInstanceOf(\cebe\openapi\spec\Reference::class, $fooPath->getReference());
$this->assertInstanceOf(\cebe\openapi\spec\Reference::class, $barPath->getReference());
$this->assertNull($fooPath->getReference()->resolve());
$this->assertInstanceOf(PathItem::class, $ReferencedBarPath = $barPath->getReference()->resolve());
$this->assertCount(1, $ReferencedBarPath->getOperations());
$this->assertInstanceOf(Operation::class, $ReferencedBarPath->get);
$this->assertEquals('getBar', $ReferencedBarPath->get->operationId);
$this->assertInstanceOf(Reference::class, $reference200 = $ReferencedBarPath->get->responses['200']);
$this->assertInstanceOf(Response::class, $ReferencedBarPath->get->responses['404']);
$this->assertEquals('non-existing resource', $ReferencedBarPath->get->responses['404']->description);
$path200 = $reference200->resolve();
$this->assertInstanceOf(Response::class, $path200);
$this->assertEquals('A bar', $path200->description);
/** @var $openapi OpenApi */
$openapi = Reader::readFromYamlFile($file, \cebe\openapi\spec\OpenApi::class, true);
$result = $openapi->validate();
$this->assertEquals([], $openapi->getErrors(), print_r($openapi->getErrors(), true));
$this->assertTrue($result);
$this->assertInstanceOf(Paths::class, $openapi->paths);
$this->assertInstanceOf(PathItem::class, $fooPath = $openapi->paths['/foo']);
$this->assertInstanceOf(PathItem::class, $barPath = $openapi->paths['/bar']);
$this->assertEmpty($fooPath->getOperations());
$this->assertCount(1, $barPath->getOperations());
$this->assertInstanceOf(Operation::class, $barPath->get);
$this->assertEquals('getBar', $barPath->get->operationId);
$this->assertEquals('A bar', $barPath->get->responses['200']->description);
$this->assertEquals('non-existing resource', $barPath->get->responses['404']->description);
}
}