7
7
8
8
namespace Magento \Elasticsearch \Test \Unit \Model \DataProvider \Base ;
9
9
10
+ use Elasticsearch \Common \Exceptions \BadRequest400Exception ;
11
+ use Magento \Elasticsearch \Model \Adapter \FieldMapper \Product \FieldProviderInterface ;
10
12
use Magento \Elasticsearch \Model \Config ;
13
+ use Magento \Elasticsearch \Model \DataProvider \Base \Suggestions ;
11
14
use Magento \Elasticsearch \Model \DataProvider \Suggestions as SuggestionsDataProvider ;
12
15
use Magento \Elasticsearch \SearchAdapter \ConnectionManager ;
13
16
use Magento \Elasticsearch \SearchAdapter \SearchIndexNameResolver ;
21
24
use Magento \Store \Model \StoreManagerInterface as StoreManager ;
22
25
use PHPUnit \Framework \MockObject \MockObject ;
23
26
use PHPUnit \Framework \TestCase ;
27
+ use Psr \Log \LoggerInterface ;
24
28
25
29
/**
26
30
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
@@ -62,6 +66,21 @@ class SuggestionsTest extends TestCase
62
66
*/
63
67
private $ storeManager ;
64
68
69
+ /**
70
+ * @var FieldProviderInterface|MockObject
71
+ */
72
+ private $ fieldProvider ;
73
+
74
+ /**
75
+ * @var LoggerInterface|MockObject
76
+ */
77
+ private $ logger ;
78
+
79
+ /**
80
+ * @var Elasticsearch|MockObject
81
+ */
82
+ private $ client ;
83
+
65
84
/**
66
85
* @var QueryInterface|MockObject
67
86
*/
@@ -99,7 +118,19 @@ protected function setUp(): void
99
118
->setMethods (['getIndexName ' ])
100
119
->getMock ();
101
120
102
- $ this ->storeManager = $ this ->getMockBuilder (\Magento \Store \Model \StoreManagerInterface::class)
121
+ $ this ->storeManager = $ this ->getMockBuilder (StoreManager::class)
122
+ ->disableOriginalConstructor ()
123
+ ->getMockForAbstractClass ();
124
+
125
+ $ this ->fieldProvider = $ this ->getMockBuilder (FieldProviderInterface::class)
126
+ ->disableOriginalConstructor ()
127
+ ->getMockForAbstractClass ();
128
+
129
+ $ this ->logger = $ this ->getMockBuilder (LoggerInterface::class)
130
+ ->disableOriginalConstructor ()
131
+ ->getMockForAbstractClass ();
132
+
133
+ $ this ->client = $ this ->getMockBuilder (Elasticsearch::class)
103
134
->disableOriginalConstructor ()
104
135
->getMock ();
105
136
@@ -110,81 +141,155 @@ protected function setUp(): void
110
141
$ objectManager = new ObjectManagerHelper ($ this );
111
142
112
143
$ this ->model = $ objectManager ->getObject (
113
- \ Magento \ Elasticsearch \ Model \ DataProvider \ Base \ Suggestions::class,
144
+ Suggestions::class,
114
145
[
115
146
'queryResultFactory ' => $ this ->queryResultFactory ,
116
147
'connectionManager ' => $ this ->connectionManager ,
117
148
'scopeConfig ' => $ this ->scopeConfig ,
118
149
'config ' => $ this ->config ,
119
150
'searchIndexNameResolver ' => $ this ->searchIndexNameResolver ,
120
- 'storeManager ' => $ this ->storeManager
151
+ 'storeManager ' => $ this ->storeManager ,
152
+ 'fieldProvider ' => $ this ->fieldProvider ,
153
+ 'logger ' => $ this ->logger ,
121
154
]
122
155
);
123
156
}
124
157
125
158
/**
126
- * Test getItems() method
159
+ * Test get items process with search suggestions disabled.
160
+ * @return void
127
161
*/
128
- public function testGetItems ()
162
+ public function testGetItemsWithDisabledSearchSuggestion (): void
129
163
{
130
- $ this ->scopeConfig ->expects ($ this ->any ())
131
- ->method ('getValue ' )
132
- ->willReturn (1 );
133
-
134
- $ this ->config ->expects ($ this ->any ())
135
- ->method ('isElasticsearchEnabled ' )
136
- ->willReturn (1 );
137
-
138
- $ store = $ this ->getMockBuilder (StoreInterface::class)
139
- ->disableOriginalConstructor ()
140
- ->getMockForAbstractClass ();
164
+ $ this ->scopeConfig ->expects ($ this ->once ())
165
+ ->method ('isSetFlag ' )
166
+ ->willReturn (false );
141
167
142
- $ this ->storeManager ->expects ($ this ->any ())
143
- ->method ('getStore ' )
144
- ->willReturn ($ store );
145
-
146
- $ store ->expects ($ this ->any ())
147
- ->method ('getId ' )
148
- ->willReturn (1 );
168
+ $ this ->scopeConfig ->expects ($ this ->never ())
169
+ ->method ('getValue ' );
149
170
150
- $ this ->searchIndexNameResolver ->expects ($ this ->any ())
151
- ->method ('getIndexName ' )
152
- ->willReturn (' magento2_product_1 ' );
171
+ $ this ->config ->expects ($ this ->once ())
172
+ ->method ('isElasticsearchEnabled ' )
173
+ ->willReturn (true );
153
174
154
- $ this ->query ->expects ($ this ->any ())
155
- ->method ('getQueryText ' )
156
- ->willReturn ('query ' );
175
+ $ this ->logger ->expects ($ this ->never ())
176
+ ->method ('critical ' );
157
177
158
- $ client = $ this ->getMockBuilder (Elasticsearch::class)
159
- ->disableOriginalConstructor ()
160
- ->getMock ();
178
+ $ this ->queryResultFactory ->expects ($ this ->never ())
179
+ ->method ('create ' );
161
180
162
- $ this ->connectionManager ->expects ($ this ->any ())
163
- ->method ('getConnection ' )
164
- ->willReturn ($ client );
181
+ $ this ->assertEmpty ($ this ->model ->getItems ($ this ->query ));
182
+ }
165
183
166
- $ client ->expects ($ this ->any ())
184
+ /**
185
+ * Test get items process with search suggestions enabled.
186
+ * @return void
187
+ */
188
+ public function testGetItemsWithEnabledSearchSuggestion (): void
189
+ {
190
+ $ this ->prepareSearchQuery ();
191
+ $ this ->client ->expects ($ this ->once ())
167
192
->method ('query ' )
168
193
->willReturn ([
169
194
'suggest ' => [
170
195
'phrase_field ' => [
171
- 'options ' => [
172
- 'text ' => 'query ' ,
173
- 'score ' => 1 ,
174
- 'freq ' => 1 ,
196
+ [
197
+ 'options ' => [
198
+ 'suggestion ' => [
199
+ 'text ' => 'query ' ,
200
+ 'score ' => 1 ,
201
+ 'freq ' => 1 ,
202
+ ]
203
+ ]
175
204
]
176
205
],
177
206
],
178
207
]);
179
208
209
+ $ this ->logger ->expects ($ this ->never ())
210
+ ->method ('critical ' );
211
+
180
212
$ query = $ this ->getMockBuilder (QueryResult::class)
181
213
->disableOriginalConstructor ()
182
214
->getMock ();
183
215
184
- $ this ->queryResultFactory ->expects ($ this ->any ())
216
+ $ this ->queryResultFactory ->expects ($ this ->once ())
185
217
->method ('create ' )
186
218
->willReturn ($ query );
187
219
188
- $ this ->assertIsArray ($ this ->model ->getItems ($ this ->query ));
220
+ $ this ->assertEquals ([$ query ], $ this ->model ->getItems ($ this ->query ));
221
+ }
222
+
223
+ /**
224
+ * Test get items process when throwing an exception.
225
+ * @return void
226
+ */
227
+ public function testGetItemsException (): void
228
+ {
229
+ $ this ->prepareSearchQuery ();
230
+ $ exception = new BadRequest400Exception ();
231
+
232
+ $ this ->client ->expects ($ this ->once ())
233
+ ->method ('query ' )
234
+ ->willThrowException ($ exception );
235
+
236
+ $ this ->logger ->expects ($ this ->once ())
237
+ ->method ('critical ' )
238
+ ->with ($ exception );
239
+
240
+ $ this ->queryResultFactory ->expects ($ this ->never ())
241
+ ->method ('create ' );
242
+
243
+ $ this ->assertEmpty ($ this ->model ->getItems ($ this ->query ));
244
+ }
245
+
246
+ /**
247
+ * Prepare Mocks for default get items process.
248
+ * @return void
249
+ */
250
+ private function prepareSearchQuery (): void
251
+ {
252
+ $ storeId = 1 ;
253
+
254
+ $ this ->scopeConfig ->expects ($ this ->exactly (2 ))
255
+ ->method ('isSetFlag ' )
256
+ ->willReturn (true );
257
+
258
+ $ this ->scopeConfig ->expects ($ this ->once ())
259
+ ->method ('getValue ' )
260
+ ->willReturn (1 );
261
+
262
+ $ this ->config ->expects ($ this ->once ())
263
+ ->method ('isElasticsearchEnabled ' )
264
+ ->willReturn (true );
265
+
266
+ $ store = $ this ->getMockBuilder (StoreInterface::class)
267
+ ->disableOriginalConstructor ()
268
+ ->getMockForAbstractClass ();
269
+
270
+ $ store ->expects ($ this ->once ())
271
+ ->method ('getId ' )
272
+ ->willReturn ($ storeId );
273
+
274
+ $ this ->storeManager ->expects ($ this ->once ())
275
+ ->method ('getStore ' )
276
+ ->willReturn ($ store );
277
+
278
+ $ this ->searchIndexNameResolver ->expects ($ this ->once ())
279
+ ->method ('getIndexName ' )
280
+ ->with ($ storeId , Config::ELASTICSEARCH_TYPE_DEFAULT )
281
+ ->willReturn ('magento2_product_1 ' );
282
+
283
+ $ this ->query ->expects ($ this ->once ())
284
+ ->method ('getQueryText ' )
285
+ ->willReturn ('query ' );
286
+
287
+ $ this ->fieldProvider ->expects ($ this ->once ())
288
+ ->method ('getFields ' )
289
+ ->willReturn ([]);
290
+
291
+ $ this ->connectionManager ->expects ($ this ->once ())
292
+ ->method ('getConnection ' )
293
+ ->willReturn ($ this ->client );
189
294
}
190
295
}
0 commit comments