I'm just trying to implement unit testing to my PHP application and really can't figure out how it works with mock/stub and Codeception
So I'm using Laravel and want to add testing to an API endpoint like this
$I->sendGET('/user/inventory/<user_id>');
Then in the controller I have function to authenticate the user, something like
public function getUserInventory($userId) {
if ($this->authenticateUser($userId))
{
do some logic...
if ($success) {
return $good_data;
} else {
return $bad_data;
}
}
return false;
}
authenticateUser
function is not relevant to the test and I just want to mock the return to true
or false
so I can test the actual logic on whether it should return $good_data
or $bad_data
, is that possible?
I've tried to declare stub object before calling the sendGET
function like this
Stub::make(
'App\Http\Controllers\Controller',
['authenticateUser' => true],
$this
);
$I->sendGET('/user/inventory/<user_id>');
but it doesn't seem to use the stub object inside the getUserInventory
function when I ran the test.
Is there any way to use stub/mock method inside the $I->sendGET('/user/inventory/<user_id>');
test?
Hopefully my question make sense