Skip to content

Commit df8dca1

Browse files
committed
MAGE-1218 Refactor integration tests
1 parent 948e6cb commit df8dca1

File tree

2 files changed

+71
-39
lines changed

2 files changed

+71
-39
lines changed

Test/Integration/Product/ReplicaIndexingTest.php

Lines changed: 53 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -216,9 +216,7 @@ public function testReplicaSync(): void
216216

217217
$sorting = $this->populateReplicas(1);
218218

219-
$currentSettings = $this->algoliaHelper->getSettings($primaryIndexName);
220-
$this->assertArrayHasKey('replicas', $currentSettings);
221-
$replicas = $currentSettings['replicas'];
219+
$replicas = $this->assertReplicasCreated($sorting);
222220

223221
$this->assertEquals(count($sorting), count($replicas));
224222
$this->assertSortToReplicaConfigParity($primaryIndexName, $sorting, $replicas);
@@ -229,26 +227,14 @@ public function testReplicaSync(): void
229227
*/
230228
public function testReplicaDelete(): void
231229
{
232-
$primaryIndexName = $this->indexName;
233-
234230
// Make one replica virtual
235231
$this->mockSortUpdate('price', 'asc', ['virtualReplica' => 1]);
236232

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));
244234

245235
$this->replicaManager->deleteReplicasFromAlgolia(1);
246236

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);
252238
}
253239

254240
/**
@@ -257,42 +243,28 @@ public function testReplicaDelete(): void
257243
*/
258244
public function testReplicaDeleteUnreliable(): void
259245
{
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));
269247

270248
$this->getMustPrevalidateMockReplicaManager()->deleteReplicasFromAlgolia(1);
271249

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);
277251
}
278252

279253
/**
254+
* Test the RebuildReplicasPatch with API failures
280255
* @magentoConfigFixture current_store algoliasearch_instant/instant/is_instant_enabled 1
281256
*/
282257
public function testReplicaRebuildPatch(): void
283258
{
284-
$primaryIndexName = $this->indexName;
285259
$sorting = $this->populateReplicas(1);
286-
$currentSettings = $this->algoliaHelper->getSettings($primaryIndexName);
287-
$this->assertArrayHasKey('replicas', $currentSettings);
288-
$replicas = $currentSettings['replicas'];
260+
$replicas = $this->assertReplicasCreated($sorting);
289261

290-
$this->assertTrue($this->configHelper->credentialsAreConfigured());
262+
$this->assertTrue($this->configHelper->credentialsAreConfigured(), "Credentials not available to apply patch.");
291263

292264
$patch = new \Algolia\AlgoliaSearch\Setup\Patch\Data\RebuildReplicasPatch(
293265
$this->objectManager->get(ModuleDataSetupInterface::class),
294266
$this->objectManager->get(StoreManagerInterface::class),
295-
$this->getTroublesomePatchReplicaManager(),
267+
$this->getTroublesomePatchReplicaManager($replicas),
296268
$this->objectManager->get(ProductHelper::class),
297269
$this->objectManager->get(AppState::class),
298270
$this->objectManager->get(ReplicaState::class),
@@ -301,6 +273,10 @@ public function testReplicaRebuildPatch(): void
301273
);
302274

303275
$patch->apply();
276+
277+
$this->algoliaHelper->waitLastTask();
278+
$this->assertEquals(count($sorting), count($replicas));
279+
$this->assertSortToReplicaConfigParity($this->indexName, $sorting, $replicas);
304280
}
305281

306282
protected function extractIndexFromReplicaSetting(string $setting): string {
@@ -330,8 +306,10 @@ protected function getMustPrevalidateMockReplicaManager(): ReplicaManagerInterfa
330306
/**
331307
* This mock is to recreate the scenario where a patch tries to apply up to 3 times but the replicas
332308
* 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
333311
*/
334-
protected function getTroublesomePatchReplicaManager(): ReplicaManager
312+
protected function getTroublesomePatchReplicaManager(array $replicas): ReplicaManager
335313
{
336314
$mock = $this->getMockReplicaManager([
337315
'clearReplicasSettingInAlgolia' => null,
@@ -348,9 +326,17 @@ protected function getTroublesomePatchReplicaManager(): ReplicaManager
348326
$mock
349327
->expects($this->any())
350328
->method('deleteReplicas')
351-
->willReturnCallback(function(array $replicasToDelete, ...$params) use ($mock) {
329+
->willReturnCallback(function(array $replicasToDelete, ...$params) use ($mock, $replicas) {
352330
$originalMethod = new \ReflectionMethod(ReplicaManager::class, 'deleteReplicas');
353331
$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+
);
354340
});
355341

356342
return $mock;
@@ -384,6 +370,34 @@ protected function getMockReplicaManager($mockedMethods = array()): MockObject &
384370
return $mockedReplicaManager;
385371
}
386372

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+
387401
/**
388402
* Populate replica indices for test based on store id and return sorting configuration used
389403
*

Test/Integration/TestCase.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,4 +267,22 @@ protected function getSerializer()
267267
{
268268
return $this->getObjectManager()->get(\Magento\Framework\Serialize\SerializerInterface::class);
269269
}
270+
271+
/**
272+
* Run a callback once and only once
273+
* @param callable $callback
274+
* @param string|null $key - a unique key for this operation - if null a unique key will be derived
275+
* @return mixed
276+
*/
277+
function runOnce(callable $callback, string $key = null): mixed
278+
{
279+
static $executed = [];
280+
$key ??= is_string($callback) ? $callback : spl_object_hash((object) $callback);
281+
if (!isset($executed[$key])) {
282+
$executed[$key] = true;
283+
return $callback();
284+
}
285+
286+
return null;
287+
}
270288
}

0 commit comments

Comments
 (0)