|
| 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\CatalogRule\Test\Unit\Model\Indexer; |
| 9 | + |
| 10 | +use Magento\Catalog\Model\ProductFactory; |
| 11 | +use Magento\CatalogRule\Model\Indexer\IndexBuilder; |
| 12 | +use Magento\CatalogRule\Model\Indexer\ReindexRuleProduct; |
| 13 | +use Magento\CatalogRule\Model\Indexer\ReindexRuleProductPrice; |
| 14 | +use Magento\CatalogRule\Model\ResourceModel\Rule\Collection; |
| 15 | +use Magento\CatalogRule\Model\ResourceModel\Rule\CollectionFactory; |
| 16 | +use Magento\CatalogRule\Model\Rule; |
| 17 | +use Magento\Eav\Model\Config; |
| 18 | +use Magento\Framework\App\ResourceConnection; |
| 19 | +use Magento\Framework\DB\Adapter\AdapterInterface; |
| 20 | +use Magento\Framework\Pricing\PriceCurrencyInterface; |
| 21 | +use Magento\Framework\Stdlib\DateTime; |
| 22 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; |
| 23 | +use Magento\Store\Model\StoreManagerInterface; |
| 24 | +use PHPUnit\Framework\TestCase; |
| 25 | +use Psr\Log\LoggerInterface; |
| 26 | + |
| 27 | +/** |
| 28 | + * Test for \Magento\CatalogRule\Model\Indexer\IndexBuilder. |
| 29 | + * |
| 30 | + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
| 31 | + */ |
| 32 | +class IndexBuilderTest extends TestCase |
| 33 | +{ |
| 34 | + /** |
| 35 | + * @var CollectionFactory|\PHPUnit\Framework\MockObject\MockObject |
| 36 | + */ |
| 37 | + private $ruleCollectionFactoryMock; |
| 38 | + |
| 39 | + /** |
| 40 | + * @var PriceCurrencyInterface|\PHPUnit\Framework\MockObject\MockObject |
| 41 | + */ |
| 42 | + private $priceCurrencyMock; |
| 43 | + |
| 44 | + /** |
| 45 | + * @var ResourceConnection|\PHPUnit\Framework\MockObject\MockObject |
| 46 | + */ |
| 47 | + private $resourceMock; |
| 48 | + |
| 49 | + /** |
| 50 | + * @var ResourceConnection|\PHPUnit\Framework\MockObject\MockObject |
| 51 | + */ |
| 52 | + private $connectionMock; |
| 53 | + |
| 54 | + /** |
| 55 | + * @var StoreManagerInterface|\PHPUnit\Framework\MockObject\MockObject |
| 56 | + */ |
| 57 | + private $storeManagerMock; |
| 58 | + |
| 59 | + /** |
| 60 | + * @var \PHPUnit\Framework\MockObject\MockObject|LoggerInterface |
| 61 | + */ |
| 62 | + private $loggerMock; |
| 63 | + |
| 64 | + /** |
| 65 | + * @var Config|\PHPUnit\Framework\MockObject\MockObject |
| 66 | + */ |
| 67 | + private $eavConfigMock; |
| 68 | + |
| 69 | + /** |
| 70 | + * @var DateTime|\PHPUnit\Framework\MockObject\MockObject |
| 71 | + */ |
| 72 | + private $dateFormatMock; |
| 73 | + |
| 74 | + /** |
| 75 | + * @var DateTime\DateTime|\PHPUnit\Framework\MockObject\MockObject |
| 76 | + */ |
| 77 | + private $dateTimeMock; |
| 78 | + |
| 79 | + /** |
| 80 | + * @var ProductFactory|\PHPUnit\Framework\MockObject\MockObject |
| 81 | + */ |
| 82 | + private $productFactoryMock; |
| 83 | + |
| 84 | + /** |
| 85 | + * @var int |
| 86 | + */ |
| 87 | + private $batchCountMock; |
| 88 | + |
| 89 | + /** |
| 90 | + * @var ReindexRuleProduct|\PHPUnit\Framework\MockObject\MockObject |
| 91 | + */ |
| 92 | + private $reindexRuleProductMock; |
| 93 | + |
| 94 | + /** |
| 95 | + * @var ReindexRuleProductPrice|\PHPUnit\Framework\MockObject\MockObject |
| 96 | + */ |
| 97 | + private $reindexRuleProductPriceMock; |
| 98 | + |
| 99 | + /** |
| 100 | + * @var IndexBuilder |
| 101 | + */ |
| 102 | + private $indexBuilder; |
| 103 | + |
| 104 | + protected function setUp(): void |
| 105 | + { |
| 106 | + $objectManager = new ObjectManager($this); |
| 107 | + $this->ruleCollectionFactoryMock = $this->createMock(CollectionFactory::class); |
| 108 | + $this->priceCurrencyMock = $this->getMockBuilder(PriceCurrencyInterface::class) |
| 109 | + ->getMockForAbstractClass(); |
| 110 | + $this->resourceMock = $this->createMock(ResourceConnection::class); |
| 111 | + $this->connectionMock = $this->getMockBuilder(AdapterInterface::class) |
| 112 | + ->getMockForAbstractClass(); |
| 113 | + $this->resourceMock->expects($this->once())->method('getConnection') |
| 114 | + ->willReturn($this->connectionMock); |
| 115 | + $this->storeManagerMock = $this->getMockBuilder(StoreManagerInterface::class) |
| 116 | + ->getMockForAbstractClass(); |
| 117 | + $this->loggerMock = $this->getMockBuilder(LoggerInterface::class) |
| 118 | + ->getMockForAbstractClass(); |
| 119 | + $this->eavConfigMock = $this->createMock(Config::class); |
| 120 | + $this->dateFormatMock = $this->createMock(DateTime::class); |
| 121 | + $this->dateTimeMock = $this->createMock(DateTime\DateTime::class); |
| 122 | + $this->productFactoryMock = $this->createMock(ProductFactory::class); |
| 123 | + $this->batchCountMock = 99; |
| 124 | + $this->reindexRuleProductMock = $this->createMock(ReindexRuleProduct::class); |
| 125 | + $this->reindexRuleProductPriceMock = $this->createMock(ReindexRuleProductPrice::class); |
| 126 | + |
| 127 | + $this->indexBuilder = $objectManager->getObject( |
| 128 | + IndexBuilder::class, |
| 129 | + [ |
| 130 | + 'ruleCollectionFactory' => $this->ruleCollectionFactoryMock, |
| 131 | + 'priceCurrency' => $this->priceCurrencyMock, |
| 132 | + 'resource' => $this->resourceMock, |
| 133 | + 'storeManager' => $this->storeManagerMock, |
| 134 | + 'logger' => $this->loggerMock, |
| 135 | + 'eavConfig' => $this->eavConfigMock, |
| 136 | + 'dateFormat' => $this->dateFormatMock, |
| 137 | + 'dateTime' => $this->dateTimeMock, |
| 138 | + 'productFactory' => $this->productFactoryMock, |
| 139 | + 'batchCount' => $this->batchCountMock, |
| 140 | + 'reindexRuleProduct' => $this->reindexRuleProductMock, |
| 141 | + 'reindexRuleProductPrice' => $this->reindexRuleProductPriceMock, |
| 142 | + ] |
| 143 | + ); |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * Test for \Magento\CatalogRule\Model\Indexer\IndexBuilder::reindexByIds. |
| 148 | + * |
| 149 | + * @throws \Magento\Framework\Exception\LocalizedException |
| 150 | + */ |
| 151 | + public function testReindexByIds() |
| 152 | + { |
| 153 | + $id1 = 1; |
| 154 | + $id2 = 1; |
| 155 | + $ids = [$id1, $id2]; |
| 156 | + $collectionMock = $this->createMock(Collection::class); |
| 157 | + $collectionMock->expects($this->once())->method('addFieldToFilter')->with('is_active', 1) |
| 158 | + ->willReturn($collectionMock); |
| 159 | + $ruleMock = $this->createMock(Rule::class); |
| 160 | + $ruleMock->expects($this->once())->method('setProductsFilter')->with($ids); |
| 161 | + $collectionMock->expects($this->once())->method('getItems')->willReturn([$ruleMock]); |
| 162 | + $this->ruleCollectionFactoryMock->expects($this->once())->method('create') |
| 163 | + ->willReturn($collectionMock); |
| 164 | + $this->reindexRuleProductMock->expects($this->once())->method('execute') |
| 165 | + ->with($ruleMock, $this->batchCountMock); |
| 166 | + $this->reindexRuleProductPriceMock->expects($this->exactly(2))->method('execute') |
| 167 | + ->withConsecutive([$this->batchCountMock, $id1], [$this->batchCountMock, $id2]); |
| 168 | + |
| 169 | + $this->indexBuilder->reindexByIds($ids); |
| 170 | + } |
| 171 | + |
| 172 | + /** |
| 173 | + * Test for \Magento\CatalogRule\Model\Indexer\IndexBuilder::reindexById. |
| 174 | + * |
| 175 | + * @throws \Magento\Framework\Exception\LocalizedException |
| 176 | + */ |
| 177 | + public function testReindexById() |
| 178 | + { |
| 179 | + $id = 1; |
| 180 | + $collectionMock = $this->createMock(Collection::class); |
| 181 | + $collectionMock->expects($this->once())->method('addFieldToFilter')->with('is_active', 1) |
| 182 | + ->willReturn($collectionMock); |
| 183 | + $ruleMock = $this->createMock(Rule::class); |
| 184 | + $ruleMock->expects($this->once())->method('setProductsFilter')->with([$id]); |
| 185 | + $collectionMock->expects($this->once())->method('getItems')->willReturn([$ruleMock]); |
| 186 | + $this->ruleCollectionFactoryMock->expects($this->once())->method('create') |
| 187 | + ->willReturn($collectionMock); |
| 188 | + $this->reindexRuleProductMock->expects($this->once())->method('execute') |
| 189 | + ->with($ruleMock, $this->batchCountMock); |
| 190 | + $this->reindexRuleProductPriceMock->expects($this->once())->method('execute') |
| 191 | + ->with($this->batchCountMock, $id); |
| 192 | + |
| 193 | + $this->indexBuilder->reindexById($id); |
| 194 | + } |
| 195 | +} |
0 commit comments