In PHPUnit, how do I indicate different with() on successive calls to a mocked method? In PHPUnit, how do I indicate different with() on successive calls to a mocked method? php php

In PHPUnit, how do I indicate different with() on successive calls to a mocked method?


You need to use at():

$mock->expects($this->at(0))     ->method('foo')     ->with('someValue');$mock->expects($this->at(1))     ->method('foo')     ->with('anotherValue');$mock->foo('someValue');$mock->foo('anotherValue');

Note that the indexes passed to at() apply across all method calls to the same mock object. If the second method call was to bar() you would not change the argument to at().


Referencing from the answer from a similar question,

Since PHPUnit 4.1 you can use withConsecutive eg.

$mock->expects($this->exactly(2))     ->method('set')     ->withConsecutive(         [$this->equalTo('foo'), $this->greaterThan(0)],         [$this->equalTo('bar'), $this->greaterThan(0)]       );

If you want to make it return on consecutive calls:

  $mock->method('set')         ->withConsecutive([$argA1, $argA2], [$argB1], [$argC1, $argC2])         ->willReturnOnConsecutiveCalls($retValueA, $retValueB, $retValueC);

It's not ideal to use at() if you can avoid it because as their docs claim

The $index parameter for the at() matcher refers to the index, starting at zero, in all method invocations for a given mock object. Exercise caution when using this matcher as it can lead to brittle tests which are too closely tied to specific implementation details.