-
Notifications
You must be signed in to change notification settings - Fork 97
Expand file tree
/
Copy pathMediaTypeTest.php
More file actions
175 lines (160 loc) · 5.63 KB
/
MediaTypeTest.php
File metadata and controls
175 lines (160 loc) · 5.63 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
<?php
use cebe\openapi\Reader;
use cebe\openapi\spec\Example;
use cebe\openapi\spec\MediaType;
use cebe\openapi\spec\OpenApi;
use cebe\openapi\spec\Reference;
use Symfony\Component\Yaml\Yaml;
/**
* @covers \cebe\openapi\spec\MediaType
* @covers \cebe\openapi\spec\Example
*/
class MediaTypeTest extends \PHPUnit\Framework\TestCase
{
public function testRead()
{
/** @var $mediaType MediaType */
$mediaType = Reader::readFromYaml(<<<'YAML'
schema:
$ref: "#/components/schemas/Pet"
examples:
cat:
summary: An example of a cat
value:
name: Fluffy
petType: Cat
color: White
gender: male
breed: Persian
dog:
summary: An example of a dog with a cat's name
value:
name: Puma
petType: Dog
color: Black
gender: Female
breed: Mixed
frog:
$ref: "#/components/examples/frog-example"
YAML
, MediaType::class);
$result = $mediaType->validate();
$this->assertEquals([], $mediaType->getErrors());
$this->assertTrue($result);
$this->assertInstanceOf(Reference::class, $mediaType->schema);
if (method_exists($this, 'assertIsArray')) {
$this->assertIsArray($mediaType->examples);
} else {
$this->assertInternalType('array', $mediaType->examples);
}
$this->assertCount(3, $mediaType->examples);
$this->assertArrayHasKey('cat', $mediaType->examples);
$this->assertArrayHasKey('dog', $mediaType->examples);
$this->assertArrayHasKey('frog', $mediaType->examples);
$this->assertInstanceOf(Example::class, $mediaType->examples['cat']);
$this->assertInstanceOf(Example::class, $mediaType->examples['dog']);
$this->assertInstanceOf(Reference::class, $mediaType->examples['frog']);
$this->assertEquals('An example of a cat', $mediaType->examples['cat']->summary);
$expectedCat = [ // TODO we might actually expect this to be an object of stdClass
'name' => 'Fluffy',
'petType' => 'Cat',
'color' => 'White',
'gender' => 'male',
'breed' => 'Persian',
];
$this->assertEquals($expectedCat, $mediaType->examples['cat']->value);
}
public function testCreateionFromObjects()
{
$mediaType = new MediaType([
'schema' => new \cebe\openapi\spec\Schema([
'type' => \cebe\openapi\spec\Type::OBJECT,
'properties' => [
'id' => new \cebe\openapi\spec\Schema(['type' => 'string', 'format' => 'uuid']),
'profileImage' => new \cebe\openapi\spec\Schema(['type' => 'string', 'format' => 'binary']),
],
]),
'encoding' => [
'id' => [],
'profileImage' => new \cebe\openapi\spec\Encoding([
'contentType' => 'image/png, image/jpeg',
'headers' => [
'X-Rate-Limit-Limit' => new \cebe\openapi\spec\Header([
'description' => 'The number of allowed requests in the current period',
'schema' => new \cebe\openapi\spec\Schema(['type' => 'integer']),
]),
],
]),
],
]);
// default value should be extracted
$this->assertEquals('text/plain', $mediaType->encoding['id']->contentType);
// object should be passed.
$this->assertInstanceOf(\cebe\openapi\spec\Encoding::class, $mediaType->encoding['profileImage']);
}
public function badEncodingProvider()
{
yield [['encoding' => ['id' => 'foo']], 'Encoding MUST be either array or Encoding object, "string" given'];
yield [['encoding' => ['id' => 42]], 'Encoding MUST be either array or Encoding object, "integer" given'];
yield [['encoding' => ['id' => false]], 'Encoding MUST be either array or Encoding object, "boolean" given'];
yield [['encoding' => ['id' => new stdClass()]], 'Encoding MUST be either array or Encoding object, "stdClass" given'];
// The last one can be supported in future, but now SpecBaseObjects::__construct() requires array explicitly
}
/**
* @dataProvider badEncodingProvider
*/
public function testPathsCanNotBeCreatedFromBullshit($config, $expectedException)
{
$this->expectException(\cebe\openapi\exceptions\TypeErrorException::class);
$this->expectExceptionMessage($expectedException);
new MediaType($config);
}
public function testUnresolvedReferencesInEncoding()
{
$yaml = yaml_parse(<<<'YAML'
openapi: "3.0.0"
info:
version: 1.0.0
title: Encoding test
paths:
/pets:
post:
summary: Create a pet
operationId: createPets
requestBody:
content:
multipart/form-data:
schema:
type: object
properties:
pet:
$ref: '#/components/schemas/Pet'
petImage:
type: string
format: binary
encoding:
pet:
contentType: application/json
petImage:
contentType: image/*
application/json:
schema:
$ref: '#/components/schemas/Pet'
responses:
'201':
description: Null response
components:
schemas:
Pet:
type: object
properties:
name:
type: string
YAML
);
$openapi = new OpenApi($yaml);
$result = $openapi->validate();
$this->assertEquals([], $openapi->getErrors());
$this->assertTrue($result);
}
}