我有一个方法,作为更大方法的一部分,使用不同的参数调用了多次。
$query->where("one", $id);
$query->where("two", "LIKE %{$name}%");
$query->where("three", false);
$query->where("four", true);
我正在使用 PHPUnit 10,我想为这个特定方法编写一个单元测试。我想检查 where 方法是否使用一些特定参数调用了 4 次。
例如:
$mockedQuery->expects($this->exactly(4))
->method("where")
->with(
// Here I'd like to specify the list of arguments
// or perhaps a map or something
)
->willReturn($mockedQuery);
上面的 ->will 不适用于为同一方法的连续调用指定不同的参数(或者至少我无法让它工作)。
我尝试过使用文档,但不知道到底要搜索什么,因此很难找到。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
我会回答我自己的问题,因为我必须进行一些挖掘。
对于 PHPUnit 10,方法
withConsecutive(这就是我正在寻找的,我只是不知道它叫这个)已被删除。不存在官方替代品。该问题是在 PHPUnit 存储库上提出的
我使用的解决方法是
$matcher = $this->exactly(2); $this->mock ->expects($matcher) ->method('get') ->willReturnCallback(function (string $param) use ($matcher) { match ($matcher->numberOfInvocations()) { 1 => $this->assertEquals($param, 'someValue'), 2 => $this->assertEquals($param, 'someOtherValue'), }; })