forked from codeigniter4/CodeIgniter4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestResponse.php
More file actions
495 lines (418 loc) · 13.5 KB
/
Copy pathTestResponse.php
File metadata and controls
495 lines (418 loc) · 13.5 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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
<?php
declare(strict_types=1);
/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace CodeIgniter\Test;
use CodeIgniter\HTTP\RedirectResponse;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use CodeIgniter\I18n\Time;
use PHPUnit\Framework\Assert;
use PHPUnit\Framework\Constraint\IsEqual;
/**
* Consolidated response processing
* for test results.
*
* @mixin DOMParser
*
* @see \CodeIgniter\Test\TestResponseTest
*/
class TestResponse
{
/**
* The request.
*
* @var RequestInterface|null
*/
protected $request;
/**
* The response.
*
* @var ResponseInterface
*/
protected $response;
/**
* DOM for the body.
*
* @var DOMParser
*/
protected $domParser;
/**
* Stores or the Response and parses the body in the DOM.
*/
public function __construct(ResponseInterface $response)
{
$this->setResponse($response);
}
// --------------------------------------------------------------------
// Getters / Setters
// --------------------------------------------------------------------
/**
* Sets the request.
*
* @return $this
*/
public function setRequest(RequestInterface $request)
{
$this->request = $request;
return $this;
}
/**
* Sets the Response and updates the DOM.
*
* @return $this
*/
public function setResponse(ResponseInterface $response)
{
$this->response = $response;
$this->domParser = new DOMParser();
$body = $response->getBody();
if (is_string($body) && $body !== '') {
$this->domParser->withString($body);
}
return $this;
}
/**
* Request accessor.
*
* @return RequestInterface|null
*/
public function request()
{
return $this->request;
}
/**
* Response accessor.
*
* @return ResponseInterface
*/
public function response()
{
return $this->response;
}
// --------------------------------------------------------------------
// Status Checks
// --------------------------------------------------------------------
/**
* Boils down the possible responses into a boolean valid/not-valid
* response type.
*/
public function isOK(): bool
{
$status = $this->response->getStatusCode();
// Only 200 and 300 range status codes
// are considered valid.
if ($status >= 400 || $status < 200) {
return false;
}
$body = (string) $this->response->getBody();
// Empty bodies are not considered valid, unless in redirects
return ! ($status < 300 && $body === '');
}
/**
* Asserts that the status is a specific value.
*/
public function assertStatus(int $code): void
{
Assert::assertSame($code, $this->response->getStatusCode());
}
/**
* Asserts that the Response is considered OK.
*/
public function assertOK(): void
{
Assert::assertTrue(
$this->isOK(),
"{$this->response->getStatusCode()} is not a successful status code, or Response has an empty body.",
);
}
/**
* Asserts that the Response is considered not OK.
*/
public function assertNotOK(): void
{
Assert::assertFalse(
$this->isOK(),
"{$this->response->getStatusCode()} is an unexpected successful status code, or Response body has content.",
);
}
// --------------------------------------------------------------------
// Redirection
// --------------------------------------------------------------------
/**
* Returns whether or not the Response was a redirect or RedirectResponse
*/
public function isRedirect(): bool
{
return $this->response instanceof RedirectResponse
|| $this->response->hasHeader('Location')
|| $this->response->hasHeader('Refresh');
}
/**
* Assert that the given response was a redirect.
*/
public function assertRedirect(): void
{
Assert::assertTrue($this->isRedirect(), 'Response is not a redirect or instance of RedirectResponse.');
}
/**
* Assert that a given response was a redirect
* and it was redirect to a specific URI.
*/
public function assertRedirectTo(string $uri): void
{
$this->assertRedirect();
$uri = trim(strtolower($uri));
$redirectUri = strtolower($this->getRedirectUrl());
$matches = $uri === $redirectUri
|| strtolower(site_url($uri)) === $redirectUri
|| $uri === site_url($redirectUri);
Assert::assertTrue($matches, "Redirect URL '{$uri}' does not match '{$redirectUri}'.");
}
/**
* Assert that the given response was not a redirect.
*/
public function assertNotRedirect(): void
{
Assert::assertFalse($this->isRedirect(), 'Response is an unexpected redirect or instance of RedirectResponse.');
}
/**
* Returns the URL set for redirection.
*/
public function getRedirectUrl(): ?string
{
if (! $this->isRedirect()) {
return null;
}
if ($this->response->hasHeader('Location')) {
return $this->response->getHeaderLine('Location');
}
if ($this->response->hasHeader('Refresh')) {
return str_replace('0;url=', '', $this->response->getHeaderLine('Refresh'));
}
return null;
}
// --------------------------------------------------------------------
// Session
// --------------------------------------------------------------------
/**
* Asserts that an SESSION key has been set and, optionally, test its value.
*
* @param mixed $value
*/
public function assertSessionHas(string $key, $value = null): void
{
Assert::assertArrayHasKey($key, $_SESSION, "Key '{$key}' is not in the current \$_SESSION");
if ($value === null) {
return;
}
if (is_scalar($value)) {
Assert::assertSame($value, $_SESSION[$key], "The value of key '{$key}' ({$value}) does not match expected value.");
return;
}
Assert::assertSame($value, $_SESSION[$key], "The value of key '{$key}' does not match expected value.");
}
/**
* Asserts the session is missing $key.
*/
public function assertSessionMissing(string $key): void
{
Assert::assertArrayNotHasKey($key, $_SESSION, "Key '{$key}' should not be present in \$_SESSION.");
}
// --------------------------------------------------------------------
// Headers
// --------------------------------------------------------------------
/**
* Asserts that the Response contains a specific header.
*
* @param string|null $value
*/
public function assertHeader(string $key, $value = null): void
{
Assert::assertTrue($this->response->hasHeader($key), "Header '{$key}' is not a valid Response header.");
if ($value !== null) {
Assert::assertSame(
$value,
$this->response->getHeaderLine($key),
"The value of '{$key}' header ({$this->response->getHeaderLine($key)}) does not match expected value.",
);
}
}
/**
* Asserts the Response headers does not contain the specified header.
*/
public function assertHeaderMissing(string $key): void
{
Assert::assertFalse($this->response->hasHeader($key), "Header '{$key}' should not be in the Response headers.");
}
// --------------------------------------------------------------------
// Cookies
// --------------------------------------------------------------------
/**
* Asserts that the response has the specified cookie.
*
* @param string|null $value
*/
public function assertCookie(string $key, $value = null, string $prefix = ''): void
{
Assert::assertTrue($this->response->hasCookie($key, $value, $prefix), "Cookie named '{$key}' is not found.");
}
/**
* Assert the Response does not have the specified cookie set.
*/
public function assertCookieMissing(string $key): void
{
Assert::assertFalse($this->response->hasCookie($key), "Cookie named '{$key}' should not be set.");
}
/**
* Asserts that a cookie exists and has an expired time.
*/
public function assertCookieExpired(string $key, string $prefix = ''): void
{
Assert::assertTrue($this->response->hasCookie($key, null, $prefix));
Assert::assertGreaterThan(
Time::now()->getTimestamp(),
$this->response->getCookie($key, $prefix)->getExpiresTimestamp(),
);
}
// --------------------------------------------------------------------
// JSON
// --------------------------------------------------------------------
/**
* Returns the response's body as JSON
*
* @return false|string
*/
public function getJSON()
{
$response = $this->response->getJSON();
if ($response === null) {
return false;
}
return $response;
}
/**
* Test that the response contains a matching JSON fragment.
*/
public function assertJSONFragment(array $fragment, bool $strict = false): void
{
$json = json_decode($this->getJSON(), true);
Assert::assertIsArray($json, 'Response is not a valid JSON.');
$patched = array_replace_recursive($json, $fragment);
if ($strict) {
Assert::assertSame($json, $patched, 'Response does not contain a matching JSON fragment.');
return;
}
Assert::assertThat($patched, new IsEqual($json), 'Response does not contain a matching JSON fragment.');
}
/**
* Asserts that the JSON exactly matches the passed in data.
* If the value being passed in is a string, it must be a json_encoded string.
*
* @param array|object|string $test
*/
public function assertJSONExact($test): void
{
$json = $this->getJSON();
if (is_object($test)) {
$test = method_exists($test, 'toArray') ? $test->toArray() : (array) $test;
}
if (is_array($test)) {
$test = service('format')->getFormatter('application/json')->format($test);
}
Assert::assertJsonStringEqualsJsonString($test, $json, 'Response does not contain matching JSON.');
}
// --------------------------------------------------------------------
// XML Methods
// --------------------------------------------------------------------
/**
* Returns the response' body as XML
*
* @return bool|string|null
*/
public function getXML()
{
return $this->response->getXML();
}
// --------------------------------------------------------------------
// DomParser
// --------------------------------------------------------------------
/**
* Assert that the desired text can be found in the result body.
*/
public function assertSee(?string $search = null, ?string $element = null): void
{
Assert::assertTrue(
$this->domParser->see($search, $element),
"Text '{$search}' is not seen in response.",
);
}
/**
* Asserts that we do not see the specified text.
*/
public function assertDontSee(?string $search = null, ?string $element = null): void
{
Assert::assertTrue(
$this->domParser->dontSee($search, $element),
"Text '{$search}' is unexpectedly seen in response.",
);
}
/**
* Assert that we see an element selected via a CSS selector.
*/
public function assertSeeElement(string $search): void
{
Assert::assertTrue(
$this->domParser->seeElement($search),
"Element with selector '{$search}' is not seen in response.",
);
}
/**
* Assert that we do not see an element selected via a CSS selector.
*/
public function assertDontSeeElement(string $search): void
{
Assert::assertTrue(
$this->domParser->dontSeeElement($search),
"Element with selector '{$search}' is unexpectedly seen in response.'",
);
}
/**
* Assert that we see a link with the matching text and/or class.
*/
public function assertSeeLink(string $text, ?string $details = null): void
{
Assert::assertTrue(
$this->domParser->seeLink($text, $details),
"Anchor tag with text '{$text}' is not seen in response.",
);
}
/**
* Assert that we see an input with name/value.
*/
public function assertSeeInField(string $field, ?string $value = null): void
{
Assert::assertTrue(
$this->domParser->seeInField($field, $value),
"Input named '{$field}' with value '{$value}' is not seen in response.",
);
}
/**
* Forward any unrecognized method calls to our DOMParser instance.
*
* @param list<mixed> $params
*/
public function __call(string $function, array $params): mixed
{
if (method_exists($this->domParser, $function)) {
return $this->domParser->{$function}(...$params);
}
return null;
}
}