Rey Libutan
Rey Libutan

Reputation: 5314

PHPUnit Different return values every call of mocked method

For example I have a mocked Class like below:

$mock= $this->getMockBuilder("SomeClass")->disableOriginalConstructor()->getMock();

$mock->expects($this->any())
     ->method("someMethod")
     ->will($this->returnValue("RETURN VALUE"));

The only param of someMethod is an array $arr.

What I want to do is to return $arr[0] when someMethod is called for the first time, $arr[1] for the second time and so on.

The size of $arr is dynamic.

Any idea how to achieve this if this is even possible?

Upvotes: 18

Views: 9714

Answers (1)

SenseException
SenseException

Reputation: 1075

$mock->expects($this->any())
    ->method("someMethod")
    ->will($this->onConsecutiveCalls(1, 2, 3));

With onConsecutiveCalls you can set a return value for every call of someMethod. The first call returns 1. The second call 2. The third call 3.

Upvotes: 37

Related Questions