imdhemy / es-testing-utils
Elasticsearch Testing utils
Installs: 20 215
Dependents: 0
Suggesters: 0
Security: 0
Stars: 10
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- php: >=7.4
- ext-json: *
- elasticsearch/elasticsearch: ^8.4
- fakerphp/faker: ^1.20
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.10
- phpunit/phpunit: ^9.5
- vimeo/psalm: ^4.26
README
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