Unit testing a PHP API client

879 Views Asked by At

I am writing an API client in php that sends request to an API server to get the details of a person based on his ID on the API server. The API server has a token based authentication and when queried with the correct ID and the token, the details of that person are returned as a JSON response. I am comfortable with the implementation of the class. However, when it comes to unit testing, I have not much idea about what all should be the best test cases that I should write. Unfortunately, I couldn't find any resources on the same. I have written a few test cases that validates the request parameters and the end point to query. Can anyone shed some more light on unit testing API clients?

1

There are 1 best solutions below

0
On BEST ANSWER

I am not a test expert but I do test when creation libs and components, so these are my advices.

Usually when I do similar tasks as you described, I create a library as a facade of the remote API, that ( possibly ) simplifies them.

So I have two distinguished types of tests, "unit test" and "integration test": the former are test for library components to check that they give correct results against different data model passed in input, the latter to check if library call ( eg. "getPersonDetails" that internally makes one or more remote API requests ) gives the correct result.

In your case you may create an "integration test" for your "getPersonDetails" method to check if the remote API ( possibly TEST environment ) gives answer and if your method's response is what you expect. You can also try to pass wrong input data anche check to raise the correct exception etc.

Any internal components that are used within "getPersonDetails" can be tested in "unit test".