Skip to content

Commit 1f80359

Browse files
committed
Merge branch 'MAGETWO-98366' into 2.3-develop-pr18
2 parents 7421614 + 1ad1611 commit 1f80359

File tree

2 files changed

+107
-21
lines changed
  • app/code/Magento/ConfigurableProduct

2 files changed

+107
-21
lines changed

app/code/Magento/ConfigurableProduct/Model/Product/Type/Configurable/Price.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,26 @@
77
*/
88
namespace Magento\ConfigurableProduct\Model\Product\Type\Configurable;
99

10+
use Magento\Catalog\Model\Product;
11+
12+
/**
13+
* Class Price for configurable product
14+
*/
1015
class Price extends \Magento\Catalog\Model\Product\Type\Price
1116
{
1217
/**
13-
* Get product final price
14-
*
15-
* @param float $qty
16-
* @param \Magento\Catalog\Model\Product $product
17-
* @return float
18+
* @inheritdoc
1819
*/
1920
public function getFinalPrice($qty, $product)
2021
{
2122
if ($qty === null && $product->getCalculatedFinalPrice() !== null) {
2223
return $product->getCalculatedFinalPrice();
2324
}
2425
if ($product->getCustomOption('simple_product') && $product->getCustomOption('simple_product')->getProduct()) {
25-
$finalPrice = parent::getFinalPrice($qty, $product->getCustomOption('simple_product')->getProduct());
26+
/** @var Product $simpleProduct */
27+
$simpleProduct = $product->getCustomOption('simple_product')->getProduct();
28+
$simpleProduct->setCustomerGroupId($product->getCustomerGroupId());
29+
$finalPrice = parent::getFinalPrice($qty, $simpleProduct);
2630
} else {
2731
$priceInfo = $product->getPriceInfo();
2832
$finalPrice = $priceInfo->getPrice('final_price')->getAmount()->getValue();
@@ -35,7 +39,7 @@ public function getFinalPrice($qty, $product)
3539
}
3640

3741
/**
38-
* {@inheritdoc}
42+
* @inheritdoc
3943
*/
4044
public function getPrice($product)
4145
{
@@ -48,6 +52,7 @@ public function getPrice($product)
4852
}
4953
}
5054
}
55+
5156
return 0;
5257
}
5358
}

app/code/Magento/ConfigurableProduct/Test/Unit/Model/Product/Type/Configurable/PriceTest.php

Lines changed: 95 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,52 +6,77 @@
66

77
namespace Magento\ConfigurableProduct\Test\Unit\Model\Product\Type\Configurable;
88

9+
use Magento\Catalog\Model\Product;
10+
use Magento\Catalog\Model\Product\Configuration\Item\Option;
11+
use Magento\ConfigurableProduct\Model\Product\Type\Configurable\Price as ConfigurablePrice;
12+
use Magento\Framework\Event\ManagerInterface;
13+
use Magento\Framework\Pricing\Amount\AmountInterface;
14+
use Magento\Framework\Pricing\Price\PriceInterface;
15+
use Magento\Framework\Pricing\PriceInfo\Base as PriceInfoBase;
916
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
17+
use PHPUnit\Framework\MockObject\MockObject;
1018

1119
class PriceTest extends \PHPUnit\Framework\TestCase
1220
{
13-
/** @var \Magento\ConfigurableProduct\Model\Product\Type\Configurable\Price */
21+
/**
22+
* @var ObjectManagerHelper
23+
*/
24+
protected $objectManagerHelper;
25+
26+
/**
27+
* @var ConfigurablePrice
28+
*/
1429
protected $model;
1530

16-
/** @var ObjectManagerHelper */
17-
protected $objectManagerHelper;
31+
/**
32+
* @var ManagerInterface|MockObject
33+
*/
34+
private $eventManagerMock;
1835

36+
/**
37+
* @inheritdoc
38+
*/
1939
protected function setUp()
2040
{
2141
$this->objectManagerHelper = new ObjectManagerHelper($this);
2242

43+
$this->eventManagerMock = $this->createPartialMock(
44+
ManagerInterface::class,
45+
['dispatch']
46+
);
2347
$this->model = $this->objectManagerHelper->getObject(
24-
\Magento\ConfigurableProduct\Model\Product\Type\Configurable\Price::class
48+
ConfigurablePrice::class,
49+
['eventManager' => $this->eventManagerMock]
2550
);
2651
}
2752

2853
public function testGetFinalPrice()
2954
{
3055
$finalPrice = 10;
3156
$qty = 1;
32-
$configurableProduct = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
33-
->disableOriginalConstructor()
34-
->setMethods(['getCustomOption', 'getPriceInfo', 'setFinalPrice', '__wakeUp'])
35-
->getMock();
36-
$customOption = $this->getMockBuilder(\Magento\Catalog\Model\Product\Configuration\Item\Option::class)
57+
58+
/** @var Product|MockObject $configurableProduct */
59+
$configurableProduct = $this->getMockBuilder(Product::class)
3760
->disableOriginalConstructor()
38-
->setMethods(['getProduct'])
61+
->setMethods(['getCustomOption', 'getPriceInfo', 'setFinalPrice'])
3962
->getMock();
40-
$priceInfo = $this->getMockBuilder(\Magento\Framework\Pricing\PriceInfo\Base::class)
63+
/** @var PriceInfoBase|MockObject $priceInfo */
64+
$priceInfo = $this->getMockBuilder(PriceInfoBase::class)
4165
->disableOriginalConstructor()
4266
->setMethods(['getPrice'])
4367
->getMock();
44-
$price = $this->getMockBuilder(\Magento\Framework\Pricing\Price\PriceInterface::class)
68+
/** @var PriceInterface|MockObject $price */
69+
$price = $this->getMockBuilder(PriceInterface::class)
4570
->disableOriginalConstructor()
4671
->getMock();
47-
$amount = $this->getMockBuilder(\Magento\Framework\Pricing\Amount\AmountInterface::class)
72+
/** @var AmountInterface|MockObject $amount */
73+
$amount = $this->getMockBuilder(AmountInterface::class)
4874
->disableOriginalConstructor()
4975
->getMock();
5076

5177
$configurableProduct->expects($this->any())
5278
->method('getCustomOption')
5379
->willReturnMap([['simple_product', false], ['option_ids', false]]);
54-
$customOption->expects($this->never())->method('getProduct');
5580
$configurableProduct->expects($this->once())->method('getPriceInfo')->willReturn($priceInfo);
5681
$priceInfo->expects($this->once())->method('getPrice')->with('final_price')->willReturn($price);
5782
$price->expects($this->once())->method('getAmount')->willReturn($amount);
@@ -60,4 +85,60 @@ public function testGetFinalPrice()
6085

6186
$this->assertEquals($finalPrice, $this->model->getFinalPrice($qty, $configurableProduct));
6287
}
88+
89+
public function testGetFinalPriceWithSimpleProduct()
90+
{
91+
$finalPrice = 10;
92+
$qty = 1;
93+
$customerGroupId = 1;
94+
95+
/** @var Product|MockObject $configurableProduct */
96+
$configurableProduct = $this->createPartialMock(
97+
Product::class,
98+
['getCustomOption', 'setFinalPrice', 'getCustomerGroupId']
99+
);
100+
/** @var Option|MockObject $customOption */
101+
$customOption = $this->createPartialMock(
102+
Option::class,
103+
['getProduct']
104+
);
105+
/** @var Product|MockObject $simpleProduct */
106+
$simpleProduct = $this->createPartialMock(
107+
Product::class,
108+
['setCustomerGroupId', 'setFinalPrice', 'getPrice', 'getTierPrice', 'getData', 'getCustomOption']
109+
);
110+
111+
$configurableProduct->method('getCustomOption')
112+
->willReturnMap([
113+
['simple_product', $customOption],
114+
['option_ids', false]
115+
]);
116+
$configurableProduct->method('getCustomerGroupId')->willReturn($customerGroupId);
117+
$configurableProduct->expects($this->atLeastOnce())
118+
->method('setFinalPrice')
119+
->with($finalPrice)
120+
->willReturnSelf();
121+
$customOption->method('getProduct')->willReturn($simpleProduct);
122+
$simpleProduct->expects($this->atLeastOnce())
123+
->method('setCustomerGroupId')
124+
->with($customerGroupId)
125+
->willReturnSelf();
126+
$simpleProduct->method('getPrice')->willReturn($finalPrice);
127+
$simpleProduct->method('getTierPrice')->with($qty)->willReturn($finalPrice);
128+
$simpleProduct->expects($this->atLeastOnce())
129+
->method('setFinalPrice')
130+
->with($finalPrice)
131+
->willReturnSelf();
132+
$simpleProduct->method('getData')->with('final_price')->willReturn($finalPrice);
133+
$simpleProduct->method('getCustomOption')->with('option_ids')->willReturn(false);
134+
$this->eventManagerMock->expects($this->once())
135+
->method('dispatch')
136+
->with('catalog_product_get_final_price', ['product' => $simpleProduct, 'qty' => $qty]);
137+
138+
$this->assertEquals(
139+
$finalPrice,
140+
$this->model->getFinalPrice($qty, $configurableProduct),
141+
'The final price calculation is wrong'
142+
);
143+
}
63144
}

0 commit comments

Comments
 (0)