Passed
Push — master ( 7b52e4...d40832 )
by Jakub
01:50
created

TAssertions::getCounter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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