Skip to content

Commit 0ddd8ea

Browse files
authored
Merge pull request #1721 from algolia/feat/MAGE-1226-update-integration-tests
MAGE-1226: Update indexing integration tests
2 parents a521fcc + 5844eab commit 0ddd8ea

13 files changed

+189
-157
lines changed

Test/Integration/Indexing/Category/CategoriesIndexingTest.php

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,20 @@
22

33
namespace Algolia\AlgoliaSearch\Test\Integration\Indexing\Category;
44

5-
use Algolia\AlgoliaSearch\Model\Indexer\Category;
5+
use Algolia\AlgoliaSearch\Console\Command\Indexer\IndexCategoriesCommand;
6+
use Algolia\AlgoliaSearch\Service\Category\BatchQueueProcessor as CategoryBatchQueueProcessor;
67
use Algolia\AlgoliaSearch\Test\Integration\Indexing\IndexingTestCase;
78

89
class CategoriesIndexingTest extends IndexingTestCase
910
{
1011
public function testCategories()
1112
{
12-
/** @var Category $categoriesIndexer */
13-
$categoriesIndexer = $this->getObjectManager()->create(Category::class);
14-
$this->processTest($categoriesIndexer, 'categories', $this->assertValues->expectedCategory);
13+
$categoryBatchQueueProcessor = $this->objectManager->get(CategoryBatchQueueProcessor::class);
14+
$this->processTest(
15+
$categoryBatchQueueProcessor,
16+
'categories',
17+
$this->assertValues->expectedCategory
18+
);
1519
}
1620

1721
public function testDefaultIndexableAttributes()
@@ -21,10 +25,8 @@ public function testDefaultIndexableAttributes()
2125
$this->getSerializer()->serialize([])
2226
);
2327

24-
/** @var Category $categoriesIndexer */
25-
$categoriesIndexer = $this->getObjectManager()->create(Category::class);
26-
$categoriesIndexer->executeRow(3);
27-
28+
$categoryBatchQueueProcessor = $this->objectManager->get(CategoryBatchQueueProcessor::class);
29+
$categoryBatchQueueProcessor->processBatch(1, [3]);
2830
$this->algoliaHelper->waitLastTask();
2931

3032
$results = $this->algoliaHelper->getObjects($this->indexPrefix . 'default_categories', ['3']);
@@ -51,4 +53,10 @@ public function testDefaultIndexableAttributes()
5153
$extraAttributes = implode(', ', array_keys($hit));
5254
$this->assertTrue(empty($hit), 'Extra category attributes (' . $extraAttributes . ') are indexed and should not be.');
5355
}
56+
57+
public function testIndexingCategoriesCommand()
58+
{
59+
$indexCategoriesCmd = $this->objectManager->get(IndexCategoriesCommand::class);
60+
$this->processCommandTest($indexCategoriesCmd,'categories', $this->assertValues->expectedCategory);
61+
}
5462
}

Test/Integration/Indexing/Category/MultiStoreCategoriesTest.php

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use Algolia\AlgoliaSearch\Exceptions\AlgoliaException;
66
use Algolia\AlgoliaSearch\Exceptions\ExceededRetriesException;
7-
use Algolia\AlgoliaSearch\Model\Indexer\Category;
7+
use Algolia\AlgoliaSearch\Service\Category\BatchQueueProcessor as CategoryBatchQueueProcessor;
88
use Algolia\AlgoliaSearch\Test\Integration\Indexing\MultiStoreTestCase;
99
use Magento\Catalog\Api\CategoryRepositoryInterface;
1010
use Magento\Catalog\Api\Data\CategoryInterface;
@@ -19,15 +19,15 @@
1919
*/
2020
class MultiStoreCategoriesTest extends MultiStoreTestCase
2121
{
22-
/** @var Category */
23-
protected $categoriesIndexer;
24-
2522
/** @var CategoryRepositoryInterface */
2623
protected $categoryRepository;
2724

2825
/** @var CollectionFactory */
2926
private $categoryCollectionFactory;
3027

28+
/** @var CategoryBatchQueueProcessor */
29+
protected $categoryBatchQueueProcessor;
30+
3131
const BAGS_CATEGORY_ID = 4;
3232
const BAGS_CATEGORY_NAME = "Bags";
3333
const BAGS_CATEGORY_NAME_ALT = "Bags Alt";
@@ -36,13 +36,11 @@ protected function setUp():void
3636
{
3737
parent::setUp();
3838

39-
$this->categoriesIndexer = $this->objectManager->get(Category::class);
4039
$this->categoryRepository = $this->objectManager->get(CategoryRepositoryInterface::class);
4140
$this->categoryCollectionFactory = $this->objectManager->get(CollectionFactory::class);
4241

43-
44-
$this->categoriesIndexer->executeFull();
45-
$this->algoliaHelper->waitLastTask();
42+
$this->categoryBatchQueueProcessor = $this->objectManager->get(CategoryBatchQueueProcessor::class);
43+
$this->reindexToAllStores($this->categoryBatchQueueProcessor);
4644
}
4745

4846
/**
@@ -80,10 +78,7 @@ public function testMultiStoreCategoryIndices()
8078
$this->assertEquals(self::BAGS_CATEGORY_NAME, $bagsCategory->getName());
8179
$this->assertEquals(self::BAGS_CATEGORY_NAME_ALT, $bagsCategoryAlt->getName());
8280

83-
$this->categoriesIndexer->execute([self::BAGS_CATEGORY_ID]);
84-
85-
$this->algoliaHelper->waitLastTask($defaultStore->getId());
86-
$this->algoliaHelper->waitLastTask($fixtureSecondStore->getId());
81+
$this->reindexToAllStores($this->categoryBatchQueueProcessor, [self::BAGS_CATEGORY_ID]);
8782

8883
$this->assertAlgoliaRecordValues(
8984
$this->indexPrefix . 'default_categories',
@@ -106,10 +101,7 @@ public function testMultiStoreCategoryIndices()
106101
['is_active' => 0]
107102
);
108103

109-
$this->categoriesIndexer->execute([self::BAGS_CATEGORY_ID]);
110-
111-
$this->algoliaHelper->waitLastTask($defaultStore->getId());
112-
$this->algoliaHelper->waitLastTask($fixtureSecondStore->getId());
104+
$this->reindexToAllStores($this->categoryBatchQueueProcessor, [self::BAGS_CATEGORY_ID]);
113105

114106
$this->assertNbOfRecordsPerStore(
115107
$defaultStore->getCode(),

Test/Integration/Indexing/Config/ConfigTest.php

Lines changed: 1 addition & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -58,64 +58,7 @@ public function testQueryRules()
5858

5959
public function testAutomaticalSetOfCategoriesFacet()
6060
{
61-
/** @var IndicesConfigurator $indicesConfigurator */
62-
$indicesConfigurator = $this->getObjectManager()->create(IndicesConfigurator::class);
63-
64-
// Remove categories from facets
65-
$facets = $this->configHelper->getFacets();
66-
foreach ($facets as $key => $facet) {
67-
if ($facet['attribute'] === 'categories') {
68-
unset($facets[$key]);
69-
70-
break;
71-
}
72-
}
73-
74-
$this->setConfig('algoliasearch_instant/instant_facets/facets', $this->getSerializer()->serialize($facets));
75-
76-
// Set don't replace category pages with Algolia - categories attribute shouldn't be included in facets
77-
$this->setConfig('algoliasearch_instant/instant/replace_categories', '0');
78-
79-
$indicesConfigurator->saveConfigurationToAlgolia(1);
80-
81-
$this->algoliaHelper->waitLastTask();
82-
83-
$indexSettings = $this->algoliaHelper->getSettings($this->indexPrefix . 'default_products');
84-
85-
$this->assertEquals($this->assertValues->automaticalSetOfCategoryAttributesForFaceting, count($indexSettings['attributesForFaceting']));
86-
87-
$categoriesAttributeIsIncluded = false;
88-
foreach ($indexSettings['attributesForFaceting'] as $attribute) {
89-
if ($attribute === 'categories') {
90-
$categoriesAttributeIsIncluded = true;
91-
92-
break;
93-
}
94-
}
95-
96-
$this->assertFalse($categoriesAttributeIsIncluded, 'Categories attribute should not be included in facets, but it is');
97-
98-
// Set replace category pages with Algolia - categories attribute should be included in facets
99-
$this->setConfig('algoliasearch_instant/instant/replace_categories', '1');
100-
101-
$indicesConfigurator->saveConfigurationToAlgolia(1);
102-
103-
$this->algoliaHelper->waitLastTask();
104-
105-
$indexSettings = $this->algoliaHelper->getSettings($this->indexPrefix . 'default_products');
106-
107-
$this->assertEquals($this->assertValues->automaticalSetOfCategoryAttributesForFaceting + 1, count($indexSettings['attributesForFaceting']));
108-
109-
$categoriesAttributeIsIncluded = false;
110-
foreach ($indexSettings['attributesForFaceting'] as $attribute) {
111-
if ($attribute === 'categories') {
112-
$categoriesAttributeIsIncluded = true;
113-
114-
break;
115-
}
116-
}
117-
118-
$this->assertTrue($categoriesAttributeIsIncluded, 'Categories attribute should be included in facets, but it is not');
61+
// Removed test, the addition/deletion of the "categories" attribute is now checked by the FacetBuilder unit test
11962
}
12063

12164
public function testRetrievableAttributes()

Test/Integration/Indexing/IndexingTestCase.php

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22

33
namespace Algolia\AlgoliaSearch\Test\Integration\Indexing;
44

5+
use Algolia\AlgoliaSearch\Api\Processor\BatchQueueProcessorInterface;
6+
use Algolia\AlgoliaSearch\Console\Command\Indexer\AbstractIndexerCommand;
57
use Algolia\AlgoliaSearch\Exceptions\AlgoliaException;
68
use Algolia\AlgoliaSearch\Test\Integration\TestCase;
7-
use Magento\Framework\Indexer\ActionInterface;
9+
use Symfony\Component\Console\Input\InputInterface;
10+
use Symfony\Component\Console\Output\OutputInterface;
811

912
abstract class IndexingTestCase extends TestCase
1013
{
@@ -15,16 +18,43 @@ protected function setUp(): void
1518
$this->setConfig('algoliasearch_queue/queue/active', '0');
1619
}
1720

18-
protected function processTest(ActionInterface $indexer, $indexSuffix, $expectedNbHits)
19-
{
21+
protected function processTest(
22+
BatchQueueProcessorInterface $batchQueueProcessor,
23+
$indexSuffix,
24+
$expectedNbHits
25+
) {
2026
$this->algoliaHelper->clearIndex($this->indexPrefix . 'default_' . $indexSuffix);
2127

22-
$indexer->executeFull();
28+
$batchQueueProcessor->processBatch(1);
29+
$this->algoliaHelper->waitLastTask();
30+
31+
$this->assertNumberofHits($indexSuffix, $expectedNbHits);
32+
}
33+
34+
protected function processCommandTest(
35+
AbstractIndexerCommand $command,
36+
$indexSuffix,
37+
$expectedNbHits
38+
) {
39+
$this->algoliaHelper->clearIndex($this->indexPrefix . 'default_' . $indexSuffix);
2340

41+
$this->mockProperty($command, 'output', OutputInterface::class);
42+
$this->invokeMethod(
43+
$command,
44+
'execute',
45+
[
46+
$this->createMock(InputInterface::class),
47+
$this->createMock(OutputInterface::class)
48+
]
49+
);
2450
$this->algoliaHelper->waitLastTask();
2551

26-
$resultsDefault = $this->algoliaHelper->query($this->indexPrefix . 'default_' . $indexSuffix, '', []);
52+
$this->assertNumberofHits($indexSuffix, $expectedNbHits);
53+
}
2754

55+
protected function assertNumberofHits($indexSuffix, $expectedNbHits)
56+
{
57+
$resultsDefault = $this->algoliaHelper->query($this->indexPrefix . 'default_' . $indexSuffix, '', []);
2858
$this->assertEquals($expectedNbHits, $resultsDefault['results'][0]['nbHits']);
2959
}
3060

Test/Integration/Indexing/MultiStoreTestCase.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Algolia\AlgoliaSearch\Test\Integration\Indexing;
44

5+
use Algolia\AlgoliaSearch\Api\Processor\BatchQueueProcessorInterface;
56
use Algolia\AlgoliaSearch\Exceptions\AlgoliaException;
67
use Algolia\AlgoliaSearch\Exceptions\ExceededRetriesException;
78
use Algolia\AlgoliaSearch\Helper\ConfigHelper;
@@ -41,6 +42,17 @@ protected function setUp(): void
4142
}
4243
}
4344

45+
protected function reindexToAllStores(
46+
BatchQueueProcessorInterface $batchQueueProcessor,
47+
array $categoryIds = null
48+
): void
49+
{
50+
foreach (array_keys($this->storeManager->getStores()) as $storeId) {
51+
$batchQueueProcessor->processBatch($storeId, $categoryIds);
52+
$this->algoliaHelper->waitLastTask($storeId);
53+
}
54+
}
55+
4456
/**
4557
* @param string $storeCode
4658
* @param string $entity

Test/Integration/Indexing/Page/MultiStorePagesTest.php

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use Algolia\AlgoliaSearch\Exceptions\AlgoliaException;
66
use Algolia\AlgoliaSearch\Exceptions\ExceededRetriesException;
77
use Algolia\AlgoliaSearch\Helper\ConfigHelper;
8-
use Algolia\AlgoliaSearch\Model\Indexer\Page;
8+
use Algolia\AlgoliaSearch\Service\Page\BatchQueueProcessor as PageBatchQueueProcessor;
99
use Algolia\AlgoliaSearch\Test\Integration\Indexing\MultiStoreTestCase;
1010
use Magento\Cms\Api\Data\PageInterface;
1111
use Magento\Cms\Api\PageRepositoryInterface;
@@ -21,27 +21,26 @@
2121
*/
2222
class MultiStorePagesTest extends MultiStoreTestCase
2323
{
24-
/** @var Page */
25-
protected $pagesIndexer;
26-
2724
/** @var PageRepositoryInterface */
2825
protected $pageRepository;
2926

3027
/** @var CollectionFactory */
3128
private $pageCollectionFactory;
3229

30+
/** @var PageBatchQueueProcessor */
31+
protected $pageBatchQueueProcessor;
32+
3333
const HOME_PAGE_ID = 2;
3434

3535
public function setUp():void
3636
{
3737
parent::setUp();
3838

39-
$this->pagesIndexer = $this->objectManager->get(Page::class);
4039
$this->pageRepository = $this->objectManager->get(PageRepositoryInterface::class);
4140
$this->pageCollectionFactory = $this->objectManager->get(CollectionFactory::class);
4241

43-
$this->pagesIndexer->executeFull();
44-
$this->algoliaHelper->waitLastTask();
42+
$this->pageBatchQueueProcessor = $this->objectManager->get(PageBatchQueueProcessor::class);
43+
$this->reindexToAllStores($this->pageBatchQueueProcessor);
4544
}
4645

4746
/***
@@ -77,10 +76,7 @@ public function testMultiStorePageIndices()
7776
$homePage->setStores([$defaultStore->getId()]);
7877
$this->pageRepository->save($homePage);
7978

80-
$this->pagesIndexer->execute([self::HOME_PAGE_ID]);
81-
82-
$this->algoliaHelper->waitLastTask($defaultStore->getId());
83-
$this->algoliaHelper->waitLastTask($fixtureSecondStore->getId());
79+
$this->reindexToAllStores($this->pageBatchQueueProcessor, [self::HOME_PAGE_ID]);
8480

8581
$this->assertNbOfRecordsPerStore(
8682
$defaultStore->getCode(),

Test/Integration/Indexing/Page/PagesIndexingTest.php

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
namespace Algolia\AlgoliaSearch\Test\Integration\Indexing\Page;
44

5+
use Algolia\AlgoliaSearch\Console\Command\Indexer\IndexPagesCommand;
56
use Algolia\AlgoliaSearch\Helper\Entity\PageHelper;
6-
use Algolia\AlgoliaSearch\Model\Indexer\Page;
7+
use Algolia\AlgoliaSearch\Service\Page\BatchQueueProcessor as PageBatchQueueProcessor;
78
use Algolia\AlgoliaSearch\Test\Integration\Indexing\IndexingTestCase;
89
use Magento\Cms\Model\PageFactory;
910

@@ -16,10 +17,8 @@ public function testNoExcludedPages()
1617
$this->getSerializer()->serialize([])
1718
);
1819

19-
/** @var Page $indexer */
20-
$indexer = $this->getObjectManager()->create(Page::class);
21-
22-
$this->processTest($indexer, 'pages', $this->assertValues->expectedPages);
20+
$pageBatchQueueProcessor = $this->objectManager->get(PageBatchQueueProcessor::class);
21+
$this->processTest($pageBatchQueueProcessor, 'pages', $this->assertValues->expectedPages);
2322
}
2423

2524
public function testExcludedPages()
@@ -33,9 +32,8 @@ public function testExcludedPages()
3332
$this->getSerializer()->serialize($excludedPages)
3433
);
3534

36-
/** @var Page $indexer */
37-
$indexer = $this->getObjectManager()->create(Page::class);
38-
$this->processTest($indexer, 'pages', $this->assertValues->expectedExcludePages);
35+
$pageBatchQueueProcessor = $this->objectManager->get(PageBatchQueueProcessor::class);
36+
$this->processTest($pageBatchQueueProcessor, 'pages', $this->assertValues->expectedExcludePages);
3937

4038
$response = $this->algoliaHelper->query($this->indexPrefix . 'default_pages', '', []);
4139
$hits = reset($response['results'])['hits'];
@@ -61,10 +59,8 @@ public function testExcludedPages()
6159

6260
public function testDefaultIndexableAttributes()
6361
{
64-
/** @var Page $indexer */
65-
$indexer = $this->getObjectManager()->create(Page::class);
66-
$indexer->executeFull();
67-
62+
$pageBatchQueueProcessor = $this->objectManager->get(PageBatchQueueProcessor::class);
63+
$pageBatchQueueProcessor->processBatch(1);
6864
$this->algoliaHelper->waitLastTask();
6965

7066
$response = $this->algoliaHelper->query($this->indexPrefix . 'default_pages', '', ['hitsPerPage' => 1]);
@@ -152,4 +148,15 @@ public function testUtf8()
152148

153149
$testPage->delete();
154150
}
151+
152+
public function testIndexingPagesCommand()
153+
{
154+
$this->setConfig(
155+
'algoliasearch_autocomplete/autocomplete/excluded_pages',
156+
$this->getSerializer()->serialize([])
157+
);
158+
159+
$indexPagesCmd = $this->objectManager->get(IndexPagesCommand::class);
160+
$this->processCommandTest($indexPagesCmd, 'pages', $this->assertValues->expectedPages);
161+
}
155162
}

0 commit comments

Comments
 (0)