0

I am working with Meilisearch in my Laravel application, and am trying to figure out the best way I can mock my Meilisearch indexes.

The tests themselves are currently not that advanced, for example my test to simply create an index looks like this

public function test_create_index()
{
    $model = Package::factory()->create([
        'language' => 'DA'
    ]);
    sleep(1);

    $this->assertIsArray($this->meiliClient->index('Package_DA')->getDocument($model->id));

    $this->meiliClient->index('Package_DA')->deleteDocument($model->id);
}

I am using Laravel Observer to fire a method when a new model is created, which calls my Meilisearch Service to create a new index and insert a new record. The downside here though is that in order for Meilisearch to have time to register the new record in the index, I must sleep the script for 1 second, or else it won't have time to update. This is something I have to do everytime I call the Meilisearch client. Combine this with 8 calls in total, and my test file will take over 8 seconds to run.

Next issue is, as shown in the last code line, that I have to manually delete the created record from the index once the test is run. What I would like to do is to fake the Meilisearch calls somehow to not actually create the records, but simply test if the creation succeeded, similar to when you fake Events or Job dispatches.

I am considering making a separate trait to handle this, but I am not sure if it is even possible to achieve the faking of Meilisearch Indexes in the way that I want it.

I'm open for ideas or suggestions on how this could be done, or if it would even be possible

Thanks

Oskar Mikael
  • 153
  • 3
  • 17
  • You just need to use `Mockery` to mock your service (unless you are doing an integration test), what is the issue you have? – matiaslauriti Jun 07 '22 at 13:21
  • @matiaslauriti I found Mockery to be somewhat what I was looking for. The problem was that I want to Mock my requests to my Meilisearch client. Using the MockBuilder I am able to create a mocked client, and define what each method in the Meili client should return, without actually saving any records in Meilisearch – Oskar Mikael Jun 07 '22 at 13:54
  • 1
    Can you include some of these request or how you communicate with the meili client? That will help a lot. Couldn't an approach be to fake the events and test separately if the meili integration works? – mrhn Jun 11 '22 at 22:09

0 Answers0