Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
IndexNameResolverTest.php
Go to the documentation of this file.
1 <?php
7 
9 use Psr\Log\LoggerInterface;
12 use Magento\Elasticsearch\Model\Client\Elasticsearch as ElasticsearchClient;
14 
18 class IndexNameResolverTest extends \PHPUnit\Framework\TestCase
19 {
23  protected $model;
24 
28  protected $connectionManager;
29 
33  protected $clientConfig;
34 
38  protected $logger;
39 
43  protected $client;
44 
48  protected $objectManager;
49 
53  protected $entityType;
54 
58  protected $storeId;
59 
64  protected function setUp()
65  {
66  $this->objectManager = new ObjectManagerHelper($this);
67 
68  $this->connectionManager = $this->getMockBuilder(\Magento\Elasticsearch\SearchAdapter\ConnectionManager::class)
69  ->disableOriginalConstructor()
70  ->setMethods([
71  'getConnection',
72  ])
73  ->getMock();
74 
75  $this->clientConfig = $this->getMockBuilder(\Magento\Elasticsearch\Model\Config::class)
76  ->disableOriginalConstructor()
77  ->setMethods([
78  'getIndexPrefix',
79  'getEntityType',
80  'getIndexSettings',
81  ])
82  ->getMock();
83 
84  $this->logger = $this->getMockBuilder(\Psr\Log\LoggerInterface::class)
85  ->disableOriginalConstructor()
86  ->getMock();
87 
88  $elasticsearchClientMock = $this->getMockBuilder(\Elasticsearch\Client::class)
89  ->setMethods([
90  'indices',
91  'ping',
92  'bulk',
93  'search',
94  'scroll',
95  ])
96  ->disableOriginalConstructor()
97  ->getMock();
98 
99  $indicesMock = $this->getMockBuilder(\Elasticsearch\Namespaces\IndicesNamespace::class)
100  ->setMethods([
101  'exists',
102  'getSettings',
103  'create',
104  'putMapping',
105  'deleteMapping',
106  'existsAlias',
107  'updateAliases',
108  'stats'
109  ])
110  ->disableOriginalConstructor()
111  ->getMock();
112  $elasticsearchClientMock->expects($this->any())
113  ->method('indices')
114  ->willReturn($indicesMock);
115  $this->client = $this->getMockBuilder(\Magento\Elasticsearch\Model\Client\Elasticsearch::class)
116  ->setConstructorArgs([
117  'options' => $this->getClientOptions(),
118  'elasticsearchClient' => $elasticsearchClientMock
119  ])
120  ->getMock();
121 
122  $this->connectionManager->expects($this->any())
123  ->method('getConnection')
124  ->willReturn($this->client);
125 
126  $this->clientConfig->expects($this->any())
127  ->method('getIndexPrefix')
128  ->willReturn('indexName');
129  $this->clientConfig->expects($this->any())
130  ->method('getEntityType')
131  ->willReturn('product');
132  $this->entityType = 'product';
133  $this->storeId = 1;
134 
135  $objectManager = new ObjectManagerHelper($this);
136  $this->model = $objectManager->getObject(
137  \Magento\Elasticsearch\Model\Adapter\Index\IndexNameResolver::class,
138  [
139  'connectionManager' => $this->connectionManager,
140  'clientConfig' => $this->clientConfig,
141  'logger' => $this->logger,
142  'options' => [],
143  ]
144  );
145  }
146 
150  public function testGetIndexNameForAlias()
151  {
152  $this->clientConfig->expects($this->any())
153  ->method('getIndexPrefix')
154  ->willReturn('indexName');
155 
156  $this->assertEquals(
157  'indexName_product_1',
158  $this->model->getIndexNameForAlias($this->storeId, $this->entityType)
159  );
160  }
161 
166  {
167  $preparedIndex = ['1' => 'product'];
168 
169  $this->assertEquals(
170  'product',
171  $this->model->getIndexName($this->storeId, $this->entityType, $preparedIndex)
172  );
173  }
174 
179  {
180  $preparedIndex = [];
181 
182  $this->assertEquals(
183  'indexName_product_1_v1',
184  $this->model->getIndexName($this->storeId, $this->entityType, $preparedIndex)
185  );
186  }
187 
191  public function testGetIndexPattern()
192  {
193  $this->assertEquals(
194  'indexName_product_1_v',
195  $this->model->getIndexPattern($this->storeId, $this->entityType)
196  );
197  }
198 
202  public function testUpdateAliasWithOldIndex()
203  {
204  $this->client->expects($this->any())
205  ->method('getAlias')
206  ->with('indexName_product_1')
207  ->willReturn(
208  [
209  'indexName_product_1_v2' => [
210  'aliases' => [
211  'indexName_product_1' => [],
212  ],
213  ],
214  ]
215  );
216 
217  $this->client->expects($this->any())
218  ->method('existsAlias')
219  ->with('indexName_product_1')
220  ->willReturn(true);
221 
222  $this->assertEquals(
223  'indexName_product_1_v2',
224  $this->model->getIndexFromAlias($this->storeId, $this->entityType)
225  );
226  }
227 
231  public function testConnectException()
232  {
233  $connectionManager = $this->getMockBuilder(\Magento\Elasticsearch\SearchAdapter\ConnectionManager::class)
234  ->disableOriginalConstructor()
235  ->setMethods([
236  'getConnection',
237  ])
238  ->getMock();
239 
240  $connectionManager->expects($this->any())
241  ->method('getConnection')
242  ->willThrowException(new \Exception('Something went wrong'));
243 
244  $this->objectManager->getObject(
245  \Magento\Elasticsearch\Model\Adapter\Index\IndexNameResolver::class,
246  [
247  'connectionManager' => $connectionManager,
248  'clientConfig' => $this->clientConfig,
249  'logger' => $this->logger,
250  'options' => []
251  ]
252  );
253  }
254 
259  {
260  $this->assertEquals(
261  'product',
262  $this->model->getIndexMapping('catalogsearch_fulltext')
263  );
264  }
265 
269  public function testGetIndexName()
270  {
271  $this->assertEquals(
272  'else_index_id',
273  $this->model->getIndexMapping('else_index_id')
274  );
275  }
276 
282  protected function getClientOptions()
283  {
284  return [
285  'hostname' => 'localhost',
286  'port' => '9200',
287  'timeout' => 15,
288  'index' => 'magento2',
289  'enableAuth' => 1,
290  'username' => 'user',
291  'password' => 'my-password',
292  ];
293  }
294 }