|
| 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\CatalogRuleConfigurable\Model\Indexer\Product; |
| 9 | + |
| 10 | +use Magento\Catalog\Api\ProductRepositoryInterface; |
| 11 | +use Magento\Catalog\Model\Product; |
| 12 | +use Magento\CatalogRule\Model\Indexer\Product\ProductRuleIndexer; |
| 13 | +use Magento\CatalogRule\Pricing\Price\CatalogRulePrice; |
| 14 | +use Magento\Framework\Pricing\Price\Factory as PriceFactory; |
| 15 | +use Magento\TestFramework\Helper\Bootstrap; |
| 16 | +use PHPUnit\Framework\TestCase; |
| 17 | + |
| 18 | +/** |
| 19 | + * @magentoDbIsolation disabled |
| 20 | + * @magentoAppArea adminhtml |
| 21 | + * @magentoDataFixture Magento/CatalogRuleConfigurable/_files/configurable_product_with_percent_rule.php |
| 22 | + */ |
| 23 | +class ProductRuleIndexerTest extends TestCase |
| 24 | +{ |
| 25 | + /** |
| 26 | + * @var ProductRepositoryInterface |
| 27 | + */ |
| 28 | + private $productRepository; |
| 29 | + |
| 30 | + /** |
| 31 | + * @var PriceFactory |
| 32 | + */ |
| 33 | + private $priceFactory; |
| 34 | + |
| 35 | + /** |
| 36 | + * @var ProductRuleIndexer |
| 37 | + */ |
| 38 | + private $productRuleIndexer; |
| 39 | + |
| 40 | + /** |
| 41 | + * @inheritdoc |
| 42 | + */ |
| 43 | + protected function setUp(): void |
| 44 | + { |
| 45 | + $objectManager = Bootstrap::getObjectManager(); |
| 46 | + $this->productRepository = $objectManager->get(ProductRepositoryInterface::class); |
| 47 | + $this->priceFactory = $objectManager->get(PriceFactory::class); |
| 48 | + $this->productRuleIndexer = $objectManager->create(ProductRuleIndexer::class); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * @return void |
| 53 | + */ |
| 54 | + public function testExecute(): void |
| 55 | + { |
| 56 | + $product = $this->productRepository->get('configurable'); |
| 57 | + $this->productRuleIndexer->execute([$product->getId()]); |
| 58 | + |
| 59 | + $product = $this->productRepository->get('simple_10'); |
| 60 | + $price = $this->getCatalogRulePrice($product); |
| 61 | + $this->assertEquals(5, $price); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * @return void |
| 66 | + */ |
| 67 | + public function testExecuteRow(): void |
| 68 | + { |
| 69 | + $product = $this->productRepository->get('configurable'); |
| 70 | + $this->productRuleIndexer->executeRow($product->getId()); |
| 71 | + |
| 72 | + $product = $this->productRepository->get('simple_10'); |
| 73 | + $price = $this->getCatalogRulePrice($product); |
| 74 | + $this->assertEquals(5, $price); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * @return void |
| 79 | + */ |
| 80 | + public function testExecuteList(): void |
| 81 | + { |
| 82 | + $product = $this->productRepository->get('configurable'); |
| 83 | + $this->productRuleIndexer->executeList([$product->getId()]); |
| 84 | + |
| 85 | + $product = $this->productRepository->get('simple_10'); |
| 86 | + $price = $this->getCatalogRulePrice($product); |
| 87 | + $this->assertEquals(5, $price); |
| 88 | + } |
| 89 | + |
| 90 | + public function testExecuteFull(): void |
| 91 | + { |
| 92 | + $this->productRuleIndexer->executeFull(); |
| 93 | + |
| 94 | + $product = $this->productRepository->get('simple_10'); |
| 95 | + $price = $this->getCatalogRulePrice($product); |
| 96 | + $this->assertEquals(5, $price); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * @param Product $product |
| 101 | + * @return float|bool |
| 102 | + */ |
| 103 | + private function getCatalogRulePrice(Product $product) |
| 104 | + { |
| 105 | + $catalogRulePrice = $this->priceFactory->create($product, CatalogRulePrice::class, 1); |
| 106 | + $price = $catalogRulePrice->getValue(); |
| 107 | + |
| 108 | + return $price; |
| 109 | + } |
| 110 | +} |
0 commit comments