1
|
|
|
<?php |
2
|
|
|
namespace Tests\Integration; |
3
|
|
|
|
4
|
|
|
use Elasticsearch\Client; |
5
|
|
|
use Tests\TestCase; |
6
|
|
|
use Triadev\Es\Mapping\Contract\ElasticsearchMappingContract; |
7
|
|
|
use Triadev\Es\Mapping\Facade\ElasticMapping; |
8
|
|
|
use Triadev\Es\Mapping\Mapping\Blueprint; |
9
|
|
|
|
10
|
|
|
class ElasticsearchMappingTest extends TestCase |
11
|
|
|
{ |
12
|
|
|
const INDEX = 'phpunit_index'; |
13
|
|
|
const TYPE = 'phpunit_type'; |
14
|
|
|
|
15
|
|
|
/** @var ElasticsearchMappingContract */ |
16
|
|
|
private $service; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Setup the test environment. |
20
|
|
|
*/ |
21
|
|
|
public function setUp() |
22
|
|
|
{ |
23
|
|
|
parent::setUp(); |
24
|
|
|
|
25
|
|
|
$this->service = app(ElasticsearchMappingContract::class); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Refresh elasticsearch mappings |
30
|
|
|
*/ |
31
|
|
|
private function refreshElasticsearchMappings() |
32
|
|
|
{ |
33
|
|
|
$this->deleteElasticsearchMappings(); |
34
|
|
|
|
35
|
|
|
ElasticMapping::getEsClient()->indices()->create([ |
36
|
|
|
'index' => self::INDEX |
37
|
|
|
]); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Delete elasticsearch mappings |
42
|
|
|
*/ |
43
|
|
|
private function deleteElasticsearchMappings() |
44
|
|
|
{ |
45
|
|
|
if (ElasticMapping::getEsClient()->indices()->exists(['index' => self::INDEX])) { |
46
|
|
|
ElasticMapping::getEsClient()->indices()->delete(['index' => self::INDEX]); |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return array |
52
|
|
|
*/ |
53
|
|
|
private function getEsMapping() : array |
54
|
|
|
{ |
55
|
|
|
return ElasticMapping::getEsClient()->indices()->getMapping(['index' => self::INDEX]); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @return array |
60
|
|
|
*/ |
61
|
|
|
private function getEsSetting() : array |
62
|
|
|
{ |
63
|
|
|
return ElasticMapping::getEsClient()->indices()->getSettings(['index' => self::INDEX]); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @test |
68
|
|
|
*/ |
69
|
|
|
public function it_gets_an_elasticsearch_client() |
70
|
|
|
{ |
71
|
|
|
$this->assertInstanceOf(Client::class, $this->service->getEsClient()); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @test |
76
|
|
|
*/ |
77
|
|
|
public function it_creates_a_new_index_with_mapping_and_settings() |
78
|
|
|
{ |
79
|
|
|
$this->deleteElasticsearchMappings(); |
80
|
|
|
|
81
|
|
|
$this->assertFalse(ElasticMapping::getEsClient()->indices()->exists(['index' => self::INDEX])); |
82
|
|
|
|
83
|
|
|
ElasticMapping::map($this->getTestBlueprintClosure(), self::INDEX, self::TYPE); |
84
|
|
|
|
85
|
|
|
$this->assertEquals($this->getMappingForEqualTest(), $this->getEsMapping()); |
86
|
|
|
|
87
|
|
|
$settings = $this->getEsSetting(); |
88
|
|
|
|
89
|
|
|
$this->assertEquals('30s', array_get($settings, 'phpunit_index.settings.index.refresh_interval')); |
90
|
|
|
$this->assertEquals(10, array_get($settings, 'phpunit_index.settings.index.number_of_replicas')); |
91
|
|
|
$this->assertEquals(12, array_get($settings, 'phpunit_index.settings.index.number_of_shards')); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @test |
96
|
|
|
*/ |
97
|
|
|
public function it_runs_a_mapping_update() |
98
|
|
|
{ |
99
|
|
|
$this->refreshElasticsearchMappings(); |
100
|
|
|
|
101
|
|
|
$this->assertEquals([ |
102
|
|
|
self::INDEX => [ |
103
|
|
|
'mappings' => [] |
104
|
|
|
] |
105
|
|
|
], $this->getEsMapping()); |
106
|
|
|
|
107
|
|
|
ElasticMapping::map($this->getTestBlueprintClosure(), self::INDEX, self::TYPE); |
108
|
|
|
|
109
|
|
|
$this->assertEquals($this->getMappingForEqualTest(), $this->getEsMapping()); |
110
|
|
|
|
111
|
|
|
$settings = $this->getEsSetting(); |
112
|
|
|
|
113
|
|
|
$this->assertNull(array_get($settings, 'phpunit_index.settings.index.refresh_interval')); |
114
|
|
|
$this->assertEquals(1, array_get($settings, 'phpunit_index.settings.index.number_of_replicas')); |
115
|
|
|
$this->assertEquals(5, array_get($settings, 'phpunit_index.settings.index.number_of_shards')); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
private function getTestBlueprintClosure() : \Closure |
119
|
|
|
{ |
120
|
|
|
return function (Blueprint $blueprint) { |
121
|
|
|
$blueprint->integer('id'); |
122
|
|
|
$blueprint->text('name'); |
123
|
|
|
$blueprint->keyword('email'); |
124
|
|
|
|
125
|
|
|
$blueprint->settings([ |
126
|
|
|
'index' => [ |
127
|
|
|
'number_of_replicas' => 10, |
128
|
|
|
'number_of_shards' => 12, |
129
|
|
|
'refresh_interval' => '30s' |
130
|
|
|
] |
131
|
|
|
]); |
132
|
|
|
}; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
private function getMappingForEqualTest() : array |
136
|
|
|
{ |
137
|
|
|
return [ |
138
|
|
|
self::INDEX => [ |
139
|
|
|
'mappings' => [ |
140
|
|
|
self::TYPE => [ |
141
|
|
|
'properties' => [ |
142
|
|
|
'id' => [ |
143
|
|
|
'type' => 'integer' |
144
|
|
|
], |
145
|
|
|
'name' => [ |
146
|
|
|
'type' => 'text' |
147
|
|
|
], |
148
|
|
|
'email' => [ |
149
|
|
|
'type' => 'keyword' |
150
|
|
|
] |
151
|
|
|
] |
152
|
|
|
] |
153
|
|
|
] |
154
|
|
|
] |
155
|
|
|
]; |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|