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?
Should I have code that only exists for testing?
php;testing;repository
It's a matter of perspective. Writing tests for our code forces us to decouple the code. You COULD view this as code that exists only for the tests, but I think this is misleading.What you're actually doing is decoupling your code. You are removing hard dependencies and getting rid of the headaches that come with code that news up everything it needs. These are desirable goals in their own right. It ALSO has the effect that the code becomes more testable, so you can look at it either way.For code to be testable, it has to be good (used in a very sloppy way, I am aware that any discipline can be misused to produce a mess). For code to be good, it has to be testable (at least in large parts).You can take your pick what you like best: having automatic regression tests, or having a design that is easier to modify and/or refactor later on.