|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace MyTester; |
|
6
|
|
|
|
|
7
|
|
|
trait TAssertions |
|
8
|
|
|
{ |
|
9
|
|
|
/** @internal */ |
|
10
|
|
|
protected int $taskCount = 0; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Prints result of a test |
|
14
|
|
|
* |
|
15
|
|
|
* @internal |
|
16
|
|
|
*/ |
|
17
|
1 |
|
protected function testResult(string $text, bool $success = true): void |
|
18
|
|
|
{ |
|
19
|
1 |
|
$this->incCounter(); |
|
20
|
1 |
|
if ($success) { |
|
21
|
1 |
|
return; |
|
22
|
|
|
} |
|
23
|
|
|
echo "Test $this->taskCount failed. $text\n"; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Increases task counter |
|
28
|
|
|
* |
|
29
|
|
|
* @internal |
|
30
|
|
|
*/ |
|
31
|
1 |
|
protected function incCounter(): void |
|
32
|
|
|
{ |
|
33
|
1 |
|
$this->taskCount++; |
|
34
|
1 |
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Resets task counter |
|
38
|
|
|
* |
|
39
|
|
|
* @internal |
|
40
|
|
|
*/ |
|
41
|
1 |
|
protected function resetCounter(): void |
|
42
|
|
|
{ |
|
43
|
1 |
|
$this->taskCount = 0; |
|
44
|
1 |
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @internal |
|
48
|
|
|
*/ |
|
49
|
1 |
|
protected function getCounter(): int |
|
50
|
|
|
{ |
|
51
|
1 |
|
return $this->taskCount; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
protected function showStringOrArray(string|array $variable): string |
|
55
|
|
|
{ |
|
56
|
|
|
return (is_string($variable) ? $variable : "(array)"); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Tries an assertion |
|
61
|
|
|
*/ |
|
62
|
1 |
|
protected function assert(mixed $code, string $failureText = ""): void |
|
63
|
|
|
{ |
|
64
|
1 |
|
$success = ($code == true); |
|
65
|
1 |
|
$message = ""; |
|
66
|
1 |
|
if ($code != true) { |
|
67
|
|
|
$message = ($failureText === "") ? "Assertion \"$code\" is not true." : $failureText; |
|
68
|
|
|
} |
|
69
|
1 |
|
$this->testResult($message, $success); |
|
70
|
1 |
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Are both values same? |
|
74
|
|
|
*/ |
|
75
|
1 |
|
protected function assertSame(mixed $expected, mixed $actual): void |
|
76
|
|
|
{ |
|
77
|
1 |
|
$success = ($expected == $actual); |
|
78
|
1 |
|
$message = ($success) ? "" : "The value is not $expected but $actual."; |
|
79
|
1 |
|
$this->testResult($message, $success); |
|
80
|
1 |
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Are not both values same? |
|
84
|
|
|
*/ |
|
85
|
1 |
|
protected function assertNotSame(mixed $expected, mixed $actual): void |
|
86
|
|
|
{ |
|
87
|
1 |
|
$success = ($expected !== $actual); |
|
88
|
1 |
|
$message = ($success) ? "" : "The value is $expected."; |
|
89
|
1 |
|
$this->testResult($message, $success); |
|
90
|
1 |
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Is $actual equal to true? |
|
94
|
|
|
*/ |
|
95
|
1 |
|
protected function assertTrue(bool $actual): void |
|
96
|
|
|
{ |
|
97
|
1 |
|
$success = ($actual); |
|
98
|
1 |
|
$message = ($success) ? "" : "The value is not true."; |
|
99
|
1 |
|
$this->testResult($message, $success); |
|
100
|
1 |
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Is the expression true? |
|
104
|
|
|
*/ |
|
105
|
1 |
|
protected function assertTruthy(mixed $actual): void |
|
106
|
|
|
{ |
|
107
|
1 |
|
$success = ($actual == true); |
|
108
|
1 |
|
$message = ($success) ? "" : "The expression is not true."; |
|
109
|
1 |
|
$this->testResult($message, $success); |
|
110
|
1 |
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Is $actual equal to false? |
|
114
|
|
|
*/ |
|
115
|
1 |
|
protected function assertFalse(bool $actual): void |
|
116
|
|
|
{ |
|
117
|
1 |
|
$success = (!$actual); |
|
118
|
1 |
|
$message = ($success) ? "" : "The value is not false."; |
|
119
|
1 |
|
$this->testResult($message, $success); |
|
120
|
1 |
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Is the expression false? |
|
124
|
|
|
*/ |
|
125
|
1 |
|
protected function assertFalsey(mixed $actual): void |
|
126
|
|
|
{ |
|
127
|
1 |
|
$success = ($actual == false); |
|
128
|
1 |
|
$message = ($success) ? "" : "The expression is not false."; |
|
129
|
1 |
|
$this->testResult($message, $success); |
|
130
|
1 |
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Is the value null? |
|
134
|
|
|
*/ |
|
135
|
1 |
|
protected function assertNull(mixed $actual): void |
|
136
|
|
|
{ |
|
137
|
1 |
|
$success = ($actual == null); |
|
138
|
1 |
|
$message = ($success) ? "" : "The value is not null."; |
|
139
|
1 |
|
$this->testResult($message, $success); |
|
140
|
1 |
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Is not the value null? |
|
144
|
|
|
*/ |
|
145
|
1 |
|
protected function assertNotNull(mixed $actual): void |
|
146
|
|
|
{ |
|
147
|
1 |
|
$success = ($actual !== null); |
|
148
|
1 |
|
$message = ($success) ? "" : "The value is null."; |
|
149
|
1 |
|
$this->testResult($message, $success); |
|
150
|
1 |
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Does $actual contain $needle? |
|
154
|
|
|
*/ |
|
155
|
1 |
|
protected function assertContains(string|array $needle, string|array $actual): void |
|
156
|
|
|
{ |
|
157
|
1 |
|
if (is_string($actual) && is_string($needle)) { |
|
158
|
1 |
|
$success = ($needle !== "" && str_contains($actual, $needle)); |
|
159
|
1 |
|
if ($success) { |
|
160
|
1 |
|
$this->testResult(""); |
|
161
|
|
|
} else { |
|
162
|
1 |
|
$this->testResult("$needle is not in the variable.", false); |
|
163
|
|
|
} |
|
164
|
1 |
|
} elseif (is_array($actual)) { |
|
165
|
1 |
|
$success = (in_array($needle, $actual)); |
|
166
|
1 |
|
$message = ($success) ? "" : $this->showStringOrArray($needle) . " is not in the variable."; |
|
167
|
1 |
|
$this->testResult($message, $success); |
|
168
|
|
|
} else { |
|
169
|
|
|
$this->testResult($this->showStringOrArray($needle) . " is not in the variable.", false); |
|
170
|
|
|
} |
|
171
|
1 |
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* Does $actual not contain $needle? |
|
175
|
|
|
*/ |
|
176
|
1 |
|
protected function assertNotContains(string|array $needle, string|array $actual): void |
|
177
|
|
|
{ |
|
178
|
1 |
|
if (is_string($actual) && is_string($needle)) { |
|
179
|
|
|
$success = ($needle === "" || !str_contains($actual, $needle)); |
|
180
|
|
|
$message = ($success) ? "" : "$needle is in the variable."; |
|
181
|
|
|
$this->testResult($message, $success); |
|
182
|
1 |
|
} elseif (is_array($actual)) { |
|
183
|
1 |
|
$success = (!in_array($needle, $actual)); |
|
184
|
1 |
|
$message = ($success) ? "" : $this->showStringOrArray($needle) . " is in the variable."; |
|
185
|
1 |
|
$this->testResult($message, $success); |
|
186
|
|
|
} else { |
|
187
|
|
|
$this->testResult($this->showStringOrArray($needle) . " is not in the variable.", false); |
|
188
|
|
|
} |
|
189
|
1 |
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* Does $value contain $count items? |
|
193
|
|
|
*/ |
|
194
|
1 |
|
protected function assertCount(int $count, string|array|\Countable $value): void |
|
195
|
|
|
{ |
|
196
|
1 |
|
if (!is_array($value) && !$value instanceof \Countable) { |
|
197
|
|
|
$this->testResult("The variable is not array or countable object.", false); |
|
198
|
|
|
return; |
|
199
|
|
|
} |
|
200
|
1 |
|
$actual = count($value); |
|
201
|
1 |
|
$success = ($actual === $count); |
|
202
|
1 |
|
$message = ($success) ? "" : "Count of the variable is $actual."; |
|
203
|
1 |
|
$this->testResult($message, $success); |
|
204
|
1 |
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* Does $value not contain $count items? |
|
208
|
|
|
*/ |
|
209
|
1 |
|
protected function assertNotCount(int $count, string|array|\Countable $value): void |
|
210
|
|
|
{ |
|
211
|
1 |
|
if (!is_array($value) && !$value instanceof \Countable) { |
|
212
|
|
|
$this->testResult("The variable is not array or countable object.", false); |
|
213
|
|
|
return; |
|
214
|
|
|
} |
|
215
|
1 |
|
$actual = count($value); |
|
216
|
1 |
|
$success = ($actual !== $count); |
|
217
|
1 |
|
$message = ($success) ? "" : "Count of the variable is $actual."; |
|
218
|
1 |
|
$this->testResult($message, $success); |
|
219
|
1 |
|
} |
|
220
|
|
|
|
|
221
|
|
|
/** |
|
222
|
|
|
* Is $value of type $type? |
|
223
|
|
|
*/ |
|
224
|
1 |
|
protected function assertType(string|object $type, mixed $value): void |
|
225
|
|
|
{ |
|
226
|
|
|
if ( |
|
227
|
1 |
|
in_array($type, [ |
|
228
|
1 |
|
"array", "bool", "float", "int", "string", "null", "object", "resource", |
|
229
|
|
|
"scalar", "iterable", "callable", |
|
230
|
1 |
|
], true) |
|
231
|
|
|
) { |
|
232
|
1 |
|
$success = (call_user_func("is_$type", $value)); |
|
233
|
1 |
|
$actual = gettype($value); |
|
234
|
1 |
|
$message = ($success) ? "" : "The variable is $actual."; |
|
235
|
1 |
|
$this->testResult($message, $success); |
|
236
|
1 |
|
return; |
|
237
|
|
|
} |
|
238
|
1 |
|
$success = ($value instanceof $type); |
|
239
|
1 |
|
$actual = get_debug_type($value); |
|
240
|
1 |
|
$message = ($success) ? "" : "The variable is instance of $actual."; |
|
241
|
1 |
|
$this->testResult($message, $success); |
|
242
|
1 |
|
} |
|
243
|
|
|
} |
|
244
|
|
|
|