1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Cviebrock\LaravelElasticsearch\Tests\Console\Command; |
6
|
|
|
|
7
|
|
|
use Cviebrock\LaravelElasticsearch\Tests\TestCase; |
8
|
|
|
use Elasticsearch\Client; |
9
|
|
|
use Elasticsearch\Namespaces\IndicesNamespace; |
10
|
|
|
use Exception; |
11
|
|
|
use Generator; |
12
|
|
|
use Illuminate\Contracts\Filesystem\Filesystem; |
13
|
|
|
use Mockery\MockInterface; |
14
|
|
|
|
15
|
|
|
final class IndexCreateOrUpdateMappingCommandTest extends TestCase |
|
|
|
|
16
|
|
|
{ |
17
|
|
View Code Duplication |
public function testCreateOrUpdateMappingMustSucceed(): void |
|
|
|
|
18
|
|
|
{ |
19
|
|
|
$this->mock(Filesystem::class, function (MockInterface $mock) { |
|
|
|
|
20
|
|
|
$mock->shouldReceive('exists') |
21
|
|
|
->once() |
22
|
|
|
->andReturn(true); |
23
|
|
|
|
24
|
|
|
$mock->shouldReceive('get') |
25
|
|
|
->once() |
26
|
|
|
->andReturn('{}'); |
27
|
|
|
}); |
28
|
|
|
|
29
|
|
|
$this->mock(Client::class, function (MockInterface $mock) { |
|
|
|
|
30
|
|
|
$mock->shouldReceive('indices') |
31
|
|
|
->times(2) |
32
|
|
|
->andReturn( |
33
|
|
|
$this->mock(IndicesNamespace::class, function (MockInterface $mock) { |
|
|
|
|
34
|
|
|
$mock->shouldReceive('exists') |
35
|
|
|
->once() |
36
|
|
|
->andReturn(true); |
37
|
|
|
|
38
|
|
|
$mock->shouldReceive('putMapping') |
39
|
|
|
->once() |
40
|
|
|
->andReturn([]); |
41
|
|
|
}) |
42
|
|
|
); |
43
|
|
|
}); |
44
|
|
|
|
45
|
|
|
$this->artisan( |
46
|
|
|
'laravel-elasticsearch:utils:index-create-or-update-mapping', |
47
|
|
|
[ |
48
|
|
|
'index-name' => 'valid_index_name', |
49
|
|
|
'mapping-file-path' => '/path/to/existing_mapping_file.json', |
50
|
|
|
] |
51
|
|
|
)->assertExitCode(0) |
52
|
|
|
->expectsOutput('Mapping created or updated for index valid_index_name using file /path/to/existing_mapping_file.json.'); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function testCreateOrUpdateMappingMustFailBecauseMappingFileDoesntExistsOnFilesystem(): void |
56
|
|
|
{ |
57
|
|
|
$this->mock(Filesystem::class, function (MockInterface $mock) { |
|
|
|
|
58
|
|
|
$mock->shouldReceive('exists') |
59
|
|
|
->once() |
60
|
|
|
->andReturn(false); |
61
|
|
|
}); |
62
|
|
|
|
63
|
|
|
$this->mock(Client::class, function (MockInterface $mock) { |
|
|
|
|
64
|
|
|
$mock->shouldNotReceive('indices'); |
65
|
|
|
}); |
66
|
|
|
|
67
|
|
|
$this->artisan( |
68
|
|
|
'laravel-elasticsearch:utils:index-create-or-update-mapping', |
69
|
|
|
[ |
70
|
|
|
'index-name' => 'valid_index_name', |
71
|
|
|
'mapping-file-path' => '/path/to/non_existing_mapping_file.json', |
72
|
|
|
] |
73
|
|
|
)->assertExitCode(1) |
74
|
|
|
->expectsOutput('Argument mapping-file-path must exists on filesystem and must be a non empty string.'); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
View Code Duplication |
public function testCreateOrUpdateMappingMustCreateNewIndexIfIndexDoesntExists(): void |
|
|
|
|
78
|
|
|
{ |
79
|
|
|
$this->mock(Filesystem::class, function (MockInterface $mock) { |
|
|
|
|
80
|
|
|
$mock->shouldReceive('exists') |
81
|
|
|
->once() |
82
|
|
|
->andReturn(true); |
83
|
|
|
|
84
|
|
|
$mock->shouldReceive('get') |
85
|
|
|
->once() |
86
|
|
|
->andReturn('{}'); |
87
|
|
|
}); |
88
|
|
|
|
89
|
|
|
$this->mock(Client::class, function (MockInterface $mock) { |
|
|
|
|
90
|
|
|
$mock->shouldReceive('indices') |
91
|
|
|
->times(2) |
92
|
|
|
->andReturn( |
93
|
|
|
$this->mock(IndicesNamespace::class, function (MockInterface $mock) { |
|
|
|
|
94
|
|
|
$mock->shouldReceive('exists') |
95
|
|
|
->once() |
96
|
|
|
->andReturn(false); |
97
|
|
|
|
98
|
|
|
$mock->shouldReceive('create') |
99
|
|
|
->once() |
100
|
|
|
->andReturn(true); |
101
|
|
|
}) |
102
|
|
|
); |
103
|
|
|
}); |
104
|
|
|
|
105
|
|
|
$this->artisan( |
106
|
|
|
'laravel-elasticsearch:utils:index-create-or-update-mapping', |
107
|
|
|
[ |
108
|
|
|
'index-name' => 'valid_index_name', |
109
|
|
|
'mapping-file-path' => '/path/to/existing_mapping_file.json', |
110
|
|
|
] |
111
|
|
|
)->assertExitCode(0) |
112
|
|
|
->expectsOutput('Index valid_index_name doesn\'t exist, a new index was created with mapping/settings using file /path/to/existing_mapping_file.json.'); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
View Code Duplication |
public function testCreateOrUpdateMappingMustFail(): void |
|
|
|
|
116
|
|
|
{ |
117
|
|
|
$this->mock(Filesystem::class, function (MockInterface $mock) { |
|
|
|
|
118
|
|
|
$mock->shouldReceive('exists') |
119
|
|
|
->once() |
120
|
|
|
->andReturn(true); |
121
|
|
|
|
122
|
|
|
$mock->shouldReceive('get') |
123
|
|
|
->once() |
124
|
|
|
->andReturn('{}'); |
125
|
|
|
}); |
126
|
|
|
|
127
|
|
|
$this->mock(Client::class, function (MockInterface $mock) { |
|
|
|
|
128
|
|
|
$mock->shouldReceive('indices') |
129
|
|
|
->times(2) |
130
|
|
|
->andReturn( |
131
|
|
|
$this->mock(IndicesNamespace::class, function (MockInterface $mock) { |
|
|
|
|
132
|
|
|
$mock->shouldReceive('exists') |
133
|
|
|
->once() |
134
|
|
|
->andReturn(true); |
135
|
|
|
|
136
|
|
|
$mock->shouldReceive('putMapping') |
137
|
|
|
->once() |
138
|
|
|
->andThrow( |
139
|
|
|
new Exception('error creating or updating mapping test exception') |
140
|
|
|
); |
141
|
|
|
}) |
142
|
|
|
); |
143
|
|
|
}); |
144
|
|
|
|
145
|
|
|
$this->artisan( |
146
|
|
|
'laravel-elasticsearch:utils:index-create-or-update-mapping', |
147
|
|
|
[ |
148
|
|
|
'index-name' => 'valid_index_name', |
149
|
|
|
'mapping-file-path' => '/path/to/existing_mapping_file.json', |
150
|
|
|
] |
151
|
|
|
)->assertExitCode(1) |
152
|
|
|
->expectsOutput( |
153
|
|
|
'Error creating or updating mapping for index valid_index_name, given mapping file: /path/to/existing_mapping_file.json - error message: error creating or updating mapping test exception.' |
154
|
|
|
); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @dataProvider invalidIndexNameDataProvider |
159
|
|
|
*/ |
160
|
|
|
public function testArgumentIndexNameAndAliasAreInValid( |
161
|
|
|
$invalidIndexName, |
162
|
|
|
$invalidAliasName, |
163
|
|
|
string $expectedOutputMessage |
164
|
|
|
): void { |
165
|
|
|
$this->artisan('laravel-elasticsearch:utils:index-create-or-update-mapping', |
166
|
|
|
[ |
167
|
|
|
'index-name' => $invalidIndexName, |
168
|
|
|
'mapping-file-path' => $invalidAliasName, |
169
|
|
|
] |
170
|
|
|
)->assertExitCode(1) |
171
|
|
|
->expectsOutput($expectedOutputMessage); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
public function invalidIndexNameDataProvider(): Generator |
175
|
|
|
{ |
176
|
|
|
yield [ |
177
|
|
|
null, |
178
|
|
|
'/valid/path/mapping.json', |
179
|
|
|
'Argument index-name must be a non empty string.' |
180
|
|
|
]; |
181
|
|
|
|
182
|
|
|
yield [ |
183
|
|
|
'', |
184
|
|
|
'/valid/path/mapping.json', |
185
|
|
|
'Argument index-name must be a non empty string.' |
186
|
|
|
]; |
187
|
|
|
|
188
|
|
|
yield [ |
189
|
|
|
true, |
190
|
|
|
'/valid/path/mapping.json', |
191
|
|
|
'Argument index-name must be a non empty string.' |
192
|
|
|
]; |
193
|
|
|
|
194
|
|
|
yield [ |
195
|
|
|
1, |
196
|
|
|
'/valid/path/mapping.json', |
197
|
|
|
'Argument index-name must be a non empty string.' |
198
|
|
|
]; |
199
|
|
|
|
200
|
|
|
yield [ |
201
|
|
|
[], |
202
|
|
|
'/valid/path/mapping.json', |
203
|
|
|
'Argument index-name must be a non empty string.' |
204
|
|
|
]; |
205
|
|
|
|
206
|
|
|
yield [ |
207
|
|
|
'valid_index_name', |
208
|
|
|
null, |
209
|
|
|
'Argument mapping-file-path must exists on filesystem and must be a non empty string.' |
210
|
|
|
]; |
211
|
|
|
|
212
|
|
|
yield [ |
213
|
|
|
'valid_index_name', |
214
|
|
|
'', |
215
|
|
|
'Argument mapping-file-path must exists on filesystem and must be a non empty string.' |
216
|
|
|
]; |
217
|
|
|
|
218
|
|
|
yield [ |
219
|
|
|
'valid_index_name', |
220
|
|
|
true, |
221
|
|
|
'Argument mapping-file-path must exists on filesystem and must be a non empty string.' |
222
|
|
|
]; |
223
|
|
|
|
224
|
|
|
yield [ |
225
|
|
|
'valid_index_name', |
226
|
|
|
1, |
227
|
|
|
'Argument mapping-file-path must exists on filesystem and must be a non empty string.' |
228
|
|
|
]; |
229
|
|
|
|
230
|
|
|
yield [ |
231
|
|
|
'valid_index_name', |
232
|
|
|
[], |
233
|
|
|
'Argument mapping-file-path must exists on filesystem and must be a non empty string.' |
234
|
|
|
]; |
235
|
|
|
} |
236
|
|
|
} |
237
|
|
|
|