I have this example class
class Class
{
public function getStuff()
{
$data = $this->getData('Here');
$data2 = $this->getData('There');
return $data . ' ' . $data2;
}
public function getData( $string )
{
return $string;
}
}
I want to be able to test the getStuff method and mock the getData method.
What would be the best way of mocking this method?
Thanks
I think the
getDatamethod should be part of a different class, separating data from logic. You could then pass a mock of that class to theTestClassinstance as a dependency:The mock would have to implement a
TestRepositoryinterface. This is called dependency injection. E.g.:The advantage of using an interface and enforcing it in the
TestClassconstructor method is that an interface guarantees presence of certain methods that you define, likegetData()above – whatever the implementation is, the method must be there.