Search code examples
testingphpunitdeprecated

Replace PHPUnit method `withConsecutive` (abandoned in PHPUnit 10)


As method withConsecutive will be deleted in PHPUnit 10 (in 9.6 it's deprecated) I need to replace all of occurrences of this method to new code.

Try to find some solutions and didn't find any of reasonable solution.

For example, I have a code

    $this->personServiceMock->expects($this->exactly(2))
        ->method('prepare')
        ->withConsecutive(
            [$personFirst, $employeeFirst],
            [$personSecond, $employeeSecond],
        )
        ->willReturnOnConsecutiveCalls($personDTO, $personSecondDTO);

To which code should I replace withConsecutive ?

P.S. Documentation on official site still shows how use withConsecutive


Solution

  • I have replaced withConsecutive with the following.

    $matcher = $this->exactly(2);
    $this->service
        ->expects($matcher)
        ->method('functionName')
        ->willReturnCallback(function (string $key, string $value) use ($matcher,$expected1, $expected2) {
            match ($matcher->numberOfInvocations()) {
                1 =>  $this->assertEquals($expected1, $value),
                2 =>  $this->assertEquals($expected2, $value),
            };
        });