ElasticsearchTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Elasticsearch6\Test\Unit\Model\Client;
  7. use Magento\Elasticsearch\Model\Client\Elasticsearch as ElasticsearchClient;
  8. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
  9. class ElasticsearchTest extends \PHPUnit\Framework\TestCase
  10. {
  11. /**
  12. * @var ElasticsearchClient
  13. */
  14. protected $model;
  15. /**
  16. * @var \Elasticsearch\Client|\PHPUnit_Framework_MockObject_MockObject
  17. */
  18. protected $elasticsearchClientMock;
  19. /**
  20. * @var \Elasticsearch\Namespaces\IndicesNamespace|\PHPUnit_Framework_MockObject_MockObject
  21. */
  22. protected $indicesMock;
  23. /**
  24. * @var ObjectManagerHelper
  25. */
  26. protected $objectManager;
  27. /**
  28. * Setup
  29. *
  30. * @return void
  31. */
  32. protected function setUp()
  33. {
  34. $this->elasticsearchClientMock = $this->getMockBuilder(\Elasticsearch\Client::class)
  35. ->setMethods([
  36. 'indices',
  37. 'ping',
  38. 'bulk',
  39. 'search',
  40. 'scroll',
  41. 'suggest',
  42. 'info',
  43. ])
  44. ->disableOriginalConstructor()
  45. ->getMock();
  46. $this->indicesMock = $this->getMockBuilder(\Elasticsearch\Namespaces\IndicesNamespace::class)
  47. ->setMethods([
  48. 'exists',
  49. 'getSettings',
  50. 'create',
  51. 'delete',
  52. 'putMapping',
  53. 'deleteMapping',
  54. 'stats',
  55. 'updateAliases',
  56. 'existsAlias',
  57. 'getAlias',
  58. ])
  59. ->disableOriginalConstructor()
  60. ->getMock();
  61. $this->elasticsearchClientMock->expects($this->any())
  62. ->method('indices')
  63. ->willReturn($this->indicesMock);
  64. $this->elasticsearchClientMock->expects($this->any())
  65. ->method('ping')
  66. ->willReturn(true);
  67. $this->elasticsearchClientMock->expects($this->any())
  68. ->method('info')
  69. ->willReturn(['version' => ['number' => '6.0.0']]);
  70. $this->objectManager = new ObjectManagerHelper($this);
  71. $this->model = $this->objectManager->getObject(
  72. \Magento\Elasticsearch6\Model\Client\Elasticsearch::class,
  73. [
  74. 'options' => $this->getOptions(),
  75. 'elasticsearchClient' => $this->elasticsearchClientMock
  76. ]
  77. );
  78. }
  79. /**
  80. * @expectedException \Magento\Framework\Exception\LocalizedException
  81. */
  82. public function testConstructorOptionsException()
  83. {
  84. $result = $this->objectManager->getObject(
  85. \Magento\Elasticsearch6\Model\Client\Elasticsearch::class,
  86. [
  87. 'options' => []
  88. ]
  89. );
  90. $this->assertNotNull($result);
  91. }
  92. /**
  93. * Test client creation from the list of options
  94. */
  95. public function testConstructorWithOptions()
  96. {
  97. $result = $this->objectManager->getObject(
  98. \Magento\Elasticsearch6\Model\Client\Elasticsearch::class,
  99. [
  100. 'options' => $this->getOptions()
  101. ]
  102. );
  103. $this->assertNotNull($result);
  104. }
  105. /**
  106. * Test ping functionality
  107. */
  108. public function testPing()
  109. {
  110. $this->elasticsearchClientMock->expects($this->once())->method('ping')->willReturn(true);
  111. $this->assertEquals(true, $this->model->ping());
  112. }
  113. /**
  114. * Test validation of connection parameters
  115. */
  116. public function testTestConnection()
  117. {
  118. $this->elasticsearchClientMock->expects($this->once())->method('ping')->willReturn(true);
  119. $this->assertEquals(true, $this->model->testConnection());
  120. }
  121. /**
  122. * Test validation of connection parameters returns false
  123. */
  124. public function testTestConnectionFalse()
  125. {
  126. $this->elasticsearchClientMock->expects($this->once())->method('ping')->willReturn(false);
  127. $this->assertEquals(true, $this->model->testConnection());
  128. }
  129. /**
  130. * Test validation of connection parameters
  131. */
  132. public function testTestConnectionPing()
  133. {
  134. $this->model = $this->objectManager->getObject(
  135. \Magento\Elasticsearch6\Model\Client\Elasticsearch::class,
  136. [
  137. 'options' => $this->getEmptyIndexOption(),
  138. 'elasticsearchClient' => $this->elasticsearchClientMock
  139. ]
  140. );
  141. $this->model->ping();
  142. $this->assertEquals(true, $this->model->testConnection());
  143. }
  144. /**
  145. * Test bulkQuery() method
  146. */
  147. public function testBulkQuery()
  148. {
  149. $this->elasticsearchClientMock->expects($this->once())
  150. ->method('bulk')
  151. ->with([]);
  152. $this->model->bulkQuery([]);
  153. }
  154. /**
  155. * Test createIndex() method, case when such index exists
  156. */
  157. public function testCreateIndexExists()
  158. {
  159. $this->indicesMock->expects($this->once())
  160. ->method('create')
  161. ->with([
  162. 'index' => 'indexName',
  163. 'body' => [],
  164. ]);
  165. $this->model->createIndex('indexName', []);
  166. }
  167. /**
  168. * Test deleteIndex() method.
  169. */
  170. public function testDeleteIndex()
  171. {
  172. $this->indicesMock->expects($this->once())
  173. ->method('delete')
  174. ->with(['index' => 'indexName']);
  175. $this->model->deleteIndex('indexName');
  176. }
  177. /**
  178. * Test isEmptyIndex() method.
  179. */
  180. public function testIsEmptyIndex()
  181. {
  182. $indexName = 'magento2_index';
  183. $stats['indices'][$indexName]['primaries']['docs']['count'] = 0;
  184. $this->indicesMock->expects($this->once())
  185. ->method('stats')
  186. ->with(['index' => $indexName, 'metric' => 'docs'])
  187. ->willReturn($stats);
  188. $this->assertTrue($this->model->isEmptyIndex($indexName));
  189. }
  190. /**
  191. * Test isEmptyIndex() method returns false.
  192. */
  193. public function testIsEmptyIndexFalse()
  194. {
  195. $indexName = 'magento2_index';
  196. $stats['indices'][$indexName]['primaries']['docs']['count'] = 1;
  197. $this->indicesMock->expects($this->once())
  198. ->method('stats')
  199. ->with(['index' => $indexName, 'metric' => 'docs'])
  200. ->willReturn($stats);
  201. $this->assertFalse($this->model->isEmptyIndex($indexName));
  202. }
  203. /**
  204. * Test updateAlias() method with new index.
  205. */
  206. public function testUpdateAlias()
  207. {
  208. $alias = 'alias1';
  209. $index = 'index1';
  210. $params['body']['actions'][] = ['add' => ['alias' => $alias, 'index' => $index]];
  211. $this->indicesMock->expects($this->once())
  212. ->method('updateAliases')
  213. ->with($params);
  214. $this->model->updateAlias($alias, $index);
  215. }
  216. /**
  217. * Test updateAlias() method with new and old index.
  218. */
  219. public function testUpdateAliasRemoveOldIndex()
  220. {
  221. $alias = 'alias1';
  222. $newIndex = 'index1';
  223. $oldIndex = 'indexOld';
  224. $params['body']['actions'][] = ['remove' => ['alias' => $alias, 'index' => $oldIndex]];
  225. $params['body']['actions'][] = ['add' => ['alias' => $alias, 'index' => $newIndex]];
  226. $this->indicesMock->expects($this->once())
  227. ->method('updateAliases')
  228. ->with($params);
  229. $this->model->updateAlias($alias, $newIndex, $oldIndex);
  230. }
  231. /**
  232. * Test indexExists() method, case when no such index exists
  233. */
  234. public function testIndexExists()
  235. {
  236. $this->indicesMock->expects($this->once())
  237. ->method('exists')
  238. ->with([
  239. 'index' => 'indexName',
  240. ])
  241. ->willReturn(true);
  242. $this->model->indexExists('indexName');
  243. }
  244. /**
  245. * Tests existsAlias() method checking for alias.
  246. */
  247. public function testExistsAlias()
  248. {
  249. $alias = 'alias1';
  250. $params = ['name' => $alias];
  251. $this->indicesMock->expects($this->once())
  252. ->method('existsAlias')
  253. ->with($params)
  254. ->willReturn(true);
  255. $this->assertTrue($this->model->existsAlias($alias));
  256. }
  257. /**
  258. * Tests existsAlias() method checking for alias and index.
  259. */
  260. public function testExistsAliasWithIndex()
  261. {
  262. $alias = 'alias1';
  263. $index = 'index1';
  264. $params = ['name' => $alias, 'index' => $index];
  265. $this->indicesMock->expects($this->once())
  266. ->method('existsAlias')
  267. ->with($params)
  268. ->willReturn(true);
  269. $this->assertTrue($this->model->existsAlias($alias, $index));
  270. }
  271. /**
  272. * Test getAlias() method.
  273. */
  274. public function testGetAlias()
  275. {
  276. $alias = 'alias1';
  277. $params = ['name' => $alias];
  278. $this->indicesMock->expects($this->once())
  279. ->method('getAlias')
  280. ->with($params)
  281. ->willReturn([]);
  282. $this->assertEquals([], $this->model->getAlias($alias));
  283. }
  284. /**
  285. * Test createIndexIfNotExists() method, case when operation fails
  286. * @expectedException \Exception
  287. */
  288. public function testCreateIndexFailure()
  289. {
  290. $this->indicesMock->expects($this->once())
  291. ->method('create')
  292. ->with([
  293. 'index' => 'indexName',
  294. 'body' => [],
  295. ])
  296. ->willThrowException(new \Exception('Something went wrong'));
  297. $this->model->createIndex('indexName', []);
  298. }
  299. /**
  300. * Test testAddFieldsMapping() method
  301. */
  302. public function testAddFieldsMapping()
  303. {
  304. $this->indicesMock->expects($this->once())
  305. ->method('putMapping')
  306. ->with([
  307. 'index' => 'indexName',
  308. 'type' => 'product',
  309. 'body' => [
  310. 'product' => [
  311. 'properties' => [
  312. '_search' => [
  313. 'type' => 'text',
  314. ],
  315. 'name' => [
  316. 'type' => 'text',
  317. ],
  318. ],
  319. 'dynamic_templates' => [
  320. [
  321. 'price_mapping' => [
  322. 'match' => 'price_*',
  323. 'match_mapping_type' => 'string',
  324. 'mapping' => [
  325. 'type' => 'float',
  326. 'store' => true,
  327. ],
  328. ],
  329. ],
  330. [
  331. 'string_mapping' => [
  332. 'match' => '*',
  333. 'match_mapping_type' => 'string',
  334. 'mapping' => [
  335. 'type' => 'text',
  336. 'index' => false,
  337. 'copy_to' => '_search'
  338. ],
  339. ],
  340. ],
  341. [
  342. 'position_mapping' => [
  343. 'match' => 'position_*',
  344. 'match_mapping_type' => 'string',
  345. 'mapping' => [
  346. 'type' => 'int',
  347. ],
  348. ],
  349. ],
  350. ],
  351. ],
  352. ],
  353. ]);
  354. $this->model->addFieldsMapping(
  355. [
  356. 'name' => [
  357. 'type' => 'text',
  358. ],
  359. ],
  360. 'indexName',
  361. 'product'
  362. );
  363. }
  364. /**
  365. * Test testAddFieldsMapping() method
  366. * @expectedException \Exception
  367. */
  368. public function testAddFieldsMappingFailure()
  369. {
  370. $this->indicesMock->expects($this->once())
  371. ->method('putMapping')
  372. ->with([
  373. 'index' => 'indexName',
  374. 'type' => 'product',
  375. 'body' => [
  376. 'product' => [
  377. 'properties' => [
  378. '_search' => [
  379. 'type' => 'text',
  380. ],
  381. 'name' => [
  382. 'type' => 'text',
  383. ],
  384. ],
  385. 'dynamic_templates' => [
  386. [
  387. 'price_mapping' => [
  388. 'match' => 'price_*',
  389. 'match_mapping_type' => 'string',
  390. 'mapping' => [
  391. 'type' => 'float',
  392. 'store' => true,
  393. ],
  394. ],
  395. ],
  396. [
  397. 'string_mapping' => [
  398. 'match' => '*',
  399. 'match_mapping_type' => 'string',
  400. 'mapping' => [
  401. 'type' => 'text',
  402. 'index' => false,
  403. 'copy_to' => '_search'
  404. ],
  405. ],
  406. ],
  407. [
  408. 'position_mapping' => [
  409. 'match' => 'position_*',
  410. 'match_mapping_type' => 'string',
  411. 'mapping' => [
  412. 'type' => 'int',
  413. ],
  414. ],
  415. ],
  416. ],
  417. ],
  418. ],
  419. ])
  420. ->willThrowException(new \Exception('Something went wrong'));
  421. $this->model->addFieldsMapping(
  422. [
  423. 'name' => [
  424. 'type' => 'text',
  425. ],
  426. ],
  427. 'indexName',
  428. 'product'
  429. );
  430. }
  431. /**
  432. * Test deleteMapping() method
  433. */
  434. public function testDeleteMapping()
  435. {
  436. $this->indicesMock->expects($this->once())
  437. ->method('deleteMapping')
  438. ->with([
  439. 'index' => 'indexName',
  440. 'type' => 'product',
  441. ]);
  442. $this->model->deleteMapping(
  443. 'indexName',
  444. 'product'
  445. );
  446. }
  447. /**
  448. * Test deleteMapping() method
  449. * @expectedException \Exception
  450. */
  451. public function testDeleteMappingFailure()
  452. {
  453. $this->indicesMock->expects($this->once())
  454. ->method('deleteMapping')
  455. ->with([
  456. 'index' => 'indexName',
  457. 'type' => 'product',
  458. ])
  459. ->willThrowException(new \Exception('Something went wrong'));
  460. $this->model->deleteMapping(
  461. 'indexName',
  462. 'product'
  463. );
  464. }
  465. /**
  466. * Test query() method
  467. * @return void
  468. */
  469. public function testQuery()
  470. {
  471. $query = 'test phrase query';
  472. $this->elasticsearchClientMock->expects($this->once())
  473. ->method('search')
  474. ->with($query)
  475. ->willReturn([]);
  476. $this->assertEquals([], $this->model->query($query));
  477. }
  478. /**
  479. * Test suggest() method
  480. * @return void
  481. */
  482. public function testSuggest()
  483. {
  484. $query = 'query';
  485. $this->elasticsearchClientMock->expects($this->once())
  486. ->method('suggest')
  487. ->willReturn([]);
  488. $this->assertEquals([], $this->model->suggest($query));
  489. }
  490. /**
  491. * Get elasticsearch client options
  492. *
  493. * @return array
  494. */
  495. protected function getOptions()
  496. {
  497. return [
  498. 'hostname' => 'localhost',
  499. 'port' => '9200',
  500. 'timeout' => 15,
  501. 'index' => 'magento2',
  502. 'enableAuth' => 1,
  503. 'username' => 'user',
  504. 'password' => 'passwd',
  505. ];
  506. }
  507. /**
  508. * @return array
  509. */
  510. protected function getEmptyIndexOption()
  511. {
  512. return [
  513. 'hostname' => 'localhost',
  514. 'port' => '9200',
  515. 'index' => '',
  516. 'timeout' => 15,
  517. 'enableAuth' => 1,
  518. 'username' => 'user',
  519. 'password' => 'passwd',
  520. ];
  521. }
  522. }