I am using Codeception with REST, Phalcon and Mockery modules. The issue is that replacing services with Mockery mocks doesn't seem to work. When I run the following code it still tries to use the real class instance instead of the mock:
public function someTest(ApiTester $I)
{
$I->wantTo('test sth');
$di = $I->getApplication()->getDI();
$myServiceMock = Mockery::mock(MyService::class);
$myServiceMock->shouldReceive('someMethod')
->andReturn(null);
$myServiceMock->shouldReceive('setDI')
->andReturn($di);
$I->haveServiceInDi('myService', function() use ($myServiceMock) {
return $myServiceMock;
}, true);
$I->haveHttpHeader('Content-Type', 'application/json');
$I->sendPOST('someUrl', []);
$I->seeResponseCodeIs(HttpCode::OK);
$I->seeResponseIsJson();
$I->seeResponseContainsJson([
'success' => true
]);
}
Is there a better way to replace DI services with mock objects during runtime to make it work?
[I also tried AspectMock but it didn't work at all with Phalcon.]