-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathResponse.php
More file actions
292 lines (249 loc) · 6.38 KB
/
Copy pathResponse.php
File metadata and controls
292 lines (249 loc) · 6.38 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
<?php
declare(strict_types=1);
namespace Bow\Testing;
use Bow\Http\Client\Response as HttpClientResponse;
use InvalidArgumentException;
use JsonException;
class Response
{
/**
* The http http_response
*
* @var HttpClientResponse
*/
private HttpClientResponse $http_response;
/**
* The http_response content
*
* @var string
*/
private string $content;
/**
* Behavior constructor.
*
* @param HttpClientResponse $http_response
*/
public function __construct(HttpClientResponse $http_response)
{
$this->http_response = $http_response;
$this->content = $http_response->getContent();
}
/**
* Get the response content
*
* @return string
*/
public function getContent(): string
{
return $this->content;
}
/**
* Check if the content is json format
*
* @param string $message
* @return Response
*/
public function assertJson(string $message = ''): Response
{
Assert::assertJson(json_encode($this->content), $message);
return $this;
}
/**
* Check if the content is json format and the parsed data is
* some to the content
*
* @param array $data
* @param string $message
* @return Response
*/
public function assertExactJson(array $data, string $message = ''): Response
{
$response = $this->http_response->toJson(true);
foreach ($response as $key => $value) {
Assert::assertArrayHasKey($key, $data, $message);
Assert::assertEquals($value, $data[$key], $message);
}
return $this;
}
/**
* Check if the content is some parse data
*
* @param string $data
* @param string $message
*
* @return Response
*/
public function assertContainsExactText(string $data, string $message = ''): Response
{
Assert::assertEquals($this->content, $data, $message);
return $this;
}
/**
* Check if the header exists
*
* @param string $header
* @param string $message
*
* @return Response
*/
public function assertHeader(string $header, string $message = ''): Response
{
Assert::assertArrayHasKey($header, $this->http_response->getHeaders(), $message);
return $this;
}
/**
* Check if the content is array format
*
* @param string $message
*
* @return Response
*/
public function assertArray(string $message = ''): Response
{
Assert::assertTrue(is_array($this->http_response->toArray()), $message);
return $this;
}
/**
* Get the response content as array
*
* @return array|object
* @throws JsonException
*/
public function toArray(): array|object
{
return json_decode($this->content, true, 1024, JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE);
}
/**
* Check if the content type is application/json
*
* @param string $message
*
* @return Response
*/
public function assertContentTypeJson(string $message = ''): Response
{
$this->assertContentType('application/json', $message);
return $this;
}
/**
* Check the content type
*
* @param string $content_type
* @param string $message
*
* @return Response
*/
public function assertContentType(string $content_type, string $message = ''): Response
{
$type = $this->http_response->getContentType();
Assert::assertEquals(
$content_type,
current(preg_split('/;(\s+)?/', $type)),
$message
);
return $this;
}
/**
* Check if the content type is text/plain
*
* @param string $message
*
* @return Response
*/
public function assertContentTypeText(string $message = ''): Response
{
$this->assertContentType('text/plain', $message);
return $this;
}
/**
* Check if the content type is text/html
*
* @param string $message
*
* @return Response
*/
public function assertContentTypeHtml(string $message = ''): Response
{
$this->assertContentType('text/html', $message);
return $this;
}
/**
* Check if the content type is text/xml
*
* @param string $message
*
* @return Response
*/
public function assertContentTypeXml(string $message = ''): Response
{
$this->assertContentType('text/xml', $message);
return $this;
}
/**
* Check the status code
*
* @param int $code
* @param string $message
* @return Response
*/
public function assertStatus(int $code, string $message = ''): Response
{
Assert::assertEquals($this->http_response->getCode(), $code, $message);
return $this;
}
/**
* @param string $key
* @param string $message
* @return Response
*/
public function assertKeyExists(string $key, string $message = ''): Response
{
$data = $this->http_response->toArray();
Assert::assertTrue(isset($data[$key]), $message);
return $this;
}
/**
* @param string|int $key
* @param string $value
* @param string $message
*
* @return Response
*/
public function assertKeyMatchValue(string|int $key, mixed $value, string $message = ''): Response
{
$data = json_encode($this->content);
if (isset($data[$key])) {
Assert::assertFalse(true);
} else {
Assert::assertEquals($data[$key], $value, $message);
}
return $this;
}
/**
* Check if the content contains the parsed text
*
* @param string $text
* @return Response
*/
public function assertContains(string $text): Response
{
Assert::assertContains($text, $this->content);
return $this;
}
/**
* __call
*
* @param string $method
* @param array $params
* @return mixed
*/
public function __call(string $method, array $params = [])
{
if (method_exists($this->http_response, $method)) {
return call_user_func([$this->http_response, $method]);
}
throw new InvalidArgumentException(
"The method [$method] is not exists"
);
}
}