|
| 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\Catalog\Test\Unit\Model\Indexer\Product\Category\Action; |
| 9 | + |
| 10 | +use Magento\Eav\Model\Entity\Attribute\AbstractAttribute; |
| 11 | +use Magento\Store\Model\StoreManagerInterface; |
| 12 | +use Magento\Store\Model\Store; |
| 13 | +use Magento\Catalog\Model\Config; |
| 14 | +use Magento\Catalog\Model\Indexer\Product\Category\Action\Rows; |
| 15 | +use Magento\Catalog\Model\Indexer\Category\Product as CategoryProductIndexer; |
| 16 | +use Magento\Catalog\Model\Indexer\Category\Product\TableMaintainer; |
| 17 | +use Magento\Indexer\Model\WorkingStateProvider; |
| 18 | +use Magento\Framework\EntityManager\EntityMetadataInterface; |
| 19 | +use Magento\Framework\App\ResourceConnection; |
| 20 | +use Magento\Framework\DB\Query\Generator as QueryGenerator; |
| 21 | +use Magento\Framework\DB\Adapter\AdapterInterface; |
| 22 | +use Magento\Framework\DB\Select; |
| 23 | +use Magento\Framework\DB\Ddl\Table; |
| 24 | +use Magento\Framework\EntityManager\MetadataPool; |
| 25 | +use Magento\Framework\Event\ManagerInterface as EventManagerInterface; |
| 26 | +use Magento\Framework\Indexer\IndexerRegistry; |
| 27 | +use Magento\Framework\Indexer\CacheContext; |
| 28 | +use Magento\Framework\Indexer\IndexerInterface; |
| 29 | +use PHPUnit\Framework\MockObject\MockObject; |
| 30 | +use PHPUnit\Framework\TestCase; |
| 31 | + |
| 32 | +/** |
| 33 | + * Test for Rows action |
| 34 | + */ |
| 35 | +class RowsTest extends TestCase |
| 36 | +{ |
| 37 | + /** |
| 38 | + * @var WorkingStateProvider|MockObject |
| 39 | + */ |
| 40 | + private $workingStateProvider; |
| 41 | + |
| 42 | + /** |
| 43 | + * @var ResourceConnection|MockObject |
| 44 | + */ |
| 45 | + private $resource; |
| 46 | + |
| 47 | + /** |
| 48 | + * @var StoreManagerInterface|MockObject |
| 49 | + */ |
| 50 | + private $storeManager; |
| 51 | + |
| 52 | + /** |
| 53 | + * @var Config|MockObject |
| 54 | + */ |
| 55 | + private $config; |
| 56 | + |
| 57 | + /** |
| 58 | + * @var QueryGenerator|MockObject |
| 59 | + */ |
| 60 | + private $queryGenerator; |
| 61 | + |
| 62 | + /** |
| 63 | + * @var MetadataPool|MockObject |
| 64 | + */ |
| 65 | + private $metadataPool; |
| 66 | + |
| 67 | + /** |
| 68 | + * @var CacheContext|MockObject |
| 69 | + */ |
| 70 | + private $cacheContext; |
| 71 | + |
| 72 | + /** |
| 73 | + * @var EventManagerInterface|MockObject |
| 74 | + */ |
| 75 | + private $eventManager; |
| 76 | + |
| 77 | + /** |
| 78 | + * @var IndexerRegistry|MockObject |
| 79 | + */ |
| 80 | + private $indexerRegistry; |
| 81 | + |
| 82 | + /** |
| 83 | + * @var TableMaintainer|MockObject |
| 84 | + */ |
| 85 | + private $tableMaintainer; |
| 86 | + |
| 87 | + /** |
| 88 | + * @var IndexerInterface|MockObject |
| 89 | + */ |
| 90 | + private $indexer; |
| 91 | + |
| 92 | + /** |
| 93 | + * @var AdapterInterface|MockObject |
| 94 | + */ |
| 95 | + private $connection; |
| 96 | + |
| 97 | + /** |
| 98 | + * @var Select|MockObject |
| 99 | + */ |
| 100 | + private $select; |
| 101 | + |
| 102 | + /** |
| 103 | + * @var Rows |
| 104 | + */ |
| 105 | + private $rowsModel; |
| 106 | + |
| 107 | + protected function setUp() : void |
| 108 | + { |
| 109 | + $this->workingStateProvider = $this->getMockBuilder(WorkingStateProvider::class) |
| 110 | + ->disableOriginalConstructor() |
| 111 | + ->getMock(); |
| 112 | + $this->resource = $this->getMockBuilder(ResourceConnection::class) |
| 113 | + ->disableOriginalConstructor() |
| 114 | + ->getMock(); |
| 115 | + $this->connection = $this->getMockBuilder(AdapterInterface::class) |
| 116 | + ->getMockForAbstractClass(); |
| 117 | + $this->resource->expects($this->any()) |
| 118 | + ->method('getConnection') |
| 119 | + ->willReturn($this->connection); |
| 120 | + $this->select = $this->getMockBuilder(Select::class) |
| 121 | + ->disableOriginalConstructor() |
| 122 | + ->getMock(); |
| 123 | + $this->select->expects($this->any()) |
| 124 | + ->method('from') |
| 125 | + ->willReturnSelf(); |
| 126 | + $this->select->expects($this->any()) |
| 127 | + ->method('where') |
| 128 | + ->willReturnSelf(); |
| 129 | + $this->select->expects($this->any()) |
| 130 | + ->method('distinct') |
| 131 | + ->willReturnSelf(); |
| 132 | + $this->select->expects($this->any()) |
| 133 | + ->method('joinInner') |
| 134 | + ->willReturnSelf(); |
| 135 | + $this->select->expects($this->any()) |
| 136 | + ->method('group') |
| 137 | + ->willReturnSelf(); |
| 138 | + $this->select->expects($this->any()) |
| 139 | + ->method('joinLeft') |
| 140 | + ->willReturnSelf(); |
| 141 | + $this->select->expects($this->any()) |
| 142 | + ->method('columns') |
| 143 | + ->willReturnSelf(); |
| 144 | + $this->connection->expects($this->any()) |
| 145 | + ->method('select') |
| 146 | + ->willReturn($this->select); |
| 147 | + $this->storeManager = $this->getMockBuilder(StoreManagerInterface::class) |
| 148 | + ->getMockForAbstractClass(); |
| 149 | + $this->config = $this->getMockBuilder(Config::class) |
| 150 | + ->disableOriginalConstructor() |
| 151 | + ->getMock(); |
| 152 | + $this->queryGenerator = $this->getMockBuilder(QueryGenerator::class) |
| 153 | + ->disableOriginalConstructor() |
| 154 | + ->getMock(); |
| 155 | + $this->metadataPool = $this->getMockBuilder(MetadataPool::class) |
| 156 | + ->disableOriginalConstructor() |
| 157 | + ->getMock(); |
| 158 | + $this->cacheContext = $this->getMockBuilder(CacheContext::class) |
| 159 | + ->disableOriginalConstructor() |
| 160 | + ->getMock(); |
| 161 | + $this->eventManager = $this->getMockBuilder(EventManagerInterface::class) |
| 162 | + ->getMockForAbstractClass(); |
| 163 | + $this->indexerRegistry = $this->getMockBuilder(IndexerRegistry::class) |
| 164 | + ->disableOriginalConstructor() |
| 165 | + ->getMock(); |
| 166 | + $this->indexer = $this->getMockBuilder(IndexerInterface::class) |
| 167 | + ->getMockForAbstractClass(); |
| 168 | + $this->tableMaintainer = $this->getMockBuilder(TableMaintainer::class) |
| 169 | + ->disableOriginalConstructor() |
| 170 | + ->getMock(); |
| 171 | + |
| 172 | + $this->rowsModel = new Rows( |
| 173 | + $this->resource, |
| 174 | + $this->storeManager, |
| 175 | + $this->config, |
| 176 | + $this->queryGenerator, |
| 177 | + $this->metadataPool, |
| 178 | + $this->tableMaintainer, |
| 179 | + $this->cacheContext, |
| 180 | + $this->eventManager, |
| 181 | + $this->indexerRegistry, |
| 182 | + $this->workingStateProvider |
| 183 | + ); |
| 184 | + } |
| 185 | + |
| 186 | + public function testExecuteWithIndexerWorking() : void |
| 187 | + { |
| 188 | + $categoryId = '1'; |
| 189 | + $store = $this->getMockBuilder(Store::class) |
| 190 | + ->disableOriginalConstructor() |
| 191 | + ->getMock(); |
| 192 | + $store->expects($this->any()) |
| 193 | + ->method('getRootCategoryId') |
| 194 | + ->willReturn($categoryId); |
| 195 | + $store->expects($this->any()) |
| 196 | + ->method('getId') |
| 197 | + ->willReturn(1); |
| 198 | + |
| 199 | + $attribute = $this->getMockBuilder(AbstractAttribute::class) |
| 200 | + ->disableOriginalConstructor() |
| 201 | + ->getMockForAbstractClass(); |
| 202 | + $this->config->expects($this->any()) |
| 203 | + ->method('getAttribute') |
| 204 | + ->willReturn($attribute); |
| 205 | + |
| 206 | + $table = $this->getMockBuilder(Table::class) |
| 207 | + ->disableOriginalConstructor() |
| 208 | + ->getMock(); |
| 209 | + $this->connection->expects($this->any()) |
| 210 | + ->method('newTable') |
| 211 | + ->willReturn($table); |
| 212 | + |
| 213 | + $metadata = $this->getMockBuilder(EntityMetadataInterface::class) |
| 214 | + ->getMockForAbstractClass(); |
| 215 | + $this->metadataPool->expects($this->any()) |
| 216 | + ->method('getMetadata') |
| 217 | + ->willReturn($metadata); |
| 218 | + |
| 219 | + $this->connection->expects($this->any()) |
| 220 | + ->method('fetchAll') |
| 221 | + ->willReturn([]); |
| 222 | + $this->connection->expects($this->any()) |
| 223 | + ->method('fetchCol') |
| 224 | + ->willReturn([]); |
| 225 | + |
| 226 | + $this->connection->expects($this->any()) |
| 227 | + ->method('fetchOne') |
| 228 | + ->willReturn($categoryId); |
| 229 | + $this->indexerRegistry->expects($this->any()) |
| 230 | + ->method('get') |
| 231 | + ->with(CategoryProductIndexer::INDEXER_ID) |
| 232 | + ->willReturn($this->indexer); |
| 233 | + $this->indexer->expects($this->any()) |
| 234 | + ->method('getId') |
| 235 | + ->willReturn(CategoryProductIndexer::INDEXER_ID); |
| 236 | + $this->workingStateProvider->expects($this->any()) |
| 237 | + ->method('isWorking') |
| 238 | + ->with(CategoryProductIndexer::INDEXER_ID) |
| 239 | + ->willReturn(true); |
| 240 | + $this->storeManager->expects($this->any()) |
| 241 | + ->method('getStores') |
| 242 | + ->willReturn([$store]); |
| 243 | + |
| 244 | + $this->connection->expects($this->once()) |
| 245 | + ->method('delete'); |
| 246 | + |
| 247 | + $result = $this->rowsModel->execute([1, 2, 3]); |
| 248 | + $this->assertInstanceOf(Rows::class, $result); |
| 249 | + } |
| 250 | +} |
0 commit comments