-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathElasticsearchResponseTest.php
More file actions
98 lines (77 loc) · 3.67 KB
/
ElasticsearchResponseTest.php
File metadata and controls
98 lines (77 loc) · 3.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<?php
use G4\DataMapper\Engine\Elasticsearch\ElasticsearchResponse;
class ElasticsearchResponseTest extends \PHPUnit\Framework\TestCase
{
/**
* @var string
*/
private $dataWithHits;
/**
* @var string
*/
private $dataWithError;
/**
* @var string
*/
private $errorMessage;
public function testGetHits()
{
$elasticsearchResponse = new ElasticsearchResponse($this->dataWithHits);
$this->assertFalse($elasticsearchResponse->hasError());
$this->assertArrayHasKey('total', $elasticsearchResponse->getHits());
$this->assertArrayHasKey('max_score', $elasticsearchResponse->getHits());
$this->assertArrayHasKey('hits', $elasticsearchResponse->getHits());
}
public function testGetHitsWithError()
{
$elasticsearchResponse = new ElasticsearchResponse($this->dataWithError);
$this->assertTrue($elasticsearchResponse->hasError());
$this->assertEquals($this->errorMessage, $elasticsearchResponse->getErrorMessage());
$this->assertArrayNotHasKey('total', $elasticsearchResponse->getHits());
$this->assertArrayNotHasKey('max_score', $elasticsearchResponse->getHits());
$this->assertArrayNotHasKey('hits', $elasticsearchResponse->getHits());
}
public function testGetTotal()
{
$elasticsearchResponse = new ElasticsearchResponse($this->dataWithHits);
$this->assertFalse($elasticsearchResponse->hasError());
$this->assertEquals(1, $elasticsearchResponse->getTotal());
}
public function testGetTotalWithError()
{
$elasticsearchResponse = new ElasticsearchResponse($this->dataWithError);
$this->assertTrue($elasticsearchResponse->hasError());
$this->assertEquals(0, $elasticsearchResponse->getTotal());
}
public function testNullResponse()
{
$elasticsearchResponse = new ElasticsearchResponse(null);
$this->assertTrue($elasticsearchResponse->hasError());
$this->assertEquals('["Error decoding response",null]', $elasticsearchResponse->getErrorMessage());
$this->assertEquals(0, $elasticsearchResponse->getTotal());
}
public function testGarbageResponse()
{
$elasticsearchResponse = new ElasticsearchResponse('some garbage');
$this->assertTrue($elasticsearchResponse->hasError());
$this->assertEquals('["Error decoding response","some garbage"]', $elasticsearchResponse->getErrorMessage());
$this->assertEquals(0, $elasticsearchResponse->getTotal());
}
protected function setUp(): void
{
/**
* @codingStandardsIgnoreStart
*/
$this->dataWithHits = '{"took":16,"timed_out":false,"_shards":{"total":1,"successful":1,"failed":0},"hits":{"total":1,"max_score":null,"hits":[1]}}';
$this->dataWithError = '{"error":{"root_cause":[{"type":"query_parsing_exception","reason":"field [location] is not a geo_point field","index":"profiles","line":1,"col":270}],"type":"search_phase_execution_exception","reason":"all shards failed","phase":"query_fetch","grouped":true,"failed_shards":[{"shard":0,"index":"profiles","node":"75N9T595S-eqHYV8_o08ng","reason":{"type":"query_parsing_exception","reason":"field [location] is not a geo_point field","index":"profiles","line":1,"col":270}}]},"status":400}';
$this->errorMessage = '["search_phase_execution_exception",[{"type":"query_parsing_exception","reason":"field [location] is not a geo_point field","index":"profiles","line":1,"col":270}]]';
/**
* @codingStandardsIgnoreEnd
*/
}
protected function tearDown(): void
{
$this->dataWithHits = null;
$this->dataWithError = null;
}
}