Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
BundleSelectionPriceTest.php
Go to the documentation of this file.
1 <?php
8 
11 
17 class BundleSelectionPriceTest extends \PHPUnit\Framework\TestCase
18 {
22  protected $selectionPrice;
23 
27  protected $calculatorMock;
28 
32  protected $productMock;
33 
37  protected $bundleMock;
38 
42  protected $eventManagerMock;
43 
47  protected $priceInfoMock;
48 
52  protected $finalPriceMock;
53 
57  protected $regularPriceMock;
58 
63 
67  protected $priceCurrencyMock;
68 
72  protected $quantity;
73 
77  protected function setUp()
78  {
79  $this->productMock = $this->createPartialMock(
80  \Magento\Catalog\Model\Product::class,
81  ['__wakeup', 'getPriceInfo', 'getSelectionPriceType', 'getSelectionPriceValue']
82  );
83 
84  $this->bundleMock = $this->createPartialMock(
85  \Magento\Catalog\Model\Product::class,
86  ['__wakeup', 'getPriceType', 'getPriceInfo', 'setFinalPrice', 'getData']
87  );
88  $this->calculatorMock = $this->getMockBuilder(\Magento\Framework\Pricing\Adjustment\CalculatorInterface::class)
89  ->getMockForAbstractClass();
90  $this->eventManagerMock = $this->createPartialMock(\Magento\Framework\Event\Manager::class, ['dispatch']);
91  $this->priceInfoMock = $this->createPartialMock(\Magento\Framework\Pricing\PriceInfo\Base::class, ['getPrice']);
92  $this->discountCalculatorMock = $this->createMock(\Magento\Bundle\Pricing\Price\DiscountCalculator::class);
93  $this->finalPriceMock = $this->createMock(\Magento\Catalog\Pricing\Price\FinalPrice::class);
94  $this->regularPriceMock = $this->createMock(\Magento\Catalog\Pricing\Price\RegularPrice::class);
95  $this->productMock->expects($this->atLeastOnce())
96  ->method('getPriceInfo')
97  ->will($this->returnValue($this->priceInfoMock));
98 
99  $this->priceCurrencyMock = $this->createMock(\Magento\Framework\Pricing\PriceCurrencyInterface::class);
100 
101  $this->quantity = 1;
102 
103  $this->setupSelectionPrice();
104  }
105 
109  protected function setupSelectionPrice($useRegularPrice = false)
110  {
111  $this->selectionPrice = new \Magento\Bundle\Pricing\Price\BundleSelectionPrice(
112  $this->productMock,
113  $this->quantity,
114  $this->calculatorMock,
115  $this->priceCurrencyMock,
116  $this->bundleMock,
117  $this->eventManagerMock,
118  $this->discountCalculatorMock,
119  $useRegularPrice
120  );
121  }
122 
130  public function testGetValueTypeDynamic($useRegularPrice)
131  {
132  $this->setupSelectionPrice($useRegularPrice);
133  $priceCode = $useRegularPrice ? RegularPrice::PRICE_CODE : FinalPrice::PRICE_CODE;
134  $regularPrice = 100.125;
135  $discountedPrice = 70.453;
136  $actualPrice = $useRegularPrice ? $regularPrice : $discountedPrice;
137  $expectedPrice = $useRegularPrice ? round($regularPrice, 2) : round($discountedPrice, 2);
138 
139  $this->bundleMock->expects($this->once())
140  ->method('getPriceType')
141  ->will($this->returnValue(\Magento\Bundle\Model\Product\Price::PRICE_TYPE_DYNAMIC));
142  $this->priceInfoMock->expects($this->once())
143  ->method('getPrice')
144  ->with($this->equalTo($priceCode))
145  ->will($this->returnValue($this->finalPriceMock));
146  $this->finalPriceMock->expects($this->once())
147  ->method('getValue')
148  ->will($this->returnValue($actualPrice));
149 
150  if (!$useRegularPrice) {
151  $this->discountCalculatorMock->expects($this->once())
152  ->method('calculateDiscount')
153  ->with(
154  $this->equalTo($this->bundleMock),
155  $this->equalTo($actualPrice)
156  )
157  ->will($this->returnValue($discountedPrice));
158  }
159 
160  $this->priceCurrencyMock->expects($this->once())
161  ->method('round')
162  ->with($actualPrice)
163  ->will($this->returnValue($expectedPrice));
164 
165  $this->assertEquals($expectedPrice, $this->selectionPrice->getValue());
166  }
167 
176  public function testGetValueTypeFixedWithSelectionPriceType(bool $useRegularPrice)
177  {
178  $this->setupSelectionPrice($useRegularPrice);
179  $regularPrice = 100.125;
180  $discountedPrice = 70.453;
181  $actualPrice = $useRegularPrice ? $regularPrice : $discountedPrice;
182  $expectedPrice = $useRegularPrice ? round($regularPrice, 2) : round($discountedPrice, 2);
183 
184  $this->bundleMock->expects($this->once())
185  ->method('getPriceType')
186  ->willReturn(\Magento\Bundle\Model\Product\Price::PRICE_TYPE_FIXED);
187  $this->bundleMock->expects($this->atLeastOnce())
188  ->method('getPriceInfo')
189  ->willReturn($this->priceInfoMock);
190  $this->priceInfoMock->expects($this->once())
191  ->method('getPrice')
193  ->willReturn($this->regularPriceMock);
194  $this->regularPriceMock->expects($this->once())
195  ->method('getValue')
196  ->willReturn($actualPrice);
197  $this->bundleMock->expects($this->once())
198  ->method('setFinalPrice')
199  ->willReturnSelf();
200  $this->eventManagerMock->expects($this->once())
201  ->method('dispatch');
202  $this->bundleMock->expects($this->exactly(2))
203  ->method('getData')
204  ->willReturnMap(
205  [
206  ['qty', null, 1],
207  ['final_price', null, 100],
208  ['price', null, 100],
209  ]
210  );
211  $this->productMock->expects($this->once())
212  ->method('getSelectionPriceType')
213  ->willReturn(true);
214  $this->productMock->expects($this->any())
215  ->method('getSelectionPriceValue')
216  ->willReturn($actualPrice);
217 
218  if (!$useRegularPrice) {
219  $this->discountCalculatorMock->expects($this->once())
220  ->method('calculateDiscount')
221  ->with($this->bundleMock, $actualPrice)
222  ->willReturn($discountedPrice);
223  }
224 
225  $this->priceCurrencyMock->expects($this->once())
226  ->method('round')
227  ->with($actualPrice)
228  ->willReturn($expectedPrice);
229 
230  $this->assertEquals($expectedPrice, $this->selectionPrice->getValue());
231  }
232 
239  public function testGetValueTypeFixedWithoutSelectionPriceType($useRegularPrice)
240  {
241  $this->setupSelectionPrice($useRegularPrice);
242  $regularPrice = 100.125;
243  $discountedPrice = 70.453;
244  $convertedValue = 100.247;
245  $actualPrice = $useRegularPrice ? $convertedValue : $discountedPrice;
246  $expectedPrice = $useRegularPrice ? round($convertedValue, 2) : round($discountedPrice, 2);
247 
248  $this->bundleMock->expects($this->once())
249  ->method('getPriceType')
250  ->will($this->returnValue(\Magento\Bundle\Model\Product\Price::PRICE_TYPE_FIXED));
251  $this->productMock->expects($this->once())
252  ->method('getSelectionPriceType')
253  ->will($this->returnValue(false));
254  $this->productMock->expects($this->any())
255  ->method('getSelectionPriceValue')
256  ->will($this->returnValue($regularPrice));
257 
258  $this->priceCurrencyMock->expects($this->once())
259  ->method('convert')
260  ->with($regularPrice)
261  ->will($this->returnValue($convertedValue));
262 
263  if (!$useRegularPrice) {
264  $this->discountCalculatorMock->expects($this->once())
265  ->method('calculateDiscount')
266  ->with(
267  $this->equalTo($this->bundleMock),
268  $this->equalTo($convertedValue)
269  )
270  ->will($this->returnValue($discountedPrice));
271  }
272 
273  $this->priceCurrencyMock->expects($this->once())
274  ->method('round')
275  ->with($actualPrice)
276  ->will($this->returnValue($expectedPrice));
277 
278  $this->assertEquals($expectedPrice, $this->selectionPrice->getValue());
279  }
280 
287  public function testFixedPriceWithMultipleQty($useRegularPrice)
288  {
289  $qty = 2;
290 
291  $selectionPrice = new \Magento\Bundle\Pricing\Price\BundleSelectionPrice(
292  $this->productMock,
293  $qty,
294  $this->calculatorMock,
295  $this->priceCurrencyMock,
296  $this->bundleMock,
297  $this->eventManagerMock,
298  $this->discountCalculatorMock,
299  $useRegularPrice
300  );
301 
302  $this->setupSelectionPrice($useRegularPrice);
303  $regularPrice = 100.125;
304  $discountedPrice = 70.453;
305  $convertedValue = 100.247;
306  $actualPrice = $useRegularPrice ? $convertedValue : $discountedPrice;
307  $expectedPrice = $useRegularPrice ? round($convertedValue, 2) : round($discountedPrice, 2);
308 
309  $this->bundleMock->expects($this->once())
310  ->method('getPriceType')
311  ->will($this->returnValue(\Magento\Bundle\Model\Product\Price::PRICE_TYPE_FIXED));
312  $this->productMock->expects($this->once())
313  ->method('getSelectionPriceType')
314  ->will($this->returnValue(false));
315  $this->productMock->expects($this->any())
316  ->method('getSelectionPriceValue')
317  ->will($this->returnValue($regularPrice));
318 
319  $this->priceCurrencyMock->expects($this->once())
320  ->method('convert')
321  ->with($regularPrice)
322  ->will($this->returnValue($convertedValue));
323 
324  if (!$useRegularPrice) {
325  $this->discountCalculatorMock->expects($this->once())
326  ->method('calculateDiscount')
327  ->with(
328  $this->equalTo($this->bundleMock),
329  $this->equalTo($convertedValue)
330  )
331  ->will($this->returnValue($discountedPrice));
332  }
333 
334  $this->priceCurrencyMock->expects($this->once())
335  ->method('round')
336  ->with($actualPrice)
337  ->will($this->returnValue($expectedPrice));
338 
339  $this->assertEquals($expectedPrice, $selectionPrice->getValue());
340  }
341 
345  public function useRegularPriceDataProvider()
346  {
347  return [
348  'useRegularPrice' => [
349  true,
350  ],
351  'notUseRegularPrice' => [
352  false,
353  ],
354  ];
355  }
356 
357  public function testGetProductFixedBundle()
358  {
359  $this->bundleMock->expects($this->any())
360  ->method('getPriceType')
361  ->will($this->returnValue(\Magento\Bundle\Model\Product\Price::PRICE_TYPE_FIXED));
362  $product = $this->selectionPrice->getProduct();
363  $this->assertEquals($this->bundleMock, $product);
364  }
365 
366  public function testGetProductDynamicBundle()
367  {
368  $this->bundleMock->expects($this->any())
369  ->method('getPriceType')
370  ->will($this->returnValue(\Magento\Bundle\Model\Product\Price::PRICE_TYPE_DYNAMIC));
371  $product = $this->selectionPrice->getProduct();
372  $this->assertEquals($this->productMock, $product);
373  }
374 
375  public function testGetAmount()
376  {
377  $this->setupSelectionPrice();
378 
379  $price = 10.;
380  $amount = 20.;
381 
382  $this->priceInfoMock->expects($this->once())
383  ->method('getPrice')
384  ->with(\Magento\Bundle\Pricing\Price\FinalPrice::PRICE_CODE)
385  ->willReturn($this->finalPriceMock);
386 
387  $this->finalPriceMock->expects($this->once())
388  ->method('getValue')
389  ->willReturn($price);
390 
391  $this->discountCalculatorMock->expects($this->once())
392  ->method('calculateDiscount')
393  ->with($this->bundleMock, $price)
394  ->willReturn($price);
395 
396  $this->priceCurrencyMock->expects($this->once())
397  ->method('round')
398  ->with($price)
399  ->willReturn($price);
400 
401  $this->bundleMock->expects($this->any())
402  ->method('getPriceType')
403  ->willReturn(\Magento\Bundle\Model\Product\Price::PRICE_TYPE_DYNAMIC);
404 
405  $this->calculatorMock->expects($this->once())
406  ->method('getAmount')
407  ->with($price, $this->productMock, null)
408  ->willReturn($amount);
409 
410  $this->assertEquals($amount, $this->selectionPrice->getAmount());
411  }
412 }
$price
$amount
Definition: order.php:14