Can someone provide me a reference to a good PHPUnit mock guide? The one in the official documentation doesn't seem to be detailed enough. I am trying to study PHPUnit by reading the source code, but I am not familiar with the term matcher, invocation mocker, stub return, etc.

I need to know about the following:

1) How to expect multiple calls to a mock object's method, but each return a different sets of value?

    ->will($this->returnValue(2)); // I need the second call to return different value

2) How to expect a call to a mock object's method with multiple parameters?

Accepted Answer

[Note: All code samples from the sites linked, follow the links for more thorough explanations.]

Return Different Values

The (current) PHPUnit documentation suggests using a callback or onConsecutiveCalls():


     ->will($this->onConsecutiveCalls(2, 3, 5, 7));

Expect Multiple Parameters

with() may contain multiple parameters:


Test Multiple Calls

Although not ask, on a related topic (and not in the PHPUnit documentation that I can find), you can use at() to set expectations for multiple calls to a method:

          ->will($this->returnValue("3 4"));
          ->will($this->returnValue("4 6"));
Written by Tim Lytle
This page was build to provide you fast access to the question and the direct accepted answer.
The content is written by members of the community.
It is licensed under cc-wiki