I have some code that looks like this:
public function foo(Bar $bar) {
if ($bar instanceof Iterator) {
//...
}
}
To test this I’m using:
$this->getMock('Bar');
However, because my code is looking for an instance of Bar that implements Iterator it essentially has two types. By calling getMock(‘Bar’) or getMock(‘Iterator’) the code is untestable.
How can I make a mock implement an interface? This must be possible, surely?
To mock something
PHPUnitwill create a subclass of the class you tell it to mock.If
Barimplements Iterator yourBarMockwill also implement Iterator.Sample.php
Outputs: