Skip to content

Commit d69759c

Browse files
committed
MAGE-1044 Add mock sort update for in-memory config tests
1 parent 8a1a68f commit d69759c

File tree

1 file changed

+40
-17
lines changed

1 file changed

+40
-17
lines changed

Test/Integration/Product/ReplicaIndexingTest.php

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Algolia\AlgoliaSearch\Test\Integration\Product;
44

55
use Algolia\AlgoliaSearch\Api\Product\ReplicaManagerInterface;
6+
use Algolia\AlgoliaSearch\Helper\ConfigHelper;
67
use Algolia\AlgoliaSearch\Helper\Entity\ProductHelper;
78
use Algolia\AlgoliaSearch\Model\Indexer\Product as ProductIndexer;
89
use Algolia\AlgoliaSearch\Model\IndicesConfigurator;
@@ -83,11 +84,8 @@ public function testStandardReplicaConfig(): void
8384
public function testVirtualReplicaConfig(): void
8485
{
8586
$indexName = $this->getIndexName('default_');
86-
$ogAlgoliaSettings = $this->algoliaHelper->getSettings($indexName);
8787
$ogSortingState = $this->configHelper->getSorting();
8888

89-
$this->assertFalse(array_key_exists('replicas', $ogAlgoliaSettings));
90-
9189
$productHelper = $this->objectManager->get(ProductHelper::class);
9290
$sortAttr = 'color';
9391
$sortDir = 'asc';
@@ -105,14 +103,14 @@ public function testVirtualReplicaConfig(): void
105103
];
106104
$this->configHelper->setSorting($sorting);
107105

108-
$this->assertConfigInDb('algoliasearch_instant/instant_sorts/sorts', json_encode($sorting));
106+
$this->assertConfigInDb(ConfigHelper::SORTING_INDICES, json_encode($sorting));
109107

110108
$this->refreshConfigFromDb();
111109

112110
$this->assertSortingAttribute($sortAttr, $sortDir);
113111

114112
// Cannot use config fixture because we have disabled db isolation
115-
$this->setConfig('algoliasearch_instant/instant/is_instant_enabled', 1);
113+
$this->setConfig(ConfigHelper::IS_INSTANT_ENABLED, 1);
116114

117115
$this->indicesConfigurator->saveConfigurationToAlgolia(1);
118116
$this->algoliaHelper->waitLastTask();
@@ -136,9 +134,37 @@ public function testVirtualReplicaConfig(): void
136134
$this->assertEquals("$sortDir($sortAttr)", array_shift($replicaSettings['customRanking']));
137135

138136
// Restore prior state (for this test only)
139-
$this->algoliaHelper->setSettings($indexName, $ogAlgoliaSettings);
140137
$this->configHelper->setSorting($ogSortingState);
141-
$this->setConfig('algoliasearch_instant/instant/is_instant_enabled', 0);
138+
$this->setConfig(ConfigHelper::IS_INSTANT_ENABLED, 0);
139+
}
140+
141+
/**
142+
* ConfigHelper::setSorting used WriterInterface which does not update unless DB isolation is disabled
143+
* This provides a workaround to test using MutableScopeConfigInterface with DB isolation enabled
144+
*/
145+
protected function mockSortUpdate(string $sortAttr, string $sortDir, array $attr): void
146+
{
147+
$sorting = $this->configHelper->getSorting();
148+
$existing = array_filter($sorting, function ($item) use ($sortAttr, $sortDir) {
149+
return $item['attribute'] === $sortAttr && $item['sort'] === $sortDir;
150+
});
151+
152+
153+
if ($existing) {
154+
$idx = array_key_first($existing);
155+
$sorting[$idx] = array_merge($existing[$idx], $attr);
156+
}
157+
else {
158+
$sorting[] = array_merge(
159+
[
160+
'attribute' => $sortAttr,
161+
'sort' => $sortDir,
162+
'sortLabel' => $sortAttr
163+
],
164+
$attr
165+
);
166+
}
167+
$this->setConfig(ConfigHelper::SORTING_INDICES, json_encode($sorting));
142168
}
143169

144170
/**
@@ -148,38 +174,35 @@ public function testVirtualReplicaConfig(): void
148174
public function testReplicaRebuild(): void
149175
{
150176
$indexName = $this->getIndexName('default_');
151-
$ogAlgoliaSettings = $this->algoliaHelper->getSettings($indexName);
152177

153178
$cmd = $this->objectManager->get(\Algolia\AlgoliaSearch\Console\Command\ReplicaRebuildCommand::class);
154179
$this->assertTrue(true);
155180
}
156181

157182
/**
158183
* @magentoConfigFixture current_store algoliasearch_instant/instant/is_instant_enabled 1
184+
* @throws \ReflectionException
159185
*/
160186
public function testReplicaSync(): void
161187
{
162188
$indexName = $this->getIndexName('default_');
163-
$ogAlgoliaSettings = $this->algoliaHelper->getSettings($indexName);
164-
$this->assertFalse(array_key_exists('replicas', $ogAlgoliaSettings));
165-
$sorting = $this->configHelper->getSorting();
189+
190+
$this->mockSortUpdate('created_at', 'desc', ['virtualReplica' => 1]);
191+
192+
$sorting = $this->objectManager->get(\Algolia\AlgoliaSearch\Service\Product\SortingTransformer::class)->getSortingIndices(1, null, null, true);
166193

167194
$cmd = $this->objectManager->create(\Algolia\AlgoliaSearch\Console\Command\ReplicaSyncCommand::class);
168195

169196
$this->mockProperty($cmd, 'output', \Symfony\Component\Console\Output\OutputInterface::class);
170197

171198
$cmd->syncReplicas();
172199

173-
// $this->indicesConfigurator->saveConfigurationToAlgolia(1);
174-
// $this->algoliaHelper->waitLastTask();
175-
176200
$currentSettings = $this->algoliaHelper->getSettings($indexName);
177201
$this->assertArrayHasKey('replicas', $currentSettings);
202+
$replicas = $currentSettings['replicas'];
178203

179-
$this->assertTrue(count($currentSettings['replicas']) >= count($sorting));
204+
$this->assertTrue(count($replicas) >= count($sorting));
180205

181-
// reset
182-
$this->algoliaHelper->setSettings($indexName, $ogAlgoliaSettings);
183206
}
184207

185208

0 commit comments

Comments
 (0)