I've got a pretty straight forward user factory, except it is depending on an external API call. Since the external call has be authenticated with particular user details I'm not sure if I somehow have to mock the response or the request?
My question is if there are any suggestions on what a good way would be to user test this factory?
Thanks!
public static function getUser($id)
{
if (!IntegerValidator::valid($id)) {
throw new ValidationException('Invalid id provided');
}
$path = "/users/{$id}";
$cache = MemcachedManager::get($path);
if ($cache) {
return $cache;
}
$client = ClientFactory::getClient();
$result = $client->get($path);
$user = static::createUserFromResult($result);
MemcachedManager::set($path, $result);
return $user;
}
To make this testable you need to refactor your code. You can mock dependencies in PHPUnit very easily and create a mock response for their public API method calls. For what you want to achieve, you will need to inject
ClientFactory
into the method and then you can use a mock object in it's place for the unit test.