|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\Elasticsearch\Test\Unit\Model\Indexer\Fulltext\Plugin\Category\Product\Action; |
| 9 | + |
| 10 | +use Magento\Catalog\Model\Indexer\Category\Product\TableMaintainer; |
| 11 | +use Magento\Framework\DB\Adapter\AdapterInterface; |
| 12 | +use Magento\Framework\DB\Select; |
| 13 | +use Magento\Store\Model\Store; |
| 14 | +use PHPUnit\Framework\MockObject\MockObject; |
| 15 | +use PHPUnit\Framework\TestCase; |
| 16 | +use Magento\Framework\Indexer\IndexerRegistry; |
| 17 | +use Magento\Store\Model\StoreManagerInterface; |
| 18 | +use Magento\Elasticsearch\Model\Indexer\Fulltext\Plugin\Category\Product\Action\Rows; |
| 19 | +use Magento\Catalog\Model\Indexer\Category\Product\Action\Rows as ActionRows; |
| 20 | +use Magento\Framework\App\ResourceConnection; |
| 21 | +use Magento\CatalogSearch\Model\Indexer\Fulltext as FulltextIndexer; |
| 22 | + |
| 23 | +/** |
| 24 | + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
| 25 | + */ |
| 26 | +class RowsTest extends TestCase |
| 27 | +{ |
| 28 | + /** |
| 29 | + * @var IndexerRegistry|MockObject |
| 30 | + */ |
| 31 | + private $indexerRegistryMock; |
| 32 | + |
| 33 | + /** |
| 34 | + * @var StoreManagerInterface|MockObject |
| 35 | + */ |
| 36 | + private $storeManagerMock; |
| 37 | + |
| 38 | + /** |
| 39 | + * @var Select|MockObject |
| 40 | + */ |
| 41 | + private $selectMock; |
| 42 | + |
| 43 | + /** |
| 44 | + * @var AdapterInterface|MockObject |
| 45 | + */ |
| 46 | + private $connectionMock; |
| 47 | + |
| 48 | + /** |
| 49 | + * @var ResourceConnection|MockObject |
| 50 | + */ |
| 51 | + private $resourceMock; |
| 52 | + |
| 53 | + /** |
| 54 | + * @var TableMaintainer|MockObject |
| 55 | + */ |
| 56 | + private $tableMaintainerMock; |
| 57 | + |
| 58 | + /** |
| 59 | + * @var Rows |
| 60 | + */ |
| 61 | + private $plugin; |
| 62 | + public function setUp(): void |
| 63 | + { |
| 64 | + parent::setUp(); |
| 65 | + $this->indexerRegistryMock = $this->createMock(IndexerRegistry::class); |
| 66 | + $this->storeManagerMock = |
| 67 | + $this->getMockBuilder(StoreManagerInterface::class)->getMockForAbstractClass(); |
| 68 | + $this->connectionMock = |
| 69 | + $this->getMockBuilder(AdapterInterface::class)->getMockForAbstractClass(); |
| 70 | + $this->selectMock = $this->createMock(Select::class); |
| 71 | + $this->connectionMock->expects($this->any())->method('select')->willReturn($this->selectMock); |
| 72 | + $this->tableMaintainerMock = $this->createMock(TableMaintainer::class); |
| 73 | + $this->resourceMock = $this->getMockBuilder(ResourceConnection::class) |
| 74 | + ->disableOriginalConstructor() |
| 75 | + ->getMock(); |
| 76 | + $this->resourceMock->expects($this->any()) |
| 77 | + ->method('getConnection') |
| 78 | + ->willReturn($this->connectionMock); |
| 79 | + $this->plugin = new Rows( |
| 80 | + $this->indexerRegistryMock, |
| 81 | + $this->storeManagerMock, |
| 82 | + $this->resourceMock, |
| 83 | + $this->tableMaintainerMock |
| 84 | + ); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Test afterExecute method. |
| 89 | + * |
| 90 | + * @return void |
| 91 | + */ |
| 92 | + public function testAfterExecute(): void |
| 93 | + { |
| 94 | + $productToReindex = [1]; |
| 95 | + $storeId = 1; |
| 96 | + $categoryIds = [4]; |
| 97 | + $actionMock = $this->createMock(ActionRows::class); |
| 98 | + $storeMock = $this->createMock(Store::class); |
| 99 | + $storeMock->expects($this->once())->method('getId')->willReturn($storeId); |
| 100 | + $this->storeManagerMock->expects($this->once())->method('getStores')->willReturn([$storeMock]); |
| 101 | + $this->tableMaintainerMock->expects($this->once())->method('getMainTable')->with($storeId)->willReturn('table'); |
| 102 | + |
| 103 | + $this->getProductIdsFromIndex($productToReindex); |
| 104 | + $this->createIndexerMock($productToReindex); |
| 105 | + |
| 106 | + $this->plugin->afterExecute($actionMock, $actionMock, $categoryIds); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Creates a mock for the indexer registry to add given ids to changelog. |
| 111 | + * |
| 112 | + * @param array $ids |
| 113 | + * @return void |
| 114 | + */ |
| 115 | + private function createIndexerMock(array $ids): void |
| 116 | + { |
| 117 | + //schedule catalogsearch indexer changes to improve row index performance instead of executing them right away |
| 118 | + $changelogMock = $this->createMock(\Magento\Framework\Mview\View\Changelog::class); |
| 119 | + $changelogMock->expects($this->once())->method('addList')->with($ids); |
| 120 | + $viewMock = $this->createMock(\Magento\Framework\Mview\ViewInterface::class); |
| 121 | + $viewMock->expects($this->once())->method('getChangelog')->willReturn($changelogMock); |
| 122 | + |
| 123 | + $indexerMock = $this->createMock(\Magento\Framework\Indexer\IndexerInterface::class); |
| 124 | + $indexerMock->expects($this->once())->method('isScheduled')->willReturn(true); |
| 125 | + $indexerMock->expects($this->once())->method('getView')->willReturn($viewMock); |
| 126 | + |
| 127 | + $this->indexerRegistryMock->expects($this->once()) |
| 128 | + ->method('get') |
| 129 | + ->with(FulltextIndexer::INDEXER_ID) |
| 130 | + ->willReturn($indexerMock); |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * Mocks the connection to return the given ids. |
| 135 | + * |
| 136 | + * @param array $ids |
| 137 | + * @return void |
| 138 | + */ |
| 139 | + private function getProductIdsFromIndex(array $ids): void |
| 140 | + { |
| 141 | + $this->selectMock->expects($this->any())->method('from')->with()->willReturnSelf(); |
| 142 | + $this->selectMock->expects($this->any())->method('where')->willReturnSelf(); |
| 143 | + $this->connectionMock->expects($this->any())->method('fetchCol')->willReturn($ids); |
| 144 | + } |
| 145 | +} |
0 commit comments