Skip to content

Commit 6b08374

Browse files
committed
MAGE-1044 Test replica rebuild command
1 parent d3d00d2 commit 6b08374

File tree

2 files changed

+41
-5
lines changed

2 files changed

+41
-5
lines changed

Test/Integration/Product/ReplicaIndexingTest.php

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

55
use Algolia\AlgoliaSearch\Api\Product\ReplicaManagerInterface;
6+
use Algolia\AlgoliaSearch\Exceptions\AlgoliaException;
7+
use Algolia\AlgoliaSearch\Exceptions\ExceededRetriesException;
68
use Algolia\AlgoliaSearch\Helper\ConfigHelper;
79
use Algolia\AlgoliaSearch\Helper\Entity\ProductHelper;
810
use Algolia\AlgoliaSearch\Model\Indexer\Product as ProductIndexer;
@@ -158,17 +160,43 @@ protected function mockSortUpdate(string $sortAttr, string $sortDir, array $attr
158160
/**
159161
* @depends testReplicaSync
160162
* @magentoConfigFixture current_store algoliasearch_instant/instant/is_instant_enabled 1
163+
* @throws AlgoliaException
164+
* @throws ExceededRetriesException
165+
* @throws \ReflectionException
161166
*/
162167
public function testReplicaRebuild(): void
163168
{
164169
$indexName = $this->getIndexName('default_');
165170

166-
$cmd = $this->objectManager->get(\Algolia\AlgoliaSearch\Console\Command\ReplicaRebuildCommand::class);
167-
$this->assertTrue(true);
171+
$this->mockSortUpdate('price', 'desc', ['virtualReplica' => 1]);
172+
$sorting = $this->objectManager->get(\Algolia\AlgoliaSearch\Service\Product\SortingTransformer::class)->getSortingIndices(1, null, null, true);
173+
174+
$syncCmd = $this->objectManager->get(\Algolia\AlgoliaSearch\Console\Command\ReplicaSyncCommand::class);
175+
$this->mockProperty($syncCmd, 'output', \Symfony\Component\Console\Output\OutputInterface::class);
176+
$syncCmd->syncReplicas();
177+
$this->algoliaHelper->waitLastTask();
178+
179+
$rebuildCmd = $this->objectManager->get(\Algolia\AlgoliaSearch\Console\Command\ReplicaRebuildCommand::class);
180+
$this->callReflectedMethod(
181+
$rebuildCmd,
182+
'execute',
183+
$this->createMock(\Symfony\Component\Console\Input\InputInterface::class),
184+
$this->createMock(\Symfony\Component\Console\Output\OutputInterface::class)
185+
);
186+
$this->algoliaHelper->waitLastTask();
187+
188+
$currentSettings = $this->algoliaHelper->getSettings($indexName);
189+
$this->assertArrayHasKey('replicas', $currentSettings);
190+
$replicas = $currentSettings['replicas'];
191+
192+
$this->assertEquals(count($sorting), count($replicas));
193+
$this->assertSortToReplicaConfigParity($indexName, $sorting, $replicas);
168194
}
169195

170196
/**
171197
* @magentoConfigFixture current_store algoliasearch_instant/instant/is_instant_enabled 1
198+
* @throws AlgoliaException
199+
* @throws ExceededRetriesException
172200
* @throws \ReflectionException
173201
*/
174202
public function testReplicaSync(): void
@@ -179,7 +207,7 @@ public function testReplicaSync(): void
179207

180208
$sorting = $this->objectManager->get(\Algolia\AlgoliaSearch\Service\Product\SortingTransformer::class)->getSortingIndices(1, null, null, true);
181209

182-
$cmd = $this->objectManager->create(\Algolia\AlgoliaSearch\Console\Command\ReplicaSyncCommand::class);
210+
$cmd = $this->objectManager->get(\Algolia\AlgoliaSearch\Console\Command\ReplicaSyncCommand::class);
183211

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

Test/Integration/TestCase.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,15 +151,23 @@ protected function setConfigFromArray(array $settings): void
151151
/**
152152
* @throws \ReflectionException
153153
*/
154-
protected function mockProperty($object, $propertyName, $propertyClass): void
154+
protected function mockProperty(object $object, string $propertyName, string $propertyClass): void
155155
{
156156
$mock = $this->createMock($propertyClass);
157157
$reflection = new \ReflectionClass($object);
158158
$property = $reflection->getProperty($propertyName);
159-
$property->setAccessible(true);
160159
$property->setValue($object, $mock);
161160
}
162161

162+
/**
163+
* @throws \ReflectionException
164+
*/
165+
protected function callReflectedMethod(object $object, string $method, mixed ...$args): void
166+
{
167+
$reflection = new \ReflectionClass($object);
168+
$reflection->getMethod($method)->invoke($object, ...$args);
169+
}
170+
163171
protected function clearIndices()
164172
{
165173
$indices = $this->algoliaHelper->listIndexes();

0 commit comments

Comments
 (0)