Skip to content

Commit c0d8114

Browse files
committed
MAGE-1134: fix tests
1 parent 3ae9a4c commit c0d8114

File tree

3 files changed

+53
-44
lines changed

3 files changed

+53
-44
lines changed

Helper/AlgoliaHelper.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,10 +294,11 @@ public function castProductObject(&$productData): void
294294
}
295295

296296
/**
297+
* @param int|null $storeId
297298
* @return int
298299
*/
299-
public function getLastTaskId(): int
300+
public function getLastTaskId(?int $storeId = null): int
300301
{
301-
return $this->algoliaConnector->getLastTaskId();
302+
return $this->algoliaConnector->getLastTaskId($storeId);
302303
}
303304
}

Service/AlgoliaConnector.php

Lines changed: 47 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ class AlgoliaConnector
6060

6161
protected static ?string $lastTaskId;
6262

63+
protected static ?array $lastTaskInfoByStore;
64+
6365
public function __construct(
6466
protected ConfigHelper $config,
6567
protected ManagerInterface $messageManager,
@@ -230,7 +232,7 @@ public function setSettings(
230232

231233
$res = $this->getClient($storeId)->setSettings($indexName, $settings, $forwardToReplicas);
232234

233-
self::setLastOperationInfo($indexName, $res);
235+
self::setLastOperationInfo($indexName, $res, $storeId);
234236
}
235237

236238
/**
@@ -244,7 +246,7 @@ protected function performBatchOperation(string $indexName, array $requests, ?in
244246
{
245247
$response = $this->getClient($storeId)->batch($indexName, [ 'requests' => $requests ] );
246248

247-
self::setLastOperationInfo($indexName, $response);
249+
self::setLastOperationInfo($indexName, $response, $storeId);
248250

249251
return $response;
250252
}
@@ -259,7 +261,7 @@ public function deleteIndex(string $indexName, ?int $storeId = null): void
259261
{
260262
$res = $this->getClient($storeId)->deleteIndex($indexName);
261263

262-
self::setLastOperationInfo($indexName, $res);
264+
self::setLastOperationInfo($indexName, $res, $storeId);
263265
}
264266

265267
/**
@@ -285,7 +287,7 @@ function ($id) {
285287
)
286288
);
287289

288-
$this->performBatchOperation($indexName, $requests);
290+
$this->performBatchOperation($indexName, $requests, $storeId);
289291
}
290292

291293
/**
@@ -304,7 +306,7 @@ public function moveIndex(string $fromIndexName, string $toIndexName, ?int $stor
304306
'destination' => $toIndexName
305307
]
306308
);
307-
self::setLastOperationInfo($toIndexName, $response);
309+
self::setLastOperationInfo($toIndexName, $response, $storeId);
308310
}
309311

310312
/**
@@ -453,12 +455,20 @@ function ($object) use ($action) {
453455
/**
454456
* @param string $indexName
455457
* @param array $response
458+
* @param int|null $storeId
456459
* @return void
457460
*/
458-
protected static function setLastOperationInfo(string $indexName, array $response): void
461+
protected static function setLastOperationInfo(string $indexName, array $response, ?int $storeId = null): void
459462
{
460463
self::$lastUsedIndexName = $indexName;
461-
self::$lastTaskId= $response[self::ALGOLIA_API_TASK_ID] ?? null;
464+
self::$lastTaskId = $response[self::ALGOLIA_API_TASK_ID] ?? null;
465+
466+
if (!is_null($storeId)) {
467+
self::$lastTaskInfoByStore[$storeId] = [
468+
'indexName' => $indexName,
469+
'taskId' => $response[self::ALGOLIA_API_TASK_ID] ?? null
470+
];
471+
}
462472
}
463473

464474
/**
@@ -478,7 +488,7 @@ public function saveRule(array $rule, string $indexName, bool $forwardToReplicas
478488
$forwardToReplicas
479489
);
480490

481-
self::setLastOperationInfo($indexName, $res);
491+
self::setLastOperationInfo($indexName, $res, $storeId);
482492
}
483493

484494
/**
@@ -492,7 +502,7 @@ public function saveRules(string $indexName, array $rules, bool $forwardToReplic
492502
{
493503
$res = $this->getClient($storeId)->saveRules($indexName, $rules, $forwardToReplicas);
494504

495-
self::setLastOperationInfo($indexName, $res);
505+
self::setLastOperationInfo($indexName, $res, $storeId);
496506
}
497507

498508

@@ -512,7 +522,7 @@ public function deleteRule(
512522
{
513523
$res = $this->getClient($storeId)->deleteRule($indexName, $objectID, $forwardToReplicas);
514524

515-
self::setLastOperationInfo($indexName, $res);
525+
self::setLastOperationInfo($indexName, $res, $storeId);
516526
}
517527

518528
/**
@@ -532,7 +542,7 @@ public function copySynonyms(string $fromIndexName, string $toIndexName, ?int $s
532542
'scope' => ['synonyms']
533543
]
534544
);
535-
self::setLastOperationInfo($fromIndexName, $response);
545+
self::setLastOperationInfo($fromIndexName, $response, $storeId);
536546
}
537547

538548
/**
@@ -552,7 +562,7 @@ public function copyQueryRules(string $fromIndexName, string $toIndexName, ?int
552562
'scope' => ['rules']
553563
]
554564
);
555-
self::setLastOperationInfo($fromIndexName, $response);
565+
self::setLastOperationInfo($fromIndexName, $response, $storeId);
556566
}
557567

558568
/**
@@ -578,7 +588,7 @@ public function clearIndex(string $indexName, ?int $storeId = null): void
578588
{
579589
$res = $this->getClient($storeId)->clearObjects($indexName);
580590

581-
self::setLastOperationInfo($indexName, $res);
591+
self::setLastOperationInfo($indexName, $res, $storeId);
582592
}
583593

584594
/**
@@ -589,22 +599,30 @@ public function clearIndex(string $indexName, ?int $storeId = null): void
589599
*/
590600
public function waitLastTask(?int $storeId = null, ?string $lastUsedIndexName = null, ?int $lastTaskId = null): void
591601
{
592-
if (is_null($storeId)) {
593-
$storeId = self::ALGOLIA_DEFAULT_SCOPE;
594-
}
595-
596-
if ($lastUsedIndexName === null && isset(self::$lastUsedIndexName)) {
597-
$lastUsedIndexName = self::$lastUsedIndexName;
602+
if (is_null($lastUsedIndexName)) {
603+
if (!is_null($storeId) && isset(self::$lastTaskInfoByStore[$storeId])) {
604+
$lastUsedIndexName = self::$lastTaskInfoByStore[$storeId]['indexName'];
605+
} elseif (isset(self::$lastUsedIndexName)){
606+
$lastUsedIndexName = self::$lastUsedIndexName;
607+
}
598608
}
599609

600-
if ($lastTaskId === null && isset(self::$lastTaskId)) {
601-
$lastTaskId = self::$lastTaskId;
610+
if (is_null($lastTaskId)) {
611+
if (!is_null($storeId) && isset(self::$lastTaskInfoByStore[$storeId])) {
612+
$lastTaskId = self::$lastTaskInfoByStore[$storeId]['taskId'];
613+
} elseif (isset(self::$lastTaskId)){
614+
$lastTaskId = self::$lastTaskId;
615+
}
602616
}
603617

604618
if (!$lastUsedIndexName || !$lastTaskId) {
605619
return;
606620
}
607621

622+
if (is_null($storeId)) {
623+
$storeId = self::ALGOLIA_DEFAULT_SCOPE;
624+
}
625+
608626
$this->getClient($storeId)->waitForTask($lastUsedIndexName, $lastTaskId);
609627
}
610628

@@ -827,31 +845,19 @@ protected function castAttribute($value): mixed
827845

828846
/**
829847
* @param int|null $storeId
830-
* @return string
831-
*/
832-
public function getLastIndexName(?int $storeId = null): string
833-
{
834-
if (is_null($storeId)) {
835-
$storeId = self::ALGOLIA_DEFAULT_SCOPE;
836-
}
837-
838-
if (!isset(self::$lastUsedIndexNames[$storeId])) {
839-
return '';
840-
}
841-
842-
return self::$lastUsedIndexNames[$storeId];
843-
}
844-
845-
/**
846848
* @return int|null
847849
*/
848-
public function getLastTaskId(): int|null
850+
public function getLastTaskId(?int $storeId = null): int|null
849851
{
850-
if (!isset(self::$lastUsedIndexName)) {
851-
return null;
852+
$lastTaskId = null;
853+
854+
if (!is_null($storeId) && isset(self::$lastTaskInfoByStore[$storeId])) {
855+
$lastTaskId = self::$lastTaskInfoByStore[$storeId]['taskId'];
856+
} elseif (isset(self::$lastTaskId)){
857+
$lastTaskId = self::$lastTaskId;
852858
}
853859

854-
return self::$lastTaskId;
860+
return $lastTaskId;
855861
}
856862

857863
/**

Test/Integration/Page/MultiStorePagesTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,9 @@ public function testMultiStorePageIndices()
8080
$this->pageRepository->save($homePage);
8181

8282
$this->pagesIndexer->execute([self::HOME_PAGE_ID]);
83-
$this->algoliaHelper->waitLastTask();
83+
84+
$this->algoliaHelper->waitLastTask($defaultStore->getId());
85+
$this->algoliaHelper->waitLastTask($fixtureSecondStore->getId());
8486

8587
$this->assertNbOfRecordsPerStore(
8688
$defaultStore->getCode(),

0 commit comments

Comments
 (0)