Skip to content

Commit dd2b381

Browse files
committed
MAGETWO-94172: Elasticsearch 5.0+ exception is shown if customer searches product before reindexing
-cover fix with integration test
1 parent 498fb80 commit dd2b381

File tree

1 file changed

+107
-0
lines changed
  • dev/tests/integration/testsuite/Magento/Elasticsearch/Elasticsearch5/SearchAdapter

1 file changed

+107
-0
lines changed
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Elasticsearch\Elasticsearch5\SearchAdapter;
9+
10+
use Magento\TestFramework\Helper\Bootstrap;
11+
12+
/**
13+
* Class AdapterTest
14+
*/
15+
class AdapterTest extends \PHPUnit\Framework\TestCase
16+
{
17+
/**
18+
* @var \Magento\Elasticsearch\Elasticsearch5\SearchAdapter\Adapter
19+
*/
20+
private $adapter;
21+
22+
/**
23+
* @var \Magento\Elasticsearch\Model\Client\Elasticsearch|\PHPUnit\Framework\MockObject\MockObject
24+
*/
25+
private $clientMock;
26+
27+
/**
28+
* @var \Magento\Framework\Search\Request\Builder
29+
*/
30+
private $requestBuilder;
31+
32+
/**
33+
* @var \Psr\Log\LoggerInterface|\PHPUnit\Framework\MockObject\MockObject
34+
*/
35+
private $loggerMock;
36+
37+
/**
38+
* @return void
39+
*/
40+
protected function setUp()
41+
{
42+
$objectManager = Bootstrap::getObjectManager();
43+
$contentManager = $this->getMockBuilder(\Magento\Elasticsearch\SearchAdapter\ConnectionManager::class)
44+
->disableOriginalConstructor()
45+
->getMock();
46+
$this->clientMock = $this->getMockBuilder(\Magento\Elasticsearch\Model\Client\Elasticsearch::class)
47+
->disableOriginalConstructor()
48+
->getMock();
49+
$contentManager
50+
->expects($this->any())
51+
->method('getConnection')
52+
->willReturn($this->clientMock);
53+
/** @var \Magento\Framework\Search\Request\Config\Converter $converter */
54+
$converter = $objectManager->create(\Magento\Framework\Search\Request\Config\Converter::class);
55+
56+
$document = new \DOMDocument();
57+
$document->load($this->getRequestConfigPath());
58+
$requestConfig = $converter->convert($document);
59+
60+
/** @var \Magento\Framework\Search\Request\Config $config */
61+
$config = $objectManager->create(\Magento\Framework\Search\Request\Config::class);
62+
$config->merge($requestConfig);
63+
64+
$this->requestBuilder = $objectManager->create(
65+
\Magento\Framework\Search\Request\Builder::class,
66+
['config' => $config]
67+
);
68+
$this->loggerMock = $this->getMockForAbstractClass(\Psr\Log\LoggerInterface::class);
69+
70+
$this->adapter = $objectManager->create(
71+
\Magento\Elasticsearch\Elasticsearch5\SearchAdapter\Adapter::class,
72+
[
73+
'connectionManager' => $contentManager,
74+
'logger' => $this->loggerMock
75+
]
76+
);
77+
}
78+
79+
/**
80+
* @magentoAppIsolation enabled
81+
* @magentoConfigFixture default/catalog/search/engine elasticsearch
82+
* @magentoConfigFixture current_store catalog/search/elasticsearch_index_prefix adaptertest
83+
* @return void
84+
*/
85+
public function testQuery()
86+
{
87+
$this->requestBuilder->bind('fulltext_search_query', 'socks');
88+
$this->requestBuilder->setRequestName('one_match');
89+
$queryRequest = $this->requestBuilder->create();
90+
$exception = new \Exception('Test Message');
91+
$this->loggerMock->expects($this->once())->method('critical')->with($exception);
92+
$this->clientMock->expects($this->once())->method('query')->willThrowException($exception);
93+
$actualResponse = $this->adapter->query($queryRequest);
94+
$this->assertEmpty($actualResponse->getAggregations()->getBuckets());
95+
$this->assertEquals(0, $actualResponse->count());
96+
}
97+
98+
/**
99+
* Get request config path
100+
*
101+
* @return string
102+
*/
103+
private function getRequestConfigPath()
104+
{
105+
return __DIR__ . '/../../_files/requests.xml';
106+
}
107+
}

0 commit comments

Comments
 (0)