Skip to content

Commit 98846c7

Browse files
committed
Merge branch 'ACP2E-2799' of https://github.com/adobe-commerce-tier-4/magento2ce into 2.4-develop
2 parents 748c6b9 + 124997d commit 98846c7

File tree

2 files changed

+65
-17
lines changed

2 files changed

+65
-17
lines changed

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,21 @@
77
namespace Magento\Bundle\Pricing\Price;
88

99
use Magento\Catalog\Model\Product;
10+
use Magento\Framework\Pricing\PriceCurrencyInterface;
1011

1112
/**
12-
* Class DiscountCalculator
13+
* Check the product available discount and apply the correct discount to the price
1314
*/
1415
class DiscountCalculator
1516
{
17+
18+
/**
19+
* @param PriceCurrencyInterface $priceCurrency
20+
*/
21+
public function __construct(private readonly PriceCurrencyInterface $priceCurrency)
22+
{
23+
}
24+
1625
/**
1726
* Apply percentage discount
1827
*
@@ -32,6 +41,7 @@ public function calculateDiscount(Product $product, $value = null)
3241
$discount = min($price->getDiscountPercent(), $discount ?: $price->getDiscountPercent());
3342
}
3443
}
35-
return (null !== $discount) ? $discount/100 * $value : $value;
44+
return (null !== $discount) ?
45+
$this->priceCurrency->roundPrice($discount/100 * $value, 2) : $value;
3646
}
3747
}

app/code/Magento/Bundle/Test/Unit/Pricing/Price/DiscountCalculatorTest.php

Lines changed: 53 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
use Magento\Bundle\Pricing\Price\DiscountProviderInterface;
1212
use Magento\Catalog\Model\Product;
1313
use Magento\Catalog\Pricing\Price\FinalPrice;
14+
use Magento\Framework\Pricing\PriceCurrencyInterface;
1415
use Magento\Framework\Pricing\PriceInfo\Base;
16+
use Magento\Framework\Pricing\Price\PriceInterface;
1517
use PHPUnit\Framework\MockObject\MockObject;
1618
use PHPUnit\Framework\TestCase;
1719

@@ -42,21 +44,31 @@ class DiscountCalculatorTest extends TestCase
4244
*/
4345
protected $priceMock;
4446

47+
/**
48+
* @var PriceCurrencyInterface|MockObject
49+
*/
50+
private $priceCurrencyMock;
51+
4552
/**
4653
* Test setUp
4754
*/
4855
protected function setUp(): void
4956
{
5057
$this->productMock = $this->createMock(Product::class);
51-
$this->priceInfoMock = $this->createPartialMock(
52-
Base::class,
53-
['getPrice', 'getPrices']
54-
);
58+
$this->priceInfoMock = $this->getMockBuilder(Base::class)
59+
->disableOriginalConstructor()
60+
->onlyMethods(['getPrice', 'getPrices'])
61+
->addMethods(['getValue'])
62+
->getMock();
5563
$this->finalPriceMock = $this->createMock(FinalPrice::class);
5664
$this->priceMock = $this->getMockForAbstractClass(
5765
DiscountProviderInterface::class
5866
);
59-
$this->calculator = new DiscountCalculator();
67+
$this->priceCurrencyMock = $this->getMockBuilder(PriceCurrencyInterface::class)
68+
->disableOriginalConstructor()
69+
->addMethods(['roundPrice'])
70+
->getMockForAbstractClass();
71+
$this->calculator = new DiscountCalculator($this->priceCurrencyMock);
6072
}
6173

6274
/**
@@ -98,26 +110,52 @@ public function testCalculateDiscountWithDefaultAmount()
98110
$this->getPriceMock(40),
99111
]
100112
);
113+
$this->priceCurrencyMock->expects($this->once())
114+
->method('roundPrice')
115+
->willReturn(20);
101116
$this->assertEquals(20, $this->calculator->calculateDiscount($this->productMock));
102117
}
103118

104119
/**
105120
* test method calculateDiscount with custom price amount
121+
*
122+
* @dataProvider providerForWithDifferentAmount
106123
*/
107-
public function testCalculateDiscountWithCustomAmount()
124+
public function testCalculateDiscountWithCustomAmount(mixed $discount, mixed $value, float $expectedResult)
108125
{
109-
$this->productMock->expects($this->once())
126+
$this->productMock->expects($this->any())
110127
->method('getPriceInfo')
111128
->willReturn($this->priceInfoMock);
112-
$this->priceInfoMock->expects($this->once())
129+
$this->priceInfoMock->expects($this->any())
113130
->method('getPrices')
114-
->willReturn(
115-
[
116-
$this->getPriceMock(30),
117-
$this->getPriceMock(20),
118-
$this->getPriceMock(40),
119-
]
131+
->willReturn([$this->getPriceMock($discount)]);
132+
if ($value === null) {
133+
$abstractPriceMock = $this->getMockForAbstractClass(
134+
PriceInterface::class
120135
);
121-
$this->assertEquals(10, $this->calculator->calculateDiscount($this->productMock, 50));
136+
$this->priceInfoMock->expects($this->any())
137+
->method('getPrice')
138+
->willReturn($abstractPriceMock);
139+
$abstractPriceMock->expects($this->any())
140+
->method('getValue')
141+
->willReturn($expectedResult);
142+
}
143+
$this->priceCurrencyMock->expects($this->any())
144+
->method('roundPrice')
145+
->willReturn($expectedResult);
146+
$this->assertEquals($expectedResult, $this->calculator->calculateDiscount($this->productMock, $value));
147+
}
148+
149+
/**
150+
* @return array
151+
*/
152+
public function providerForWithDifferentAmount()
153+
{
154+
return [
155+
'test case 1 with discount amount' => [20, 50, 10],
156+
'test case 2 for null discount amount' => [null, 30, 30],
157+
'test case 3 with discount amount' => [99, 5.5, 5.45],
158+
'test case 4 with null value' => [50, null, 50]
159+
];
122160
}
123161
}

0 commit comments

Comments
 (0)