Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHPUnit mock with multiple expects() calls

Using PHPUnit, I wonder how we can have multiple expectation from the same stub/mock.

For example, I want to test that the mock will have the method display() called and return NULL. I also want to test that the method process() will be called.

In fact my test is called testProcessIsCalledIfDisplayReturnNull().

So I need to setup 2 expectations on the same mock object, and the manual doesn't really help about that :(

like image 502
FMaz008 Avatar asked Apr 29 '11 16:04

FMaz008


1 Answers

If you know, that method is called once use $this->once() in expects(), otherwise use $this->any()

$mock = $this->getMock('nameOfTheClass', array('firstMethod','secondMethod','thirdMethod'));
$mock->expects($this->once())
     ->method('firstMethod')
     ->will($this->returnValue('value'));
$mock->expects($this->once())
     ->method('secondMethod')
     ->will($this->returnValue('value'));
$mock->expects($this->once())
     ->method('thirdMethod')
     ->will($this->returnValue('value'));
like image 102
Dmitriy Koval Avatar answered Sep 29 '22 00:09

Dmitriy Koval