Skip to content

Commit 3798051

Browse files
committed
Added mock Elasticsearch client section in README
1 parent d8a07bc commit 3798051

File tree

1 file changed

+52
-1
lines changed

1 file changed

+52
-1
lines changed

README.md

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ This is the official PHP client for [Elasticsearch](https://www.elastic.co/elast
1818
- [Delete a document](#delete-a-document)
1919
- [Versioning](#versioning)
2020
- [Backward Incompatible Changes](#backward-incompatible-changes-)
21+
- [Mock the Elasticsearch client](#mock-the-elasticsearch-client)
2122
- [FAQ](#faq-)
2223
- [Contribute](#contribute-)
2324
- [License](#license-)
@@ -299,6 +300,56 @@ We tried to reduce the BC breaks as much as possible with `7.x` but there are so
299300

300301
You can have a look at the [BREAKING_CHANGES](BREAKING_CHANGES.md) file for more information.
301302

303+
## Mock the Elasticsearch client
304+
305+
If you need to mock the Elasticsearch client you just need to mock a
306+
[PSR-18](https://www.php-fig.org/psr/psr-18/) HTTP Client.
307+
308+
For instance, you can use the [php-http/mock-client](https://github.com/php-http/mock-client)
309+
as follows:
310+
311+
```php
312+
use Elastic\Elasticsearch\ClientBuilder;
313+
use Elastic\Elasticsearch\Response\Elasticsearch;
314+
use Http\Mock\Client;
315+
use Nyholm\Psr7\Response;
316+
317+
$mock = new Client(); // This is the mock client
318+
319+
$client = ClientBuilder::create()
320+
->setHttpClient($mock)
321+
->build();
322+
323+
// This is a PSR-7 response
324+
$response = new Response(
325+
200,
326+
[Elasticsearch::HEADER_CHECK => Elasticsearch::PRODUCT_NAME],
327+
'This is the body!'
328+
);
329+
$mock->addResponse($response);
330+
331+
$result = $client->info(); // Just calling an Elasticsearch endpoint
332+
333+
echo $result->asString(); // This is the body!
334+
```
335+
We are using the `ClientBuilder::setHttpClient()` to set the mock client.
336+
You can specify the response that you want to have using the `addResponse($response)`
337+
function. As you can see the `$response` is a PSR-7 response object. In this
338+
example we used the `Nyholm\Psr7\Response` object from the [nyholm/psr7](https://github.com/Nyholm/psr7)
339+
project. If you are using [PHPUnit](https://phpunit.de/) you can even mock the
340+
`ResponseInterface` as follows:
341+
342+
```php
343+
$response = $this->createMock('Psr\Http\Message\ResponseInterface');
344+
```
345+
346+
**Notice**: we added a special header in the HTTP response. This is the product check
347+
header, and it is required for guarantee that `elasticsearch-php` is communicating
348+
with an Elasticsearch server 8.0+.
349+
350+
For more information you can read the [Mock client](https://docs.php-http.org/en/latest/clients/mock-client.html)
351+
section of PHP-HTTP documentation.
352+
302353
## FAQ 🔮
303354

304355
### Where do I report issues with the client?
@@ -315,7 +366,7 @@ We welcome contributors to the project. Before you begin, a couple notes...
315366

316367
+ If you want to contribute to this project you need to subscribe to a [Contributor Agreement](https://www.elastic.co/contributor-agreement).
317368
+ Before opening a pull request, please create an issue to [discuss the scope of your proposal](https://github.com/elastic/elasticsearch-php/issues).
318-
+ If you want to send a PR for version `8.0` please use the `8.0` branch.
369+
+ If you want to send a PR for version `8.0` please use the `8.0` branch, for `8.1` use the `8.1` branch and so on.
319370
+ Never send PR to `master` unless you want to contribute to the development version of the client (`master` represents the next major version).
320371
+ Each PR should include a **unit test** using [PHPUnit](https://phpunit.de/). If you are not familiar with PHPUnit you can have a look at the [reference](https://phpunit.readthedocs.io/en/9.5/).
321372

0 commit comments

Comments
 (0)