Skip to content

Commit 8300ada

Browse files
authored
ENGCOM-9161: Inheriting from a class that doesn't exist#33334 #33449
2 parents af67525 + 7843399 commit 8300ada

File tree

2 files changed

+196
-7
lines changed

2 files changed

+196
-7
lines changed

app/code/Magento/Bundle/Pricing/Price/ConfiguredPrice.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,20 @@ class ConfiguredPrice extends CatalogPrice\FinalPrice implements ConfiguredPrice
4949
*/
5050
private $configuredPriceSelection;
5151

52+
/**
53+
* @var DiscountCalculator
54+
*/
55+
private $discountCalculator;
56+
5257
/**
5358
* @param Product $saleableItem
5459
* @param float $quantity
5560
* @param BundleCalculatorInterface $calculator
5661
* @param PriceCurrencyInterface $priceCurrency
57-
* @param ItemInterface $item
62+
* @param ItemInterface|null $item
5863
* @param JsonSerializer|null $serializer
5964
* @param ConfiguredPriceSelection|null $configuredPriceSelection
65+
* @param DiscountCalculator|null $discountCalculator
6066
*/
6167
public function __construct(
6268
Product $saleableItem,
@@ -65,14 +71,17 @@ public function __construct(
6571
PriceCurrencyInterface $priceCurrency,
6672
ItemInterface $item = null,
6773
JsonSerializer $serializer = null,
68-
ConfiguredPriceSelection $configuredPriceSelection = null
74+
ConfiguredPriceSelection $configuredPriceSelection = null,
75+
DiscountCalculator $discountCalculator = null
6976
) {
7077
$this->item = $item;
7178
$this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance()
7279
->get(JsonSerializer::class);
7380
$this->configuredPriceSelection = $configuredPriceSelection
7481
?: \Magento\Framework\App\ObjectManager::getInstance()
7582
->get(ConfiguredPriceSelection::class);
83+
$this->discountCalculator = $discountCalculator
84+
?: \Magento\Framework\App\ObjectManager::getInstance()->get(DiscountCalculator::class);
7685
parent::__construct($saleableItem, $quantity, $calculator, $priceCurrency);
7786
}
7887

@@ -144,12 +153,9 @@ public function getConfiguredAmount($baseValue = 0.)
144153
*/
145154
public function getValue()
146155
{
147-
if ($this->item) {
156+
if ($this->item && $this->item->getProduct()->getId()) {
148157
$configuredOptionsAmount = $this->getConfiguredAmount()->getBaseAmount();
149-
return parent::getValue() +
150-
$this->priceInfo
151-
->getPrice(BundleDiscountPrice::PRICE_CODE)
152-
->calculateDiscount($configuredOptionsAmount);
158+
return parent::getValue() + $this->discountCalculator->calculateDiscount($this->item->getProduct(), $configuredOptionsAmount);
153159
}
154160
return parent::getValue();
155161
}
Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
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\Bundle\Test\Unit\Pricing\Price;
9+
10+
use Magento\Bundle\Pricing\Price\DiscountCalculator;
11+
use Magento\Catalog\Model\Product;
12+
use Magento\Catalog\Model\Product\Configuration\Item\ItemInterface;
13+
use Magento\Bundle\Pricing\Price\ConfiguredPrice;
14+
use Magento\Bundle\Pricing\Adjustment\Calculator;
15+
use Magento\Catalog\Pricing\Price\ConfiguredPriceSelection;
16+
use Magento\Framework\DataObject;
17+
use Magento\Framework\Pricing\Amount\AmountInterface;
18+
use Magento\Framework\Pricing\Price\PriceInterface;
19+
use Magento\Framework\Pricing\PriceCurrencyInterface;
20+
use Magento\Framework\Pricing\PriceInfo\Base;
21+
use Magento\Framework\Serialize\Serializer\Json;
22+
use PHPUnit\Framework\MockObject\MockObject;
23+
use PHPUnit\Framework\TestCase;
24+
25+
/**
26+
* Test for \Magento\Bundle\Pricing\Price\ConfiguredPrice
27+
*/
28+
class ConfiguredPriceTest extends TestCase
29+
{
30+
/**
31+
* @var float
32+
*/
33+
private $basePriceValue = 100.00;
34+
35+
/**
36+
* @var ItemInterface|MockObject
37+
*/
38+
private $itemMock;
39+
40+
/**
41+
* @var Product|MockObject
42+
*/
43+
private $productMock;
44+
45+
/**
46+
* @var Calculator|MockObject
47+
*/
48+
private $calculatorMock;
49+
50+
/**
51+
* @var Base|MockObject
52+
*/
53+
private $priceInfoMock;
54+
55+
/**
56+
* @var ConfiguredPrice
57+
*/
58+
private $model;
59+
60+
/**
61+
* @var PriceCurrencyInterface|MockObject
62+
*/
63+
private $priceCurrencyMock;
64+
65+
/**
66+
* @var Json|MockObject
67+
*/
68+
private $jsonSerializerMock;
69+
70+
/**
71+
* @var ConfiguredPriceSelection|MockObject
72+
*/
73+
private $configuredPriceSelectionMock;
74+
75+
/**
76+
* @var AmountInterface|MockObject
77+
*/
78+
private $amountInterfaceMock;
79+
80+
/**
81+
* @var DiscountCalculator|MockObject
82+
*/
83+
private $discountCalculatorMock;
84+
85+
/**
86+
* Initialize base dependencies
87+
*/
88+
protected function setUp(): void
89+
{
90+
$basePrice = $this->getMockForAbstractClass(PriceInterface::class);
91+
$basePrice->expects($this->any())->method('getValue')->willReturn($this->basePriceValue);
92+
93+
$this->priceInfoMock = $this->createMock(Base::class);
94+
$this->priceInfoMock->expects($this->any())->method('getPrice')->willReturn($basePrice);
95+
$this->productMock = $this->getMockBuilder(Product::class)
96+
->setMethods(['getPriceInfo', 'getOptionById', 'getResource', 'getId'])
97+
->disableOriginalConstructor()
98+
->getMock();
99+
$this->productMock->expects($this->once())->method('getPriceInfo')->willReturn($this->priceInfoMock);
100+
$this->priceCurrencyMock = $this->getMockForAbstractClass(PriceCurrencyInterface::class);
101+
102+
$this->jsonSerializerMock = $this->getMockBuilder(Json::class)
103+
->getMock();
104+
$this->configuredPriceSelectionMock = $this->getMockBuilder(ConfiguredPriceSelection::class)
105+
->setMethods(['getSelectionPriceList'])
106+
->disableOriginalConstructor()
107+
->getMock();
108+
$this->configuredPriceSelectionMock->expects($this->any())->method('getSelectionPriceList')
109+
->willReturn($this->prepareAndReturnSelectionPriceDataStub());
110+
$this->amountInterfaceMock = $this->getMockBuilder(AmountInterface::class)->getMock();
111+
$this->amountInterfaceMock->expects($this->any())->method('getBaseAmount')
112+
->willReturn(100.00);
113+
$this->calculatorMock = $this->getMockBuilder(Calculator::class)
114+
->disableOriginalConstructor()
115+
->getMock();
116+
$this->calculatorMock->expects($this->any())->method('calculateBundleAmount')
117+
->willReturn($this->amountInterfaceMock);
118+
$this->discountCalculatorMock = $this->getMockBuilder(DiscountCalculator::class)
119+
->disableOriginalConstructor()
120+
->getMock();
121+
$this->discountCalculatorMock->expects($this->any())->method('calculateDiscount')
122+
->willReturn(-5.00);
123+
$this->model = new ConfiguredPrice(
124+
$this->productMock,
125+
1,
126+
$this->calculatorMock,
127+
$this->priceCurrencyMock,
128+
null,
129+
$this->jsonSerializerMock,
130+
$this->configuredPriceSelectionMock,
131+
$this->discountCalculatorMock,
132+
);
133+
}
134+
135+
/**
136+
* Test of value getter when item presented
137+
*/
138+
public function testGetValueMethod(): void
139+
{
140+
$this->productMock->expects($this->any())->method('getId')->willReturn(123);
141+
$this->itemMock = $this->getMockBuilder(ItemInterface::class)
142+
->getMock();
143+
$this->itemMock->expects($this->any())->method('getProduct')->willReturn($this->productMock);
144+
$this->model->setItem($this->itemMock);
145+
$valueFromMock = $this->model->getValue();
146+
$this->assertEquals(95.00, $valueFromMock);
147+
}
148+
149+
/**
150+
* Test of value getter if no product item
151+
*/
152+
public function testGetValueMethodNoItem(): void
153+
{
154+
$this->productMock = $this->getMockBuilder(Product::class)
155+
->disableOriginalConstructor()
156+
->getMock();
157+
$this->itemMock = $this->getMockBuilder(ItemInterface::class)
158+
->getMock();
159+
$this->itemMock->expects($this->any())->method('getProduct')->willReturn($this->productMock);
160+
$this->productMock->expects($this->any())->method('getId')->willReturn(false);
161+
$this->model->setItem($this->itemMock);
162+
$valueFromMock = $this->model->getValue();
163+
$this->assertEquals(100.00, $valueFromMock);
164+
}
165+
166+
/**
167+
* Stub data for calculation amount of bundle
168+
* @return \Magento\Framework\DataObject[]
169+
*/
170+
private function prepareAndReturnSelectionPriceDataStub(): array
171+
{
172+
$first = new DataObject();
173+
$first->setValue(2);
174+
$first->setQuantity(1);
175+
$second = new DataObject();
176+
$second->setValue(3);
177+
$second->setQuantity(1);
178+
return [
179+
$first,
180+
$second
181+
];
182+
}
183+
}

0 commit comments

Comments
 (0)