Completed
Push — master ( e625a1...0d2a97 )
by Veaceslav
02:33
created

Asserts::assertRequestContentType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 5
crap 1
1
<?php
2
/**
3
 * This file is part of Rebilly.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 *
8
 * @see http://rebilly.com
9
 */
10
11
namespace Rebilly\OpenAPI\PhpUnit;
12
13
use PHPUnit\Framework\Assert;
14
use Psr\Http\Message\RequestInterface;
15
use Psr\Http\Message\ResponseInterface;
16
use Psr\Http\Message\StreamInterface;
17
use Psr\Http\Message\UriInterface;
18
use Rebilly\OpenAPI\Schema;
19
use stdClass;
20
21
/**
22
 * Asserts data against OpenAPI specification.
23
 */
24
trait Asserts
25
{
26
    /**
27
     * Assert request matches against declared specification.
28
     *
29
     * The list of constraints:
30
     *
31
     * - Assert request method defined
32
     * - Assert request URI declared by host, basePath, schemes and parameters (path, query)
33
     * - Assert content-type declared by consumes
34
     * - Assert headers declared by parameters (header)
35
     * - Assert body declared by parameters (body)
36
     */
37 4
    final protected static function assertRequest(Schema $schema, string $path, RequestInterface $request, string $msg = ''): void
38
    {
39 4
        self::assertMethodAllowed($schema, $path, $request->getMethod(), $msg);
40 3
        self::assertUri($schema, $path, $request->getMethod(), $request->getUri(), $msg);
41 3
        self::assertRequestHeaders($schema, $path, $request->getMethod(), $request->getHeaders(), $msg);
42 2
        self::assertRequestBody($schema, $path, $request->getMethod(), $request->getBody(), $msg);
43
    }
44
45
    /**
46
     * Assert response matches against declared specification.
47
     *
48
     * The list of constraints:
49
     *
50
     * - Assert response status code or default is defined
51
     * - Assert content-type declared by produces from operation
52
     * - Assert headers
53
     * - Assert body
54
     */
55 3
    final protected static function assertResponse(Schema $schema, string $path, string $method, ResponseInterface $response, string $msg = ''): void
56
    {
57 3
        self::assertResponseDefined($schema, $path, $method, $response->getStatusCode(), $msg);
58 3
        self::assertResponseHeaders(
59 3
            $schema,
60 3
            $path,
61 3
            $method,
62 3
            $response->getStatusCode(),
63 3
            $response->getHeaders(),
64 3
            $msg
65
        );
66 3
        self::assertResponseBody(
67 3
            $schema,
68 3
            $path,
69 3
            $method,
70 3
            $response->getStatusCode(),
71 3
            $response->getBody(),
72 3
            $msg
73
        );
74
    }
75
76
    /**
77
     * Assert URI matches against declared host, basePath, schemes and parameters (path, query).
78
     *
79
     * The list of constraints:
80
     *
81
     * - Assert URI scheme matches allowed schemes
82
     * - Assert URI host matches defined
83
     * - Assert URI path starts with defined base path
84
     * - Assert URI path matches defined template and path parameters
85
     * - Assert URI path matches defined query parameters
86
     */
87 3
    final protected static function assertUri(Schema $schema, string $path, string $method, UriInterface $uri, string $msg = ''): void
88
    {
89 3
        Assert::assertThat(
90 3
            $uri,
91 3
            new UriConstraint(
92 3
                $schema->getServers(),
93 3
                $path,
94 3
                $schema->getRequestPathParameters($path, $method),
95 3
                $schema->getRequestQueryParameters($path, $method)
96
            ),
97 3
            $msg
98
        );
99
    }
100
101
    /**
102
     * Assert the endpoint supports given operation.
103
     */
104 4
    final protected static function assertMethodAllowed(Schema $schema, string $path, string $method, string $msg = ''): void
105
    {
106 4
        Assert::assertThat(
107 4
            $method,
108 4
            new MethodsAllowedConstraint($schema->getAllowedMethods($path)),
109 4
            $msg
110
        );
111
    }
112
113
    /**
114
     * Assert the response status code defined.
115
     */
116 3
    final protected static function assertResponseDefined(Schema $schema, string $template, string $method, string $status, string $msg = ''): void
117
    {
118 3
        Assert::assertTrue(
119 3
            $schema->isResponseDefined($template, strtolower($method), $status),
120 3
            $msg ?: "Operation \"{$method} {$template}\" does not support response code \"{$status}\""
121
        );
122
    }
123
124
    /**
125
     * Assert the endpoint supports given operation.
126
     */
127 2
    final protected static function assertRequestContentType(Schema $schema, string $path, string $method, string $contentType, string $msg = ''): void
128
    {
129 2
        Assert::assertThat(
130 2
            $contentType,
131 2
            new ContentTypeConstraint($schema->getRequestContentTypes($path, $method)),
132 2
            $msg
133
        );
134
    }
135
136
    /**
137
     * Assert the endpoint supports given operation.
138
     */
139 2
    final protected static function assertResponseContentType(Schema $schema, string $path, string $method, string $status, string $contentType, string $msg = ''): void
140
    {
141 2
        Assert::assertThat(
142 2
            $contentType,
143 2
            new ContentTypeConstraint($schema->getResponseContentTypes($path, $method, $status)),
144 2
            $msg
145
        );
146
    }
147
148 3
    final protected static function assertRequestHeaders(Schema $schema, string $path, string $method, array $headers, string $msg = ''): void
149
    {
150 3
        Assert::assertThat(
151 3
            $headers,
152 3
            new HeadersConstraint($schema->getRequestHeaderSchemas($path, strtolower($method))),
153 3
            $msg
154
        );
155
156 3
        if ($schema->isRequestBodyDefined($path, $method) && isset($headers['Content-Type'][0])) {
157 2
            self::assertRequestContentType(
158 2
                $schema,
159 2
                $path,
160 2
                strtolower($method),
161 2
                $headers['Content-Type'][0],
162 2
                $msg
163
            );
164
        }
165
    }
166
167 3
    final protected static function assertResponseHeaders(Schema $schema, string $path, string $method, string $status, array $headers, string $msg = ''): void
168
    {
169 3
        Assert::assertThat(
170 3
            $headers,
171 3
            new HeadersConstraint(
172 3
                $schema->getResponseHeaderSchemas($path, strtolower($method), $status)
173
            ),
174 3
            $msg
175
        );
176
177 3
        if ($schema->isResponseBodyDefined($path, $method, $status) && isset($headers['Content-Type'][0])) {
178 2
            self::assertResponseContentType(
179 2
                $schema,
180 2
                $path,
181 2
                $method,
182 2
                $status,
183 2
                $headers['Content-Type'][0],
184 2
                $msg
185
            );
186
        }
187
188 3
        if (isset($headers['Allow'])) {
189 1
            if (isset($headers['Allow'][0]) && strpos($headers['Allow'][0], ',') !== false) {
190 1
                $headers['Allow'] = preg_split('#\s*,\s*#', $headers['Allow'][0], -1, PREG_SPLIT_NO_EMPTY);
191
            }
192
193 1
            Assert::assertThat(
194 1
                $headers['Allow'],
195 1
                new MethodsAllowedConstraint($schema->getAllowedMethods($path)),
196 1
                $msg
197
            );
198
        }
199
    }
200
201 2 View Code Duplication
    final protected static function assertRequestBody(Schema $schema, string $path, string $method, StreamInterface $body = null, string $msg = ''): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
202
    {
203 2
        $bodySchema = $schema->getRequestBodySchema($path, strtolower($method));
204
205 2
        if ($bodySchema) {
206 1
            Assert::assertThat(
207 1
                json_decode($body),
208 1
                new JsonSchemaConstraint($bodySchema, 'request body'),
209 1
                $msg
210
            );
211
        } else {
212 1
            Assert::assertEmpty(json_decode($body), $msg);
213
        }
214
    }
215
216 3 View Code Duplication
    final protected static function assertResponseBody(Schema $schema, string $path, string $method, string $status, StreamInterface $body = null, string $msg = ''): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
217
    {
218 3
        $bodySchema = $schema->getResponseBodySchema($path, strtolower($method), $status);
219
220 3
        if ($bodySchema) {
221 2
            Assert::assertThat(
222 2
                json_decode($body),
223 2
                new JsonSchemaConstraint($bodySchema, 'response body'),
224 2
                $msg
225
            );
226
        } else {
227 1
            Assert::assertEmpty(json_decode($body), $msg);
228
        }
229
    }
230
231 1
    final protected static function assertDefinitionSchema(Schema $schema, string $class, stdClass $actual, string $msg = ''): void
232
    {
233 1
        Assert::assertThat(
234 1
            $actual,
235 1
            new JsonSchemaConstraint($schema->getDefinition($class)),
236 1
            $msg
237
        );
238
    }
239
}
240