Skip to content

Commit 4e95782

Browse files
committed
MAGE-1084: add replica commands test
1 parent 9c92395 commit 4e95782

File tree

3 files changed

+131
-33
lines changed

3 files changed

+131
-33
lines changed

Test/Integration/Product/MultiStoreReplicaTest.php

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,16 @@
33
namespace Algolia\AlgoliaSearch\Test\Integration\Product;
44

55
use Algolia\AlgoliaSearch\Api\Product\ReplicaManagerInterface;
6+
use Algolia\AlgoliaSearch\Console\Command\ReplicaRebuildCommand;
7+
use Algolia\AlgoliaSearch\Console\Command\ReplicaSyncCommand;
68
use Algolia\AlgoliaSearch\Helper\ConfigHelper;
9+
use Algolia\AlgoliaSearch\Service\Product\SortingTransformer;
710
use Algolia\AlgoliaSearch\Test\Integration\MultiStoreTestCase;
11+
use Algolia\AlgoliaSearch\Test\Integration\Product\Traits\ReplicaAssertionsTrait;
812
use Magento\Framework\Serialize\SerializerInterface;
913
use Magento\Store\Api\Data\StoreInterface;
14+
use Symfony\Component\Console\Input\InputInterface;
15+
use Symfony\Component\Console\Output\OutputInterface;
1016

1117
/**
1218
* @magentoDataFixture Magento/Store/_files/second_website_with_two_stores.php
@@ -53,6 +59,80 @@ public function testMultiStoreReplicaConfig()
5359
// Check replica config for color asc
5460
$this->checkReplicaIsStandard($defaultStore, self::COLOR_ATTR, self::ASC_DIR);
5561
$this->checkReplicaIsVirtual($fixtureSecondStore, self::COLOR_ATTR, self::ASC_DIR);
62+
63+
$this->resetAllSortings();
64+
}
65+
66+
public function testReplicaCommands()
67+
{
68+
$defaultStore = $this->storeRepository->get('default');
69+
$fixtureSecondStore = $this->storeRepository->get('fixture_second_store');
70+
71+
$defaultIndexName = $this->indexPrefix . $defaultStore->getCode() . '_products';
72+
$fixtureIndexName = $this->indexPrefix . $fixtureSecondStore->getCode() . '_products';
73+
74+
// Update store config for fixture only
75+
$this->mockSortUpdate('price', 'desc', ['virtualReplica' => 1], $fixtureSecondStore);
76+
77+
$defaultSortings = $this->objectManager->get(SortingTransformer::class)->getSortingIndices(
78+
$defaultStore->getId(),
79+
null,
80+
null,
81+
true
82+
);
83+
84+
$fixtureSortings = $this->objectManager->get(SortingTransformer::class)->getSortingIndices(
85+
$fixtureSecondStore->getId(),
86+
null,
87+
null,
88+
true
89+
);
90+
91+
// Executing commands - Start
92+
$syncCmd = $this->objectManager->get(ReplicaSyncCommand::class);
93+
$this->mockProperty($syncCmd, 'output', OutputInterface::class);
94+
$syncCmd->syncReplicas();
95+
$this->algoliaHelper->waitLastTask($defaultStore->getId());
96+
$this->algoliaHelper->waitLastTask($fixtureSecondStore->getId());
97+
98+
$rebuildCmd = $this->objectManager->get(ReplicaRebuildCommand::class);
99+
$this->invokeMethod(
100+
$rebuildCmd,
101+
'execute',
102+
[
103+
$this->createMock(InputInterface::class),
104+
$this->createMock(OutputInterface::class)
105+
]
106+
);
107+
$this->algoliaHelper->waitLastTask($defaultStore->getId());
108+
$this->algoliaHelper->waitLastTask($fixtureSecondStore->getId());
109+
// Executing commands - End
110+
111+
$currentDefaultSettings = $this->algoliaHelper->getSettings($defaultIndexName, $defaultStore->getId());
112+
$currentFixtureSettings = $this->algoliaHelper->getSettings($fixtureIndexName, $fixtureSecondStore->getId());
113+
114+
$this->assertArrayHasKey('replicas', $currentDefaultSettings);
115+
$this->assertArrayHasKey('replicas', $currentFixtureSettings);
116+
117+
$defaultReplicas = $currentDefaultSettings['replicas'];
118+
$fixtureReplicas = $currentFixtureSettings['replicas'];
119+
120+
$this->assertEquals(count($defaultSortings), count($defaultReplicas));
121+
$this->assertEquals(count($fixtureSortings), count($fixtureReplicas));
122+
123+
$this->assertSortToReplicaConfigParity(
124+
$defaultIndexName,
125+
$defaultSortings,
126+
$defaultReplicas,
127+
$defaultStore->getId()
128+
);
129+
130+
$this->assertSortToReplicaConfigParity(
131+
$fixtureIndexName,
132+
$fixtureSortings,
133+
$fixtureReplicas,
134+
$fixtureSecondStore->getId()
135+
);
56136
}
57137

58138
protected function checkReplicaIsStandard(StoreInterface $store, $sortAttr, $sortDir)
@@ -112,7 +192,7 @@ protected function addSortingByStore(StoreInterface $store, $attr, $dir, $isVir
112192
$this->algoliaHelper->waitLastTask($store->getId());
113193
}
114194

115-
protected function tearDown(): void
195+
protected function resetAllSortings()
116196
{
117197
$stores = $this->storeManager->getStores();
118198

@@ -139,6 +219,11 @@ protected function tearDown(): void
139219
$store->getCode()
140220
);
141221
}
222+
}
223+
224+
protected function tearDown(): void
225+
{
226+
$this->resetAllSortings();
142227

143228
parent::tearDown();
144229
}

Test/Integration/Product/ReplicaIndexingTest.php

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Algolia\AlgoliaSearch\Helper\ConfigHelper;
99
use Algolia\AlgoliaSearch\Helper\Entity\ProductHelper;
1010
use Algolia\AlgoliaSearch\Model\IndicesConfigurator;
11+
use Algolia\AlgoliaSearch\Test\Integration\Product\Traits\ReplicaAssertionsTrait;
1112
use Algolia\AlgoliaSearch\Test\Integration\TestCase;
1213

1314
class ReplicaIndexingTest extends TestCase
@@ -118,35 +119,6 @@ public function testVirtualReplicaConfig(): void
118119
$this->setConfig(ConfigHelper::IS_INSTANT_ENABLED, 0);
119120
}
120121

121-
/**
122-
* ConfigHelper::setSorting uses WriterInterface which does not update unless DB isolation is disabled
123-
* This provides a workaround to test using MutableScopeConfigInterface with DB isolation enabled
124-
*/
125-
protected function mockSortUpdate(string $sortAttr, string $sortDir, array $attr): void
126-
{
127-
$sorting = $this->configHelper->getSorting();
128-
$existing = array_filter($sorting, function ($item) use ($sortAttr, $sortDir) {
129-
return $item['attribute'] === $sortAttr && $item['sort'] === $sortDir;
130-
});
131-
132-
133-
if ($existing) {
134-
$idx = array_key_first($existing);
135-
$sorting[$idx] = array_merge($existing[$idx], $attr);
136-
}
137-
else {
138-
$sorting[] = array_merge(
139-
[
140-
'attribute' => $sortAttr,
141-
'sort' => $sortDir,
142-
'sortLabel' => $sortAttr
143-
],
144-
$attr
145-
);
146-
}
147-
$this->setConfig(ConfigHelper::SORTING_INDICES, json_encode($sorting));
148-
}
149-
150122
/**
151123
* @depends testReplicaSync
152124
* @magentoConfigFixture current_store algoliasearch_instant/instant/is_instant_enabled 1

Test/Integration/Product/ReplicaAssertionsTrait.php renamed to Test/Integration/Product/Traits/ReplicaAssertionsTrait.php

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
<?php
22

3-
namespace Algolia\AlgoliaSearch\Test\Integration\Product;
3+
namespace Algolia\AlgoliaSearch\Test\Integration\Product\Traits;
4+
5+
use Algolia\AlgoliaSearch\Helper\ConfigHelper;
6+
use Magento\Store\Api\Data\StoreInterface;
47

58
trait ReplicaAssertionsTrait
69
{
7-
protected function assertSortToReplicaConfigParity(string $primaryIndexName, array $sorting, array $replicas): void
10+
protected function assertSortToReplicaConfigParity(
11+
string $primaryIndexName,
12+
array $sorting,
13+
array $replicas,
14+
?int $storeId = null
15+
): void
816
{
917
foreach ($sorting as $sortAttr) {
1018
$replicaIndexName = $sortAttr['name'];
@@ -14,7 +22,7 @@ protected function assertSortToReplicaConfigParity(string $primaryIndexName, arr
1422
: $replicaIndexName;
1523
$this->assertContains($needle, $replicas);
1624

17-
$replicaSettings = $this->assertReplicaIndexExists($primaryIndexName, $replicaIndexName);
25+
$replicaSettings = $this->assertReplicaIndexExists($primaryIndexName, $replicaIndexName, $storeId);
1826
$sort = reset($sortAttr['ranking']);
1927
if ($isVirtual) {
2028
$this->assertVirtualReplicaRanking($replicaSettings, $sort);
@@ -110,4 +118,37 @@ protected function assertNoSortingAttribute($sortAttr, $sortDir): void
110118
{
111119
$this->assertFalse($this->hasSortingAttribute($sortAttr, $sortDir));
112120
}
121+
122+
/**
123+
* ConfigHelper::setSorting uses WriterInterface which does not update unless DB isolation is disabled
124+
* This provides a workaround to test using MutableScopeConfigInterface with DB isolation enabled
125+
*/
126+
protected function mockSortUpdate(string $sortAttr, string $sortDir, array $attr, ?StoreInterface $store = null): void
127+
{
128+
$sorting = $this->configHelper->getSorting(!is_null($store) ? $store->getId() : null);
129+
$existing = array_filter($sorting, function ($item) use ($sortAttr, $sortDir) {
130+
return $item['attribute'] === $sortAttr && $item['sort'] === $sortDir;
131+
});
132+
133+
134+
if ($existing) {
135+
$idx = array_key_first($existing);
136+
$sorting[$idx] = array_merge($existing[$idx], $attr);
137+
}
138+
else {
139+
$sorting[] = array_merge(
140+
[
141+
'attribute' => $sortAttr,
142+
'sort' => $sortDir,
143+
'sortLabel' => $sortAttr
144+
],
145+
$attr
146+
);
147+
}
148+
$this->setConfig(
149+
ConfigHelper::SORTING_INDICES,
150+
json_encode($sorting),
151+
!is_null($store) ? $store->getCode() : 'default'
152+
);
153+
}
113154
}

0 commit comments

Comments
 (0)