phpunit 在一次调用中模拟多个方法

phpunit mock multiple methods in a single call

有两个 mock 方法 setAsy1setAsy2 即 returns 具有相同的值。目前我需要调用两次相同的方法函数来设置模拟方法。有什么方法可以通过一个电话进行设置吗?

$transferItemMockf->expects($this->any())
    ->method('setAsy1')
    ->willReturn($id);

$transferItemMockf->expects($this->any())
    ->method('setAsy2')
    ->willReturn($id);

PHPUnit\Framework\MockObject\Builder\InvocationMocker::method() 可以得到 PHPUnit\Framework\Constraint\Constraint 作为参数。因此,您可以通过一次调用从您的示例中设置模拟:

$transferItemMockf->expects($this->any())
   ->method($this->logicalOr(
        $this->equalTo('setAsy1'),
        $this->equalTo('setAsy2')
    ))
   ->willReturn($id);