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

JBZoo / PHPUnit / 5281409294

pending completion
5281409294

push

github

web-flow
Rollback to PHPUnit 9 (#26)

154 of 236 relevant lines covered (65.25%)

5.15 hits per line

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

95.38
/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);
24✔
53
}
54

55
// Asserts aliases
56

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

68
/**
69
 * @param mixed $expected
70
 * @param mixed $actual
71
 */
72
function isNot($expected, $actual, string $message = ''): void
73
{
74
    Assert::assertNotEquals($expected, $actual, $message);
4✔
75
}
76

77
function isTrue(mixed $value, string $message = ''): void
78
{
79
    Assert::assertTrue($value, $message);
72✔
80
}
81

82
function isFalse(mixed $value, string $message = ''): void
83
{
84
    Assert::assertFalse($value, $message);
16✔
85
}
86

87
/**
88
 * @param string $expected
89
 * @param mixed  $className
90
 *
91
 * @psalm-template ExpectedType of object
92
 * @psalm-param    class-string<ExpectedType> $expected
93
 * @phan-suppress  PhanPluginCanUseParamType
94
 */
95
function isClass($expected, $className, string $message = ''): void
96
{
97
    Assert::assertInstanceOf($expected, $className, $message);
4✔
98
}
99

100
/**
101
 * @param mixed $actual
102
 */
103
function isCount(int $expected, $actual, string $message = ''): void
104
{
105
    Assert::assertCount($expected, $actual, $message);
4✔
106
}
107

108
function isLike(string $pattern, string $value, string $message = ''): void
109
{
110
    Assert::assertMatchesRegularExpression($pattern, $value, $message);
4✔
111
}
112

113
function isNotLike(string $pattern, string $value, string $message = ''): void
114
{
115
    Assert::assertDoesNotMatchRegularExpression($pattern, $value, $message);
4✔
116
}
117

118
function isFileEq(string $filepathExpecte, string $filepathActual, string $message = ''): void
119
{
120
    Assert::assertFileEquals($filepathExpecte, $filepathActual, $message);
4✔
121
}
122

123
/**
124
 * @param mixed $expected
125
 * @param mixed $actual
126
 */
127
function isSame($expected, $actual, string $message = ''): void
128
{
129
    Assert::assertSame($expected, $actual, $message);
80✔
130
}
131

132
/**
133
 * @param mixed $expected
134
 * @param mixed $actual
135
 */
136
function isNotSame($expected, $actual, string $message = ''): void
137
{
138
    Assert::assertNotSame($expected, $actual, $message);
4✔
139
}
140

141
/**
142
 * @param mixed $expected
143
 */
144
function isNull($expected): void
145
{
146
    Assert::assertNull($expected);
4✔
147
}
148

149
/**
150
 * @param mixed $expected
151
 */
152
function isNotNull($expected): void
153
{
154
    Assert::assertNotNull($expected);
4✔
155
}
156

157
/**
158
 * @param mixed $expected
159
 */
160
function isEmpty($expected, string $message = ''): void
161
{
162
    Assert::assertEmpty($expected, $message);
4✔
163
}
164

165
/**
166
 * @param mixed $expected
167
 */
168
function isNotEmpty($expected, string $message = ''): void
169
{
170
    Assert::assertNotEmpty($expected, $message);
16✔
171
}
172

173
/**
174
 * @param int|string $key
175
 */
176
function isKey($key, array $array, string $message = ''): void
177
{
178
    Assert::assertArrayHasKey($key, $array, $message);
4✔
179
}
180

181
/**
182
 * @param int|string $key
183
 */
184
function isNotKey($key, array $array, string $message = ''): void
185
{
186
    Assert::assertArrayNotHasKey($key, $array, $message);
4✔
187
}
188

189
/**
190
 * Assert object has an attribute.
191
 *
192
 * @param mixed $object
193
 */
194
function isAttr(string $attrName, $object, string $message = ''): void
195
{
196
    Assert::assertNotNull($object, 'object ' . $object::class . " is not empty. {$message}");
4✔
197
    isTrue(\property_exists($object, $attrName));
4✔
198
}
199

200
/**
201
 * Assert object has an attribute.
202
 *
203
 * @param mixed $object
204
 */
205
function isNotAttr(string $attrName, $object, string $message = ''): void
206
{
207
    Assert::assertNotNull($object, 'object ' . $object::class . " is not empty. {$message}");
4✔
208
    isFalse(\property_exists($object, $attrName));
4✔
209
}
210

211
function isDir(string $directoryPath, string $message = ''): void
212
{
213
    Assert::assertFileExists($directoryPath, $message);
16✔
214
    Assert::assertDirectoryExists($directoryPath);
16✔
215
}
216

217
function isNotDir(string $notDirectoryPath, string $message = ''): void
218
{
219
    if (\is_dir($notDirectoryPath)) {
4✔
220
        fail("\"{$notDirectoryPath}\" is directory");
×
221
    } else {
222
        success($message);
4✔
223
    }
224
}
225

226
function isFile(string $filePath, string $message = ''): void
227
{
228
    Assert::assertFileExists($filePath, $message);
16✔
229
}
230

231
function isNotFile(string $notFilePath, string $message = ''): void
232
{
233
    if (!\is_dir($notFilePath)) {
4✔
234
        Assert::assertFileDoesNotExist($notFilePath, $message);
4✔
235
    } else {
236
        success($message);
4✔
237
    }
238
}
239

240
function isContain(string $expected, string $haystack, bool $ignoreCase = false, string $message = ''): void
241
{
242
    if ($ignoreCase) {
24✔
243
        Assert::assertStringContainsStringIgnoringCase($expected, $haystack, $message);
4✔
244
    } else {
245
        Assert::assertStringContainsString($expected, $haystack, $message);
24✔
246
    }
247
}
248

249
function isNotContain(string $expected, string $haystack, bool $ignoreCase = false, string $message = ''): void
250
{
251
    if ($ignoreCase) {
4✔
252
        Assert::assertStringNotContainsStringIgnoringCase($expected, $haystack, $message);
4✔
253
    } else {
254
        Assert::assertStringNotContainsString($expected, $haystack, $message);
4✔
255
    }
256
}
257

258
// Custom Asserts
259

260
function isEmail(string $email, string $message = ''): void
261
{
262
    isTrue((bool)\filter_var($email, \FILTER_VALIDATE_EMAIL), $message);
4✔
263
}
264

265
function isNotEmail(string $notEmail, string $message = ''): void
266
{
267
    isFalse((bool)\filter_var($notEmail, \FILTER_VALIDATE_EMAIL), $message);
4✔
268
}
269

270
function isCurrentDate(string $date, int $timeDiff = 300, string $message = ''): void
271
{
272
    $nowDate   = new \DateTime('now');
4✔
273
    $checkDate = new \DateTime($date);
4✔
274
    Assert::assertEqualsWithDelta($nowDate->getTimestamp(), $checkDate->getTimestamp(), $timeDiff, $message);
4✔
275
}
276

277
function isAmount(
278
    float|int|array|string $expected,
279
    float|int|array|string $actual,
280
    string $message = '',
281
    float $allowableDiff = 0.03,
282
): void {
283
    if (!\is_array($expected) && !\is_array($actual)) {
4✔
284
        Assert::assertEqualsWithDelta(
4✔
285
            (float)$expected,
4✔
286
            (float)$actual,
4✔
287
            $allowableDiff,
2✔
288
            'Diff: ' . ((float)$expected - (float)$actual) . "; Expected diff={$allowableDiff}; " . $message,
4✔
289
        );
2✔
290
    } elseif (\is_array($expected) && \is_array($actual)) {
4✔
291
        isAmountCur($expected, $actual, $message, $allowableDiff);
4✔
292
    } else {
293
        fail('$expected and $actual both must be "array" OR "float|string|int"');
×
294
    }
295
}
296

297
function isNotAmount(
298
    float|int|array|string $expected,
299
    float|int|array|string $actual,
300
    string $message = '',
301
    float $allowableDiff = 0.03,
302
): void {
303
    if (!\is_array($expected) && !\is_array($actual)) {
4✔
304
        Assert::assertNotEqualsWithDelta(
4✔
305
            (float)$expected,
4✔
306
            (float)$actual,
4✔
307
            $allowableDiff,
2✔
308
            'Diff: ' . ((float)$expected - (float)$actual) . "; Expected diff={$allowableDiff}; " . $message,
4✔
309
        );
2✔
310
    } elseif (\is_array($expected) && \is_array($actual)) {
4✔
311
        isNotAmountCur($expected, $actual, $message, $allowableDiff);
4✔
312
    } else {
313
        fail('$expected and $actual both must be "array" OR "float|string|int"');
×
314
    }
315
}
316

317
/**
318
 * @param array<float|int|string> $expected
319
 * @param array<float|int|string> $actual
320
 */
321
function isAmountCur(array $expected, array $actual, string $message = '', float $allowableDiff = 0.03): void
322
{
323
    $numberOfArgs = 2;
4✔
324

325
    isTrue(\count($expected) === $numberOfArgs, $message);
4✔
326
    isTrue(\count($actual) === $numberOfArgs, $message);
4✔
327

328
    $diff = (float)$expected[0] - (float)$actual[0];
4✔
329
    $message .= " Actual diff={$diff}; Expected diff={$allowableDiff}";
4✔
330

331
    isTrue(\is_string($expected[1]), $message);
4✔
332
    isTrue(\is_string($actual[1]), $message);
4✔
333
    isSame($expected[1], $actual[1], $message);
4✔
334
    isNotEmpty($expected[1], $message);
4✔
335
    isNotEmpty($actual[1], $message);
4✔
336

337
    Assert::assertEqualsWithDelta((float)$expected[0], (float)$actual[0], $allowableDiff, $message);
4✔
338
}
339

340
/**
341
 * @param array<float|int|string> $expected
342
 * @param array<float|int|string> $actual
343
 */
344
function isNotAmountCur(array $expected, array $actual, string $message = '', float $allowableDiff = 0.03): void
345
{
346
    $numberOfArgs = 2;
4✔
347

348
    isTrue(\count($expected) === $numberOfArgs, $message);
4✔
349
    isTrue(\count($actual) === $numberOfArgs, $message);
4✔
350

351
    $diff = (float)$expected[0] - (float)$actual[0];
4✔
352
    $message .= " Actual diff={$diff}; Expected diff={$allowableDiff}";
4✔
353

354
    isTrue(\is_string($expected[1]), $message);
4✔
355
    isTrue(\is_string($actual[1]), $message);
4✔
356
    isSame($expected[1], $actual[1], $message);
4✔
357
    isNotEmpty($expected[1], $message);
4✔
358
    isNotEmpty($actual[1], $message);
4✔
359

360
    Assert::assertNotEqualsWithDelta((float)$expected[0], (float)$actual[0], $allowableDiff, $message);
4✔
361
}
362

363
function isDiffBetweenDates(string $date1, string $date2, float $expectedDiff = 300.0, string $message = ''): void
364
{
365
    $dateObj1   = new \DateTime($date1);
4✔
366
    $dateObj2   = new \DateTime($date2);
4✔
367
    $actualDiff = \abs((float)($dateObj1->getTimestamp() - $dateObj2->getTimestamp()));
4✔
368

369
    isTrue(
4✔
370
        $actualDiff === $expectedDiff,
4✔
371
        \trim(
4✔
372
            "The expected difference between \"{$date1}\" and \"{$date2}\" is {$expectedDiff} seconds. " .
4✔
373
            "The actual value is {$actualDiff} seconds. {$message}",
4✔
374
        ),
2✔
375
    );
2✔
376
}
377

378
function isDiffBetweenDatesLessThan(
379
    string $date1,
380
    string $date2,
381
    int $expectedMaxDiff = 300,
382
    string $message = '',
383
): void {
384
    $dateObj1   = new \DateTime($date1);
4✔
385
    $dateObj2   = new \DateTime($date2);
4✔
386
    $actualDiff = \abs((float)($dateObj1->getTimestamp() - $dateObj2->getTimestamp()));
4✔
387
    isTrue(
4✔
388
        $actualDiff < $expectedMaxDiff,
4✔
389
        \trim(
4✔
390
            "Diff between dates: \"{$date1}\" and \"{$date2}\" is more than expected {$expectedMaxDiff} seconds. " .
4✔
391
            "The actual value is {$actualDiff} seconds. {$message}",
4✔
392
        ),
2✔
393
    );
2✔
394
}
395

396
function isDiffBetweenDatesMoreThan(
397
    string $date1,
398
    string $date2,
399
    int $expectedMinDiff = 300,
400
    string $message = '',
401
): void {
402
    $dateObj1   = new \DateTime($date1);
4✔
403
    $dateObj2   = new \DateTime($date2);
4✔
404
    $actualDiff = \abs((float)($dateObj1->getTimestamp() - $dateObj2->getTimestamp()));
4✔
405
    isTrue(
4✔
406
        $actualDiff > $expectedMinDiff,
4✔
407
        \trim(
4✔
408
            "Diff between dates: \"{$date1}\" and \"{$date2}\" is less than expected {$expectedMinDiff} seconds. " .
4✔
409
            "The actual value is {$actualDiff} seconds. {$message}",
4✔
410
        ),
2✔
411
    );
2✔
412
}
413

414
function isSameDate(string $expected, string $actual, string $format = 'Y-m-d', string $message = ''): void
415
{
416
    $expectedObj = new \DateTime($expected);
4✔
417
    $actualObj   = new \DateTime($actual);
4✔
418
    isSame($expectedObj->format($format), $actualObj->format($format), $message);
4✔
419
}
420

421
function isFileContains(string $expected, string $filepath, bool $ignoreCase = false, string $message = ''): void
422
{
423
    isFile($filepath);
8✔
424

425
    $errMessage = \implode("\n", [
8✔
426
        "The file doesn't contain expected text. " . $message,
8✔
427
        "See: {$filepath}",
8✔
428
        'Expected text:',
4✔
429
        \str_repeat('-', 80),
8✔
430
        $expected,
4✔
431
        \str_repeat('-', 80),
8✔
432
    ]);
4✔
433

434
    $fileContent = (string)\file_get_contents($filepath);
8✔
435

436
    if ($ignoreCase) {
8✔
437
        isTrue(\mb_stripos($fileContent, $expected) !== false, $errMessage);
4✔
438
    } else {
439
        isTrue(\str_contains($fileContent, $expected), $errMessage);
8✔
440
    }
441
}
442

443
function isFileNotContains(string $expected, string $filepath, bool $ignoreCase = false, string $message = ''): void
444
{
445
    isFile($filepath);
4✔
446

447
    $errMessage = \implode("\n", [
4✔
448
        "The file shouldn't contain expected text. " . $message,
4✔
449
        "See: {$filepath}",
4✔
450
        'Expected text:',
2✔
451
        \str_repeat('-', 80),
4✔
452
        $expected,
2✔
453
        \str_repeat('-', 80),
4✔
454
    ]);
2✔
455

456
    $fileContent = (string)\file_get_contents($filepath);
4✔
457

458
    if ($ignoreCase) {
4✔
459
        isTrue(\mb_stripos($fileContent, $expected) === false, $errMessage);
4✔
460
    } else {
461
        isTrue(!\str_contains($fileContent, $expected), $errMessage);
4✔
462
    }
463
}
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

© 2025 Coveralls, Inc