Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mocking to multiple Interfaces

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 instanceof 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
like image 952
HappyDeveloper Avatar asked Nov 25 '10 09:11

HappyDeveloper


2 Answers

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

like image 143
Lumbendil Avatar answered Oct 04 '22 03:10

Lumbendil


$this->getMockBuilder(['A', 'B'])->getMock();

like image 31
user487772 Avatar answered Oct 04 '22 04:10

user487772