1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Elasticsearch\Test\Unit\Model\DataProvider;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
use Magento\Elasticsearch\Model\DataProvider\Suggestions;
use Magento\Elasticsearch\Model\Config;
use Magento\Elasticsearch\SearchAdapter\ConnectionManager;
use Magento\Search\Model\QueryResultFactory;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Elasticsearch\SearchAdapter\SearchIndexNameResolver;
use Magento\Store\Model\StoreManagerInterface as StoreManager;
use Magento\Search\Model\QueryInterface;
/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class SuggestionsTest extends \PHPUnit\Framework\TestCase
{
/**
* @var Suggestions
*/
private $model;
/**
* @var Config|\PHPUnit_Framework_MockObject_MockObject
*/
private $config;
/**
* @var QueryResultFactory|\PHPUnit_Framework_MockObject_MockObject
*/
private $queryResultFactory;
/**
* @var ConnectionManager|\PHPUnit_Framework_MockObject_MockObject
*/
private $connectionManager;
/**
* @var ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $scopeConfig;
/**
* @var SearchIndexNameResolver|\PHPUnit_Framework_MockObject_MockObject
*/
private $searchIndexNameResolver;
/**
* @var StoreManager|\PHPUnit_Framework_MockObject_MockObject
*/
private $storeManager;
/**
* @var QueryInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $query;
/**
* Set up test environment
*
* @return void
*/
protected function setUp()
{
$this->config = $this->getMockBuilder(\Magento\Elasticsearch\Model\Config::class)
->disableOriginalConstructor()
->setMethods(['isElasticsearchEnabled'])
->getMock();
$this->queryResultFactory = $this->getMockBuilder(\Magento\Search\Model\QueryResultFactory::class)
->disableOriginalConstructor()
->setMethods(['create'])
->getMock();
$this->connectionManager = $this->getMockBuilder(\Magento\Elasticsearch\SearchAdapter\ConnectionManager::class)
->disableOriginalConstructor()
->setMethods(['getConnection'])
->getMock();
$this->scopeConfig = $this->getMockBuilder(\Magento\Framework\App\Config\ScopeConfigInterface::class)
->disableOriginalConstructor()
->getMock();
$this->searchIndexNameResolver = $this
->getMockBuilder(\Magento\Elasticsearch\SearchAdapter\SearchIndexNameResolver::class)
->disableOriginalConstructor()
->setMethods(['getIndexName'])
->getMock();
$this->storeManager = $this->getMockBuilder(\Magento\Store\Model\StoreManagerInterface::class)
->disableOriginalConstructor()
->getMock();
$this->query = $this->getMockBuilder(\Magento\Search\Model\QueryInterface::class)
->disableOriginalConstructor()
->getMock();
$objectManager = new ObjectManagerHelper($this);
$this->model = $objectManager->getObject(
\Magento\Elasticsearch\Model\DataProvider\Suggestions::class,
[
'queryResultFactory' => $this->queryResultFactory,
'connectionManager' => $this->connectionManager,
'scopeConfig' => $this->scopeConfig,
'config' => $this->config,
'searchIndexNameResolver' => $this->searchIndexNameResolver,
'storeManager' => $this->storeManager
]
);
}
/**
* Test getItems() method
*/
public function testGetItems()
{
$this->scopeConfig->expects($this->any())
->method('getValue')
->willReturn(1);
$this->config->expects($this->any())
->method('isElasticsearchEnabled')
->willReturn(1);
$store = $this->getMockBuilder(\Magento\Store\Api\Data\StoreInterface::class)
->disableOriginalConstructor()
->getMock();
$this->storeManager->expects($this->any())
->method('getStore')
->willReturn($store);
$store->expects($this->any())
->method('getId')
->willReturn(1);
$this->searchIndexNameResolver->expects($this->any())
->method('getIndexName')
->willReturn('magento2_product_1');
$this->query->expects($this->any())
->method('getQueryText')
->willReturn('query');
$client = $this->getMockBuilder(\Magento\Elasticsearch\Model\Client\Elasticsearch::class)
->disableOriginalConstructor()
->getMock();
$this->connectionManager->expects($this->any())
->method('getConnection')
->willReturn($client);
$client->expects($this->any())
->method('suggest')
->willReturn([
'suggestions' => [
[
'options' => [
'query' => [
'text' => 'query',
'score' => 1,
'freq' => 1,
],
]
],
],
]);
$query = $this->getMockBuilder(\Magento\Search\Model\QueryResult::class)
->disableOriginalConstructor()
->getMock();
$this->queryResultFactory->expects($this->any())
->method('create')
->willReturn($query);
$this->assertInternalType('array', $this->model->getItems($this->query));
}
}