PHPUnit Different return values every call of mocked method

mocking, php, phpunit

Solution

$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.

Problem

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?

Original source