Reputation: 35734
I have an abstract class that has an abstract method and a concrete method.
The concrete method calls the abstract method and uses its return value.
How can this return value be mocked?
So the abstract class is
abstract class MyAbstractClass
{
/**
* @return array()
*/
abstract function tester();
/**
* @return array()
*/
public function myconcrete()
{
$value = $this->tester(); //Should be an array
return array_merge($value, array("a","b","c");
}
}
I want to test the myconcrete method, so i want to mock the return value to tester - but it is called inside the method?
Is this possible?
Upvotes: 2
Views: 385
Reputation: 157947
Yes it is possible. Your test should look like this:
class MyTest extends PHPUnit_Framework_TestCase
{
public function testTester() {
// mock only the method tester all other methods will keep unchanged
$stub = $this->getMockBuilder('MyAbstractClass')
->setMethods(array('tester'))
->getMock();
// configure the stub so that tester() will return an array
$stub->expects($this->any())
->method('tester')
->will($this->returnValue(array('1', '2', '3')));
// test myconcrete()
$result = $stub->myconcrete();
// assert result is the correct array
$this->assertEquals($result, array(
'1', '2', '3', 'a', 'b', 'c'
));
}
}
Note that I'm using PHPUnit 3.7.10
Upvotes: 3