|
| 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\Model\Indexer; |
| 9 | + |
| 10 | +use Magento\Catalog\Api\ProductRepositoryInterface; |
| 11 | +use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory; |
| 12 | +use Magento\CatalogRule\Model\Indexer\Rule\RuleProductProcessor; |
| 13 | +use Magento\Framework\DataObject; |
| 14 | +use Magento\TestFramework\Fixture\AppIsolation; |
| 15 | +use Magento\TestFramework\Fixture\DataFixture; |
| 16 | +use Magento\TestFramework\Fixture\DbIsolation; |
| 17 | +use Magento\TestFramework\Helper\Bootstrap; |
| 18 | + |
| 19 | +class IndexerBuilderInScheduledModeTest extends \PHPUnit\Framework\TestCase |
| 20 | +{ |
| 21 | + /** |
| 22 | + * @var RuleProductProcessor |
| 23 | + */ |
| 24 | + private $ruleProductProcessor; |
| 25 | + |
| 26 | + /** |
| 27 | + * @var CollectionFactory |
| 28 | + */ |
| 29 | + private $productCollectionFactory; |
| 30 | + |
| 31 | + /** |
| 32 | + * @var ProductRepositoryInterface |
| 33 | + */ |
| 34 | + private $productRepository; |
| 35 | + |
| 36 | + protected function setUp(): void |
| 37 | + { |
| 38 | + $this->productRepository = Bootstrap::getObjectManager()->get(ProductRepositoryInterface::class); |
| 39 | + $this->ruleProductProcessor = Bootstrap::getObjectManager()->get(RuleProductProcessor::class); |
| 40 | + $this->productCollectionFactory = Bootstrap::getObjectManager()->get(CollectionFactory::class); |
| 41 | + } |
| 42 | + |
| 43 | + #[ |
| 44 | + DbIsolation(false), |
| 45 | + AppIsolation(true), |
| 46 | + DataFixture('Magento/Catalog/_files/product_with_options.php'), |
| 47 | + DataFixture('Magento/CatalogRule/_files/catalog_rule_10_off_not_logged.php'), |
| 48 | + DataFixture('Magento/CatalogRule/_files/set_indexer_to_scheduled_mode.php'), |
| 49 | + ] |
| 50 | + public function testReindexOfDependentIndexer(): void |
| 51 | + { |
| 52 | + $product = $this->productRepository->get('simple'); |
| 53 | + $productId = (int)$product->getId(); |
| 54 | + |
| 55 | + $product = $this->getProductFromCollection($productId); |
| 56 | + $this->assertEquals(9, $product->getPriceInfo()->getPrice('final_price')->getAmount()->getValue()); |
| 57 | + |
| 58 | + $product->setPrice(100); |
| 59 | + $this->productRepository->save($product); |
| 60 | + |
| 61 | + $product = $this->getProductFromCollection($productId); |
| 62 | + $this->assertEquals(9, $product->getPriceInfo()->getPrice('final_price')->getAmount()->getValue()); |
| 63 | + |
| 64 | + $this->ruleProductProcessor->getIndexer()->reindexList([$productId]); |
| 65 | + |
| 66 | + $product = $this->getProductFromCollection($productId); |
| 67 | + $this->assertEquals(90, $product->getPriceInfo()->getPrice('final_price')->getAmount()->getValue()); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Get the product from the product collection |
| 72 | + * |
| 73 | + * @param int $productId |
| 74 | + * @return DataObject |
| 75 | + */ |
| 76 | + private function getProductFromCollection(int $productId) : DataObject |
| 77 | + { |
| 78 | + $productCollection = $this->productCollectionFactory->create(); |
| 79 | + $productCollection->addIdFilter($productId); |
| 80 | + $productCollection->addPriceData(); |
| 81 | + $productCollection->load(); |
| 82 | + return $productCollection->getFirstItem(); |
| 83 | + } |
| 84 | +} |
0 commit comments