有没有办法为不同的输入参数定义不同的模拟期望?例如,我有一个名为DB的数据库层类.此类具有名为"Query(string $ query)"的方法,该方法在输入时采用SQL查询字符串.我可以为此类(DB)创建模拟,并为依赖于输入查询字符串的不同Query方法调用设置不同的返回值吗?
是否可以以这种方式配置PHPUnit mock?
$context = $this->getMockBuilder('Context')
->getMock();
$context->expects($this->any())
->method('offsetGet')
->with('Matcher')
->will($this->returnValue(new Matcher()));
$context->expects($this->any())
->method('offsetGet')
->with('Logger')
->will($this->returnValue(new Logger()));
Run Code Online (Sandbox Code Playgroud)
我使用PHPUnit 3.5.10,当我要求Matcher时它会失败,因为它需要"Logger"参数.这就像第二个期望是重写第一个,但是当我转储模拟时,一切看起来都不错.
由于该方法withConsecutive将在 PHPUnit 10 中被删除(在 9.6 中已弃用),我需要将此方法的所有出现替换为新代码。
尝试寻找一些解决方案,但没有找到任何合理的解决方案。
例如,我有一个代码
$this->personServiceMock->expects($this->exactly(2))
->method('prepare')
->withConsecutive(
[$personFirst, $employeeFirst],
[$personSecond, $employeeSecond],
)
->willReturnOnConsecutiveCalls($personDTO, $personSecondDTO);
Run Code Online (Sandbox Code Playgroud)
我应该替换哪个代码withConsecutive?
PS官方网站上的文档仍然显示了如何使用withConsecutive