I'm writing a PHP wrapper around a third party API. More so for practice, but I also don't see a good one available anywhere at the moment so maybe it'll be used by others in the future.
My unit tests have been quite simple, except now I have hit a limit.
The developer of the API has got a max requests limit (1 per second, 20 per minute), and my unit tests are accessing the API endpoints via my API wrapper, hence testing my wrapper. However running phpunit
has started to return a 429 too many requests
error. Phpunit is obviously therefore running the 15 or so tests I have, all of which are accessing the endpoints too quickly and giving me this error.
Does anybody know if I a) should be mocking these responses, and b) how I would mock the responses if i'm testing my wrapper?. What good are tests if they're not running on my actual wrapper object and surely I don't want to be making my wrapper use mock responses?
I am new to unit testing, I feel very uncomfortable with the idea at the moment, however I'm beginning to warm to it!
Very good question! It's a common problem when you're new to testing.
Firstly, make a distinction between unit and integration testing:
I'd do the following:
Mocking responses is a tricky business. If you try doing it one day you'll run into troubles when the 3rd party API changes. If you still want to go this path, have a look at https://github.com/coduo/tutu.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With