Skip to content

Commit e420ed3

Browse files
committed
MAGE-1044 Refactor common assertions
1 parent 34b41bb commit e420ed3

File tree

1 file changed

+29
-16
lines changed

1 file changed

+29
-16
lines changed

Test/Integration/Product/ReplicaIndexingTest.php

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,8 @@ public function testStandardReplicaConfig(): void
6464
$this->assertTrue($this->isStandardReplica($currentSettings['replicas'], $sortIndexName));
6565
$this->assertFalse($this->isVirtualReplica($currentSettings['replicas'], $sortIndexName));
6666

67-
// Assert replica index created
68-
$replicaSettings = $this->algoliaHelper->getSettings($sortIndexName);
69-
$this->assertArrayHasKey('primary', $replicaSettings);
70-
$this->assertEquals($indexName, $replicaSettings['primary']);
71-
72-
// Assert standard replica ranking config
73-
$this->assertArrayHasKey('ranking', $replicaSettings);
74-
$this->assertEquals("$sortDir($sortAttr)", array_shift($replicaSettings['ranking']));
75-
67+
$replicaSettings = $this->assertReplicaIndexExists($indexName, $sortIndexName);
68+
$this->assertStandardReplicaRanking($replicaSettings, $sortAttr, $sortDir);
7669
}
7770

7871
/**
@@ -125,13 +118,8 @@ public function testVirtualReplicaConfig(): void
125118
$this->assertFalse($this->isStandardReplica($currentSettings['replicas'], $sortIndexName));
126119

127120
// Assert replica index created
128-
$replicaSettings = $this->algoliaHelper->getSettings($sortIndexName);
129-
$this->assertArrayHasKey('primary', $replicaSettings);
130-
$this->assertEquals($indexName, $replicaSettings['primary']);
131-
132-
// Assert virtual replica ranking config
133-
$this->assertArrayHasKey('customRanking', $replicaSettings);
134-
$this->assertEquals("$sortDir($sortAttr)", array_shift($replicaSettings['customRanking']));
121+
$replicaSettings = $this->assertReplicaIndexExists($indexName, $sortIndexName);
122+
$this->assertVirtualReplicaRanking($replicaSettings, $sortAttr, $sortDir);
135123

136124
// Restore prior state (for this test only)
137125
$this->configHelper->setSorting($ogSortingState);
@@ -202,17 +190,42 @@ public function testReplicaSync(): void
202190
$replicas = $currentSettings['replicas'];
203191

204192
$this->assertEquals(count($sorting), count($replicas));
193+
$this->assertSortToReplicaConfigParity($sorting, $replicas);
194+
195+
// TODO: Test ranking
196+
197+
}
205198

199+
protected function assertSortToReplicaConfigParity(array $sorting, array $replicas): void
200+
{
206201
foreach ($sorting as $sortAttr) {
207202
$replicaIndexName = $sortAttr['name'];
208203
$needle = array_key_exists('virtualReplica', $sortAttr) && $sortAttr['virtualReplica']
209204
? "virtual($replicaIndexName)"
210205
: $replicaIndexName;
211206
$this->assertContains($needle, $replicas);
212207
}
208+
}
213209

210+
protected function assertReplicaIndexExists(string $primaryIndexName, string $replicaIndexName): array
211+
{
212+
$replicaSettings = $this->algoliaHelper->getSettings($replicaIndexName);
213+
$this->assertArrayHasKey('primary', $replicaSettings);
214+
$this->assertEquals($primaryIndexName, $replicaSettings['primary']);
215+
return $replicaSettings;
214216
}
215217

218+
protected function assertStandardReplicaRanking(array $replicaSettings, string $sortAttr, string $sortDir): void
219+
{
220+
$this->assertArrayHasKey('ranking', $replicaSettings);
221+
$this->assertEquals("$sortDir($sortAttr)", array_shift($replicaSettings['ranking']));
222+
}
223+
224+
protected function assertVirtualReplicaRanking(array $replicaSettings, string $sortAttr, string $sortDir): void
225+
{
226+
$this->assertArrayHasKey('customRanking', $replicaSettings);
227+
$this->assertEquals("$sortDir($sortAttr)", array_shift($replicaSettings['customRanking']));
228+
}
216229

217230
/**
218231
* @param string[] $replicaSetting

0 commit comments

Comments
 (0)