forked from cebe/php-openapi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResponseTest.php
More file actions
196 lines (174 loc) · 6.42 KB
/
Copy pathResponseTest.php
File metadata and controls
196 lines (174 loc) · 6.42 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
<?php
use cebe\openapi\Reader;
use cebe\openapi\spec\MediaType;
use cebe\openapi\spec\Response;
use cebe\openapi\spec\Responses;
/**
* @covers \cebe\openapi\spec\Response
* @covers \cebe\openapi\spec\Responses
*/
class ResponseTest extends \PHPUnit\Framework\TestCase
{
public function testRead()
{
/** @var $response Response */
$response = Reader::readFromJson(<<<'JSON'
{
"description": "A complex object array response",
"content": {
"application/json": {
"schema": {
"type": "array",
"items": {
"$ref": "#/components/schemas/VeryComplexType"
}
}
}
}
}
JSON
, Response::class);
$result = $response->validate();
$this->assertEquals([], $response->getErrors());
$this->assertTrue($result);
$this->assertEquals('A complex object array response', $response->description);
$this->assertArrayHasKey("application/json", $response->content);
$this->assertInstanceOf(MediaType::class, $response->content["application/json"]);
/** @var $response Response */
$response = Reader::readFromJson(<<<'JSON'
{
"content": {
"application/json": {
"schema": {
"type": "array",
"items": {
"$ref": "#/components/schemas/VeryComplexType"
}
}
}
}
}
JSON
, Response::class);
$result = $response->validate();
$this->assertEquals([
'Response is missing required property: description',
], $response->getErrors());
$this->assertFalse($result);
}
public function testResponses()
{
/** @var $responses Responses */
$responses = Reader::readFromYaml(<<<'YAML'
'200':
description: a pet to be returned
content:
application/json:
schema:
$ref: '#/components/schemas/Pet'
default:
description: Unexpected error
content:
application/json:
schema:
$ref: '#/components/schemas/ErrorModel'
YAML
, Responses::class);
$result = $responses->validate();
$this->assertEquals([], $responses->getErrors());
$this->assertTrue($result);
$this->assertTrue($responses->hasResponse(200));
$this->assertFalse($responses->hasResponse(201));
$this->assertTrue($responses->hasResponse('200'));
$this->assertFalse($responses->hasResponse('201'));
$this->assertTrue($responses->hasResponse('default'));
$this->assertTrue(isset($responses[200]));
$this->assertFalse(isset($responses[201]));
$this->assertTrue(isset($responses['200']));
$this->assertFalse(isset($responses['201']));
$this->assertTrue(isset($responses['default']));
$this->assertCount(2, $responses->getResponses());
$this->assertCount(2, $responses);
$this->assertInstanceOf(Response::class, $responses->getResponses()[200]);
$this->assertInstanceOf(Response::class, $responses->getResponses()['200']);
$this->assertInstanceOf(Response::class, $responses->getResponses()['default']);
$this->assertInstanceOf(Response::class, $responses->getResponse(200));
$this->assertInstanceOf(Response::class, $responses->getResponse('200'));
$this->assertInstanceOf(Response::class, $responses->getResponse('default'));
$this->assertNull($responses->getResponse('201'));
$this->assertInstanceOf(Response::class, $responses[200]);
$this->assertInstanceOf(Response::class, $responses['200']);
$this->assertInstanceOf(Response::class, $responses['default']);
$this->assertNull($responses['201']);
$this->assertEquals('a pet to be returned', $responses->getResponse('200')->description);
$this->assertEquals('a pet to be returned', $responses['200']->description);
$keys = [];
foreach($responses as $k => $response) {
$keys[] = $k;
$this->assertInstanceOf(Response::class, $response);
}
$this->assertEquals([200, 'default'], $keys);
}
public function testResponseCodes()
{
/** @var $responses Responses */
$responses = Reader::readFromYaml(<<<'YAML'
'200':
description: valid statuscode
'99':
description: invalid statuscode
'302':
description: valid statuscode
'401':
description: valid statuscode
'601':
description: invalid statuscode
'6X1':
description: invalid statuscode
'2X1':
description: invalid statuscode
'2XX':
description: valid statuscode
'default':
description: valid statuscode
'example':
description: valid statuscode
YAML
, Responses::class);
$result = $responses->validate();
$this->assertEquals([
'Responses: 99 is not a valid HTTP status code.',
'Responses: 601 is not a valid HTTP status code.',
'Responses: 6X1 is not a valid HTTP status code.',
'Responses: 2X1 is not a valid HTTP status code.',
'Responses: example is not a valid HTTP status code.',
], $responses->getErrors());
$this->assertFalse($result);
}
public function testCreateionFromObjects()
{
$responses = new Responses([
200 => new Response(['description' => 'A list of pets.']),
404 => ['description' => 'The pets list is gone 🙀'],
]);
$this->assertSame('A list of pets.', $responses->getResponse(200)->description);
$this->assertSame('The pets list is gone 🙀', $responses->getResponse(404)->description);
}
public function badResponseProvider()
{
yield [['200' => 'foo'], 'Response MUST be either an array, a Response or a Reference object, "string" given'];
yield [['200' => 42], 'Response MUST be either an array, a Response or a Reference object, "integer" given'];
yield [['200' => false], 'Response MUST be either an array, a Response or a Reference object, "boolean" given'];
yield [['200' => new stdClass()], 'Response MUST be either an array, a Response or a Reference object, "stdClass" given'];
// The last one can be supported in future, but now SpecBaseObjects::__construct() requires array explicitly
}
/**
* @dataProvider badResponseProvider
*/
public function testPathsCanNotBeCreatedFromBullshit($config, $expectedException)
{
$this->expectException(\cebe\openapi\exceptions\TypeErrorException::class);
$this->expectExceptionMessage($expectedException);
new Responses($config);
}
}