1616use Psr \Http \Message \StreamInterface ;
1717use Psr \Http \Message \UriInterface ;
1818use 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 }
0 commit comments