Arav
Arav

Reputation: 5247

phpunit mock multiple methods in a single call

Have two mock methods setAsy1 and setAsy2 that returns the same values. Currently I need to call twice the same method function to setup mock methods. Is there any way I can setup with a single call?

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

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

Upvotes: 1

Views: 434

Answers (1)

Alexander Yancharuk
Alexander Yancharuk

Reputation: 14501

PHPUnit\Framework\MockObject\Builder\InvocationMocker::method() can get PHPUnit\Framework\Constraint\Constraint as parameter. So, you can set up mock from your example with a single call:

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

Upvotes: 1

Related Questions