I'm getting to grips with PHPUnit, and have so far found it pretty easy to use, but I've run up against a test case that's causing me difficulty.
I'm writing code against a set of interfaces that objects are expected to implement (some PHP ones, some self-made) and the SUT requires an input object to implement several interfaces. For example:
class MyClass implements ArrayAccess, MyInterface
{
// ...
}
The SUT does thing such as this:
class ClassToBeTested
{
protected $obj = NULL;
public function __construct ($obj)
{
$this -> obj = $obj;
}
public function methodToBeTested ()
{
if ($this -> obj instanceof ArrayAccess)
&& ($this -> obj instanceof MyInterface)
{
// ...
}
}
public function otherMethodUnderTest ()
{
if ($this -> obj instanceof ArrayAccess)
{
// ...
}
else
if ($this -> obj instanceof MyInterface)
{
// ...
}
}
}
I can create a stub from one interface or the other, but I don't know if you can create a stub that implements them both.
protected function setUp ()
{
$stubField = $this -> getMockBuilder ('ArrayAccess')
-> getMock ();
$this -> object = new ClassToBeTested ($stubField);
}
or
protected function setUp ()
{
$stubField = $this -> getMockBuilder ('MyInterface')
-> getMock ();
$this -> object = new ClassToBeTested ($stubField);
}
Is it possible to generate stubs from a list of interfaces, or do I have to stub a concrete class that implements the expected interfaces? That in itself is causing difficulty, because the class that needs to be stubbed itself needs another object to be passed to its constructor, and I can't seem to get either disableOriginalConstructor () or setConstructorArgs () to work I think this is because the concrete classes in question don't implement the constructor themselves but inherit it from a superclass. Am I missing something obvious here?