Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Protected Member Functions | Protected Attributes
ElasticsearchTest Class Reference
Inheritance diagram for ElasticsearchTest:

Public Member Functions

 testConstructorOptionsException ()
 
 testConstructorWithOptions ()
 
 testPing ()
 
 testTestConnection ()
 
 testTestConnectionFalse ()
 
 testTestConnectionPing ()
 
 testBulkQuery ()
 
 testCreateIndexExists ()
 
 testDeleteIndex ()
 
 testIsEmptyIndex ()
 
 testIsEmptyIndexFalse ()
 
 testUpdateAlias ()
 
 testUpdateAliasRemoveOldIndex ()
 
 testIndexExists ()
 
 testExistsAlias ()
 
 testExistsAliasWithIndex ()
 
 testGetAlias ()
 
 testCreateIndexFailure ()
 
 testAddFieldsMapping ()
 
 testAddFieldsMappingFailure ()
 
 testDeleteMapping ()
 
 testDeleteMappingFailure ()
 
 testQuery ()
 
 testSuggest ()
 

Protected Member Functions

 setUp ()
 
 getOptions ()
 
 getEmptyIndexOption ()
 

Protected Attributes

 $model
 
 $elasticsearchClientMock
 
 $indicesMock
 
 $objectManager
 

Detailed Description

Definition at line 11 of file ElasticsearchTest.php.

Member Function Documentation

◆ getEmptyIndexOption()

getEmptyIndexOption ( )
protected
Returns
array

Definition at line 550 of file ElasticsearchTest.php.

551  {
552  return [
553  'hostname' => 'localhost',
554  'port' => '9200',
555  'index' => '',
556  'timeout' => 15,
557  'enableAuth' => 1,
558  'username' => 'user',
559  'password' => 'passwd',
560  ];
561  }

◆ getOptions()

getOptions ( )
protected

Get elasticsearch client options

Returns
array

Definition at line 534 of file ElasticsearchTest.php.

535  {
536  return [
537  'hostname' => 'localhost',
538  'port' => '9200',
539  'timeout' => 15,
540  'index' => 'magento2',
541  'enableAuth' => 1,
542  'username' => 'user',
543  'password' => 'passwd',
544  ];
545  }

◆ setUp()

setUp ( )
protected

Setup

Returns
void

Definition at line 38 of file ElasticsearchTest.php.

39  {
40  $this->elasticsearchClientMock = $this->getMockBuilder(\Elasticsearch\Client::class)
41  ->setMethods([
42  'indices',
43  'ping',
44  'bulk',
45  'search',
46  'scroll',
47  'suggest',
48  'info',
49  ])
50  ->disableOriginalConstructor()
51  ->getMock();
52  $this->indicesMock = $this->getMockBuilder(\Elasticsearch\Namespaces\IndicesNamespace::class)
53  ->setMethods([
54  'exists',
55  'getSettings',
56  'create',
57  'delete',
58  'putMapping',
59  'deleteMapping',
60  'stats',
61  'updateAliases',
62  'existsAlias',
63  'getAlias',
64  ])
65  ->disableOriginalConstructor()
66  ->getMock();
67  $this->elasticsearchClientMock->expects($this->any())
68  ->method('indices')
69  ->willReturn($this->indicesMock);
70  $this->elasticsearchClientMock->expects($this->any())
71  ->method('ping')
72  ->willReturn(true);
73  $this->elasticsearchClientMock->expects($this->any())
74  ->method('info')
75  ->willReturn(['version' => ['number' => '5.0.0']]);
76 
77  $this->objectManager = new ObjectManagerHelper($this);
78  $this->model = $this->objectManager->getObject(
79  \Magento\Elasticsearch\Elasticsearch5\Model\Client\Elasticsearch::class,
80  [
81  'options' => $this->getOptions(),
82  'elasticsearchClient' => $this->elasticsearchClientMock
83  ]
84  );
85  }

◆ testAddFieldsMapping()

testAddFieldsMapping ( )

Test testAddFieldsMapping() method

Definition at line 335 of file ElasticsearchTest.php.

336  {
337  $this->indicesMock->expects($this->once())
338  ->method('putMapping')
339  ->with([
340  'index' => 'indexName',
341  'type' => 'product',
342  'body' => [
343  'product' => [
344  '_all' => [
345  'enabled' => true,
346  'type' => 'text',
347  ],
348  'properties' => [
349  'name' => [
350  'type' => 'text',
351  ],
352  ],
353  'dynamic_templates' => [
354  [
355  'price_mapping' => [
356  'match' => 'price_*',
357  'match_mapping_type' => 'string',
358  'mapping' => [
359  'type' => 'float',
360  'store' => true,
361  ],
362  ],
363  ],
364  [
365  'string_mapping' => [
366  'match' => '*',
367  'match_mapping_type' => 'string',
368  'mapping' => [
369  'type' => 'text',
370  'index' => false,
371  ],
372  ],
373  ],
374  [
375  'position_mapping' => [
376  'match' => 'position_*',
377  'match_mapping_type' => 'string',
378  'mapping' => [
379  'type' => 'int',
380  ],
381  ],
382  ],
383  ],
384  ],
385  ],
386  ]);
387  $this->model->addFieldsMapping(
388  [
389  'name' => [
390  'type' => 'text',
391  ],
392  ],
393  'indexName',
394  'product'
395  );
396  }

◆ testAddFieldsMappingFailure()

testAddFieldsMappingFailure ( )

Test testAddFieldsMapping() method @expectedException \Exception

Definition at line 402 of file ElasticsearchTest.php.

403  {
404  $this->indicesMock->expects($this->once())
405  ->method('putMapping')
406  ->with([
407  'index' => 'indexName',
408  'type' => 'product',
409  'body' => [
410  'product' => [
411  '_all' => [
412  'enabled' => true,
413  'type' => 'text',
414  ],
415  'properties' => [
416  'name' => [
417  'type' => 'text',
418  ],
419  ],
420  'dynamic_templates' => [
421  [
422  'price_mapping' => [
423  'match' => 'price_*',
424  'match_mapping_type' => 'string',
425  'mapping' => [
426  'type' => 'float',
427  'store' => true,
428  ],
429  ],
430  ],
431  [
432  'string_mapping' => [
433  'match' => '*',
434  'match_mapping_type' => 'string',
435  'mapping' => [
436  'type' => 'text',
437  'index' => false,
438  ],
439  ],
440  ],
441  [
442  'position_mapping' => [
443  'match' => 'position_*',
444  'match_mapping_type' => 'string',
445  'mapping' => [
446  'type' => 'int',
447  ],
448  ],
449  ],
450  ],
451  ],
452  ],
453  ])
454  ->willThrowException(new \Exception('Something went wrong'));
455  $this->model->addFieldsMapping(
456  [
457  'name' => [
458  'type' => 'text',
459  ],
460  ],
461  'indexName',
462  'product'
463  );
464  }

◆ testBulkQuery()

testBulkQuery ( )

Test bulkQuery() method

Definition at line 162 of file ElasticsearchTest.php.

163  {
164  $this->elasticsearchClientMock->expects($this->once())
165  ->method('bulk')
166  ->with([]);
167  $this->model->bulkQuery([]);
168  }

◆ testConstructorOptionsException()

testConstructorOptionsException ( )

@expectedException \Magento\Framework\Exception\LocalizedException

Definition at line 90 of file ElasticsearchTest.php.

91  {
92  $result = $this->objectManager->getObject(
93  \Magento\Elasticsearch\Model\Client\Elasticsearch::class,
94  [
95  'options' => []
96  ]
97  );
98  $this->assertNotNull($result);
99  }

◆ testConstructorWithOptions()

testConstructorWithOptions ( )

Test client creation from the list of options

Definition at line 104 of file ElasticsearchTest.php.

105  {
106  $result = $this->objectManager->getObject(
107  \Magento\Elasticsearch\Model\Client\Elasticsearch::class,
108  [
109  'options' => $this->getOptions()
110  ]
111  );
112  $this->assertNotNull($result);
113  }

◆ testCreateIndexExists()

testCreateIndexExists ( )

Test createIndex() method, case when such index exists

Definition at line 173 of file ElasticsearchTest.php.

174  {
175  $this->indicesMock->expects($this->once())
176  ->method('create')
177  ->with([
178  'index' => 'indexName',
179  'body' => [],
180  ]);
181  $this->model->createIndex('indexName', []);
182  }

◆ testCreateIndexFailure()

testCreateIndexFailure ( )

Test createIndexIfNotExists() method, case when operation fails @expectedException \Exception

Definition at line 320 of file ElasticsearchTest.php.

321  {
322  $this->indicesMock->expects($this->once())
323  ->method('create')
324  ->with([
325  'index' => 'indexName',
326  'body' => [],
327  ])
328  ->willThrowException(new \Exception('Something went wrong'));
329  $this->model->createIndex('indexName', []);
330  }

◆ testDeleteIndex()

testDeleteIndex ( )

Test deleteIndex() method.

Definition at line 187 of file ElasticsearchTest.php.

188  {
189  $this->indicesMock->expects($this->once())
190  ->method('delete')
191  ->with(['index' => 'indexName']);
192  $this->model->deleteIndex('indexName');
193  }

◆ testDeleteMapping()

testDeleteMapping ( )

Test deleteMapping() method

Definition at line 469 of file ElasticsearchTest.php.

470  {
471  $this->indicesMock->expects($this->once())
472  ->method('deleteMapping')
473  ->with([
474  'index' => 'indexName',
475  'type' => 'product',
476  ]);
477  $this->model->deleteMapping(
478  'indexName',
479  'product'
480  );
481  }

◆ testDeleteMappingFailure()

testDeleteMappingFailure ( )

Test deleteMapping() method @expectedException \Exception

Definition at line 487 of file ElasticsearchTest.php.

488  {
489  $this->indicesMock->expects($this->once())
490  ->method('deleteMapping')
491  ->with([
492  'index' => 'indexName',
493  'type' => 'product',
494  ])
495  ->willThrowException(new \Exception('Something went wrong'));
496  $this->model->deleteMapping(
497  'indexName',
498  'product'
499  );
500  }

◆ testExistsAlias()

testExistsAlias ( )

Tests existsAlias() method checking for alias.

Definition at line 276 of file ElasticsearchTest.php.

277  {
278  $alias = 'alias1';
279  $params = ['name' => $alias];
280  $this->indicesMock->expects($this->once())
281  ->method('existsAlias')
282  ->with($params)
283  ->willReturn(true);
284  $this->assertTrue($this->model->existsAlias($alias));
285  }
if(!trim($html)) $alias
Definition: details.phtml:20
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18

◆ testExistsAliasWithIndex()

testExistsAliasWithIndex ( )

Tests existsAlias() method checking for alias and index.

Definition at line 290 of file ElasticsearchTest.php.

291  {
292  $alias = 'alias1';
293  $index = 'index1';
294  $params = ['name' => $alias, 'index' => $index];
295  $this->indicesMock->expects($this->once())
296  ->method('existsAlias')
297  ->with($params)
298  ->willReturn(true);
299  $this->assertTrue($this->model->existsAlias($alias, $index));
300  }
if(!trim($html)) $alias
Definition: details.phtml:20
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18
$index
Definition: list.phtml:44

◆ testGetAlias()

testGetAlias ( )

Test getAlias() method.

Definition at line 305 of file ElasticsearchTest.php.

306  {
307  $alias = 'alias1';
308  $params = ['name' => $alias];
309  $this->indicesMock->expects($this->once())
310  ->method('getAlias')
311  ->with($params)
312  ->willReturn([]);
313  $this->assertEquals([], $this->model->getAlias($alias));
314  }
if(!trim($html)) $alias
Definition: details.phtml:20
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18

◆ testIndexExists()

testIndexExists ( )

Test indexExists() method, case when no such index exists

Definition at line 262 of file ElasticsearchTest.php.

263  {
264  $this->indicesMock->expects($this->once())
265  ->method('exists')
266  ->with([
267  'index' => 'indexName',
268  ])
269  ->willReturn(true);
270  $this->model->indexExists('indexName');
271  }

◆ testIsEmptyIndex()

testIsEmptyIndex ( )

Test isEmptyIndex() method.

Definition at line 198 of file ElasticsearchTest.php.

199  {
200  $indexName = 'magento2_index';
201  $stats['indices'][$indexName]['primaries']['docs']['count'] = 0;
202 
203  $this->indicesMock->expects($this->once())
204  ->method('stats')
205  ->with(['index' => $indexName, 'metric' => 'docs'])
206  ->willReturn($stats);
207  $this->assertTrue($this->model->isEmptyIndex($indexName));
208  }

◆ testIsEmptyIndexFalse()

testIsEmptyIndexFalse ( )

Test isEmptyIndex() method returns false.

Definition at line 213 of file ElasticsearchTest.php.

214  {
215  $indexName = 'magento2_index';
216  $stats['indices'][$indexName]['primaries']['docs']['count'] = 1;
217 
218  $this->indicesMock->expects($this->once())
219  ->method('stats')
220  ->with(['index' => $indexName, 'metric' => 'docs'])
221  ->willReturn($stats);
222  $this->assertFalse($this->model->isEmptyIndex($indexName));
223  }

◆ testPing()

testPing ( )

Test ping functionality

Definition at line 118 of file ElasticsearchTest.php.

119  {
120  $this->elasticsearchClientMock->expects($this->once())->method('ping')->willReturn(true);
121  $this->assertEquals(true, $this->model->ping());
122  }

◆ testQuery()

testQuery ( )

Test query() method

Returns
void

Definition at line 506 of file ElasticsearchTest.php.

507  {
508  $query = 'test phrase query';
509  $this->elasticsearchClientMock->expects($this->once())
510  ->method('search')
511  ->with($query)
512  ->willReturn([]);
513  $this->assertEquals([], $this->model->query($query));
514  }

◆ testSuggest()

testSuggest ( )

Test suggest() method

Returns
void

Definition at line 520 of file ElasticsearchTest.php.

521  {
522  $query = 'query';
523  $this->elasticsearchClientMock->expects($this->once())
524  ->method('suggest')
525  ->willReturn([]);
526  $this->assertEquals([], $this->model->suggest($query));
527  }

◆ testTestConnection()

testTestConnection ( )

Test validation of connection parameters

Definition at line 127 of file ElasticsearchTest.php.

128  {
129  $this->elasticsearchClientMock->expects($this->once())->method('ping')->willReturn(true);
130  $this->assertEquals(true, $this->model->testConnection());
131  }

◆ testTestConnectionFalse()

testTestConnectionFalse ( )

Test validation of connection parameters returns false

Definition at line 136 of file ElasticsearchTest.php.

137  {
138  $this->elasticsearchClientMock->expects($this->once())->method('ping')->willReturn(false);
139  $this->assertEquals(true, $this->model->testConnection());
140  }

◆ testTestConnectionPing()

testTestConnectionPing ( )

Test validation of connection parameters

Definition at line 145 of file ElasticsearchTest.php.

146  {
147  $this->model = $this->objectManager->getObject(
148  \Magento\Elasticsearch\Model\Client\Elasticsearch::class,
149  [
150  'options' => $this->getEmptyIndexOption(),
151  'elasticsearchClient' => $this->elasticsearchClientMock
152  ]
153  );
154 
155  $this->model->ping();
156  $this->assertEquals(true, $this->model->testConnection());
157  }

◆ testUpdateAlias()

testUpdateAlias ( )

Test updateAlias() method with new index.

Definition at line 228 of file ElasticsearchTest.php.

229  {
230  $alias = 'alias1';
231  $index = 'index1';
232 
233  $params['body']['actions'][] = ['add' => ['alias' => $alias, 'index' => $index]];
234 
235  $this->indicesMock->expects($this->once())
236  ->method('updateAliases')
237  ->with($params);
238  $this->model->updateAlias($alias, $index);
239  }
if(!trim($html)) $alias
Definition: details.phtml:20
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18
$index
Definition: list.phtml:44

◆ testUpdateAliasRemoveOldIndex()

testUpdateAliasRemoveOldIndex ( )

Test updateAlias() method with new and old index.

Definition at line 244 of file ElasticsearchTest.php.

245  {
246  $alias = 'alias1';
247  $newIndex = 'index1';
248  $oldIndex = 'indexOld';
249 
250  $params['body']['actions'][] = ['remove' => ['alias' => $alias, 'index' => $oldIndex]];
251  $params['body']['actions'][] = ['add' => ['alias' => $alias, 'index' => $newIndex]];
252 
253  $this->indicesMock->expects($this->once())
254  ->method('updateAliases')
255  ->with($params);
256  $this->model->updateAlias($alias, $newIndex, $oldIndex);
257  }
if(!trim($html)) $alias
Definition: details.phtml:20
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18

Field Documentation

◆ $elasticsearchClientMock

$elasticsearchClientMock
protected

Definition at line 21 of file ElasticsearchTest.php.

◆ $indicesMock

$indicesMock
protected

Definition at line 26 of file ElasticsearchTest.php.

◆ $model

$model
protected

Definition at line 16 of file ElasticsearchTest.php.

◆ $objectManager

$objectManager
protected

Definition at line 31 of file ElasticsearchTest.php.


The documentation for this class was generated from the following file: