|
1
|
|
|
<?php |
|
2
|
|
|
namespace ElevenLabs\Api\Factory; |
|
3
|
|
|
|
|
4
|
|
|
use ElevenLabs\Api\Definition\RequestDefinition; |
|
5
|
|
|
use ElevenLabs\Api\Definition\Parameter; |
|
6
|
|
|
use ElevenLabs\Api\Definition\Parameters; |
|
7
|
|
|
use ElevenLabs\Api\Definition\ResponseDefinition; |
|
8
|
|
|
use ElevenLabs\Api\Schema; |
|
9
|
|
|
use PHPUnit\Framework\TestCase; |
|
10
|
|
|
|
|
11
|
|
|
class SwaggerSchemaFactoryTest extends TestCase |
|
12
|
|
|
{ |
|
13
|
|
|
/** @test */ |
|
14
|
|
|
public function itCanCreateASchemaFromAJsonFile() |
|
15
|
|
|
{ |
|
16
|
|
|
$schema = $this->getPetStoreSchemaJson(); |
|
17
|
|
|
|
|
18
|
|
|
assertThat($schema, isInstanceOf(Schema::class)); |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
/** @test */ |
|
22
|
|
|
public function itCanCreateASchemaFromAYamlFile() |
|
23
|
|
|
{ |
|
24
|
|
|
$schema = $this->getPetStoreSchemaYaml(); |
|
25
|
|
|
|
|
26
|
|
|
assertThat($schema, isInstanceOf(Schema::class)); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** @test */ |
|
30
|
|
|
public function itThrowAnExceptionWhenTheSchemaFileIsNotSupported() |
|
31
|
|
|
{ |
|
32
|
|
|
$unsupportedFile = 'file://'.dirname(__DIR__).'/fixtures/petstore.txt'; |
|
33
|
|
|
|
|
34
|
|
|
$this->expectException(\InvalidArgumentException::class); |
|
35
|
|
|
$this->expectExceptionMessageRegExp('/does not provide a supported extension/'); |
|
36
|
|
|
|
|
37
|
|
|
(new SwaggerSchemaFactory())->createSchema($unsupportedFile); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** @test */ |
|
41
|
|
|
public function itShouldHaveSchemaProperties() |
|
42
|
|
|
{ |
|
43
|
|
|
$schema = $this->getPetStoreSchemaJson(); |
|
44
|
|
|
|
|
45
|
|
|
assertThat($schema->getHost(), equalTo('petstore.swagger.io')); |
|
46
|
|
|
assertThat($schema->getBasePath(), equalTo('/v2')); |
|
47
|
|
|
assertThat($schema->getSchemes(), equalTo(['https', 'http'])); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** @test */ |
|
51
|
|
|
public function itThrowAnExceptionWhenAnOperationDoesNotProvideAnId() |
|
52
|
|
|
{ |
|
53
|
|
|
$this->expectException(\LogicException::class); |
|
54
|
|
|
$this->expectExceptionMessage('You need to provide an operationId for GET /something'); |
|
55
|
|
|
|
|
56
|
|
|
$this->getSchemaFromFile('operation-without-an-id.json'); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** @test */ |
|
60
|
|
|
public function itThrowAnExceptionWhenAnOperationDoesNotProvideResponses() |
|
61
|
|
|
{ |
|
62
|
|
|
$this->expectException(\LogicException::class); |
|
63
|
|
|
$this->expectExceptionMessage('You need to specify at least one response for GET /something'); |
|
64
|
|
|
|
|
65
|
|
|
$this->getSchemaFromFile('operation-without-responses.json'); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** @test */ |
|
69
|
|
|
public function itSupportAnOperationWithoutParameters() |
|
70
|
|
|
{ |
|
71
|
|
|
$schema = $this->getSchemaFromFile('operation-without-parameters.json'); |
|
72
|
|
|
$definition = $schema->getRequestDefinition('getSomething'); |
|
73
|
|
|
|
|
74
|
|
|
assertThat($definition->hasHeadersSchema(), isFalse()); |
|
75
|
|
|
assertThat($definition->hasBodySchema(), isFalse()); |
|
76
|
|
|
assertThat($definition->hasQueryParametersSchema(), isFalse()); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** @test */ |
|
80
|
|
|
public function itCanCreateARequestDefinition() |
|
81
|
|
|
{ |
|
82
|
|
|
$schema = $this->getPetStoreSchemaJson(); |
|
83
|
|
|
|
|
84
|
|
|
$requestDefinition = $schema->getRequestDefinition('findPetsByStatus'); |
|
85
|
|
|
|
|
86
|
|
|
assertThat($requestDefinition, isInstanceOf(RequestDefinition::class)); |
|
87
|
|
|
assertThat($requestDefinition->getMethod(), equalTo('GET')); |
|
88
|
|
|
assertThat($requestDefinition->getOperationId(), equalTo('findPetsByStatus')); |
|
89
|
|
|
assertThat($requestDefinition->getPathTemplate(), equalTo('/v2/pet/findByStatus')); |
|
90
|
|
|
assertThat($requestDefinition->getContentTypes(), equalTo([])); |
|
91
|
|
|
assertThat($requestDefinition->getRequestParameters(), isInstanceOf(Parameters::class)); |
|
92
|
|
|
assertThat($requestDefinition->getResponseDefinition(200), isInstanceOf(ResponseDefinition::class)); |
|
93
|
|
|
assertThat($requestDefinition->getResponseDefinition(400), isInstanceOf(ResponseDefinition::class)); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** @test */ |
|
97
|
|
|
public function itCanCreateARequestBodyParameter() |
|
98
|
|
|
{ |
|
99
|
|
|
$schema = $this->getPetStoreSchemaJson(); |
|
100
|
|
|
|
|
101
|
|
|
$requestParameters = $schema->getRequestDefinition('addPet')->getRequestParameters(); |
|
102
|
|
|
|
|
103
|
|
|
assertThat($requestParameters, isInstanceOf(Parameters::class)); |
|
104
|
|
|
assertThat($requestParameters->getBody(), isInstanceOf(Parameter::class)); |
|
105
|
|
|
assertThat($requestParameters->hasBodySchema(), isTrue()); |
|
106
|
|
|
assertThat($requestParameters->getBodySchema(), isType('object')); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** @test */ |
|
110
|
|
|
public function itCanCreateRequestPath() |
|
111
|
|
|
{ |
|
112
|
|
|
$schema = $this->getPetStoreSchemaJson(); |
|
113
|
|
|
|
|
114
|
|
|
$requestParameters = $schema->getRequestDefinition('getPetById')->getRequestParameters(); |
|
115
|
|
|
|
|
116
|
|
|
assertThat($requestParameters->getPath(), containsOnlyInstancesOf(Parameter::class)); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** @test */ |
|
120
|
|
|
public function itCanCreateRequestQueryParameters() |
|
121
|
|
|
{ |
|
122
|
|
|
$schema = $this->getPetStoreSchemaJson(); |
|
123
|
|
|
|
|
124
|
|
|
$requestParameters = $schema->getRequestDefinition('findPetsByStatus')->getRequestParameters(); |
|
125
|
|
|
|
|
126
|
|
|
assertThat($requestParameters->getQuery(), containsOnlyInstancesOf(Parameter::class)); |
|
127
|
|
|
assertThat($requestParameters->getQueryParametersSchema(), isType('object')); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** @test */ |
|
131
|
|
|
public function itCanCreateRequestHeadersParameter() |
|
132
|
|
|
{ |
|
133
|
|
|
$schema = $this->getPetStoreSchemaJson(); |
|
134
|
|
|
|
|
135
|
|
|
$requestParameters = $schema->getRequestDefinition('deletePet')->getRequestParameters(); |
|
136
|
|
|
|
|
137
|
|
|
assertThat($requestParameters->getHeaders(), containsOnlyInstancesOf(Parameter::class)); |
|
138
|
|
|
assertThat($requestParameters->hasHeadersSchema(), isTrue()); |
|
139
|
|
|
assertThat($requestParameters->getHeadersSchema(), isType('object')); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** @test */ |
|
143
|
|
|
public function itCanCreateAResponseDefinition() |
|
144
|
|
|
{ |
|
145
|
|
|
$schema = $this->getPetStoreSchemaJson(); |
|
146
|
|
|
|
|
147
|
|
|
$responseDefinition = $schema->getRequestDefinition('getPetById')->getResponseDefinition(200); |
|
148
|
|
|
|
|
149
|
|
|
assertThat($responseDefinition, isInstanceOf(ResponseDefinition::class)); |
|
150
|
|
|
assertThat($responseDefinition->getBodySchema(), isType('object')); |
|
151
|
|
|
assertThat($responseDefinition->getStatusCode(), equalTo(200)); |
|
152
|
|
|
assertThat($responseDefinition->getContentTypes(), contains('application/json')); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
public function itUseTheSchemaDefaultConsumesPropertyWhenNotProvidedByAnOperation() |
|
156
|
|
|
{ |
|
157
|
|
|
$schema = $this->getSchemaFromFile('schema-with-default-consumes-and-produces-properties.json'); |
|
158
|
|
|
$definition = $schema->getRequestDefinition('postSomething'); |
|
159
|
|
|
|
|
160
|
|
|
assertThat($definition->getContentTypes(), contains('application/json')); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** @test */ |
|
164
|
|
|
public function itUseTheSchemaDefaultProducesPropertyWhenNotProvidedByAnOperationResponse() |
|
165
|
|
|
{ |
|
166
|
|
|
$schema = $this->getSchemaFromFile('schema-with-default-consumes-and-produces-properties.json'); |
|
167
|
|
|
$responseDefinition = $schema |
|
168
|
|
|
->getRequestDefinition('postSomething') |
|
169
|
|
|
->getResponseDefinition(201); |
|
170
|
|
|
|
|
171
|
|
|
assertThat($responseDefinition->getContentTypes(), contains('application/json')); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* @test |
|
176
|
|
|
* @dataProvider getGuessableContentTypes |
|
177
|
|
|
*/ |
|
178
|
|
|
public function itGuessTheContentTypeFromRequestParameters($operationId, $expectedContentType) |
|
179
|
|
|
{ |
|
180
|
|
|
$schema = $this->getSchemaFromFile('request-without-content-types.json'); |
|
181
|
|
|
|
|
182
|
|
|
$definition = $schema->getRequestDefinition($operationId); |
|
183
|
|
|
|
|
184
|
|
|
assertThat($definition->getContentTypes(), contains($expectedContentType)); |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
public function getGuessableContentTypes() |
|
188
|
|
|
{ |
|
189
|
|
|
return [ |
|
190
|
|
|
'body' => [ |
|
191
|
|
|
'operationId' => 'postBodyWithoutAContentType', |
|
192
|
|
|
'contentType' => 'application/json' |
|
193
|
|
|
], |
|
194
|
|
|
'formData' => [ |
|
195
|
|
|
'operationId' => 'postFromDataWithoutAContentType', |
|
196
|
|
|
'contentType' => 'application/x-www-form-urlencoded' |
|
197
|
|
|
], |
|
198
|
|
|
]; |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** @test */ |
|
202
|
|
|
public function itFailWhenTryingToGuessTheContentTypeFromARequestWithMultipleBodyLocations() |
|
203
|
|
|
{ |
|
204
|
|
|
$this->expectException(\LogicException::class); |
|
205
|
|
|
$this->expectExceptionMessage( |
|
206
|
|
|
'Parameters cannot have body and formData locations ' . |
|
207
|
|
|
'at the same time in /post/with-conflicting-locations' |
|
208
|
|
|
); |
|
209
|
|
|
|
|
210
|
|
|
$schemaFile = 'file://'.dirname(__DIR__).'/fixtures/request-with-conflicting-locations.json'; |
|
211
|
|
|
(new SwaggerSchemaFactory())->createSchema($schemaFile); |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
/** |
|
215
|
|
|
* @return Schema |
|
216
|
|
|
*/ |
|
217
|
|
|
private function getPetStoreSchemaJson() |
|
218
|
|
|
{ |
|
219
|
|
|
return $this->getSchemaFromFile('petstore.json'); |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
/** |
|
223
|
|
|
* @return Schema |
|
224
|
|
|
*/ |
|
225
|
|
|
private function getPetStoreSchemaYaml() |
|
226
|
|
|
{ |
|
227
|
|
|
return $this->getSchemaFromFile('petstore.yaml'); |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
/** |
|
231
|
|
|
* @param $name |
|
232
|
|
|
* |
|
233
|
|
|
* @return Schema |
|
234
|
|
|
*/ |
|
235
|
|
|
private function getSchemaFromFile($name) |
|
236
|
|
|
{ |
|
237
|
|
|
$schemaFile = 'file://' . dirname(__DIR__) . '/fixtures/'.$name; |
|
238
|
|
|
$factory = new SwaggerSchemaFactory(); |
|
239
|
|
|
|
|
240
|
|
|
return $factory->createSchema($schemaFile); |
|
241
|
|
|
} |
|
242
|
|
|
} |
|
243
|
|
|
|