'phpunit - fake api response or real response connecting to service?

I have created a few methods in a service class to connect to external service/provider via Guzzle using API POST Request.

I like to use phpunit for testing - should I use fake the HTTP Json response without connecting to a service or should it connect to a service to get real response from service?



Solution 1:[1]

You should mock external API calls. Your tests are intended to test YOUR code not the external API.

Note: Ultimately if you DO use the external API with your tests you will likely be connecting to a test version of the API (different to the live version of the API that your production environment would connect to) so you're not really ensuring the consistency of the API response anyway.

Solution 2:[2]

You don't want to keep testing a 3rd party remote API whenever you run your own app tests, even if you group/isolate them and don't run them often.

Test your code up to the point of calling the real API, then trigger a mock response. This tells you that all your code is working, and if the API would have been connectable and would have returned a response then everything is fine.

Unit tests are not for testing remote APIs, that is the job of some other area of the site to ensure is up, like server monitoring with it and your app issuing error logs on production when something with the remote API fails.

The chance is so low of running your tests at the time that the remote API has some issue, you will run your tests and the remote API works fine, and you deploy, then next day the remote API fails and no-one is running the tests.

You have to assume the remote API works 99% of the time, and when it doesn't you get an error log that the request timed out or returned a bad response etc. And if the remote only works a low amount of time like 50/60%, it's time to find another service provider.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 omarjebari
Solution 2 James