Skip to content

Commit 1623d44

Browse files
pwaterzpolyfractal
authored andcommitted
[Docs] Updating readme with mock testing instructions (#591)
1 parent 69dbd4c commit 1623d44

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

README.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,27 @@ Array
317317
)
318318
```
319319

320-
320+
Unit Testing using Mock a Elastic Client
321+
========================================
322+
```php
323+
use GuzzleHttp\Ring\Client\MockHandler;
324+
use Elasticsearch\ClientBuilder;
325+
326+
// The connection class requires 'body' to be a file stream handle
327+
// Depending on what kind of request you do, you may need to set more values here
328+
$handler = new MockHandler([
329+
'status' => 200,
330+
'transfer_stats' => [
331+
'total_time' => 100
332+
],
333+
'body' => fopen('somefile.json')
334+
]);
335+
$builder = ClientBuilder::create();
336+
$builder->setHosts(['somehost']);
337+
$builder->setHandler($handler);
338+
$client = $builder->build();
339+
// Do a request and you'll get back the 'body' response above
340+
```
321341

322342
Wrap up
323343
=======

0 commit comments

Comments
 (0)