tamir
tamir

Reputation: 3297

PHPUnit assert method call at index and number of calls

I need to assert 3 different method calls (of the same method) with different arguments and return values. I can do that using $this->at(). How can I also assert that exactly 3 calls where made?
Normally it would be easy using $this->exactly(3) but I can't use it in my case..

Upvotes: 0

Views: 898

Answers (1)

Schleis
Schleis

Reputation: 43710

You want to use returnValueMap with your mock. You are then able to use the exactly(3) and it will return the correct values for each call.

Slightly modified example from PHPUnit documentation:

public function testReturnValueMapStub()
{
    // Create a stub for the SomeClass class.
    $stub = $this->getMockBuilder('SomeClass')
                 ->getMock();

    // Create a map of arguments to return values.
    $map = array(
      array('a', 'b', 'c', 'd'),
      array('e', 'f', 'g', 'h'),
      array('i', 'j', 'k', 'l'),
    );

    // Configure the stub.
    $stub->expects($this->exactly(3))
         ->method('doSomething')
         ->will($this->returnValueMap($map));

    // $stub->doSomething() returns different values depending on
    // the provided arguments.
    $this->assertEquals('d', $stub->doSomething('a', 'b', 'c'));
    $this->assertEquals('h', $stub->doSomething('e', 'f', 'g'));
}

Upvotes: 1

Related Questions