Passed
Push — master ( 3cc3ad...47373b )
by Jakub
01:33
created

TAssertions::incCounter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
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
  protected function testResult(string $text, bool $success = true): void {
18
    $this->incCounter();
19
    if($success) {
20
      return;
21
    }
22
    echo "Test $this->taskCount failed. $text\n";
23
  }
24
25
  /**
26
   * Increases task counter
27
   *
28
   * @internal
29
   */
30
  protected function incCounter(): void {
31
    $this->taskCount++;
32
    Environment::incCounter();
0 ignored issues
show
Deprecated Code introduced by
The function MyTester\Environment::incCounter() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

32
    /** @scrutinizer ignore-deprecated */ Environment::incCounter();
Loading history...
33
  }
34
35
  /**
36
   * Resets task counter
37
   *
38
   * @internal
39
   */
40
  protected function resetCounter(): void {
41
    $this->taskCount = 0;
42
    Environment::resetCounter();
0 ignored issues
show
Deprecated Code introduced by
The function MyTester\Environment::resetCounter() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

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