Skip to content

Commit 531e867

Browse files
authored
MAGE-1060: POC for multi-store indexing testing (#1622)
* MAGE-1060: POC for multi-store bootstrap * MAGE-1056: adding a asbtract class for multiple stores scenario + starting of the category class * MAGE-1056: cleaning
1 parent e078704 commit 531e867

File tree

4 files changed

+161
-3
lines changed

4 files changed

+161
-3
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Algolia\AlgoliaSearch\Test\Integration;
4+
5+
use Algolia\AlgoliaSearch\Model\Indexer\Category;
6+
7+
/**
8+
* @magentoDataFixture Magento/Store/_files/second_website_with_two_stores.php
9+
* @magentoDbIsolation disabled
10+
* @magentoAppIsolation enabled
11+
*/
12+
class MultiStoreCategoriesTest extends MultiStoreTestCase
13+
{
14+
/** @var Category */
15+
protected $categoriesIndexer;
16+
17+
public function setUp():void
18+
{
19+
parent::setUp();
20+
21+
/** @var Category $categoriesIndexer */
22+
$this->categoriesIndexer = $this->getObjectManager()->create(Category::class);
23+
24+
$this->categoriesIndexer->executeFull();
25+
$this->algoliaHelper->waitLastTask();
26+
}
27+
28+
public function testMultiStoreCategoryIndices()
29+
{
30+
foreach ($this->storeManager->getStores() as $store) {
31+
$this->assertNbOfRecordsPerStore($store->getCode(), 'categories', $this->assertValues->expectedCategory);
32+
}
33+
}
34+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace Algolia\AlgoliaSearch\Test\Integration;
4+
5+
/**
6+
* @magentoDataFixture Magento/Store/_files/second_website_with_two_stores.php
7+
*/
8+
class MultiStoreConfigTest extends MultiStoreTestCase
9+
{
10+
public function testMultiStoreIndicesCreation()
11+
{
12+
$websites = $this->storeManager->getWebsites();
13+
$stores = $this->storeManager->getStores();
14+
15+
// Check that stores and websites are properly created
16+
$this->assertEquals(count($websites), 2);
17+
$this->assertEquals(count($stores), 3);
18+
19+
foreach ($stores as $store) {
20+
$this->setupStore($store);
21+
}
22+
23+
$indicesCreatedByTest = 0;
24+
$indices = $this->algoliaHelper->listIndexes();
25+
26+
foreach ($indices['items'] as $index) {
27+
$name = $index['name'];
28+
29+
if (mb_strpos($name, $this->indexPrefix) === 0) {
30+
$indicesCreatedByTest++;
31+
}
32+
}
33+
34+
// Check that the configuration created the appropriate number of indices (4 per store => 3*4=12)
35+
$this->assertEquals($indicesCreatedByTest, 12);
36+
}
37+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
namespace Algolia\AlgoliaSearch\Test\Integration;
4+
5+
use Algolia\AlgoliaSearch\Exceptions\AlgoliaException;
6+
use Algolia\AlgoliaSearch\Model\IndicesConfigurator;
7+
use Magento\Framework\Exception\LocalizedException;
8+
use Magento\Framework\Exception\NoSuchEntityException;
9+
use Magento\Store\Api\Data\StoreInterface;
10+
use Magento\Store\Model\StoreManager;
11+
12+
abstract class MultiStoreTestCase extends IndexingTestCase
13+
{
14+
/** @var StoreManager */
15+
protected $storeManager;
16+
17+
/** @var IndicesConfigurator */
18+
protected $indicesConfigurator;
19+
20+
public function setUp():void
21+
{
22+
/** @var StoreManager $storeManager */
23+
$this->storeManager = $this->getObjectManager()->create(StoreManager::class);
24+
25+
/** @var IndicesConfigurator $indicesConfigurator */
26+
$this->indicesConfigurator = $this->getObjectManager()->create(IndicesConfigurator::class);
27+
28+
parent::setUp();
29+
30+
foreach ($this->storeManager->getStores() as $store) {
31+
$this->setupStore($store);
32+
}
33+
}
34+
35+
/**
36+
* @param string $storeCode
37+
* @param string $entity
38+
* @param int $expectedNumber
39+
*
40+
* @return void
41+
* @throws AlgoliaException
42+
*/
43+
protected function assertNbOfRecordsPerStore(string $storeCode, string $entity, int $expectedNumber): void
44+
{
45+
$resultsDefault = $this->algoliaHelper->query($this->indexPrefix . $storeCode . '_' . $entity, '', []);
46+
47+
$this->assertEquals($expectedNumber, $resultsDefault['results'][0]['nbHits']);
48+
}
49+
50+
/**
51+
* @param StoreInterface $store
52+
*
53+
* @return void
54+
* @throws AlgoliaException
55+
* @throws LocalizedException
56+
* @throws NoSuchEntityException
57+
*/
58+
protected function setupStore(StoreInterface $store): void
59+
{
60+
$this->setConfig(
61+
'algoliasearch_credentials/credentials/application_id',
62+
getenv('ALGOLIA_APPLICATION_ID'),
63+
$store->getCode()
64+
);
65+
$this->setConfig(
66+
'algoliasearch_credentials/credentials/search_only_api_key',
67+
getenv('ALGOLIA_SEARCH_KEY_1') ?: getenv('ALGOLIA_SEARCH_API_KEY'),
68+
$store->getCode()
69+
);
70+
$this->setConfig(
71+
'algoliasearch_credentials/credentials/api_key',
72+
getenv('ALGOLIA_API_KEY'),
73+
$store->getCode()
74+
);
75+
$this->setConfig(
76+
'algoliasearch_credentials/credentials/index_prefix',
77+
$this->indexPrefix,
78+
$store->getCode()
79+
);
80+
81+
$this->indicesConfigurator->saveConfigurationToAlgolia($store->getId());
82+
}
83+
84+
}

Test/Integration/TestCase.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,16 @@ protected function resetConfigs($configs = [])
5858
}
5959
}
6060

61-
protected function setConfig($path, $value)
62-
{
61+
protected function setConfig(
62+
$path,
63+
$value,
64+
$scopeCode = 'default'
65+
) {
6366
$this->getObjectManager()->get(\Magento\Framework\App\Config\MutableScopeConfigInterface::class)->setValue(
6467
$path,
6568
$value,
6669
ScopeInterface::SCOPE_STORE,
67-
'default'
70+
$scopeCode
6871
);
6972
}
7073

0 commit comments

Comments
 (0)