Reputation: 391
I have function test which return the repsonse of static function staticABC that belongs to separate class
function test()
{
return testA::staticABC();
}
Now I want to write PHPUnit cases for function test by mocking staticABC() function.Can any techie have idea about this ?
Upvotes: 0
Views: 46
Reputation: 2864
I don't think there is a way of mocking a function, but what you could do is use some kind of test double:
class SUT{
$staticCreator = array('testA::staticABC'); //Initialized to a default
//for production, would be better if injected somehow before using
function setStaticCreator($staticCreator){
$this->staticCreator=$staticCreator;
}
function test(){
return call_user_func($this->staticCreator);
}
}
and then run your test this way:
class Test extends ...{
function mockStaticABC(){
return "mock_string";
}
test_testfunction(){
$sut = new SUT();
$staticCreator = array($this,'mockStaticABC');
$sut->setStaticCreator($staticCreator);
$mock_return = $sut->test();
$this->assertEquals("mock_string",$mock_return);
}
}
Upvotes: 1