7
7
8
8
namespace Magento \MagentoCloud \Test \Unit \Process \Deploy \InstallUpdate \ConfigUpdate ;
9
9
10
+ use Magento \MagentoCloud \Config \Deploy \Reader as EnvReader ;
10
11
use Magento \MagentoCloud \Config \Deploy \Writer as EnvWriter ;
12
+ use Magento \MagentoCloud \Config \SearchEngine as SearchEngineConfig ;
13
+ use Magento \MagentoCloud \Config \Shared \Reader as SharedReader ;
11
14
use Magento \MagentoCloud \Config \Shared \Writer as SharedWriter ;
12
15
use Magento \MagentoCloud \Filesystem \FileSystemException ;
13
16
use Magento \MagentoCloud \Package \MagentoVersion ;
14
17
use Magento \MagentoCloud \Package \UndefinedPackageException ;
15
18
use Magento \MagentoCloud \Process \Deploy \InstallUpdate \ConfigUpdate \SearchEngine ;
16
- use Magento \MagentoCloud \Config \SearchEngine as SearchEngineConfig ;
17
19
use Magento \MagentoCloud \Process \ProcessException ;
20
+ use PHPUnit \Framework \MockObject \Matcher \InvokedCount ;
18
21
use PHPUnit \Framework \MockObject \MockObject ;
19
22
use PHPUnit \Framework \TestCase ;
20
23
use Psr \Log \LoggerInterface ;
@@ -54,36 +57,65 @@ class SearchEngineTest extends TestCase
54
57
*/
55
58
private $ configMock ;
56
59
60
+ /**
61
+ * @var EnvReader|MockObject
62
+ */
63
+ private $ envReaderMock ;
64
+
65
+ /**
66
+ * @var SharedReader|MockObject
67
+ */
68
+ private $ sharedReaderMock ;
69
+
57
70
/**
58
71
* @inheritdoc
59
72
*/
60
73
protected function setUp ()
61
74
{
62
75
$ this ->envWriterMock = $ this ->createMock (EnvWriter::class);
76
+ $ this ->envReaderMock = $ this ->createMock (EnvReader::class);
63
77
$ this ->sharedWriterMock = $ this ->createMock (SharedWriter::class);
78
+ $ this ->sharedReaderMock = $ this ->createMock (SharedReader::class);
64
79
$ this ->loggerMock = $ this ->getMockForAbstractClass (LoggerInterface::class);
65
80
$ this ->magentoVersionMock = $ this ->createMock (MagentoVersion::class);
66
81
$ this ->configMock = $ this ->createMock (SearchEngineConfig::class);
67
82
68
83
$ this ->process = new SearchEngine (
69
84
$ this ->loggerMock ,
70
85
$ this ->envWriterMock ,
86
+ $ this ->envReaderMock ,
71
87
$ this ->sharedWriterMock ,
88
+ $ this ->sharedReaderMock ,
72
89
$ this ->magentoVersionMock ,
73
90
$ this ->configMock
74
91
);
75
92
}
76
93
77
94
/**
95
+ * @param bool $is21
96
+ * @param InvokedCount $useSharedWriter
97
+ * @param InvokedCount $useSharedReader
98
+ * @param InvokedCount $useEnvWriter
99
+ * @param InvokedCount $useEnvReader
100
+ * @param array $searchConfig
101
+ * @param array $fileConfig
102
+ * @param array $expectedConfig
78
103
* @throws ProcessException
104
+ * @dataProvider executeDataProvider
79
105
*/
80
- public function testExecute ()
81
- {
82
- $ config ['system ' ]['default ' ]['catalog ' ]['search ' ] = ['engine ' => 'mysql ' ];
83
-
106
+ public function testExecute (
107
+ bool $ is21 ,
108
+ InvokedCount $ useSharedWriter ,
109
+ InvokedCount $ useSharedReader ,
110
+ InvokedCount $ useEnvWriter ,
111
+ InvokedCount $ useEnvReader ,
112
+ array $ searchConfig ,
113
+ array $ fileConfig ,
114
+ array $ expectedConfig
115
+ ) {
84
116
$ this ->configMock ->expects ($ this ->once ())
85
117
->method ('getConfig ' )
86
- ->willReturn ($ config );
118
+ ->willReturn ($ searchConfig );
87
119
$ this ->configMock ->expects ($ this ->once ())
88
120
->method ('getName ' )
89
121
->willReturn ('mysql ' );
@@ -96,46 +128,19 @@ public function testExecute()
96
128
$ this ->magentoVersionMock ->expects ($ this ->once ())
97
129
->method ('satisfies ' )
98
130
->with ('2.1.* ' )
99
- ->willReturn (false );
100
- $ this ->sharedWriterMock ->expects ($ this ->never ())
101
- ->method ('update ' )
102
- ->with ($ config );
103
- $ this ->envWriterMock ->expects ($ this ->once ())
104
- ->method ('update ' )
105
- ->with ($ config );
106
-
107
- $ this ->process ->execute ();
108
- }
109
-
110
- /**
111
- * @throws ProcessException
112
- */
113
- public function testExecute21 ()
114
- {
115
- $ config ['system ' ]['default ' ]['catalog ' ]['search ' ] = ['engine ' => 'mysql ' ];
116
-
117
- $ this ->configMock ->expects ($ this ->once ())
118
- ->method ('getConfig ' )
119
- ->willReturn ($ config );
120
- $ this ->configMock ->expects ($ this ->once ())
121
- ->method ('getName ' )
122
- ->willReturn ('mysql ' );
123
- $ this ->loggerMock ->expects ($ this ->exactly (2 ))
124
- ->method ('info ' )
125
- ->withConsecutive (
126
- ['Updating search engine configuration. ' ],
127
- ['Set search engine to: mysql ' ]
128
- );
129
- $ this ->magentoVersionMock ->expects ($ this ->once ())
130
- ->method ('satisfies ' )
131
- ->with ('2.1.* ' )
132
- ->willReturn (true );
133
- $ this ->sharedWriterMock ->expects ($ this ->once ())
134
- ->method ('update ' )
135
- ->with ($ config );
136
- $ this ->envWriterMock ->expects ($ this ->never ())
137
- ->method ('update ' )
138
- ->with ($ config );
131
+ ->willReturn ($ is21 );
132
+ $ this ->sharedReaderMock ->expects ($ useSharedReader )
133
+ ->method ('read ' )
134
+ ->willReturn ($ fileConfig );
135
+ $ this ->sharedWriterMock ->expects ($ useSharedWriter )
136
+ ->method ('create ' )
137
+ ->with ($ expectedConfig );
138
+ $ this ->envReaderMock ->expects ($ useEnvReader )
139
+ ->method ('read ' )
140
+ ->willReturn ($ fileConfig );
141
+ $ this ->envWriterMock ->expects ($ useEnvWriter )
142
+ ->method ('create ' )
143
+ ->with ($ expectedConfig );
139
144
140
145
$ this ->process ->execute ();
141
146
}
@@ -145,17 +150,139 @@ public function testExecute21()
145
150
*/
146
151
public function executeDataProvider (): array
147
152
{
153
+ $ mysqlSearchConfig ['system ' ]['default ' ]['catalog ' ]['search ' ] = ['engine ' => 'mysql ' ];
154
+ $ elasticSearchConfig = [
155
+ 'system ' => [
156
+ 'default ' => [
157
+ 'smile_elasticsuite_core_base_settings ' => [
158
+ 'option3 ' => 'value3 ' ,
159
+ 'option4 ' => 'value4 '
160
+ ],
161
+ 'catalog ' => [
162
+ 'search ' => [
163
+ 'engine ' => 'elasticsearch5 ' ,
164
+ 'elasticsearh5_host ' => 'localhost ' ,
165
+ 'elasticsearh5_port ' => '9200 ' ,
166
+ ]
167
+ ]
168
+ ]
169
+ ]
170
+ ];
171
+ $ fileConfig = [
172
+ 'config ' => 'value ' ,
173
+ 'system ' => [
174
+ 'default ' => [
175
+ 'smile_elasticsuite_core_base_settings ' => [
176
+ 'option1 ' => 'value1 ' ,
177
+ 'option2 ' => 'value2 '
178
+ ],
179
+ 'category ' => [
180
+ 'option ' => 'value '
181
+ ],
182
+ 'catalog ' => [
183
+ 'search ' => [
184
+ 'engine ' => 'elasticsearch ' ,
185
+ 'elasticsearh_host ' => 'localhost ' ,
186
+ 'elasticsearh_port ' => '9200 ' ,
187
+ ]
188
+ ]
189
+ ],
190
+ 'store1 ' => [
191
+ 'category ' => [
192
+ 'option ' => 'value '
193
+ ],
194
+ ],
195
+ ]
196
+ ];
197
+ $ mysqlExpectedConfig = [
198
+ 'config ' => 'value ' ,
199
+ 'system ' => [
200
+ 'default ' => [
201
+ 'catalog ' => [
202
+ 'search ' => [
203
+ 'engine ' => 'mysql '
204
+ ],
205
+ ],
206
+ 'category ' => [
207
+ 'option ' => 'value '
208
+ ],
209
+ ],
210
+ 'store1 ' => [
211
+ 'category ' => [
212
+ 'option ' => 'value '
213
+ ],
214
+ ],
215
+ ]
216
+ ];
217
+ $ elasticExpectedConfig = [
218
+ 'config ' => 'value ' ,
219
+ 'system ' => [
220
+ 'default ' => [
221
+ 'smile_elasticsuite_core_base_settings ' => [
222
+ 'option3 ' => 'value3 ' ,
223
+ 'option4 ' => 'value4 '
224
+ ],
225
+ 'category ' => [
226
+ 'option ' => 'value '
227
+ ],
228
+ 'catalog ' => [
229
+ 'search ' => [
230
+ 'engine ' => 'elasticsearch5 ' ,
231
+ 'elasticsearh5_host ' => 'localhost ' ,
232
+ 'elasticsearh5_port ' => '9200 ' ,
233
+ ]
234
+ ]
235
+ ],
236
+ 'store1 ' => [
237
+ 'category ' => [
238
+ 'option ' => 'value '
239
+ ],
240
+ ],
241
+ ]
242
+ ];
243
+
148
244
return [
149
- [
245
+ 'magento version 2.1 mysql config ' => [
246
+ 'is21 ' => true ,
247
+ 'useSharedWriter ' => $ this ->once (),
248
+ 'useSharedReader ' => $ this ->once (),
249
+ 'useEnvWriter ' => $ this ->never (),
250
+ 'useEnvReader ' => $ this ->never (),
251
+ 'searchConfig ' => $ mysqlSearchConfig ,
252
+ 'fileConfig ' => $ fileConfig ,
253
+ 'expectedConfig ' => $ mysqlExpectedConfig
254
+ ],
255
+ 'magento version > 2.1 mysql config ' => [
150
256
'is21 ' => false ,
151
257
'useSharedWriter ' => $ this ->never (),
258
+ 'useSharedReader ' => $ this ->never (),
152
259
'useEnvWriter ' => $ this ->once (),
260
+ 'useEnvReader ' => $ this ->once (),
261
+ 'searchConfig ' => $ mysqlSearchConfig ,
262
+ 'fileConfig ' => $ fileConfig ,
263
+ 'expectedConfig ' => $ mysqlExpectedConfig
153
264
],
154
- [
265
+ ' magento version 2.1 elasticsearch config ' => [
155
266
'is21 ' => true ,
156
267
'useSharedWriter ' => $ this ->once (),
268
+ 'useSharedReader ' => $ this ->once (),
157
269
'useEnvWriter ' => $ this ->never (),
270
+ 'useEnvReader ' => $ this ->never (),
271
+ 'searchConfig ' => $ elasticSearchConfig ,
272
+ 'fileConfig ' => $ fileConfig ,
273
+ 'expectedConfig ' => $ elasticExpectedConfig
158
274
],
275
+ 'magento version > 2.1 elasticsearch config ' => [
276
+ 'is21 ' => false ,
277
+ 'useSharedWriter ' => $ this ->never (),
278
+ 'useSharedReader ' => $ this ->never (),
279
+ 'useEnvWriter ' => $ this ->once (),
280
+ 'useEnvReader ' => $ this ->once (),
281
+ 'searchConfig ' => $ elasticSearchConfig ,
282
+ 'fileConfig ' => $ fileConfig ,
283
+ 'expectedConfig ' => $ elasticExpectedConfig
284
+ ],
285
+
159
286
];
160
287
}
161
288
@@ -189,7 +316,7 @@ public function testExecuteWithException()
189
316
->method ('update ' )
190
317
->with ($ config );
191
318
$ this ->envWriterMock ->expects ($ this ->once ())
192
- ->method ('update ' )
319
+ ->method ('create ' )
193
320
->with ($ config )
194
321
->willThrowException (new FileSystemException ('Some error ' ));
195
322
0 commit comments