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