I am using phpunit PHPUnitFrameworkTestCase
to test some controller action.
At some point in the code, I call some API that I want to mock
public function storeData($id)
{
$someService = new SomeService();
$resp = $someService->getDataById($Id);
...
}
What I am mocking is getDataById()
from SomeService
But for some reason I get Method was expected to be called 1 times, actually called 0 times
Is it because its nested inside another function?
Any ideas?
The test is something like this:
private function setApiMock($ID): void
{
$apiMock = $this->getMockBuilder(SomeService::class)
->onlyMethods(['getDataById'])
->disableOriginalConstructor()
->getMock();
$apiMock->expects($this->once())
->method('getDataById')
->willReturn([]);
}
public function testSaveTransactionDataAction(): void
{
$id = '123';
$this->setApiMock($id);
// $this->assertSame(200, something);
}
Thanks