|
11 | 11 | use Magento\Bundle\Pricing\Price\DiscountProviderInterface;
|
12 | 12 | use Magento\Catalog\Model\Product;
|
13 | 13 | use Magento\Catalog\Pricing\Price\FinalPrice;
|
| 14 | +use Magento\Framework\Pricing\PriceCurrencyInterface; |
14 | 15 | use Magento\Framework\Pricing\PriceInfo\Base;
|
| 16 | +use Magento\Framework\Pricing\Price\PriceInterface; |
15 | 17 | use PHPUnit\Framework\MockObject\MockObject;
|
16 | 18 | use PHPUnit\Framework\TestCase;
|
17 | 19 |
|
@@ -42,21 +44,31 @@ class DiscountCalculatorTest extends TestCase
|
42 | 44 | */
|
43 | 45 | protected $priceMock;
|
44 | 46 |
|
| 47 | + /** |
| 48 | + * @var PriceCurrencyInterface|MockObject |
| 49 | + */ |
| 50 | + private $priceCurrencyMock; |
| 51 | + |
45 | 52 | /**
|
46 | 53 | * Test setUp
|
47 | 54 | */
|
48 | 55 | protected function setUp(): void
|
49 | 56 | {
|
50 | 57 | $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(); |
55 | 63 | $this->finalPriceMock = $this->createMock(FinalPrice::class);
|
56 | 64 | $this->priceMock = $this->getMockForAbstractClass(
|
57 | 65 | DiscountProviderInterface::class
|
58 | 66 | );
|
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); |
60 | 72 | }
|
61 | 73 |
|
62 | 74 | /**
|
@@ -98,26 +110,52 @@ public function testCalculateDiscountWithDefaultAmount()
|
98 | 110 | $this->getPriceMock(40),
|
99 | 111 | ]
|
100 | 112 | );
|
| 113 | + $this->priceCurrencyMock->expects($this->once()) |
| 114 | + ->method('roundPrice') |
| 115 | + ->willReturn(20); |
101 | 116 | $this->assertEquals(20, $this->calculator->calculateDiscount($this->productMock));
|
102 | 117 | }
|
103 | 118 |
|
104 | 119 | /**
|
105 | 120 | * test method calculateDiscount with custom price amount
|
| 121 | + * |
| 122 | + * @dataProvider providerForWithDifferentAmount |
106 | 123 | */
|
107 |
| - public function testCalculateDiscountWithCustomAmount() |
| 124 | + public function testCalculateDiscountWithCustomAmount(mixed $discount, mixed $value, float $expectedResult) |
108 | 125 | {
|
109 |
| - $this->productMock->expects($this->once()) |
| 126 | + $this->productMock->expects($this->any()) |
110 | 127 | ->method('getPriceInfo')
|
111 | 128 | ->willReturn($this->priceInfoMock);
|
112 |
| - $this->priceInfoMock->expects($this->once()) |
| 129 | + $this->priceInfoMock->expects($this->any()) |
113 | 130 | ->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 |
120 | 135 | );
|
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 | + ]; |
122 | 160 | }
|
123 | 161 | }
|
0 commit comments