Testing API integrations has been one of those things I could not find an elegant solution for. This was before I found yii2’s http client. With the use of their “Transports” we can fake API calls for testing without actually hitting the API. By faking responses we can test how our applications handles different responses.
The main two advantages we get from this is our test are much faster, and we are not using are API quota for the service we are using. We are also testing how our applications handles the response from the API not the API itself. One thing to watch with this approach is that if the endpoints that you are hitting change, your test will not catch that. So it may be a good idea to have one test that hits the API, saves the results that all the other test use.
We can set our own transport in the app config if the environment is not production. You can always use ‘YII_ENV_TEST’ if you only want to use the transport in a testing environment.