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