In the issue of PHPUnit there are several
possibilities to replace missing functionality withConsecutive
of PHPUnit.
Most of the comments use some sort of the callback:
$expectedArguments = [
...
]
->withConsecutive(function ($arg1, $arg2) use (&$index) {
if ($index === 0) {
self::assertEquals('some', $arg1);
...
}
});
Eventually i've got sick of writing such code. Writing boilerplate and repeating yourself is no fun.
The class solve also the issue, where you need to operate with arguments <-> return value relation. Giving the developer an ability to define what's returned with each argument set.
composer require --dev biozshock/phpunit-consecutive
When you need to mock the method which returns a value.
$mock->method('add')
->withConsecutive($a, $b)
->willReturn(1, 2);
Is replaced by
$mock->method('add')
->willRecturnCallback(Consecutive::consecutiveMap([
[$a, 1],
[$b, 2]
]));
Or return callback, which accepts given arguments:
$mock->method('add')
->willRecturnCallback(Consecutive::consecutiveMap([
[$a, $b, static function (int $a, string $b): bool {
return $a === (int) $b;
}],
[$c, $d, static function (int $c, string $d): bool {
return str_starts_with($d, (string) $c);
}]
]));
Also, you can test methods that return one of arguments. In this example the test expects zero-index argument $a
to be returned:
$mock->method('add')
->willRecturnCallback(Consecutive::consecutiveMap([
[$a, $b],
[$a, $d]
], 0));
Otherwise, when mocked method returns void
.
$mock->method('add')
->withConsecutive($a, $b);
Is replaced by
$mock->method('add')
->willRecturnCallback(Consecutive::consecutiveCall([
[$a],
[$b]
]));