DiscountCalculatorTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Bundle\Test\Unit\Pricing\Price;
  7. /**
  8. * Class DiscountCalculatorTest
  9. */
  10. class DiscountCalculatorTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /**
  13. * @var \Magento\Bundle\Pricing\Price\DiscountCalculator
  14. */
  15. protected $calculator;
  16. /**
  17. * @var \Magento\Catalog\Model\Product|\PHPUnit_Framework_MockObject_MockObject
  18. */
  19. protected $productMock;
  20. /**
  21. * @var \Magento\Framework\Pricing\PriceInfo\Base |\PHPUnit_Framework_MockObject_MockObject
  22. */
  23. protected $priceInfoMock;
  24. /**
  25. * @var \Magento\Catalog\Pricing\Price\FinalPrice|\PHPUnit_Framework_MockObject_MockObject
  26. */
  27. protected $finalPriceMock;
  28. /**
  29. * @var \Magento\Bundle\Pricing\Price\DiscountProviderInterface|\PHPUnit_Framework_MockObject_MockObject
  30. */
  31. protected $priceMock;
  32. /**
  33. * Test setUp
  34. */
  35. protected function setUp()
  36. {
  37. $this->productMock = $this->createMock(\Magento\Catalog\Model\Product::class);
  38. $this->priceInfoMock = $this->createPartialMock(
  39. \Magento\Framework\Pricing\PriceInfo\Base::class,
  40. ['getPrice', 'getPrices']
  41. );
  42. $this->finalPriceMock = $this->createMock(\Magento\Catalog\Pricing\Price\FinalPrice::class);
  43. $this->priceMock = $this->getMockForAbstractClass(
  44. \Magento\Bundle\Pricing\Price\DiscountProviderInterface::class
  45. );
  46. $this->calculator = new \Magento\Bundle\Pricing\Price\DiscountCalculator();
  47. }
  48. /**
  49. * Returns price mock with specified %
  50. *
  51. * @param int $value
  52. * @return \PHPUnit_Framework_MockObject_MockObject
  53. */
  54. protected function getPriceMock($value)
  55. {
  56. $price = clone $this->priceMock;
  57. $price->expects($this->exactly(3))
  58. ->method('getDiscountPercent')
  59. ->will($this->returnValue($value));
  60. return $price;
  61. }
  62. /**
  63. * test method calculateDiscount with default price amount
  64. */
  65. public function testCalculateDiscountWithDefaultAmount()
  66. {
  67. $this->productMock->expects($this->exactly(2))
  68. ->method('getPriceInfo')
  69. ->will($this->returnValue($this->priceInfoMock));
  70. $this->priceInfoMock->expects($this->once())
  71. ->method('getPrice')
  72. ->with($this->equalTo(\Magento\Catalog\Pricing\Price\FinalPrice::PRICE_CODE))
  73. ->will($this->returnValue($this->finalPriceMock));
  74. $this->finalPriceMock->expects($this->once())
  75. ->method('getValue')
  76. ->will($this->returnValue(100));
  77. $this->priceInfoMock->expects($this->once())
  78. ->method('getPrices')
  79. ->will(
  80. $this->returnValue(
  81. [
  82. $this->getPriceMock(30),
  83. $this->getPriceMock(20),
  84. $this->getPriceMock(40),
  85. ]
  86. )
  87. );
  88. $this->assertEquals(20, $this->calculator->calculateDiscount($this->productMock));
  89. }
  90. /**
  91. * test method calculateDiscount with custom price amount
  92. */
  93. public function testCalculateDiscountWithCustomAmount()
  94. {
  95. $this->productMock->expects($this->once())
  96. ->method('getPriceInfo')
  97. ->will($this->returnValue($this->priceInfoMock));
  98. $this->priceInfoMock->expects($this->once())
  99. ->method('getPrices')
  100. ->will(
  101. $this->returnValue(
  102. [
  103. $this->getPriceMock(30),
  104. $this->getPriceMock(20),
  105. $this->getPriceMock(40),
  106. ]
  107. )
  108. );
  109. $this->assertEquals(10, $this->calculator->calculateDiscount($this->productMock, 50));
  110. }
  111. }