I have a service class which is responsible for instantiating repositories and using them. It makes an external Http request which ideally I would like to mock for testing. However, I am not quite sure the best way to do this.
For example, right now the class looks like this:
public function lookUp($identifier)
{
$client = new Curl();
$data = $this->provider->lookUp($identifier, $client);
// do stuff w the response
}
It is not possible to mock the Http response since it is contained within the method.
An alternative might be to do something like this:
public function lookUp($identifier, $client = null)
{
if ($client === null) {
$client = new Curl();
}
$data = $this->provider->lookUp($identifier, $client);
// do stuff w the response
}
and then I could have a $client for mocking in my test. However, this would mean changing (and messying) my code just for the purpose of the test.
Should I do this? Is there a better way to test this?