Is it possible to mock an object in a way that it fakes multiple interfaces implementations? (Note: I am using "padraic's mockery" https://github.com/padraic/mockery)
Suppose I have a class Mysql, which implements Db_Interface and Configurable_Interface, and which I need to mock just to be able to test another class. I want to create a mock with another name, not Mysql (because it could change or disappear in the future, that's why we use interfaces, right?), so I don't want to do Mockery::mock('Mysql').
I know I could create it like Mockery::mock('Db_Interface') and it would pass the instanceo开发者_运维问答f Db_Interface check. But how can I make it pass the check for the other interface too?
@Gordon ok heres the code:
$m = Mockery::mock('Configurable_Interface');
var_dump($m instanceof Configurable_Interface); // true
var_dump($m instanceof Db_Interface); // false of course, since I don't know how to make a mock implement 2 interfaces
For anyone stumbling into this. In Mockery, you can call Mockery::mock('firstInterface, secondInterface');
to mock an object wich needs to implement multiple interfaces.
Source: Mockery README
$this->getMockBuilder(['A', 'B'])->getMock();
This thread comes out when the question is about phpspec !
So here is a way to do it with phpspec:
$prophecy = $this->prophesize(InterfaceA::class);
$prophecy->willImplement(InterfaceB::class);
精彩评论