• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

JBZoo / PHPUnit / 18063775066

27 Sep 2025 07:06PM UTC coverage: 65.385% (+0.3%) from 65.134%
18063775066

push

github

web-flow
chore(ci): Bump CI environment and dependencies (#33)

- Remove scheduled workflow trigger.
- Update PHP matrix to include 8.4 and drop 8.1.
- Upgrade `actions/upload-artifact` to v4.
- Update `jbzoo/codestyle` dev dependency to `^7.1.5`.

170 of 260 relevant lines covered (65.38%)

7.73 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.89
/src/functions/aliases.php
1
<?php
2

3
/**
4
 * JBZoo Toolbox - PHPUnit.
5
 *
6
 * This file is part of the JBZoo Toolbox project.
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license    MIT
11
 * @copyright  Copyright (C) JBZoo.com, All rights reserved.
12
 * @see        https://github.com/JBZoo/PHPUnit
13
 */
14

15
declare(strict_types=1);
16

17
namespace JBZoo\PHPUnit;
18

19
use PHPUnit\Framework\Assert;
20

21
// Controls
22

23
/**
24
 * Skip current test.
25
 */
26
function skip(string $message): void
27
{
28
    Assert::markTestSkipped($message);
×
29
}
30

31
/**
32
 * Incomplete current test.
33
 */
34
function incomplete(string $message): void
35
{
36
    Assert::markTestIncomplete($message);
×
37
}
38

39
/**
40
 * Fail current test.
41
 */
42
function fail(string $message = ''): void
43
{
44
    Assert::fail($message);
×
45
}
46

47
/**
48
 * Success current test.
49
 */
50
function success(string $message = ''): void
51
{
52
    isTrue(true, $message);
72✔
53
}
54

55
// Asserts aliases
56

57
/**
58
 * @SuppressWarnings(PHPMD.ShortMethodName)
59
 */
60
function is(mixed $expected, mixed $actual, string $message = ''): void
61
{
62
    Assert::assertEquals($expected, $actual, $message);
12✔
63
}
64

65
function isNot(mixed $expected, mixed $actual, string $message = ''): void
66
{
67
    Assert::assertNotEquals($expected, $actual, $message);
6✔
68
}
69

70
function isTrue(mixed $value, string $message = ''): void
71
{
72
    Assert::assertTrue($value, $message);
138✔
73
}
74

75
function isFalse(mixed $value, string $message = ''): void
76
{
77
    Assert::assertFalse($value, $message);
24✔
78
}
79

80
/**
81
 * @param string $expected
82
 * @param mixed  $className
83
 *
84
 * @psalm-template ExpectedType of object
85
 * @psalm-param    class-string<ExpectedType> $expected
86
 * @phan-suppress  PhanPluginCanUseParamType
87
 */
88
function isClass($expected, $className, string $message = ''): void
89
{
90
    Assert::assertInstanceOf($expected, $className, $message);
6✔
91
}
92

93
function isCount(int $expected, mixed $actual, string $message = ''): void
94
{
95
    Assert::assertCount($expected, $actual, $message);
6✔
96
}
97

98
function isLike(string $pattern, string $value, string $message = ''): void
99
{
100
    Assert::assertMatchesRegularExpression($pattern, $value, $message);
6✔
101
}
102

103
function isNotLike(string $pattern, string $value, string $message = ''): void
104
{
105
    Assert::assertDoesNotMatchRegularExpression($pattern, $value, $message);
6✔
106
}
107

108
function isFileEq(string $filepathExpecte, string $filepathActual, string $message = ''): void
109
{
110
    Assert::assertFileEquals($filepathExpecte, $filepathActual, $message);
6✔
111
}
112

113
function isSame(mixed $expected, mixed $actual, string $message = ''): void
114
{
115
    Assert::assertSame($expected, $actual, $message);
126✔
116
}
117

118
function isNotSame(mixed $expected, mixed $actual, string $message = ''): void
119
{
120
    Assert::assertNotSame($expected, $actual, $message);
6✔
121
}
122

123
function isNull(mixed $expected): void
124
{
125
    Assert::assertNull($expected);
6✔
126
}
127

128
function isNotNull(mixed $expected): void
129
{
130
    Assert::assertNotNull($expected);
6✔
131
}
132

133
function isEmpty(mixed $expected, string $message = ''): void
134
{
135
    Assert::assertEmpty($expected, $message);
6✔
136
}
137

138
function isNotEmpty(mixed $expected, string $message = ''): void
139
{
140
    Assert::assertNotEmpty($expected, $message);
24✔
141
}
142

143
function isKey(int|string $key, array $array, string $message = ''): void
144
{
145
    Assert::assertArrayHasKey($key, $array, $message);
6✔
146
}
147

148
function isNotKey(int|string $key, array $array, string $message = ''): void
149
{
150
    Assert::assertArrayNotHasKey($key, $array, $message);
6✔
151
}
152

153
/**
154
 * Assert object has an attribute.
155
 */
156
function isAttr(string $attrName, mixed $object, string $message = ''): void
157
{
158
    Assert::assertNotNull($object, 'object ' . $object::class . " is not empty. {$message}");
6✔
159
    isTrue(\property_exists($object, $attrName));
6✔
160
}
161

162
/**
163
 * Assert object has an attribute.
164
 */
165
function isNotAttr(string $attrName, mixed $object, string $message = ''): void
166
{
167
    Assert::assertNotNull($object, 'object ' . $object::class . " is not empty. {$message}");
6✔
168
    isFalse(\property_exists($object, $attrName));
6✔
169
}
170

171
function isDir(string $directoryPath, string $message = ''): void
172
{
173
    Assert::assertFileExists($directoryPath, $message);
24✔
174
    Assert::assertDirectoryExists($directoryPath);
24✔
175
}
176

177
function isNotDir(string $notDirectoryPath, string $message = ''): void
178
{
179
    if (\is_dir($notDirectoryPath)) {
6✔
180
        fail("\"{$notDirectoryPath}\" is directory");
×
181
    } else {
182
        success($message);
6✔
183
    }
184
}
185

186
function isFile(string $filePath, string $message = ''): void
187
{
188
    Assert::assertFileExists($filePath, $message);
24✔
189
}
190

191
function isNotFile(string $notFilePath, string $message = ''): void
192
{
193
    if (!\is_dir($notFilePath)) {
6✔
194
        Assert::assertFileDoesNotExist($notFilePath, $message);
6✔
195
    } else {
196
        success($message);
6✔
197
    }
198
}
199

200
function isContain(string $expected, string $haystack, bool $ignoreCase = false, string $message = ''): void
201
{
202
    if ($ignoreCase) {
36✔
203
        Assert::assertStringContainsStringIgnoringCase($expected, $haystack, $message);
6✔
204
    } else {
205
        Assert::assertStringContainsString($expected, $haystack, $message);
36✔
206
    }
207
}
208

209
function isNotContain(string $expected, string $haystack, bool $ignoreCase = false, string $message = ''): void
210
{
211
    if ($ignoreCase) {
6✔
212
        Assert::assertStringNotContainsStringIgnoringCase($expected, $haystack, $message);
6✔
213
    } else {
214
        Assert::assertStringNotContainsString($expected, $haystack, $message);
6✔
215
    }
216
}
217

218
// Custom Asserts
219

220
function isEmail(string $email, string $message = ''): void
221
{
222
    isTrue((bool)\filter_var($email, \FILTER_VALIDATE_EMAIL), $message);
6✔
223
}
224

225
function isNotEmail(string $notEmail, string $message = ''): void
226
{
227
    isFalse((bool)\filter_var($notEmail, \FILTER_VALIDATE_EMAIL), $message);
6✔
228
}
229

230
function isCurrentDate(string $date, int $timeDiff = 300, string $message = ''): void
231
{
232
    $nowDate   = new \DateTime('now');
6✔
233
    $checkDate = new \DateTime($date);
6✔
234
    Assert::assertEqualsWithDelta($nowDate->getTimestamp(), $checkDate->getTimestamp(), $timeDiff, $message);
6✔
235
}
236

237
function isAmount(
238
    array|float|int|string $expected,
239
    array|float|int|string $actual,
240
    string $message = '',
241
    float $allowableDiff = 0.03,
242
): void {
243
    if (!\is_array($expected) && !\is_array($actual)) {
6✔
244
        Assert::assertEqualsWithDelta(
6✔
245
            (float)$expected,
6✔
246
            (float)$actual,
6✔
247
            $allowableDiff,
6✔
248
            'Diff: ' . ((float)$expected - (float)$actual) . "; Expected diff={$allowableDiff}; " . $message,
6✔
249
        );
6✔
250
    } elseif (\is_array($expected) && \is_array($actual)) {
6✔
251
        isAmountCur($expected, $actual, $message, $allowableDiff);
6✔
252
    } else {
253
        fail('$expected and $actual both must be "array" OR "float|string|int"');
×
254
    }
255
}
256

257
function isNotAmount(
258
    array|float|int|string $expected,
259
    array|float|int|string $actual,
260
    string $message = '',
261
    float $allowableDiff = 0.03,
262
): void {
263
    if (!\is_array($expected) && !\is_array($actual)) {
6✔
264
        Assert::assertNotEqualsWithDelta(
6✔
265
            (float)$expected,
6✔
266
            (float)$actual,
6✔
267
            $allowableDiff,
6✔
268
            'Diff: ' . ((float)$expected - (float)$actual) . "; Expected diff={$allowableDiff}; " . $message,
6✔
269
        );
6✔
270
    } elseif (\is_array($expected) && \is_array($actual)) {
6✔
271
        isNotAmountCur($expected, $actual, $message, $allowableDiff);
6✔
272
    } else {
273
        fail('$expected and $actual both must be "array" OR "float|string|int"');
×
274
    }
275
}
276

277
/**
278
 * @param array<float|int|string> $expected
279
 * @param array<float|int|string> $actual
280
 */
281
function isAmountCur(array $expected, array $actual, string $message = '', float $allowableDiff = 0.03): void
282
{
283
    $numberOfArgs = 2;
6✔
284

285
    isTrue(\count($expected) === $numberOfArgs, $message);
6✔
286
    isTrue(\count($actual) === $numberOfArgs, $message);
6✔
287

288
    $diff = (float)$expected[0] - (float)$actual[0];
6✔
289
    $message .= " Actual diff={$diff}; Expected diff={$allowableDiff}";
6✔
290

291
    isTrue(\is_string($expected[1]), $message);
6✔
292
    isTrue(\is_string($actual[1]), $message);
6✔
293
    isSame($expected[1], $actual[1], $message);
6✔
294
    isNotEmpty($expected[1], $message);
6✔
295
    isNotEmpty($actual[1], $message);
6✔
296

297
    Assert::assertEqualsWithDelta((float)$expected[0], (float)$actual[0], $allowableDiff, $message);
6✔
298
}
299

300
/**
301
 * @param array<float|int|string> $expected
302
 * @param array<float|int|string> $actual
303
 */
304
function isNotAmountCur(array $expected, array $actual, string $message = '', float $allowableDiff = 0.03): void
305
{
306
    $numberOfArgs = 2;
6✔
307

308
    isTrue(\count($expected) === $numberOfArgs, $message);
6✔
309
    isTrue(\count($actual) === $numberOfArgs, $message);
6✔
310

311
    $diff = (float)$expected[0] - (float)$actual[0];
6✔
312
    $message .= " Actual diff={$diff}; Expected diff={$allowableDiff}";
6✔
313

314
    isTrue(\is_string($expected[1]), $message);
6✔
315
    isTrue(\is_string($actual[1]), $message);
6✔
316
    isSame($expected[1], $actual[1], $message);
6✔
317
    isNotEmpty($expected[1], $message);
6✔
318
    isNotEmpty($actual[1], $message);
6✔
319

320
    Assert::assertNotEqualsWithDelta((float)$expected[0], (float)$actual[0], $allowableDiff, $message);
6✔
321
}
322

323
function isDiffBetweenDates(string $date1, string $date2, float $expectedDiff = 300.0, string $message = ''): void
324
{
325
    $dateObj1   = new \DateTime($date1);
6✔
326
    $dateObj2   = new \DateTime($date2);
6✔
327
    $actualDiff = \abs((float)($dateObj1->getTimestamp() - $dateObj2->getTimestamp()));
6✔
328

329
    isTrue(
6✔
330
        $actualDiff === $expectedDiff,
6✔
331
        \trim(
6✔
332
            "The expected difference between \"{$date1}\" and \"{$date2}\" is {$expectedDiff} seconds. "
6✔
333
            . "The actual value is {$actualDiff} seconds. {$message}",
6✔
334
        ),
6✔
335
    );
6✔
336
}
337

338
function isDiffBetweenDatesLessThan(
339
    string $date1,
340
    string $date2,
341
    int $expectedMaxDiff = 300,
342
    string $message = '',
343
): void {
344
    $dateObj1   = new \DateTime($date1);
6✔
345
    $dateObj2   = new \DateTime($date2);
6✔
346
    $actualDiff = \abs((float)($dateObj1->getTimestamp() - $dateObj2->getTimestamp()));
6✔
347
    isTrue(
6✔
348
        $actualDiff < $expectedMaxDiff,
6✔
349
        \trim(
6✔
350
            "Diff between dates: \"{$date1}\" and \"{$date2}\" is more than expected {$expectedMaxDiff} seconds. "
6✔
351
            . "The actual value is {$actualDiff} seconds. {$message}",
6✔
352
        ),
6✔
353
    );
6✔
354
}
355

356
function isDiffBetweenDatesMoreThan(
357
    string $date1,
358
    string $date2,
359
    int $expectedMinDiff = 300,
360
    string $message = '',
361
): void {
362
    $dateObj1   = new \DateTime($date1);
6✔
363
    $dateObj2   = new \DateTime($date2);
6✔
364
    $actualDiff = \abs((float)($dateObj1->getTimestamp() - $dateObj2->getTimestamp()));
6✔
365
    isTrue(
6✔
366
        $actualDiff > $expectedMinDiff,
6✔
367
        \trim(
6✔
368
            "Diff between dates: \"{$date1}\" and \"{$date2}\" is less than expected {$expectedMinDiff} seconds. "
6✔
369
            . "The actual value is {$actualDiff} seconds. {$message}",
6✔
370
        ),
6✔
371
    );
6✔
372
}
373

374
function isSameDate(string $expected, string $actual, string $format = 'Y-m-d', string $message = ''): void
375
{
376
    $expectedObj = new \DateTime($expected);
6✔
377
    $actualObj   = new \DateTime($actual);
6✔
378
    isSame($expectedObj->format($format), $actualObj->format($format), $message);
6✔
379
}
380

381
function isFileContains(string $expected, string $filepath, bool $ignoreCase = false, string $message = ''): void
382
{
383
    isFile($filepath);
12✔
384

385
    $errMessage = \implode("\n", [
12✔
386
        "The file doesn't contain expected text. " . $message,
12✔
387
        "See: {$filepath}",
12✔
388
        'Expected text:',
12✔
389
        \str_repeat('-', 80),
12✔
390
        $expected,
12✔
391
        \str_repeat('-', 80),
12✔
392
    ]);
12✔
393

394
    $fileContent = (string)\file_get_contents($filepath);
12✔
395

396
    if ($ignoreCase) {
12✔
397
        isTrue(\mb_stripos($fileContent, $expected) !== false, $errMessage);
6✔
398
    } else {
399
        isTrue(\str_contains($fileContent, $expected), $errMessage);
12✔
400
    }
401
}
402

403
function isFileNotContains(string $expected, string $filepath, bool $ignoreCase = false, string $message = ''): void
404
{
405
    isFile($filepath);
6✔
406

407
    $errMessage = \implode("\n", [
6✔
408
        "The file shouldn't contain expected text. " . $message,
6✔
409
        "See: {$filepath}",
6✔
410
        'Expected text:',
6✔
411
        \str_repeat('-', 80),
6✔
412
        $expected,
6✔
413
        \str_repeat('-', 80),
6✔
414
    ]);
6✔
415

416
    $fileContent = (string)\file_get_contents($filepath);
6✔
417

418
    if ($ignoreCase) {
6✔
419
        isTrue(\mb_stripos($fileContent, $expected) === false, $errMessage);
6✔
420
    } else {
421
        isTrue(!\str_contains($fileContent, $expected), $errMessage);
6✔
422
    }
423
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc