-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathResponseAsserts.php
142 lines (124 loc) · 4.11 KB
/
ResponseAsserts.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
<?php
// This file is part of Minz.
// Copyright 2020-2024 Marien Fressinaud
// SPDX-License-Identifier: AGPL-3.0-or-later
namespace Minz\Tests;
use Minz\Response;
/**
* Provide some assert methods to help to test the response.
*
* @see \Minz\Response
*
* @phpstan-import-type ResponseHttpCode from Response
*
* @phpstan-import-type ResponseHeaders from Response
*
* @phpstan-import-type ResponseReturnable from Response
*
* @phpstan-import-type ViewPointer from \Minz\Output\View
*/
trait ResponseAsserts
{
/**
* Assert that a Response code is matching the given one.
*
* A location can be given to test the redirections destinations.
*
* @param ResponseReturnable $response
* @param ResponseHttpCode $code
*/
public function assertResponseCode(mixed $response, int $code, ?string $location = null): void
{
if ($response instanceof \Generator) {
$response = $response->current();
}
$this->assertEquals($code, $response->code());
if ($location !== null && ($code === 301 || $code === 302)) {
$response_headers = $response->headers(true);
$this->assertEquals($location, $response_headers['Location']);
}
}
/**
* Assert that a Response output equals to the given string
*
* @param ResponseReturnable $response
*/
public function assertResponseEquals(mixed $response, string $output): void
{
if ($response instanceof \Generator) {
$response = $response->current();
}
$this->assertEquals($output, $response->render());
}
/**
* Assert that a Response output contains the given string
*
* @param ResponseReturnable $response
*/
public function assertResponseContains(mixed $response, string $output): void
{
if ($response instanceof \Generator) {
$response = $response->current();
}
$this->assertStringContainsString($output, $response->render());
}
/**
* Assert that a Response output contains the given string (ignoring
* differences in casing).
*
* @param ResponseReturnable $response
*/
public function assertResponseContainsIgnoringCase(mixed $response, string $output): void
{
if ($response instanceof \Generator) {
$response = $response->current();
}
$this->assertStringContainsStringIgnoringCase($output, $response->render());
}
/**
* Assert that a Response output doesn’t contain the given string
*
* @param ResponseReturnable $response
*/
public function assertResponseNotContains(mixed $response, string $output): void
{
if ($response instanceof \Generator) {
$response = $response->current();
}
$this->assertStringNotContainsString($output, $response->render());
}
/**
* Assert that a Response declares the given headers.
*
* @param ResponseReturnable $response
* @param ResponseHeaders $headers
*/
public function assertResponseHeaders(mixed $response, array $headers): void
{
if ($response instanceof \Generator) {
$response = $response->current();
}
$response_headers = $response->headers(true);
foreach ($headers as $header => $value) {
$this->assertArrayHasKey($header, $response_headers);
$this->assertEquals($value, $response_headers[$header]);
}
}
/**
* Assert that a Response output is set with the given pointer.
*
* @param ResponseReturnable $response
* @param ViewPointer $expected_pointer
*/
public function assertResponsePointer(mixed $response, string $expected_pointer): void
{
if ($response instanceof \Generator) {
$response = $response->current();
}
$output = $response->output();
$this->assertNotNull($output, 'Response has no output');
$this->assertTrue(is_callable([$output, 'pointer']), 'Response has no pointer');
$pointer = $output->pointer();
$this->assertEquals($expected_pointer, $pointer);
}
}