IndexNameResolverTest.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Elasticsearch\Test\Unit\Model\Adapter\Index;
  7. use Magento\Elasticsearch\Model\Adapter\Index\IndexNameResolver;
  8. use Psr\Log\LoggerInterface;
  9. use Magento\Elasticsearch\SearchAdapter\ConnectionManager;
  10. use Magento\AdvancedSearch\Model\Client\ClientOptionsInterface;
  11. use Magento\Elasticsearch\Model\Client\Elasticsearch as ElasticsearchClient;
  12. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
  13. /**
  14. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  15. */
  16. class IndexNameResolverTest extends \PHPUnit\Framework\TestCase
  17. {
  18. /**
  19. * @var IndexNameResolver
  20. */
  21. protected $model;
  22. /**
  23. * @var ConnectionManager|\PHPUnit_Framework_MockObject_MockObject
  24. */
  25. protected $connectionManager;
  26. /**
  27. * @var ClientOptionsInterface|\PHPUnit_Framework_MockObject_MockObject
  28. */
  29. protected $clientConfig;
  30. /**
  31. * @var LoggerInterface|\PHPUnit_Framework_MockObject_MockObject
  32. */
  33. protected $logger;
  34. /**
  35. * @var ElasticsearchClient|\PHPUnit_Framework_MockObject_MockObject
  36. */
  37. protected $client;
  38. /**
  39. * @var ObjectManagerHelper
  40. */
  41. protected $objectManager;
  42. /**
  43. * @var string
  44. */
  45. protected $entityType;
  46. /**
  47. * @var int
  48. */
  49. protected $storeId;
  50. /**
  51. * Setup method
  52. * @return void
  53. */
  54. protected function setUp()
  55. {
  56. $this->objectManager = new ObjectManagerHelper($this);
  57. $this->connectionManager = $this->getMockBuilder(\Magento\Elasticsearch\SearchAdapter\ConnectionManager::class)
  58. ->disableOriginalConstructor()
  59. ->setMethods([
  60. 'getConnection',
  61. ])
  62. ->getMock();
  63. $this->clientConfig = $this->getMockBuilder(\Magento\Elasticsearch\Model\Config::class)
  64. ->disableOriginalConstructor()
  65. ->setMethods([
  66. 'getIndexPrefix',
  67. 'getEntityType',
  68. 'getIndexSettings',
  69. ])
  70. ->getMock();
  71. $this->logger = $this->getMockBuilder(\Psr\Log\LoggerInterface::class)
  72. ->disableOriginalConstructor()
  73. ->getMock();
  74. $elasticsearchClientMock = $this->getMockBuilder(\Elasticsearch\Client::class)
  75. ->setMethods([
  76. 'indices',
  77. 'ping',
  78. 'bulk',
  79. 'search',
  80. 'scroll',
  81. ])
  82. ->disableOriginalConstructor()
  83. ->getMock();
  84. $indicesMock = $this->getMockBuilder(\Elasticsearch\Namespaces\IndicesNamespace::class)
  85. ->setMethods([
  86. 'exists',
  87. 'getSettings',
  88. 'create',
  89. 'putMapping',
  90. 'deleteMapping',
  91. 'existsAlias',
  92. 'updateAliases',
  93. 'stats'
  94. ])
  95. ->disableOriginalConstructor()
  96. ->getMock();
  97. $elasticsearchClientMock->expects($this->any())
  98. ->method('indices')
  99. ->willReturn($indicesMock);
  100. $this->client = $this->getMockBuilder(\Magento\Elasticsearch\Model\Client\Elasticsearch::class)
  101. ->setConstructorArgs([
  102. 'options' => $this->getClientOptions(),
  103. 'elasticsearchClient' => $elasticsearchClientMock
  104. ])
  105. ->getMock();
  106. $this->connectionManager->expects($this->any())
  107. ->method('getConnection')
  108. ->willReturn($this->client);
  109. $this->clientConfig->expects($this->any())
  110. ->method('getIndexPrefix')
  111. ->willReturn('indexName');
  112. $this->clientConfig->expects($this->any())
  113. ->method('getEntityType')
  114. ->willReturn('product');
  115. $this->entityType = 'product';
  116. $this->storeId = 1;
  117. $objectManager = new ObjectManagerHelper($this);
  118. $this->model = $objectManager->getObject(
  119. \Magento\Elasticsearch\Model\Adapter\Index\IndexNameResolver::class,
  120. [
  121. 'connectionManager' => $this->connectionManager,
  122. 'clientConfig' => $this->clientConfig,
  123. 'logger' => $this->logger,
  124. 'options' => [],
  125. ]
  126. );
  127. }
  128. /**
  129. * Test getIndexNameForAlias() method
  130. */
  131. public function testGetIndexNameForAlias()
  132. {
  133. $this->clientConfig->expects($this->any())
  134. ->method('getIndexPrefix')
  135. ->willReturn('indexName');
  136. $this->assertEquals(
  137. 'indexName_product_1',
  138. $this->model->getIndexNameForAlias($this->storeId, $this->entityType)
  139. );
  140. }
  141. /**
  142. * Test getIndexName() method with prepared index
  143. */
  144. public function testGetIndexNameWithPreparedIndex()
  145. {
  146. $preparedIndex = ['1' => 'product'];
  147. $this->assertEquals(
  148. 'product',
  149. $this->model->getIndexName($this->storeId, $this->entityType, $preparedIndex)
  150. );
  151. }
  152. /**
  153. * Test getIndexName() method without prepared index
  154. */
  155. public function testGetIndexNameWithoutPreparedIndexWithIndexName()
  156. {
  157. $preparedIndex = [];
  158. $this->assertEquals(
  159. 'indexName_product_1_v1',
  160. $this->model->getIndexName($this->storeId, $this->entityType, $preparedIndex)
  161. );
  162. }
  163. /**
  164. * Test getIndexPattern() method
  165. */
  166. public function testGetIndexPattern()
  167. {
  168. $this->assertEquals(
  169. 'indexName_product_1_v',
  170. $this->model->getIndexPattern($this->storeId, $this->entityType)
  171. );
  172. }
  173. /**
  174. * Test getIndexFromAlias() method
  175. */
  176. public function testUpdateAliasWithOldIndex()
  177. {
  178. $this->client->expects($this->any())
  179. ->method('getAlias')
  180. ->with('indexName_product_1')
  181. ->willReturn(
  182. [
  183. 'indexName_product_1_v2' => [
  184. 'aliases' => [
  185. 'indexName_product_1' => [],
  186. ],
  187. ],
  188. ]
  189. );
  190. $this->client->expects($this->any())
  191. ->method('existsAlias')
  192. ->with('indexName_product_1')
  193. ->willReturn(true);
  194. $this->assertEquals(
  195. 'indexName_product_1_v2',
  196. $this->model->getIndexFromAlias($this->storeId, $this->entityType)
  197. );
  198. }
  199. /**
  200. * @expectedException \Magento\Framework\Exception\LocalizedException
  201. */
  202. public function testConnectException()
  203. {
  204. $connectionManager = $this->getMockBuilder(\Magento\Elasticsearch\SearchAdapter\ConnectionManager::class)
  205. ->disableOriginalConstructor()
  206. ->setMethods([
  207. 'getConnection',
  208. ])
  209. ->getMock();
  210. $connectionManager->expects($this->any())
  211. ->method('getConnection')
  212. ->willThrowException(new \Exception('Something went wrong'));
  213. $this->objectManager->getObject(
  214. \Magento\Elasticsearch\Model\Adapter\Index\IndexNameResolver::class,
  215. [
  216. 'connectionManager' => $connectionManager,
  217. 'clientConfig' => $this->clientConfig,
  218. 'logger' => $this->logger,
  219. 'options' => []
  220. ]
  221. );
  222. }
  223. /**
  224. * Test getIndexName() indexerId 'catalogsearch_fulltext'
  225. */
  226. public function testGetIndexNameCatalogSearchFullText()
  227. {
  228. $this->assertEquals(
  229. 'product',
  230. $this->model->getIndexMapping('catalogsearch_fulltext')
  231. );
  232. }
  233. /**
  234. * Test getIndexName() with any ndex
  235. */
  236. public function testGetIndexName()
  237. {
  238. $this->assertEquals(
  239. 'else_index_id',
  240. $this->model->getIndexMapping('else_index_id')
  241. );
  242. }
  243. /**
  244. * Get elasticsearch client options
  245. *
  246. * @return array
  247. */
  248. protected function getClientOptions()
  249. {
  250. return [
  251. 'hostname' => 'localhost',
  252. 'port' => '9200',
  253. 'timeout' => 15,
  254. 'index' => 'magento2',
  255. 'enableAuth' => 1,
  256. 'username' => 'user',
  257. 'password' => 'my-password',
  258. ];
  259. }
  260. }