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