php - How to mock inner function in phpunit?
one text
This is my Foo class
<?php
class FooClass
{
public function doFoo($total)
{
$gst = (($this->getTaxRate() / 100) * $total);
return $gst;
}
public function getTaxRate()
{
return 5;
}
}
And this is my test.
public function fooTest()
{
$mockedObj = $this->createMock(FooClass::class);
$mockedObj->expects($this->exactly(1))
->method('getTaxRate')
->will($this->returnValue(10));
$this->assertEquals(10, $mockedObj->doFoo(100));
}
Output:
Failed asserting that null matches expected 10.
I can see getTaxRate() function is not being mocked .It is also returning null instead of 5(default value). I am expecting getTaxRate() inside doFoo() to return the mock value i.e 10 else if I do not mock a getTaxRate, it should return 5.
Anything that I am missing here?
Source