Skip to content
This repository was archived by the owner on Dec 14, 2023. It is now read-only.

Commit 0d2a979

Browse files
authored
Implement OpenApi v3.0 specs (#30)
* Add openapi3.json double * Remove unnecessary class SchemaFactory * Update UriConstraint * Update MethodsAllowedConstraint * Improve fixtures openapi3.json * Refactor JsonSchemaConstraintTest * Refactor Validator * Fix SchemaTest * Redo Schema * Refactor HeadersConstraintTest * Refactor ContentTypeConstraintTest * Fix HeadersConstraint * Fix Schema * Fix request headers fixtures * Rename fixture file, add some more * Fix Schema * Enhance SchemaTest * Fix HeadersConstraint * Fix HeadersConstraintTest * Fix UriConstraintTest * Fix Asserts * Remove swagger.json v2 * Update CHANGELOG * Add PHP 7.3 to Travis CI
1 parent e625a15 commit 0d2a979

20 files changed

Lines changed: 611 additions & 1084 deletions

.travis.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@ matrix:
66
fast_finish: true
77
include:
88
- php: 7.1
9+
- php: 7.2
910
env:
1011
- EXECUTE_COVERAGE=true
11-
- php: 7.2
12+
- php: 7.3
13+
allow_failures:
14+
- php: 7.3
1215

1316
notifications:
1417
email: false

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ All notable changes to this project will be documented in this file
33
using the [Keep a CHANGELOG](http://keepachangelog.com/) principles.
44
This project adheres to [Semantic Versioning](http://semver.org/).
55

6+
## [0.6.0] 2018-12-18
7+
8+
### Changed
9+
10+
- Migrate library to OpenAPI v3.0 format
11+
612
## [0.5.0] 2018-12-18
713

814
### Changed

src/JsonSchema/Validator.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,6 @@ public function __construct(string $context)
3434
$this->context = $context;
3535
}
3636

37-
/**
38-
* @param mixed $value
39-
* @param stdClass $schema
40-
* @param JsonPointer|null $path
41-
*
42-
* @return array
43-
*/
4437
public function validate($value, stdClass $schema, JsonPointer $path = null): array
4538
{
4639
$this->validator->check($value, $schema, $path);

src/PhpUnit/Asserts.php

Lines changed: 47 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Psr\Http\Message\StreamInterface;
1717
use Psr\Http\Message\UriInterface;
1818
use Rebilly\OpenAPI\Schema;
19+
use stdClass;
1920

2021
/**
2122
* Asserts data against OpenAPI specification.
@@ -32,18 +33,13 @@ trait Asserts
3233
* - Assert content-type declared by consumes
3334
* - Assert headers declared by parameters (header)
3435
* - Assert body declared by parameters (body)
35-
*
36-
* @param Schema $spec
37-
* @param string $template
38-
* @param RequestInterface $request
39-
* @param string $msg
4036
*/
41-
final protected static function assertRequest(Schema $spec, $template, RequestInterface $request, $msg = '')
37+
final protected static function assertRequest(Schema $schema, string $path, RequestInterface $request, string $msg = ''): void
4238
{
43-
self::assertMethodAllowed($spec, $template, $request->getMethod(), $msg);
44-
self::assertUri($spec, $template, $request->getMethod(), $request->getUri(), $msg);
45-
self::assertRequestHeaders($spec, $template, $request->getMethod(), $request->getHeaders(), $msg);
46-
self::assertRequestBody($spec, $template, $request->getMethod(), $request->getBody(), $msg);
39+
self::assertMethodAllowed($schema, $path, $request->getMethod(), $msg);
40+
self::assertUri($schema, $path, $request->getMethod(), $request->getUri(), $msg);
41+
self::assertRequestHeaders($schema, $path, $request->getMethod(), $request->getHeaders(), $msg);
42+
self::assertRequestBody($schema, $path, $request->getMethod(), $request->getBody(), $msg);
4743
}
4844

4945
/**
@@ -55,27 +51,21 @@ final protected static function assertRequest(Schema $spec, $template, RequestIn
5551
* - Assert content-type declared by produces from operation
5652
* - Assert headers
5753
* - Assert body
58-
*
59-
* @param Schema $spec
60-
* @param string $template
61-
* @param string $method
62-
* @param ResponseInterface $response
63-
* @param string $msg
6454
*/
65-
final protected static function assertResponse(Schema $spec, $template, $method, ResponseInterface $response, $msg = '')
55+
final protected static function assertResponse(Schema $schema, string $path, string $method, ResponseInterface $response, string $msg = ''): void
6656
{
67-
self::assertResponseDefined($spec, $template, $method, $response->getStatusCode(), $msg);
57+
self::assertResponseDefined($schema, $path, $method, $response->getStatusCode(), $msg);
6858
self::assertResponseHeaders(
69-
$spec,
70-
$template,
59+
$schema,
60+
$path,
7161
$method,
7262
$response->getStatusCode(),
7363
$response->getHeaders(),
7464
$msg
7565
);
7666
self::assertResponseBody(
77-
$spec,
78-
$template,
67+
$schema,
68+
$path,
7969
$method,
8070
$response->getStatusCode(),
8171
$response->getBody(),
@@ -93,148 +83,103 @@ final protected static function assertResponse(Schema $spec, $template, $method,
9383
* - Assert URI path starts with defined base path
9484
* - Assert URI path matches defined template and path parameters
9585
* - Assert URI path matches defined query parameters
96-
*
97-
* @param Schema $spec
98-
* @param string $template
99-
* @param string $method
100-
* @param UriInterface $uri
101-
* @param string $msg
10286
*/
103-
final protected static function assertUri(Schema $spec, $template, $method, UriInterface $uri, $msg = '')
87+
final protected static function assertUri(Schema $schema, string $path, string $method, UriInterface $uri, string $msg = ''): void
10488
{
10589
Assert::assertThat(
10690
$uri,
10791
new UriConstraint(
108-
$spec->getSupportedSchemes($template, $method),
109-
$spec->getHost(),
110-
$spec->getBasePath(),
111-
$template,
112-
$spec->getRequestPathParameters($template, $method),
113-
$spec->getRequestQueryParameters($template, $method)
92+
$schema->getServers(),
93+
$path,
94+
$schema->getRequestPathParameters($path, $method),
95+
$schema->getRequestQueryParameters($path, $method)
11496
),
11597
$msg
11698
);
11799
}
118100

119101
/**
120102
* Assert the endpoint supports given operation.
121-
*
122-
* @param Schema $spec
123-
* @param string $template
124-
* @param string $method
125-
* @param string $msg
126103
*/
127-
final protected static function assertMethodAllowed(Schema $spec, $template, $method, $msg = '')
104+
final protected static function assertMethodAllowed(Schema $schema, string $path, string $method, string $msg = ''): void
128105
{
129106
Assert::assertThat(
130107
$method,
131-
new MethodsAllowedConstraint($spec->getAllowedMethods($template)),
108+
new MethodsAllowedConstraint($schema->getAllowedMethods($path)),
132109
$msg
133110
);
134111
}
135112

136113
/**
137114
* Assert the response status code defined.
138-
*
139-
* @param Schema $spec
140-
* @param string $template
141-
* @param string $method
142-
* @param string $status
143-
* @param string $msg
144115
*/
145-
final protected static function assertResponseDefined(Schema $spec, $template, $method, $status, $msg = '')
116+
final protected static function assertResponseDefined(Schema $schema, string $template, string $method, string $status, string $msg = ''): void
146117
{
147118
Assert::assertTrue(
148-
in_array((int) $status, $spec->getResponseCodes($template, strtolower($method)), true),
119+
$schema->isResponseDefined($template, strtolower($method), $status),
149120
$msg ?: "Operation \"{$method} {$template}\" does not support response code \"{$status}\""
150121
);
151122
}
152123

153124
/**
154125
* Assert the endpoint supports given operation.
155-
*
156-
* @param Schema $spec
157-
* @param string $template
158-
* @param string $method
159-
* @param string $contentType
160-
* @param string $msg
161126
*/
162-
final protected static function assertRequestContentType(Schema $spec, $template, $method, $contentType, $msg = '')
127+
final protected static function assertRequestContentType(Schema $schema, string $path, string $method, string $contentType, string $msg = ''): void
163128
{
164129
Assert::assertThat(
165130
$contentType,
166-
new ContentTypeConstraint($spec->getRequestContentTypes($template, $method)),
131+
new ContentTypeConstraint($schema->getRequestContentTypes($path, $method)),
167132
$msg
168133
);
169134
}
170135

171136
/**
172137
* Assert the endpoint supports given operation.
173-
*
174-
* @param Schema $spec
175-
* @param string $template
176-
* @param string $method
177-
* @param string $contentType
178-
* @param string $msg
179138
*/
180-
final protected static function assertResponseContentType(Schema $spec, $template, $method, $contentType, $msg = '')
139+
final protected static function assertResponseContentType(Schema $schema, string $path, string $method, string $status, string $contentType, string $msg = ''): void
181140
{
182141
Assert::assertThat(
183142
$contentType,
184-
new ContentTypeConstraint($spec->getResponseContentTypes($template, $method)),
143+
new ContentTypeConstraint($schema->getResponseContentTypes($path, $method, $status)),
185144
$msg
186145
);
187146
}
188147

189-
/**
190-
* @param Schema $spec
191-
* @param string $template
192-
* @param string $method
193-
* @param array $headers
194-
* @param string $msg
195-
*/
196-
final protected static function assertRequestHeaders(Schema $spec, $template, $method, array $headers, $msg = '')
148+
final protected static function assertRequestHeaders(Schema $schema, string $path, string $method, array $headers, string $msg = ''): void
197149
{
198150
Assert::assertThat(
199151
$headers,
200-
new HeadersConstraint($spec->getRequestHeaderSchemas($template, strtolower($method))),
152+
new HeadersConstraint($schema->getRequestHeaderSchemas($path, strtolower($method))),
201153
$msg
202154
);
203155

204-
if (isset($headers['Content-Type'][0])) {
156+
if ($schema->isRequestBodyDefined($path, $method) && isset($headers['Content-Type'][0])) {
205157
self::assertRequestContentType(
206-
$spec,
207-
$template,
158+
$schema,
159+
$path,
208160
strtolower($method),
209161
$headers['Content-Type'][0],
210162
$msg
211163
);
212164
}
213165
}
214166

215-
/**
216-
* @param Schema $spec
217-
* @param string $template
218-
* @param string $method
219-
* @param string $status
220-
* @param array $headers
221-
* @param string $msg
222-
*/
223-
final protected static function assertResponseHeaders(Schema $spec, $template, $method, $status, array $headers, $msg = '')
167+
final protected static function assertResponseHeaders(Schema $schema, string $path, string $method, string $status, array $headers, string $msg = ''): void
224168
{
225169
Assert::assertThat(
226170
$headers,
227171
new HeadersConstraint(
228-
$spec->getResponseHeaderSchemas($template, strtolower($method), $status)
172+
$schema->getResponseHeaderSchemas($path, strtolower($method), $status)
229173
),
230174
$msg
231175
);
232176

233-
if (isset($headers['Content-Type'][0])) {
177+
if ($schema->isResponseBodyDefined($path, $method, $status) && isset($headers['Content-Type'][0])) {
234178
self::assertResponseContentType(
235-
$spec,
236-
$template,
179+
$schema,
180+
$path,
237181
$method,
182+
$status,
238183
$headers['Content-Type'][0],
239184
$msg
240185
);
@@ -247,68 +192,47 @@ final protected static function assertResponseHeaders(Schema $spec, $template, $
247192

248193
Assert::assertThat(
249194
$headers['Allow'],
250-
new MethodsAllowedConstraint($spec->getAllowedMethods($template)),
195+
new MethodsAllowedConstraint($schema->getAllowedMethods($path)),
251196
$msg
252197
);
253198
}
254199
}
255200

256-
/**
257-
* @param Schema $spec
258-
* @param string $template
259-
* @param string $method
260-
* @param StreamInterface|null $body
261-
* @param string $msg
262-
*/
263-
final protected static function assertRequestBody(Schema $spec, $template, $method, StreamInterface $body = null, $msg = '')
201+
final protected static function assertRequestBody(Schema $schema, string $path, string $method, StreamInterface $body = null, string $msg = ''): void
264202
{
265-
$schema = $spec->getRequestBodySchema($template, strtolower($method));
203+
$bodySchema = $schema->getRequestBodySchema($path, strtolower($method));
266204

267-
if ($schema) {
205+
if ($bodySchema) {
268206
Assert::assertThat(
269207
json_decode($body),
270-
new JsonSchemaConstraint($schema, 'request body'),
208+
new JsonSchemaConstraint($bodySchema, 'request body'),
271209
$msg
272210
);
273211
} else {
274212
Assert::assertEmpty(json_decode($body), $msg);
275213
}
276214
}
277215

278-
/**
279-
* @param Schema $spec
280-
* @param string $template
281-
* @param string $method
282-
* @param string $status
283-
* @param StreamInterface|null $body
284-
* @param string $msg
285-
*/
286-
final protected static function assertResponseBody(Schema $spec, $template, $method, $status, StreamInterface $body = null, $msg = '')
216+
final protected static function assertResponseBody(Schema $schema, string $path, string $method, string $status, StreamInterface $body = null, string $msg = ''): void
287217
{
288-
$schema = $spec->getResponseBodySchema($template, strtolower($method), $status);
218+
$bodySchema = $schema->getResponseBodySchema($path, strtolower($method), $status);
289219

290-
if ($schema) {
220+
if ($bodySchema) {
291221
Assert::assertThat(
292222
json_decode($body),
293-
new JsonSchemaConstraint($schema, 'response body'),
223+
new JsonSchemaConstraint($bodySchema, 'response body'),
294224
$msg
295225
);
296226
} else {
297227
Assert::assertEmpty(json_decode($body), $msg);
298228
}
299229
}
300230

301-
/**
302-
* @param Schema $spec
303-
* @param string $class
304-
* @param mixed $actual
305-
* @param string $msg
306-
*/
307-
final protected static function assertDefinitionSchema(Schema $spec, $class, $actual, $msg = '')
231+
final protected static function assertDefinitionSchema(Schema $schema, string $class, stdClass $actual, string $msg = ''): void
308232
{
309233
Assert::assertThat(
310234
$actual,
311-
new JsonSchemaConstraint($spec->getDefinition($class)),
235+
new JsonSchemaConstraint($schema->getDefinition($class)),
312236
$msg
313237
);
314238
}

src/PhpUnit/HeadersConstraint.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public function toString(): string
7676

7777
private static function normalizeJsonSchema($schema): stdClass
7878
{
79-
return (object) $schema;
79+
return json_decode(json_encode($schema));
8080
}
8181

8282
/**

0 commit comments

Comments
 (0)