Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 28 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

[![Tests](https://github.com/imdhemy/es-testing-utils/actions/workflows/tests.yml/badge.svg)](https://github.com/imdhemy/es-testing-utils/actions/workflows/tests.yml) [![Latest Stable Version](https://poser.pugx.org/imdhemy/es-testing-utils/v/stable)](https://packagist.org/packages/imdhemy/es-testing-utils) [![Total Downloads](https://poser.pugx.org/imdhemy/es-testing-utils/downloads)](https://packagist.org/packages/imdhemy/es-testing-utils)

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
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
Expand All @@ -23,7 +25,10 @@ composer require --dev imdhemy/es-testing-utils

## Usage

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

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

```php
use Imdhemy\EsUtils\EsMocker;
Expand Down Expand Up @@ -62,11 +67,30 @@ $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.

```php
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

- [Mohamad Eldhemy](https://imdhemy.com)
- [All Contributors](https://github.com/imdhemy/es-testing-utils/graphs/contributors)

## License

The ES testing utils is open-sourced software licensed under the [MIT license](/LICENSE)
The ES testing utils is open-sourced software licensed under
the [MIT license](/LICENSE)
18 changes: 12 additions & 6 deletions src/es-utils/Faker.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,7 @@ public function esIndex(): string
*/
public function esDeleteIndex(): array
{
return [
'acknowledged' => true,
];
return $this->esAcknowledged();
}

/**
Expand All @@ -178,9 +176,7 @@ public function esDeleteIndex(): array
*/
public function esPutIndexSettings(): array
{
return [
'acknowledged' => true,
];
return $this->esAcknowledged();
}

/**
Expand Down Expand Up @@ -225,6 +221,16 @@ public function esGetIndexSettings(?string $index = null, array $settings = []):
* @return array
*/
public function esPutIndexMappings(): array
{
return $this->esAcknowledged();
}

/**
* Generates es acknowledged response
*
* @return array
*/
public function esAcknowledged(): array
{
return [
'acknowledged' => true,
Expand Down
15 changes: 15 additions & 0 deletions tests/Unit/FakerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,4 +192,19 @@ public function es_put_index_mappings(): void
$this->sut->esPutIndexMappings()
);
}

/**
* @test
*/
public function es_acknowledge_response(): void
{
$expected = [
'acknowledged' => true,
];

$this->assertEquals(
$expected,
$this->sut->esAcknowledged()
);
}
}