-
Notifications
You must be signed in to change notification settings - Fork 26
/
Psr7AssertsTrait.php
150 lines (132 loc) · 3.9 KB
/
Psr7AssertsTrait.php
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
<?php
declare(strict_types=1);
namespace FR3D\SwaggerAssertions\PhpUnit;
use FR3D\SwaggerAssertions\SchemaManager;
use PHPUnit\Framework\ExpectationFailedException;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
/**
* Facade functions for interact with PSR7 Interfaces.
*/
trait Psr7AssertsTrait
{
use AssertsTrait;
/**
* Asserts response match with the response schema.
*
* @param string $path percent-encoded path used on the request.
*/
public static function assertResponseMatch(
ResponseInterface $response,
SchemaManager $schemaManager,
string $path,
string $httpMethod,
string $message = ''
): void {
if (!empty((string) $response->getBody())) {
self::assertResponseMediaTypeMatch(
$response->getHeaderLine('Content-Type'),
$schemaManager,
$path,
$httpMethod,
$message
);
}
$httpCode = $response->getStatusCode();
$headers = self::inlineHeaders($response->getHeaders());
self::assertResponseHeadersMatch(
$headers,
$schemaManager,
$path,
$httpMethod,
$httpCode,
$message
);
self::assertResponseBodyMatch(
json_decode((string) $response->getBody()),
$schemaManager,
$path,
$httpMethod,
$httpCode,
$message
);
}
/**
* Asserts request match with the request schema.
*/
public static function assertRequestMatch(
RequestInterface $request,
SchemaManager $schemaManager,
string $message = ''
): void {
$path = $request->getUri()->getPath();
$httpMethod = $request->getMethod();
$headers = self::inlineHeaders($request->getHeaders());
$queryString = $request->getUri()->getQuery();
parse_str(html_entity_decode($queryString), $query);
self::assertRequestHeadersMatch(
$headers,
$schemaManager,
$path,
$httpMethod,
$message
);
if (!empty((string) $request->getBody())) {
self::assertRequestMediaTypeMatch(
$request->getHeaderLine('Content-Type'),
$schemaManager,
$path,
$httpMethod,
$message
);
}
self::assertRequestQueryMatch(
$query,
$schemaManager,
$path,
$httpMethod,
$message
);
self::assertRequestBodyMatch(
json_decode((string) $request->getBody()),
$schemaManager,
$path,
$httpMethod,
$message
);
}
/**
* Asserts response match with the response schema.
*/
public static function assertResponseAndRequestMatch(
ResponseInterface $response,
RequestInterface $request,
SchemaManager $schemaManager,
string $message = ''
): void {
try {
self::assertRequestMatch($request, $schemaManager, $message);
} catch (ExpectationFailedException $e) {
// If response represent a Client error then ignore.
$statusCode = $response->getStatusCode();
if ($statusCode < 400 || $statusCode > 499) {
throw $e;
}
}
self::assertResponseMatch($response, $schemaManager, $request->getUri()->getPath(), $request->getMethod(), $message);
}
/**
* @param string[] $headers
*
* @return string[]
*/
protected static function inlineHeaders(array $headers): array
{
return array_map(
function (array $headers) {
return implode(', ', $headers);
},
$headers
);
}
}