Proper way to test this ? #1689
-
Hello, I'm trying to increase my coverage in my project. I noticed that there is a mutant escaping despite the fact that the test should cover it. The class is: <?php
declare(strict_types=1);
namespace loophp\collection\Utils;
use Closure;
/**
* @internal
*
* @immutable
*
* phpcs:disable Generic.Files.LineLength.TooLong
*/
final class CallbacksArrayReducer
{
/**
* @return Closure(array<array-key, callable(mixed...): bool>): Closure(mixed...): bool
*/
public static function or(): Closure
{
return
/**
* @param array<array-key, callable(mixed...): bool> $callbacks
*
* @return Closure(mixed...): bool
*/
static fn (array $callbacks): Closure =>
/**
* @param mixed ...$parameters
*/
static fn (...$parameters): bool => array_reduce(
$callbacks,
/**
* @param bool $carry
* @param callable(mixed...): bool $callable
*/
static fn (bool $carry, callable $callable): bool => $carry || $callable(...$parameters),
false
);
}
} The PHPUnit tests are: <?php
declare(strict_types=1);
namespace tests\loophp\collection\Utils;
use loophp\collection\Utils\CallbacksArrayReducer;
use PHPUnit\Framework\TestCase;
/**
* @internal
* @coversDefaultClass \loophp\collection\Utils\CallbacksArrayReducer
*/
final class CallbacksArrayReducerTest extends TestCase
{
public function dataProvider()
{
// Ensure Callbacks receive the needed arguments.
yield [
[
static fn (string $value, string $key, iterable $iterable): bool => 'value_key_a_b_c' === sprintf('%s_%s_%s', $value, $key, implode('_', $iterable)),
],
'value',
'key',
range('a', 'c'),
true,
];
// Reduce empty callbacks array
yield [
[],
0,
0,
range('a', 'c'),
false,
];
// Reduce multiple callbacks
yield [
[
static fn (int $v): bool => 5 < $v,
static fn (int $v): bool => 0 === $v % 2,
],
0,
0,
range(0, 10),
true,
];
yield [
[
static fn (int $v): bool => 5 < $v,
static fn (int $v): bool => 0 === $v % 2,
],
13,
0,
range(0, 10),
true,
];
yield [
[
static fn (int $v): bool => 5 < $v,
static fn (int $v): bool => 0 === $v % 2,
],
3,
0,
range(0, 10),
false,
];
// Reduce single callback
yield [
[
static fn (int $v): bool => 5 < $v,
],
0,
0,
range(0, 10),
false,
];
yield [
[
static fn (int $v): bool => 5 < $v,
],
6,
0,
range(0, 10),
true,
];
yield [
[
static fn (): bool => false,
],
[],
[],
[],
false,
];
}
/**
* @dataProvider dataProvider
*
* @param mixed $current
* @param mixed $key
*/
public function testGeneric(
array $callbacks,
$current,
$key,
array $iterator,
bool $expected
): void {
self::assertSame($expected, CallbacksArrayReducer::or()($callbacks)($current, $key, $iterator));
}
} |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 13 replies
-
It looks like this line of code is marked as non-covered by underlying coverage driver. Could you please try to generate code coverage HTML report in the same tools (PHP version, PHPUnit and code coverage driver version) and check if this line is covered? |
Beta Was this translation helpful? Give feedback.
-
Hi ! Thanks for your answer. Here's the screenshot of the HTML Code Coverage on that file: The line 45 containing |
Beta Was this translation helpful? Give feedback.
It looks like this line of code is marked as non-covered by underlying coverage driver. Could you please try to generate code coverage HTML report in the same tools (PHP version, PHPUnit and code coverage driver version) and check if this line is covered?