Passed
Push — master ( d6f237...737295 )
by Jakub
01:43
created

TAssertions::resetCounter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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