5
5
*/
6
6
namespace Magento \MagentoCloud \Test \Unit \Process \Deploy \InstallUpdate \ConfigUpdate ;
7
7
8
+ use GuzzleHttp \Client ;
9
+ use GuzzleHttp \Psr7 \Response ;
8
10
use Magento \MagentoCloud \Config \Deploy \Writer as EnvWriter ;
9
11
use Magento \MagentoCloud \Config \Shared \Writer as SharedWriter ;
10
12
use Magento \MagentoCloud \Config \Environment ;
11
13
use Magento \MagentoCloud \Config \Stage \DeployInterface ;
14
+ use Magento \MagentoCloud \Http \ClientFactory ;
12
15
use Magento \MagentoCloud \Package \MagentoVersion ;
13
16
use Magento \MagentoCloud \Process \Deploy \InstallUpdate \ConfigUpdate \SearchEngine ;
14
17
use PHPUnit \Framework \TestCase ;
15
18
use PHPUnit_Framework_MockObject_MockObject as Mock ;
19
+ use Psr \Http \Message \StreamInterface ;
16
20
use Psr \Log \LoggerInterface ;
17
21
18
22
/**
@@ -55,6 +59,11 @@ class SearchEngineTest extends TestCase
55
59
*/
56
60
private $ magentoVersionMock ;
57
61
62
+ /**
63
+ * @var ClientFactory|Mock
64
+ */
65
+ private $ clientFactoryMock ;
66
+
58
67
/**
59
68
* @inheritdoc
60
69
*/
@@ -66,14 +75,16 @@ protected function setUp()
66
75
$ this ->loggerMock = $ this ->getMockForAbstractClass (LoggerInterface::class);
67
76
$ this ->stageConfigMock = $ this ->getMockForAbstractClass (DeployInterface::class);
68
77
$ this ->magentoVersionMock = $ this ->createMock (MagentoVersion::class);
78
+ $ this ->clientFactoryMock = $ this ->createMock (ClientFactory::class);
69
79
70
80
$ this ->process = new SearchEngine (
71
81
$ this ->environmentMock ,
72
82
$ this ->loggerMock ,
73
83
$ this ->envWriterMock ,
74
84
$ this ->sharedWriterMock ,
75
85
$ this ->stageConfigMock ,
76
- $ this ->magentoVersionMock
86
+ $ this ->magentoVersionMock ,
87
+ $ this ->clientFactoryMock
77
88
);
78
89
}
79
90
@@ -108,26 +119,63 @@ public function testExecute()
108
119
public function magentoVersionTestDataProvider (): array
109
120
{
110
121
return [
111
- [ 'newVersion ' => true ],
112
- [ 'newVersion ' => false ],
122
+ ['newVersion ' => true ],
123
+ ['newVersion ' => false ],
113
124
];
114
125
}
115
126
116
127
/**
117
128
* @param bool newVersion
118
- * @dataProvider magentoVersionTestDataProvider
129
+ * @param string $version
130
+ * @param array $relationships
131
+ * @param array $expected
132
+ * @dataProvider executeWithElasticSearchDataProvider
119
133
*/
120
- public function testExecuteWithElasticSearch (bool $ newVersion )
121
- {
122
- $ config ['system ' ]['default ' ]['catalog ' ]['search ' ] = [
123
- 'engine ' => 'elasticsearch ' ,
124
- 'elasticsearch_server_hostname ' => 'localhost ' ,
125
- 'elasticsearch_server_port ' => 1234 ,
126
- ];
134
+ public function testExecuteWithElasticSearch (
135
+ bool $ newVersion ,
136
+ string $ version ,
137
+ array $ relationships ,
138
+ array $ expected
139
+ ) {
140
+ $ config ['system ' ]['default ' ]['catalog ' ]['search ' ] = $ expected ;
141
+
142
+ $ clientMock = $ this ->getMockBuilder (Client::class)
143
+ ->setMethods (['get ' ])
144
+ ->getMock ();
145
+ $ responseMock = $ this ->createMock (Response::class);
146
+ $ streamMock = $ this ->getMockForAbstractClass (StreamInterface::class);
147
+
148
+ $ clientMock ->expects ($ this ->once ())
149
+ ->method ('get ' )
150
+ ->with ($ relationships ['host ' ] . ': ' . $ relationships ['port ' ])
151
+ ->willReturn ($ responseMock );
152
+ $ responseMock ->expects ($ this ->once ())
153
+ ->method ('getBody ' )
154
+ ->willReturn ($ streamMock );
155
+ $ streamMock ->expects ($ this ->once ())
156
+ ->method ('getContents ' )
157
+ ->willReturn ('{
158
+ "name" : "ZaIj9mo",
159
+ "cluster_name" : "elasticsearch",
160
+ "cluster_uuid" : "CIXBGIVdS6mwM_0lmVhF4g",
161
+ "version" : {
162
+ "number" : " ' . $ version . '",
163
+ "build_hash" : "c59ff00",
164
+ "build_date" : "2018-03-13T10:06:29.741383Z",
165
+ "build_snapshot" : false,
166
+ "lucene_version" : "7.2.1",
167
+ "minimum_wire_compatibility_version" : "5.6.0",
168
+ "minimum_index_compatibility_version" : "5.0.0"
169
+ },
170
+ "tagline" : "You Know, for Search"
171
+ }
172
+ ' );
127
173
174
+ $ this ->clientFactoryMock ->expects ($ this ->once ())
175
+ ->method ('create ' )
176
+ ->willReturn ($ clientMock );
128
177
$ this ->magentoVersionMock ->method ('isGreaterOrEqual ' )
129
178
->willReturn ($ newVersion );
130
-
131
179
$ this ->stageConfigMock ->expects ($ this ->once ())
132
180
->method ('get ' )
133
181
->with (DeployInterface::VAR_SEARCH_CONFIGURATION )
@@ -137,10 +185,7 @@ public function testExecuteWithElasticSearch(bool $newVersion)
137
185
->willReturn (
138
186
[
139
187
'elasticsearch ' => [
140
- [
141
- 'host ' => 'localhost ' ,
142
- 'port ' => 1234 ,
143
- ],
188
+ $ relationships ,
144
189
],
145
190
]
146
191
);
@@ -157,8 +202,127 @@ public function testExecuteWithElasticSearch(bool $newVersion)
157
202
->method ('info ' )
158
203
->withConsecutive (
159
204
['Updating search engine configuration. ' ],
160
- ['Set search engine to: elasticsearch ' ]
205
+ ['Set search engine to: ' . $ expected ['engine ' ]]
206
+ );
207
+
208
+ $ this ->process ->execute ();
209
+ }
210
+
211
+ /**
212
+ * @return array
213
+ */
214
+ public function executeWithElasticSearchDataProvider (): array
215
+ {
216
+ return [
217
+ [
218
+ 'newVersion ' => true ,
219
+ 'version ' => '2.4 ' ,
220
+ 'relationships ' => [
221
+ 'host ' => 'localhost ' ,
222
+ 'port ' => 1234 ,
223
+ ],
224
+ 'expected ' => [
225
+ 'engine ' => 'elasticsearch ' ,
226
+ 'elasticsearch_server_hostname ' => 'localhost ' ,
227
+ 'elasticsearch_server_port ' => 1234 ,
228
+ ],
229
+ ],
230
+ [
231
+ 'newVersion ' => true ,
232
+ 'version ' => '5 ' ,
233
+ 'relationships ' => [
234
+ 'host ' => 'localhost ' ,
235
+ 'port ' => 1234 ,
236
+ ],
237
+ 'expected ' => [
238
+ 'engine ' => 'elasticsearch5 ' ,
239
+ 'elasticsearch5_server_hostname ' => 'localhost ' ,
240
+ 'elasticsearch5_server_port ' => 1234 ,
241
+ ],
242
+ ],
243
+ [
244
+ 'newVersion ' => false ,
245
+ 'version ' => '5.1 ' ,
246
+ 'relationships ' => [
247
+ 'host ' => 'localhost ' ,
248
+ 'port ' => 1234 ,
249
+ ],
250
+ 'expected ' => [
251
+ 'engine ' => 'elasticsearch5 ' ,
252
+ 'elasticsearch5_server_hostname ' => 'localhost ' ,
253
+ 'elasticsearch5_server_port ' => 1234 ,
254
+ ],
255
+ ],
256
+ [
257
+ 'newVersion ' => false ,
258
+ 'version ' => '6.2 ' ,
259
+ 'relationships ' => [
260
+ 'host ' => 'localhost ' ,
261
+ 'port ' => 1234 ,
262
+ ],
263
+ 'expected ' => [
264
+ 'engine ' => 'elasticsearch5 ' ,
265
+ 'elasticsearch5_server_hostname ' => 'localhost ' ,
266
+ 'elasticsearch5_server_port ' => 1234 ,
267
+ ],
268
+ ],
269
+ ];
270
+ }
271
+
272
+ public function testExecuteWithElasticSearchException ()
273
+ {
274
+ $ relationships = [
275
+ 'host ' => 'localhost ' ,
276
+ 'port ' => 1234 ,
277
+ ];
278
+ $ expected = [
279
+ 'engine ' => 'elasticsearch ' ,
280
+ 'elasticsearch_server_hostname ' => 'localhost ' ,
281
+ 'elasticsearch_server_port ' => 1234 ,
282
+ ];
283
+
284
+ $ config ['system ' ]['default ' ]['catalog ' ]['search ' ] = $ expected ;
285
+
286
+ $ clientMock = $ this ->getMockBuilder (Client::class)
287
+ ->setMethods (['get ' ])
288
+ ->getMock ();
289
+
290
+ $ clientMock ->expects ($ this ->once ())
291
+ ->method ('get ' )
292
+ ->with ($ relationships ['host ' ] . ': ' . $ relationships ['port ' ])
293
+ ->willThrowException (new \RuntimeException ('ES is not available ' ));
294
+
295
+ $ this ->clientFactoryMock ->expects ($ this ->once ())
296
+ ->method ('create ' )
297
+ ->willReturn ($ clientMock );
298
+ $ this ->magentoVersionMock ->method ('isGreaterOrEqual ' )
299
+ ->willReturn (true );
300
+ $ this ->stageConfigMock ->expects ($ this ->once ())
301
+ ->method ('get ' )
302
+ ->with (DeployInterface::VAR_SEARCH_CONFIGURATION )
303
+ ->willReturn ([]);
304
+ $ this ->environmentMock ->expects ($ this ->once ())
305
+ ->method ('getRelationships ' )
306
+ ->willReturn (
307
+ [
308
+ 'elasticsearch ' => [
309
+ $ relationships ,
310
+ ],
311
+ ]
312
+ );
313
+ $ this ->envWriterMock ->expects ($ this ->once ())
314
+ ->method ('update ' )
315
+ ->with ($ config );
316
+
317
+ $ this ->loggerMock ->expects ($ this ->exactly (2 ))
318
+ ->method ('info ' )
319
+ ->withConsecutive (
320
+ ['Updating search engine configuration. ' ],
321
+ ['Set search engine to: ' . $ expected ['engine ' ]]
161
322
);
323
+ $ this ->loggerMock ->expects ($ this ->once ())
324
+ ->method ('warning ' )
325
+ ->with ('ES is not available ' );
162
326
163
327
$ this ->process ->execute ();
164
328
}
@@ -179,7 +343,7 @@ public function testExecuteWithElasticSolr(bool $newVersion)
179
343
180
344
$ this ->magentoVersionMock ->method ('isGreaterOrEqual ' )
181
345
->willReturn ($ newVersion );
182
-
346
+
183
347
$ this ->stageConfigMock ->expects ($ this ->once ())
184
348
->method ('get ' )
185
349
->with (DeployInterface::VAR_SEARCH_CONFIGURATION )
@@ -231,7 +395,7 @@ public function testExecuteEnvironmentConfiguration(bool $newVersion)
231
395
232
396
$ this ->magentoVersionMock ->method ('isGreaterOrEqual ' )
233
397
->willReturn ($ newVersion );
234
-
398
+
235
399
$ this ->stageConfigMock ->expects ($ this ->once ())
236
400
->method ('get ' )
237
401
->with (DeployInterface::VAR_SEARCH_CONFIGURATION )
0 commit comments