@@ -216,9 +216,7 @@ public function testReplicaSync(): void
216
216
217
217
$ sorting = $ this ->populateReplicas (1 );
218
218
219
- $ currentSettings = $ this ->algoliaHelper ->getSettings ($ primaryIndexName );
220
- $ this ->assertArrayHasKey ('replicas ' , $ currentSettings );
221
- $ replicas = $ currentSettings ['replicas ' ];
219
+ $ replicas = $ this ->assertReplicasCreated ($ sorting );
222
220
223
221
$ this ->assertEquals (count ($ sorting ), count ($ replicas ));
224
222
$ this ->assertSortToReplicaConfigParity ($ primaryIndexName , $ sorting , $ replicas );
@@ -229,26 +227,14 @@ public function testReplicaSync(): void
229
227
*/
230
228
public function testReplicaDelete (): void
231
229
{
232
- $ primaryIndexName = $ this ->indexName ;
233
-
234
230
// Make one replica virtual
235
231
$ this ->mockSortUpdate ('price ' , 'asc ' , ['virtualReplica ' => 1 ]);
236
232
237
- $ sorting = $ this ->populateReplicas (1 );
238
-
239
- $ currentSettings = $ this ->algoliaHelper ->getSettings ($ primaryIndexName );
240
- $ this ->assertArrayHasKey ('replicas ' , $ currentSettings );
241
- $ replicas = $ currentSettings ['replicas ' ];
242
-
243
- $ this ->assertEquals (count ($ sorting ), count ($ replicas ));
233
+ $ replicas = $ this ->assertReplicasCreated ($ this ->populateReplicas (1 ));
244
234
245
235
$ this ->replicaManager ->deleteReplicasFromAlgolia (1 );
246
236
247
- $ newSettings = $ this ->algoliaHelper ->getSettings ($ primaryIndexName );
248
- $ this ->assertArrayNotHasKey ('replicas ' , $ newSettings );
249
- foreach ($ replicas as $ replica ) {
250
- $ this ->assertIndexNotExists ($ this ->extractIndexFromReplicaSetting ($ replica ));
251
- }
237
+ $ this ->assertReplicasDeleted ($ replicas );
252
238
}
253
239
254
240
/**
@@ -257,42 +243,28 @@ public function testReplicaDelete(): void
257
243
*/
258
244
public function testReplicaDeleteUnreliable (): void
259
245
{
260
- $ primaryIndexName = $ this ->indexName ;
261
-
262
- $ sorting = $ this ->populateReplicas (1 );
263
-
264
- $ currentSettings = $ this ->algoliaHelper ->getSettings ($ primaryIndexName );
265
- $ this ->assertArrayHasKey ('replicas ' , $ currentSettings );
266
- $ replicas = $ currentSettings ['replicas ' ];
267
-
268
- $ this ->assertEquals (count ($ sorting ), count ($ replicas ));
246
+ $ replicas = $ this ->assertReplicasCreated ($ this ->populateReplicas (1 ));
269
247
270
248
$ this ->getMustPrevalidateMockReplicaManager ()->deleteReplicasFromAlgolia (1 );
271
249
272
- $ newSettings = $ this ->algoliaHelper ->getSettings ($ primaryIndexName );
273
- $ this ->assertArrayNotHasKey ('replicas ' , $ newSettings );
274
- foreach ($ replicas as $ replica ) {
275
- $ this ->assertIndexNotExists ($ this ->extractIndexFromReplicaSetting ($ replica ));
276
- }
250
+ $ this ->assertReplicasDeleted ($ replicas );
277
251
}
278
252
279
253
/**
254
+ * Test the RebuildReplicasPatch with API failures
280
255
* @magentoConfigFixture current_store algoliasearch_instant/instant/is_instant_enabled 1
281
256
*/
282
257
public function testReplicaRebuildPatch (): void
283
258
{
284
- $ primaryIndexName = $ this ->indexName ;
285
259
$ sorting = $ this ->populateReplicas (1 );
286
- $ currentSettings = $ this ->algoliaHelper ->getSettings ($ primaryIndexName );
287
- $ this ->assertArrayHasKey ('replicas ' , $ currentSettings );
288
- $ replicas = $ currentSettings ['replicas ' ];
260
+ $ replicas = $ this ->assertReplicasCreated ($ sorting );
289
261
290
- $ this ->assertTrue ($ this ->configHelper ->credentialsAreConfigured ());
262
+ $ this ->assertTrue ($ this ->configHelper ->credentialsAreConfigured (), " Credentials not available to apply patch. " );
291
263
292
264
$ patch = new \Algolia \AlgoliaSearch \Setup \Patch \Data \RebuildReplicasPatch (
293
265
$ this ->objectManager ->get (ModuleDataSetupInterface::class),
294
266
$ this ->objectManager ->get (StoreManagerInterface::class),
295
- $ this ->getTroublesomePatchReplicaManager (),
267
+ $ this ->getTroublesomePatchReplicaManager ($ replicas ),
296
268
$ this ->objectManager ->get (ProductHelper::class),
297
269
$ this ->objectManager ->get (AppState::class),
298
270
$ this ->objectManager ->get (ReplicaState::class),
@@ -301,6 +273,10 @@ public function testReplicaRebuildPatch(): void
301
273
);
302
274
303
275
$ patch ->apply ();
276
+
277
+ $ this ->algoliaHelper ->waitLastTask ();
278
+ $ this ->assertEquals (count ($ sorting ), count ($ replicas ));
279
+ $ this ->assertSortToReplicaConfigParity ($ this ->indexName , $ sorting , $ replicas );
304
280
}
305
281
306
282
protected function extractIndexFromReplicaSetting (string $ setting ): string {
@@ -330,8 +306,10 @@ protected function getMustPrevalidateMockReplicaManager(): ReplicaManagerInterfa
330
306
/**
331
307
* This mock is to recreate the scenario where a patch tries to apply up to 3 times but the replicas
332
308
* are never detached which throws a replica delete error until the last attempt which should succeed
309
+ *
310
+ * @param array $replicas - replicas that are to be deleted
333
311
*/
334
- protected function getTroublesomePatchReplicaManager (): ReplicaManager
312
+ protected function getTroublesomePatchReplicaManager (array $ replicas ): ReplicaManager
335
313
{
336
314
$ mock = $ this ->getMockReplicaManager ([
337
315
'clearReplicasSettingInAlgolia ' => null ,
@@ -348,9 +326,17 @@ protected function getTroublesomePatchReplicaManager(): ReplicaManager
348
326
$ mock
349
327
->expects ($ this ->any ())
350
328
->method ('deleteReplicas ' )
351
- ->willReturnCallback (function (array $ replicasToDelete , ...$ params ) use ($ mock ) {
329
+ ->willReturnCallback (function (array $ replicasToDelete , ...$ params ) use ($ mock, $ replicas ) {
352
330
$ originalMethod = new \ReflectionMethod (ReplicaManager::class, 'deleteReplicas ' );
353
331
$ originalMethod ->invoke ($ mock , $ replicasToDelete , false , false );
332
+ if ($ this ->patchRetries ) return ;
333
+ $ this ->runOnce (
334
+ function () use ($ replicas ) {
335
+ $ this ->algoliaHelper ->waitLastTask ();
336
+ $ this ->assertReplicasDeleted ($ replicas );
337
+ },
338
+ 'patchDeleteTest '
339
+ );
354
340
});
355
341
356
342
return $ mock ;
@@ -384,6 +370,34 @@ protected function getMockReplicaManager($mockedMethods = array()): MockObject &
384
370
return $ mockedReplicaManager ;
385
371
}
386
372
373
+ /**
374
+ * Setup replicas for testing and assert that they have been synced to Algolia
375
+ * @param array $sorting - the array of sorts from Magento
376
+ * @return array - The replica setting from Algolia
377
+ * @throws AlgoliaException
378
+ * @throws ExceededRetriesException
379
+ * @throws \ReflectionException
380
+ */
381
+ protected function assertReplicasCreated (array $ sorting ): array
382
+ {
383
+ $ currentSettings = $ this ->algoliaHelper ->getSettings ($ this ->indexName );
384
+ $ this ->assertArrayHasKey (ReplicaManager::ALGOLIA_SETTINGS_KEY_REPLICAS , $ currentSettings );
385
+ $ replicas = $ currentSettings [ReplicaManager::ALGOLIA_SETTINGS_KEY_REPLICAS ];
386
+
387
+ $ this ->assertEquals (count ($ sorting ), count ($ replicas ));
388
+
389
+ return $ replicas ;
390
+ }
391
+
392
+ protected function assertReplicasDeleted ($ originalReplicas ): void
393
+ {
394
+ $ newSettings = $ this ->algoliaHelper ->getSettings ($ this ->indexName );
395
+ $ this ->assertArrayNotHasKey (ReplicaManager::ALGOLIA_SETTINGS_KEY_REPLICAS , $ newSettings );
396
+ foreach ($ originalReplicas as $ replica ) {
397
+ $ this ->assertIndexNotExists ($ this ->extractIndexFromReplicaSetting ($ replica ));
398
+ }
399
+ }
400
+
387
401
/**
388
402
* Populate replica indices for test based on store id and return sorting configuration used
389
403
*
0 commit comments