Skip to content

Commit 6556cae

Browse files
committed
MAGE-1251 Update integration tests to use AlgoliaConnector
1 parent 44408ea commit 6556cae

File tree

4 files changed

+99
-87
lines changed

4 files changed

+99
-87
lines changed

Service/Product/ReplicaManager.php

Lines changed: 38 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
use Algolia\AlgoliaSearch\Exception\TooManyCustomerGroupsAsReplicasException;
1010
use Algolia\AlgoliaSearch\Exceptions\AlgoliaException;
1111
use Algolia\AlgoliaSearch\Exceptions\ExceededRetriesException;
12-
use Algolia\AlgoliaSearch\Helper\AlgoliaHelper;
1312
use Algolia\AlgoliaSearch\Helper\ConfigHelper;
1413
use Algolia\AlgoliaSearch\Logger\DiagnosticsLogger;
1514
use Algolia\AlgoliaSearch\Registry\ReplicaState;
15+
use Algolia\AlgoliaSearch\Service\AlgoliaConnector;
1616
use Algolia\AlgoliaSearch\Service\IndexNameFetcher;
1717
use Algolia\AlgoliaSearch\Service\StoreNameFetcher;
1818
use Algolia\AlgoliaSearch\Validator\VirtualReplicaValidatorFactory;
@@ -57,7 +57,8 @@ class ReplicaManager implements ReplicaManagerInterface
5757

5858
public function __construct(
5959
protected ConfigHelper $configHelper,
60-
protected AlgoliaHelper $algoliaHelper,
60+
protected AlgoliaConnector $algoliaConnector,
61+
protected IndexOptionsBuilder $indexOptionsBuilder,
6162
protected ReplicaState $replicaState,
6263
protected VirtualReplicaValidatorFactory $validatorFactory,
6364
protected IndexNameFetcher $indexNameFetcher,
@@ -99,20 +100,18 @@ protected function hasReplicaConfigurationChanged(int $storeId): bool
99100
/**
100101
* @param int $storeId
101102
* @param bool $refreshCache
102-
* @param string|null $primaryIndexName
103103
* @return string[]
104104
* @throws LocalizedException
105105
* @throws NoSuchEntityException
106106
*/
107107
protected function getReplicaConfigurationFromAlgolia(
108108
int $storeId,
109-
bool $refreshCache = false,
110-
?string $primaryIndexName = null): array
109+
bool $refreshCache = false): array
111110
{
112-
$primaryIndexName ??= $this->indexNameFetcher->getProductIndexName($storeId);
111+
$primaryIndexName = $this->indexNameFetcher->getProductIndexName($storeId);
113112
if ($refreshCache || !isset($this->_algoliaReplicaConfig[$primaryIndexName])) {
114113
try {
115-
$currentSettings = $this->algoliaHelper->getSettings($primaryIndexName, $storeId);
114+
$currentSettings = $this->algoliaConnector->getSettings($this->indexOptionsBuilder->buildEntityIndexOptions($storeId));
116115
$this->_algoliaReplicaConfig[$primaryIndexName] = array_key_exists(self::ALGOLIA_SETTINGS_KEY_REPLICAS, $currentSettings)
117116
? $currentSettings[self::ALGOLIA_SETTINGS_KEY_REPLICAS]
118117
: [];
@@ -143,15 +142,18 @@ protected function clearAlgoliaReplicaSettingCache($primaryIndexName = null): vo
143142
* @return string[] Array of replica index names
144143
* @throws LocalizedException
145144
* @throws NoSuchEntityException
145+
* @throws AlgoliaException
146146
*/
147147
protected function getMagentoReplicaConfigurationFromAlgolia(
148148
int $storeId,
149149
bool $refreshCache = false
150150
): array
151151
{
152-
$primaryIndexName = $this->indexNameFetcher->getProductIndexName($storeId);
153-
$algoliaReplicas = $this->getReplicaConfigurationFromAlgolia($storeId, $refreshCache, $primaryIndexName);
154-
$magentoReplicas = $this->getMagentoReplicaSettings($primaryIndexName, $algoliaReplicas);
152+
$algoliaReplicas = $this->getReplicaConfigurationFromAlgolia($storeId, $refreshCache);
153+
$magentoReplicas = $this->getMagentoReplicaSettings(
154+
$this->indexNameFetcher->getProductIndexName($storeId),
155+
$algoliaReplicas
156+
);
155157
return array_values(array_intersect($magentoReplicas, $algoliaReplicas));
156158
}
157159

@@ -265,16 +267,12 @@ protected function setReplicasOnPrimaryIndex(int $storeId): array
265267
$replicasToRank = $this->getBareIndexNamesFromReplicaSetting(array_diff($newMagentoReplicasSetting, $oldMagentoReplicasSetting));
266268
$replicasToUpdate = array_diff($replicasToRank, $replicasToAdd);
267269

268-
$this->algoliaHelper->setSettings(
269-
$indexName,
270-
[self::ALGOLIA_SETTINGS_KEY_REPLICAS => array_merge($newMagentoReplicasSetting, $nonMagentoReplicasSetting)],
271-
false,
272-
false,
273-
'',
274-
$storeId
270+
$this->algoliaConnector->setSettings(
271+
$this->indexOptionsBuilder->buildEntityIndexOptions($storeId),
272+
[self::ALGOLIA_SETTINGS_KEY_REPLICAS => array_merge($newMagentoReplicasSetting, $nonMagentoReplicasSetting)]
275273
);
276-
$setReplicasTaskId = $this->algoliaHelper->getLastTaskId($storeId);
277-
$this->algoliaHelper->waitLastTask($storeId, $indexName, $setReplicasTaskId);
274+
$setReplicasTaskId = $this->algoliaConnector->getLastTaskId($storeId);
275+
$this->algoliaConnector->waitLastTask($storeId, $indexName, $setReplicasTaskId);
278276
$this->clearAlgoliaReplicaSettingCache($indexName);
279277
$this->deleteReplicas($storeId, $replicasToDelete, false, false);
280278

@@ -390,7 +388,7 @@ protected function deleteReplicas(
390388
foreach ($replicasToDelete as $deletedReplica) {
391389
$this->deleteReplica($storeId, $deletedReplica, $safeMode);
392390
if ($waitLastTask) {
393-
$this->algoliaHelper->waitLastTask($storeId, $deletedReplica);
391+
$this->algoliaConnector->waitLastTask($storeId, $deletedReplica);
394392
}
395393
}
396394
}
@@ -402,14 +400,15 @@ protected function deleteReplicas(
402400
*/
403401
protected function deleteReplica(int $storeId, string $replicaIndexName, bool $safeMode = false): void
404402
{
403+
$indexOptions = $this->indexOptionsBuilder->buildWithEnforcedIndex($replicaIndexName, $storeId);
405404
if ($safeMode) {
406-
$settings = $this->algoliaHelper->getSettings($replicaIndexName, $storeId);
405+
$settings = $this->algoliaConnector->getSettings($indexOptions);
407406
if (isset($settings[self::ALGOLIA_SETTINGS_KEY_PRIMARY])) {
408-
$this->detachReplica($storeId, $replicaIndexName, $settings[self::ALGOLIA_SETTINGS_KEY_PRIMARY]);
407+
$this->detachReplica($storeId, $replicaIndexName);
409408
}
410409
}
411410

412-
$this->algoliaHelper->deleteIndex($replicaIndexName, $storeId);
411+
$this->algoliaConnector->deleteIndex($indexOptions);
413412
}
414413

415414
/**
@@ -419,24 +418,20 @@ protected function deleteReplica(int $storeId, string $replicaIndexName, bool $s
419418
* @throws AlgoliaException
420419
* @throws NoSuchEntityException
421420
*/
422-
protected function detachReplica(int $storeId, string $replicaIndexName, ?string $primaryIndexName = null): void
421+
protected function detachReplica(int $storeId, string $replicaIndexName): void
423422
{
424-
$primaryIndexName ??= $this->indexNameFetcher->getProductIndexName($storeId);
423+
$indexOptions = $this->indexOptionsBuilder->buildEntityIndexOptions($storeId);
425424

426-
$settings = $this->algoliaHelper->getSettings($primaryIndexName, $storeId);
425+
$settings = $this->algoliaConnector->getSettings($indexOptions);
427426
if (!isset($settings[self::ALGOLIA_SETTINGS_KEY_REPLICAS])) {
428427
return;
429428
}
430-
$newReplicas = $this->removeReplicaFromReplicaSetting($settings[self::ALGOLIA_SETTINGS_KEY_REPLICAS], $replicaIndexName);
431-
$this->algoliaHelper->setSettings(
432-
$primaryIndexName,
433-
[ self::ALGOLIA_SETTINGS_KEY_REPLICAS => $newReplicas],
434-
false,
435-
false,
436-
'',
437-
$storeId
429+
$newReplicas = $this->removeReplicaFromReplicaSetting(
430+
$settings[self::ALGOLIA_SETTINGS_KEY_REPLICAS],
431+
$replicaIndexName
438432
);
439-
$this->algoliaHelper->waitLastTask($storeId, $primaryIndexName);
433+
$this->algoliaConnector->setSettings($indexOptions, [ self::ALGOLIA_SETTINGS_KEY_REPLICAS => $newReplicas]);
434+
$this->algoliaConnector->waitLastTask($storeId);
440435
}
441436

442437
/**
@@ -478,32 +473,18 @@ function($replica) use ($replicas) {
478473
}
479474
);
480475
foreach ($replicaDetails as $replica) {
481-
$replicaName = $replica['name'];
476+
$indexOptions = $this->indexOptionsBuilder->buildWithEnforcedIndex($replica['name'], $storeId);
482477
// Virtual replicas - relevant sort
483478
if (!empty($replica[self::SORT_KEY_VIRTUAL_REPLICA])) {
484479
$customRanking = array_key_exists('customRanking', $primaryIndexSettings)
485480
? $primaryIndexSettings['customRanking']
486481
: [];
487482
array_unshift($customRanking, $replica['ranking'][0]);
488-
$this->algoliaHelper->setSettings(
489-
$replicaName,
490-
[ 'customRanking' => $customRanking ],
491-
false,
492-
false,
493-
'',
494-
$storeId
495-
);
483+
$this->algoliaConnector->setSettings($indexOptions, [ 'customRanking' => $customRanking ]);
496484
// Standard replicas - exhaustive sort
497485
} else {
498486
$primaryIndexSettings['ranking'] = $replica['ranking'];
499-
$this->algoliaHelper->setSettings(
500-
$replicaName,
501-
$primaryIndexSettings,
502-
false,
503-
false,
504-
'',
505-
$storeId
506-
);
487+
$this->algoliaConnector->setSettings($indexOptions, $primaryIndexSettings);
507488
}
508489
}
509490
}
@@ -526,20 +507,13 @@ public function getMaxVirtualReplicasPerIndex() : int
526507

527508
/**
528509
* @throws AlgoliaException
510+
* @throws NoSuchEntityException
529511
*/
530512
protected function clearReplicasSettingInAlgolia(int $storeId): void
531513
{
532-
$primaryIndexName = $this->indexNameFetcher->getProductIndexName($storeId);
533-
$this->algoliaHelper->setSettings(
534-
$primaryIndexName,
535-
[ self::ALGOLIA_SETTINGS_KEY_REPLICAS => []],
536-
false,
537-
false,
538-
'',
539-
$storeId
540-
)
541-
;
542-
$this->algoliaHelper->waitLastTask($storeId, $primaryIndexName);
514+
$indexOptions = $this->indexOptionsBuilder->buildEntityIndexOptions($storeId);
515+
$this->algoliaConnector->setSettings($indexOptions, [ self::ALGOLIA_SETTINGS_KEY_REPLICAS => []]);
516+
$this->algoliaConnector->waitLastTask($storeId);
543517
}
544518

545519
/**
@@ -578,7 +552,7 @@ public function getUnusedReplicaIndices(int $storeId): array
578552
if (!isset($this->_unusedReplicaIndices[$storeId])) {
579553
$currentReplicas = $this->getMagentoReplicaIndicesFromAlgolia($storeId);
580554
$unusedReplicas = [];
581-
$allIndices = $this->algoliaHelper->listIndexes($storeId);
555+
$allIndices = $this->algoliaConnector->listIndexes($storeId);
582556

583557
foreach ($allIndices['items'] as $indexInfo) {
584558
$indexName = $indexInfo['name'];

Test/Integration/Indexing/Product/MultiStoreReplicaTest.php

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Algolia\AlgoliaSearch\Console\Command\ReplicaRebuildCommand;
77
use Algolia\AlgoliaSearch\Console\Command\ReplicaSyncCommand;
88
use Algolia\AlgoliaSearch\Helper\ConfigHelper;
9+
use Algolia\AlgoliaSearch\Service\Product\IndexOptionsBuilder;
910
use Algolia\AlgoliaSearch\Service\Product\SortingTransformer;
1011
use Algolia\AlgoliaSearch\Test\Integration\Indexing\Config\Traits\ConfigAssertionsTrait;
1112
use Algolia\AlgoliaSearch\Test\Integration\Indexing\MultiStoreTestCase;
@@ -32,11 +33,14 @@ class MultiStoreReplicaTest extends MultiStoreTestCase
3233

3334
protected ?SerializerInterface $serializer = null;
3435

36+
protected ?IndexOptionsBuilder $indexOptionsBuilder = null;
37+
3538
protected function setUp(): void
3639
{
3740
parent::setUp();
3841
$this->replicaManager = $this->objectManager->get(ReplicaManagerInterface::class);
3942
$this->serializer = $this->objectManager->get(SerializerInterface::class);
43+
$this->indexOptionsBuilder = $this->objectManager->get(IndexOptionsBuilder::class);
4044

4145
$stores = $this->storeManager->getStores();
4246

@@ -72,7 +76,7 @@ public function testCustomerGroupsConfig()
7276
// Enable customer groups for second fixture store and save configuration
7377
$this->setConfig( ConfigHelper::CUSTOMER_GROUPS_ENABLE, 1, $fixtureSecondStore->getCode());
7478
$this->indicesConfigurator->saveConfigurationToAlgolia($fixtureSecondStore->getId());
75-
$this->algoliaHelper->waitLastTask($fixtureSecondStore->getId());
79+
$this->algoliaConnector->waitLastTask($fixtureSecondStore->getId());
7680

7781
// 7 indices for default store and third store:
7882
// - 1 for categories
@@ -95,8 +99,8 @@ public function testReplicaCommands()
9599
$defaultStore = $this->storeRepository->get('default');
96100
$fixtureSecondStore = $this->storeRepository->get('fixture_second_store');
97101

98-
$defaultIndexName = $this->indexPrefix . $defaultStore->getCode() . '_products';
99-
$fixtureIndexName = $this->indexPrefix . $fixtureSecondStore->getCode() . '_products';
102+
$defaultIndexName = $this->indexNameFetcher->getProductIndexName($defaultStore->getId());
103+
$fixtureIndexName = $this->indexNameFetcher->getProductIndexName($fixtureSecondStore->getId());
100104

101105
// Update store config for fixture only
102106
$this->mockSortUpdate('price', 'desc', ['virtualReplica' => 1], $fixtureSecondStore);
@@ -119,8 +123,8 @@ public function testReplicaCommands()
119123
$syncCmd = $this->objectManager->get(ReplicaSyncCommand::class);
120124
$this->mockProperty($syncCmd, 'output', OutputInterface::class);
121125
$syncCmd->syncReplicas();
122-
$this->algoliaHelper->waitLastTask($defaultStore->getId());
123-
$this->algoliaHelper->waitLastTask($fixtureSecondStore->getId());
126+
$this->algoliaConnector->waitLastTask($defaultStore->getId());
127+
$this->algoliaConnector->waitLastTask($fixtureSecondStore->getId());
124128

125129
$rebuildCmd = $this->objectManager->get(ReplicaRebuildCommand::class);
126130
$this->invokeMethod(
@@ -131,12 +135,18 @@ public function testReplicaCommands()
131135
$this->createMock(OutputInterface::class)
132136
]
133137
);
134-
$this->algoliaHelper->waitLastTask($defaultStore->getId());
135-
$this->algoliaHelper->waitLastTask($fixtureSecondStore->getId());
138+
$this->algoliaConnector->waitLastTask($defaultStore->getId());
139+
$this->algoliaConnector->waitLastTask($fixtureSecondStore->getId());
136140
// Executing commands - End
137141

138142
$currentDefaultSettings = $this->algoliaHelper->getSettings($defaultIndexName, $defaultStore->getId());
143+
$currentDefaultSettings = $this->algoliaConnector->getSettings(
144+
$this->indexOptionsBuilder->buildEntityIndexOptions($defaultStore->getId())
145+
);
139146
$currentFixtureSettings = $this->algoliaHelper->getSettings($fixtureIndexName, $fixtureSecondStore->getId());
147+
$currentFixtureSettings = $this->algoliaConnector->getSettings(
148+
$this->indexOptionsBuilder->buildEntityIndexOptions($fixtureSecondStore->getId())
149+
);
140150

141151
$this->assertArrayHasKey('replicas', $currentDefaultSettings);
142152
$this->assertArrayHasKey('replicas', $currentFixtureSettings);
@@ -176,7 +186,9 @@ protected function checkReplicaConfigByStore(StoreInterface $store, $sortAttr, $
176186
{
177187
$indexName = $this->indexPrefix . $store->getCode() . '_products';
178188

179-
$settings = $this->algoliaHelper->getSettings($indexName, $store->getId());
189+
$settings = $this->algoliaConnector->getSettings(
190+
$this->indexOptionsBuilder->buildWithEnforcedIndex($indexName, $store->getId())
191+
);
180192

181193
$this->assertArrayHasKey('replicas', $settings);
182194

@@ -216,7 +228,7 @@ protected function addSortingByStore(StoreInterface $store, $attr, $dir, $isVir
216228

217229
$this->assertSortingAttribute($attr, $dir);
218230
$this->indicesConfigurator->saveConfigurationToAlgolia($store->getId());
219-
$this->algoliaHelper->waitLastTask($store->getId());
231+
$this->algoliaConnector->waitLastTask($store->getId());
220232
}
221233

222234
protected function resetAllSortings()

0 commit comments

Comments
 (0)