Skip to content

Commit db6e12b

Browse files
MC-30809: Indexer price calculation for configurable product
1 parent 73314c6 commit db6e12b

File tree

5 files changed

+49
-40
lines changed

5 files changed

+49
-40
lines changed

dev/tests/integration/testsuite/Magento/CatalogRuleConfigurable/Model/Product/Type/Configurable/PriceTest.php

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
class PriceTest extends TestCase
2727
{
2828
/**
29-
* @var ObjectManagerInterface
29+
* @var ObjectManagerInterface
3030
*/
3131
private $objectManager;
3232

@@ -45,6 +45,11 @@ class PriceTest extends TestCase
4545
*/
4646
private $getDataFromIndexTable;
4747

48+
/**
49+
* @var array
50+
*/
51+
private $priceIndexFields = ['price', 'final_price', 'min_price', 'max_price', 'tier_price'];
52+
4853
/**
4954
* @inheritdoc
5055
*/
@@ -63,12 +68,12 @@ protected function setUp()
6368
*/
6469
public function testGetFinalPriceWithCustomOptionAndCatalogRule(): void
6570
{
66-
$this->assertConfigurableProductPrice(
67-
20,
68-
25,
69-
['price' => 10, 'final_price' => 9, 'min_price' => 9, 'max_price' => 9, 'tier_price' => null],
70-
['price' => 20, 'final_price' => 15, 'min_price' => 15, 'max_price' => 15, 'tier_price' => 15]
71-
);
71+
$indexPrices = [
72+
'simple_10' => [10, 9, 9, 9, null],
73+
'simple_20' => [20, 15, 15, 15, 15],
74+
'configurable' => [0, 0, 9, 30, 15],
75+
];
76+
$this->assertConfigurableProductPrice(20, 25, $indexPrices);
7277
}
7378

7479
/**
@@ -77,32 +82,31 @@ public function testGetFinalPriceWithCustomOptionAndCatalogRule(): void
7782
*/
7883
public function testGetFinalPriceWithCustomOptionAndCatalogRulesForChildren(): void
7984
{
80-
$this->assertConfigurableProductPrice(
81-
19.5,
82-
23,
83-
['price' => 10, 'final_price' => 4.5, 'min_price' => 4.5, 'max_price' => 9, 'tier_price' => null],
84-
['price' => 20, 'final_price' => 8, 'min_price' => 8, 'max_price' => 15, 'tier_price' => 15]
85-
);
85+
$indexPrices = [
86+
'simple_10' => [10, 4.5, 4.5, 9, null],
87+
'simple_20' => [20, 8, 8, 15, 15],
88+
'configurable' => [0, 0, 4.5, 23, 15],
89+
];
90+
$this->assertConfigurableProductPrice(19.5, 23, $indexPrices);
8691
}
8792

8893
/**
8994
* Asserts configurable product prices.
9095
*
9196
* @param float $priceWithFirstSimple
9297
* @param float $priceWithSecondSimple
93-
* @param array $firstSimplePrices
94-
* @param array $secondSimplePrices
98+
* @param array $indexPrices
9599
* @return void
96100
*/
97101
private function assertConfigurableProductPrice(
98102
float $priceWithFirstSimple,
99103
float $priceWithSecondSimple,
100-
array $firstSimplePrices,
101-
array $secondSimplePrices
104+
array $indexPrices
102105
): void {
106+
foreach ($indexPrices as $sku => $prices) {
107+
$this->assertIndexTableData($sku, array_combine($this->priceIndexFields, $prices));
108+
}
103109
$configurable = $this->productRepository->get('configurable');
104-
$this->assertIndexTableData('simple_10', $firstSimplePrices);
105-
$this->assertIndexTableData('simple_20', $secondSimplePrices);
106110
//Add tier price option
107111
$optionId = $configurable->getOptions()[0]->getId();
108112
$configurable->addCustomOption(AbstractType::OPTION_PREFIX . $optionId, 'text');
@@ -144,7 +148,7 @@ private function assertIndexTableData(string $sku, array $expectedPrices): void
144148
);
145149
$data = reset($data);
146150
foreach ($expectedPrices as $column => $price) {
147-
$this->assertEquals($price, $data[$column], $column);
151+
$this->assertEquals($price, $data[$column]);
148152
}
149153
}
150154
}

dev/tests/integration/testsuite/Magento/CatalogRuleConfigurable/_files/configurable_product_with_percent_rule_rollback.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,15 @@
1111
use Magento\CatalogRule\Model\Rule;
1212
use Magento\TestFramework\Helper\Bootstrap;
1313

14-
require __DIR__ . '/../../ConfigurableProduct/_files/configurable_product_with_custom_option_and_simple_tier_price_rollback.php';
14+
require __DIR__
15+
. '/../../ConfigurableProduct/_files/'
16+
. 'configurable_product_with_custom_option_and_simple_tier_price_rollback.php';
1517

1618
$objectManager = Bootstrap::getObjectManager();
1719
/** @var CatalogRuleRepositoryInterface $ruleRepository */
1820
$ruleRepository = $objectManager->create(CatalogRuleRepositoryInterface::class);
21+
/** @var IndexBuilder $indexBuilder */
22+
$indexBuilder = $objectManager->get(IndexBuilder::class);
1923
/** @var CollectionFactory $ruleCollectionFactory */
2024
$ruleCollectionFactory = $objectManager->get(CollectionFactory::class);
2125
$ruleCollection = $ruleCollectionFactory->create()
@@ -26,3 +30,4 @@
2630
if ($rule->getId()) {
2731
$ruleRepository->delete($rule);
2832
}
33+
$indexBuilder->reindexFull();

dev/tests/integration/testsuite/Magento/CatalogRuleConfigurable/_files/configurable_product_with_percent_rules_for_children.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
]
4242
);
4343
$ruleRepository->save($firstRule);
44-
/** @var Rule $secondRule */
44+
4545
$secondRule = $ruleFactory->create();
4646
$secondRule->loadPost(
4747
[

dev/tests/integration/testsuite/Magento/CatalogRuleConfigurable/_files/configurable_product_with_percent_rules_for_children_rollback.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
$objectManager = Bootstrap::getObjectManager();
1616
/** @var CatalogRuleRepositoryInterface $ruleRepository */
1717
$ruleRepository = $objectManager->create(CatalogRuleRepositoryInterface::class);
18+
/** @var IndexBuilder $indexBuilder */
19+
$indexBuilder = $objectManager->get(IndexBuilder::class);
1820
/** @var CollectionFactory $ruleCollectionFactory */
1921
$ruleCollectionFactory = $objectManager->get(CollectionFactory::class);
2022
$ruleCollection = $ruleCollectionFactory->create()
@@ -30,3 +32,4 @@
3032
foreach ($ruleCollection as $rule) {
3133
$ruleRepository->delete($rule);
3234
}
35+
$indexBuilder->reindexFull();

dev/tests/integration/testsuite/Magento/ConfigurableProduct/Model/Product/Type/Configurable/PriceTest.php

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
class PriceTest extends TestCase
2525
{
2626
/**
27-
* @var ObjectManagerInterface
27+
* @var ObjectManagerInterface
2828
*/
2929
private $objectManager;
3030

@@ -63,6 +63,10 @@ protected function setUp()
6363
public function testGetFinalPrice(): void
6464
{
6565
$this->assertPrice(10);
66+
$this->assertIndexTableData(
67+
'configurable',
68+
['price' => 0, 'final_price' => 0, 'min_price' => 10, 'max_price' => 20, 'tier_price' => null]
69+
);
6670
$this->assertIndexTableData(
6771
'simple_10',
6872
['price' => 10, 'final_price' => 10, 'min_price' => 10, 'max_price' => 10, 'tier_price' => null]
@@ -115,8 +119,8 @@ public function testGetFinalPriceIncludingExcludingTax(): void
115119
*/
116120
public function testGetFinalPriceWithSelectedSimpleProduct(): void
117121
{
118-
$product = $this->getProduct('configurable');
119-
$product->addCustomOption('simple_product', 20, $this->getProduct('simple_20'));
122+
$product = $this->productRepository->get('configurable');
123+
$product->addCustomOption('simple_product', 20, $this->productRepository->get('simple_20'));
120124
$this->assertPrice(20, $product);
121125
}
122126

@@ -126,7 +130,11 @@ public function testGetFinalPriceWithSelectedSimpleProduct(): void
126130
*/
127131
public function testGetFinalPriceWithCustomOptionAndSimpleTierPrice(): void
128132
{
129-
$configurable = $this->getProduct('configurable');
133+
$configurable = $this->productRepository->get('configurable');
134+
$this->assertIndexTableData(
135+
'configurable',
136+
['price' => 0, 'final_price' => 0, 'min_price' => 9, 'max_price' => 30, 'tier_price' => 15]
137+
);
130138
$this->assertIndexTableData(
131139
'simple_10',
132140
['price' => 10, 'final_price' => 9, 'min_price' => 9, 'max_price' => 9, 'tier_price' => null]
@@ -140,7 +148,7 @@ public function testGetFinalPriceWithCustomOptionAndSimpleTierPrice(): void
140148
$configurable->addCustomOption('option_ids', $optionId);
141149
// First simple special price (9) + Option price (15)
142150
$this->assertPrice(24, $configurable);
143-
$configurable->addCustomOption('simple_product', 20, $this->getProduct('simple_20'));
151+
$configurable->addCustomOption('simple_product', 20, $this->productRepository->get('simple_20'));
144152
// Second simple tier price (15) + Option price (15)
145153
$this->assertPrice(30, $configurable);
146154
}
@@ -155,7 +163,7 @@ public function testGetFinalPriceWithCustomOptionAndSimpleTierPrice(): void
155163
private function assertIndexTableData(string $sku, array $expectedPrices): void
156164
{
157165
$data = $this->getDataFromIndexTable->execute(
158-
(int)$this->getProduct($sku)->getId(),
166+
(int)$this->productRepository->get($sku)->getId(),
159167
Group::NOT_LOGGED_IN_ID
160168
);
161169
$data = reset($data);
@@ -173,22 +181,11 @@ private function assertIndexTableData(string $sku, array $expectedPrices): void
173181
*/
174182
private function assertPrice(float $expectedPrice, ?ProductInterface $product = null): void
175183
{
176-
$product = $product ?: $this->getProduct('configurable');
184+
$product = $product ?: $this->productRepository->get('configurable');
177185
// final price is the lowest price of configurable variations
178186
$this->assertEquals(
179187
round($expectedPrice, 2),
180188
round($this->priceModel->getFinalPrice(1, $product), 2)
181189
);
182190
}
183-
184-
/**
185-
* Loads product by sku.
186-
*
187-
* @param string $sku
188-
* @return ProductInterface
189-
*/
190-
private function getProduct(string $sku): ProductInterface
191-
{
192-
return $this->productRepository->get($sku);
193-
}
194191
}

0 commit comments

Comments
 (0)