forked from cebe/php-openapi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRequestBodyTest.php
More file actions
115 lines (101 loc) · 3.89 KB
/
Copy pathRequestBodyTest.php
File metadata and controls
115 lines (101 loc) · 3.89 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
<?php
use cebe\openapi\Reader;
use cebe\openapi\spec\MediaType;
use cebe\openapi\spec\RequestBody;
use cebe\openapi\spec\Encoding;
/**
* @covers \cebe\openapi\spec\RequestBody
* @covers \cebe\openapi\spec\Encoding
*/
class RequestBodyTest extends \PHPUnit\Framework\TestCase
{
public function testRead()
{
/** @var $requestBody RequestBody */
$requestBody = Reader::readFromJson(<<<'JSON'
{
"description": "user to add to the system",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/User"
}
}
}
}
JSON
, RequestBody::class);
$result = $requestBody->validate();
$this->assertEquals([], $requestBody->getErrors());
$this->assertTrue($result);
$this->assertEquals('user to add to the system', $requestBody->description);
$this->assertArrayHasKey("application/json", $requestBody->content);
$this->assertInstanceOf(MediaType::class, $requestBody->content["application/json"]);
// required Defaults to false.
$this->assertFalse($requestBody->required);
/** @var $response RequestBody */
$requestBody = Reader::readFromJson(<<<'JSON'
{
"description": "user to add to the system"
}
JSON
, RequestBody::class);
$result = $requestBody->validate();
$this->assertEquals([
'RequestBody is missing required property: content',
], $requestBody->getErrors());
$this->assertFalse($result);
}
public function testEncoding()
{
/** @var $requestBody RequestBody */
$requestBody = Reader::readFromYaml(<<<'YAML'
content:
multipart/mixed:
schema:
type: object
properties:
id:
# default is text/plain
type: string
format: uuid
encoding:
historyMetadata:
# require XML Content-Type in utf-8 encoding
contentType: application/xml; charset=utf-8
profileImage:
# only accept png/jpeg
contentType: image/png, image/jpeg
headers:
X-Rate-Limit-Limit:
description: The number of allowed requests in the current period
schema:
type: integer
other:
style: form
YAML
, RequestBody::class);
$result = $requestBody->validate();
$this->assertEquals([], $requestBody->getErrors());
$this->assertTrue($result);
$this->assertArrayHasKey("multipart/mixed", $requestBody->content);
$this->assertInstanceOf(MediaType::class, $mediaType = $requestBody->content["multipart/mixed"]);
$this->assertCount(3, $mediaType->encoding);
$this->assertArrayHasKey("historyMetadata", $mediaType->encoding);
$this->assertArrayHasKey("profileImage", $mediaType->encoding);
$this->assertArrayHasKey("other", $mediaType->encoding);
$this->assertInstanceOf(Encoding::class, $mediaType->encoding["profileImage"]);
$this->assertInstanceOf(Encoding::class, $mediaType->encoding["historyMetadata"]);
$this->assertInstanceOf(Encoding::class, $mediaType->encoding["other"]);
$profileImage = $mediaType->encoding["profileImage"];
$this->assertEquals('image/png, image/jpeg', $profileImage->contentType);
$this->assertInstanceOf(\cebe\openapi\spec\Header::class, $profileImage->headers['X-Rate-Limit-Limit']);
$this->assertEquals('The number of allowed requests in the current period', $profileImage->headers['X-Rate-Limit-Limit']->description);
// When style is form, the default value is true. For all other styles, the default value is false.
$this->assertFalse($profileImage->explode);
$this->assertTrue($mediaType->encoding["other"]->explode);
// allowReserved default value is false
$this->assertFalse($profileImage->allowReserved);
$this->assertFalse($mediaType->encoding["other"]->allowReserved);
}
}