1
|
|
|
<?php |
2
|
|
|
namespace tests; |
3
|
|
|
|
4
|
|
|
use Soupmix\ElasticSearch; |
5
|
|
|
|
6
|
|
|
class ElasticsearchTest extends \PHPUnit_Framework_TestCase |
7
|
|
|
{ |
8
|
|
|
/** |
9
|
|
|
* @var \Soupmix\ElasticSearch $client |
10
|
|
|
*/ |
11
|
|
|
protected $client = null; |
12
|
|
|
|
13
|
|
|
protected function setUp() |
14
|
|
|
{ |
15
|
|
|
$this->client = new ElasticSearch([ |
16
|
|
|
'db_name' => 'test', |
17
|
|
|
'hosts' => ['127.0.0.1:9200'], |
18
|
|
|
]); |
19
|
|
|
$this->client->drop('test'); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public function testInsertGetDocument() |
23
|
|
|
{ |
24
|
|
|
$docId = $this->client->insert('test', ['id' => 1, 'title' => 'test']); |
25
|
|
|
$document = $this->client->get('test', $docId); |
26
|
|
|
$this->assertArrayHasKey('title', $document); |
27
|
|
|
$this->assertArrayHasKey('id', $document); |
28
|
|
|
$result = $this->client->delete('test', ['_id' => $docId]); |
29
|
|
|
$this->assertTrue($result == 1); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function testFindDocuments() |
33
|
|
|
{ |
34
|
|
|
$docIds = []; |
35
|
|
|
$data = $this->bulkData(); |
36
|
|
|
foreach ($data as $d) { |
37
|
|
|
$docId = $this->client->insert('test', $d); |
38
|
|
|
$this->assertNotNull($docId, 'Document could not inserted to ES while testing find'); |
39
|
|
|
if ($docId) { |
40
|
|
|
$docIds[] = $docId; |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
sleep(1); // waiting to be able to be searchable on elasticsearch. |
44
|
|
|
$results = $this->client->find('test', ['title' => 'test1']); |
45
|
|
|
$this->assertArrayHasKey('total', $results); |
46
|
|
|
$this->assertArrayHasKey('data', $results); |
47
|
|
|
$this->assertCount($results['total'], $results['data']); |
48
|
|
|
$this->assertGreaterThanOrEqual(1, $results['total'], 'Total not equal for field term filtering'); |
49
|
|
|
|
50
|
|
|
$results = $this->client->find('test', ['count.max__gte' => 6]); |
51
|
|
|
$this->assertGreaterThanOrEqual(2, $results['total'], |
52
|
|
|
'Total not greater than or equal to 2 on count_gte filtering'); |
53
|
|
|
|
54
|
|
|
$results = $this->client->find('test', ['count.max__gte' => 6, 'count.min__gte' => 2]); |
55
|
|
|
$this->assertGreaterThanOrEqual(1, $results['total'], |
56
|
|
|
'Total not greater than or equal to 2 on count.max__gte and count.max__gte filtering'); |
57
|
|
|
|
58
|
|
|
$results = $this->client->find('test', [['count.max__gte' => 6, 'count.min__gte' => 2], 'title' => 'test4']); |
59
|
|
|
$this->assertGreaterThanOrEqual(1, $results['total'], |
60
|
|
|
'Total not greater than or equal to 2 on count.max__gte and count.max__gte filtering'); |
61
|
|
|
|
62
|
|
|
foreach ($docIds as $docId) { |
63
|
|
|
$result = $this->client->delete('test', ['_id' => $docId]); |
64
|
|
|
$this->assertTrue($result == 1); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function testInsertUpdateGetDocument() |
69
|
|
|
{ |
70
|
|
|
// $docId = $this->client->insert('test', ['id' => 1, 'title' => 'test']); |
|
|
|
|
71
|
|
|
// sleep(1); // waiting to be able to be searchable on elasticsearch. |
72
|
|
|
// $modifiedCount = $this->client->update('test', ['title' => 'test'], ['title' => 'test2']); |
73
|
|
|
// $this->assertTrue($modifiedCount >= 1); |
74
|
|
|
// sleep(1); // waiting to be able to be searchable on elasticsearch. |
75
|
|
|
// $document = $this->client->get('test', $docId); |
76
|
|
|
// $this->assertArrayHasKey('title', $document); |
77
|
|
|
// $this->assertEquals('test2', $document['title']); |
78
|
|
|
// |
79
|
|
|
// $result = $this->client->delete('test', ['_id' => $docId]); |
80
|
|
|
// $this->assertTrue($result == 1); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function bulkData() |
84
|
|
|
{ |
85
|
|
|
return [ |
86
|
|
|
['id' => 1, 'date' => '2015-04-10 00:00:00', 'title' => 'test1', 'balance' => 100.0, 'count' => ['min' => 1, 'max' => 1]], |
87
|
|
|
['id' => 2, 'date' => '2015-04-11 00:00:00', 'title' => 'test2', 'balance' => 120.0, 'count' => ['min' => 1, 'max' => 1]], |
88
|
|
|
['id' => 3, 'date' => '2015-04-12 00:00:00', 'title' => 'test3', 'balance' => 101.5, 'count' => ['min' => 1, 'max' => 7]], |
89
|
|
|
['id' => 4, 'date' => '2015-04-12 00:00:00', 'title' => 'test4', 'balance' => 200.5, 'count' => ['min' => 3, 'max' => 6]], |
90
|
|
|
['id' => 5, 'date' => '2015-04-13 00:00:00', 'title' => 'test5', 'balance' => 150.0, 'count' => ['min' => 1, 'max' => 5]], |
91
|
|
|
['id' => 6, 'date' => '2015-04-14 00:00:00', 'title' => 'test6', 'balance' => 400.8, 'count' => ['min' => 1, 'max' => 4]], |
92
|
|
|
['id' => 7, 'date' => '2015-04-15 00:00:00', 'title' => 'test7', 'balance' => 240.0, 'count' => ['min' => 1, 'max' => 4]], |
93
|
|
|
['id' => 8, 'date' => '2015-04-20 00:00:00', 'title' => 'test8', 'balance' => 760.0, 'count' => ['min' => 1, 'max' => 5]], |
94
|
|
|
['id' => 9, 'date' => '2015-04-20 00:00:00', 'title' => 'test9', 'balance' => 50.0, 'count' => ['min' => 1, 'max' => 2]], |
95
|
|
|
['id' => 10, 'date' => '2015-04-21 00:00:00', 'title' => 'test0', 'balance' => 55.5, 'count' => ['min' => 1, 'max' => 2]], |
96
|
|
|
]; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.