imdhemy/es-testing-utils

Elasticsearch Testing utils

8.3.0 2022-10-07 09:51 UTC

This package is auto-updated.

Last update: 2024-11-07 15:45:46 UTC


README

Tests Latest Stable Version Total Downloads

Unit tests shouldn't depend on a running cluster, should be mocked out instead. To be more specific, the client responses should be mocked out. Elastic search testing utils makes it super easy for you to mock Elasticsearch responses.

Installation

You can use composer

composer require --dev imdhemy/es-testing-utils

Versions

Usage

Mocker

Es testing utils provides you a fluent Elasticsearch mock builder, you can use it as follows:

use Imdhemy\EsUtils\EsMocker;

// Create ES client that returns the mock response
$client = EsMocker::mock(['tagline' => 'You know, for search.'])->build();

Or you can mock a sequence of responses:

use Imdhemy\EsUtils\EsMocker;

// The created client will return the `$info` response with the first request,
// and the `$search` response with the second request, and so on.
// Note: the `thenFail()` method mocks a request exception.
$client = EsMocker::mock($info)->then($index)->then($search)->thenFail($error)->build();

// Or you can directly fail the first request:
$client = EsMocker::fail($message)->build();

Below is a complete example of how to use EsMocker in a test:

use Imdhemy\EsUtils\EsMocker;

$expected=['tagline' => 'You know, for search.'];
$client = EsMocker::mock($expected)->build();

$response = $client->info();
$body = (string) $response->getBody();

$this->assertEquals(json_encode($expected), $body);

Faker

The faker class provides you a set of methods to generate random data for your tests. It provides all the methods of the Faker library along with new methods to generate Elasticsearch data. All the methods related to Elasticsearch starts with es prefix.

use Imdhemy\EsUtils\Faker;

$faker = Faker::create();

$index = $faker->esIndexName(); // Returns a random index name
$createIndex = $faker->esCreateIndex(); // Returns create index response body

// Explore the Faker class to see all the available methods

Credits

License

The ES testing utils is open-sourced software licensed under the MIT license