Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Reached's avatar
Level 11

Writing a mock api for Algolia testing (ideas and feedback needed)

I am writing a bunch of tests to gain more confidence in my codebase. The tests work fine, but as part of the app there is a call that is made with Scout to Algolia and it returns some geobased results. After searching I haven't found a good way to test this behavior, so I was thinking about mocking up the API results from Algolia and then testing against that instead.

My concern is that potentially this will be very brittle, and I am unsure of whether or not it will actually serve my purpose or not.

Does anyone have any experience doing this or something similar? I am not looking for a solution, just some good ideas/advice I guess, my own solution would be to write a class with some different methods depending on what query parameters are sent with the "request".

Thanks in advance!

0 likes
5 replies
audunru's avatar

I was in the same situation recently. Not Algolia, but something else. I ended up writing tests that run against a sandboxed version of the external API. I thought about mocking the external API responses (I.e. saving the JSON response to a file and just using that from then in), but decided against it as there were just so many unknowns about integrating with the external API that I felt I had to actually connect to it to check that my code does what its supposed to. I guess when my own code is stable I could revert to mocking it, but in the process of learning how the external API works I thought this was a good approach.

1 like
Reached's avatar
Level 11

@audunru thanks for your reply! Using a sandbox is a great idea, although I am not completely sure that Algolia has something like this :/.

Would be great with some kind of API sandbox environment where you could upload a JSON file and mock it this way - perhaps a new side project idea to pursue :)

audunru's avatar

I'm sure you can set up multiple indexes or whatever they are called in Algolia, and then run your tests against your dummy index.

I just finished watching this https://laracasts.com/lessons/to-mock-external-services and it's basically the same reasoning behind why I wrote my tests to connect to the API.

Here are some test examples from the project I was referring to: https://github.com/audunru/fiken-api-php-client/blob/master/tests/Feature/ContactTest.php

Please or to participate in this conversation.