BundleRegularPriceTest.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
  8. use Magento\Catalog\Pricing\Price\CustomOptionPrice;
  9. use Magento\Bundle\Model\Product\Price;
  10. class BundleRegularPriceTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /** @var \Magento\Bundle\Pricing\Price\BundleRegularPrice */
  13. protected $regularPrice;
  14. /** @var ObjectManagerHelper */
  15. protected $objectManagerHelper;
  16. /** @var \Magento\Framework\Pricing\SaleableInterface|\PHPUnit_Framework_MockObject_MockObject */
  17. protected $saleableInterfaceMock;
  18. /** @var \Magento\Bundle\Pricing\Adjustment\BundleCalculatorInterface|\PHPUnit_Framework_MockObject_MockObject */
  19. protected $bundleCalculatorMock;
  20. /** @var \Magento\Framework\Pricing\PriceInfo\Base |\PHPUnit_Framework_MockObject_MockObject */
  21. protected $priceInfoMock;
  22. /** @var CustomOptionPrice|\PHPUnit_Framework_MockObject_MockObject */
  23. protected $customOptionPriceMock;
  24. /**
  25. * @var int
  26. */
  27. protected $quantity = 1;
  28. /**
  29. * @var \Magento\Framework\Pricing\PriceCurrencyInterface|\PHPUnit_Framework_MockObject_MockObject
  30. */
  31. protected $priceCurrencyMock;
  32. /**
  33. * @return void
  34. */
  35. protected function setUp()
  36. {
  37. $this->saleableInterfaceMock = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
  38. ->disableOriginalConstructor()
  39. ->setMethods(['getPriceInfo', 'getPriceType', 'getPrice'])
  40. ->getMock();
  41. $this->bundleCalculatorMock = $this->createMock(
  42. \Magento\Bundle\Pricing\Adjustment\BundleCalculatorInterface::class
  43. );
  44. $this->priceInfoMock = $this->createMock(\Magento\Framework\Pricing\PriceInfo\Base::class);
  45. $this->customOptionPriceMock = $this->getMockBuilder(\Magento\Catalog\Pricing\Price\CustomOptionPrice::class)
  46. ->disableOriginalConstructor()
  47. ->getMock();
  48. $this->saleableInterfaceMock->expects($this->once())
  49. ->method('getPriceInfo')
  50. ->will($this->returnValue($this->priceInfoMock));
  51. $this->priceCurrencyMock = $this->createMock(\Magento\Framework\Pricing\PriceCurrencyInterface::class);
  52. $this->objectManagerHelper = new ObjectManagerHelper($this);
  53. $this->regularPrice = new \Magento\Bundle\Pricing\Price\BundleRegularPrice(
  54. $this->saleableInterfaceMock,
  55. $this->quantity,
  56. $this->bundleCalculatorMock,
  57. $this->priceCurrencyMock
  58. );
  59. }
  60. public function testGetAmount()
  61. {
  62. $expectedResult = 5;
  63. $this->saleableInterfaceMock->expects($this->once())
  64. ->method('getPrice')
  65. ->will($this->returnValue($expectedResult));
  66. $this->bundleCalculatorMock->expects($this->once())
  67. ->method('getMinRegularAmount')
  68. ->with($expectedResult, $this->saleableInterfaceMock)
  69. ->will($this->returnValue($expectedResult));
  70. $this->priceCurrencyMock->expects($this->once())
  71. ->method('convertAndRound')
  72. ->will($this->returnArgument(0));
  73. $result = $this->regularPrice->getAmount();
  74. $this->assertEquals($expectedResult, $result, 'Incorrect amount');
  75. //Calling a second time, should use cached value
  76. $result = $this->regularPrice->getAmount();
  77. $this->assertEquals($expectedResult, $result, 'Incorrect amount');
  78. }
  79. public function testGetMaximalPrice()
  80. {
  81. $expectedResult = 5;
  82. $this->saleableInterfaceMock->expects($this->once())
  83. ->method('getPrice')
  84. ->will($this->returnValue($expectedResult));
  85. $this->bundleCalculatorMock->expects($this->once())
  86. ->method('getMaxRegularAmount')
  87. ->with($expectedResult, $this->saleableInterfaceMock)
  88. ->will($this->returnValue($expectedResult));
  89. $this->priceCurrencyMock->expects($this->once())
  90. ->method('convertAndRound')
  91. ->will($this->returnArgument(0));
  92. $result = $this->regularPrice->getMaximalPrice();
  93. $this->assertEquals($expectedResult, $result, 'Incorrect amount');
  94. //Calling a second time, should use cached value
  95. $result = $this->regularPrice->getMaximalPrice();
  96. $this->assertEquals($expectedResult, $result, 'Incorrect amount the second time');
  97. }
  98. public function testGetMaximalPriceForFixedPriceBundleWithOption()
  99. {
  100. $price = 5;
  101. $maxOptionPrice = 2;
  102. $expectedPrice = $price + $maxOptionPrice;
  103. $this->priceInfoMock->expects($this->atLeastOnce())
  104. ->method('getPrice')
  105. ->with(CustomOptionPrice::PRICE_CODE)
  106. ->willReturn($this->customOptionPriceMock);
  107. $this->customOptionPriceMock->expects($this->once())
  108. ->method('getCustomOptionRange')
  109. ->with(false)
  110. ->willReturn($maxOptionPrice);
  111. $this->saleableInterfaceMock->expects($this->once())
  112. ->method('getPriceType')
  113. ->willReturn(Price::PRICE_TYPE_FIXED);
  114. $this->saleableInterfaceMock->expects($this->once())
  115. ->method('getPrice')
  116. ->will($this->returnValue($price));
  117. $this->bundleCalculatorMock->expects($this->once())
  118. ->method('getMaxRegularAmount')
  119. ->with($expectedPrice, $this->saleableInterfaceMock)
  120. ->will($this->returnValue($expectedPrice));
  121. $this->priceCurrencyMock->expects($this->once())
  122. ->method('convertAndRound')
  123. ->will($this->returnArgument(0));
  124. $result = $this->regularPrice->getMaximalPrice();
  125. $this->assertEquals($expectedPrice, $result, 'Incorrect amount');
  126. //Calling a second time, should use cached value
  127. $result = $this->regularPrice->getMaximalPrice();
  128. $this->assertEquals($expectedPrice, $result, 'Incorrect amount the second time');
  129. }
  130. public function testGetMinimalPrice()
  131. {
  132. $expectedResult = 5;
  133. $this->saleableInterfaceMock->expects($this->once())
  134. ->method('getPrice')
  135. ->will($this->returnValue($expectedResult));
  136. $this->priceCurrencyMock->expects($this->once())
  137. ->method('convertAndRound')
  138. ->will($this->returnArgument(0));
  139. $this->bundleCalculatorMock->expects($this->once())
  140. ->method('getMinRegularAmount')
  141. ->with($expectedResult, $this->saleableInterfaceMock)
  142. ->will($this->returnValue($expectedResult));
  143. $result = $this->regularPrice->getMinimalPrice();
  144. $this->assertEquals($expectedResult, $result, 'Incorrect amount');
  145. //Calling a second time, should use cached value
  146. $result = $this->regularPrice->getMinimalPrice();
  147. $this->assertEquals($expectedResult, $result, 'Incorrect amount the second time');
  148. }
  149. public function testGetMinimalPriceForFixedPricedBundleWithOptions()
  150. {
  151. $price = 5;
  152. $minOptionPrice = 1;
  153. $expectedValue = $price + $minOptionPrice;
  154. $this->saleableInterfaceMock->expects($this->once())
  155. ->method('getPrice')
  156. ->will($this->returnValue($price));
  157. $this->saleableInterfaceMock->expects($this->once())
  158. ->method('getPriceType')
  159. ->willReturn(Price::PRICE_TYPE_FIXED);
  160. $this->priceInfoMock->expects($this->atLeastOnce())
  161. ->method('getPrice')
  162. ->with(CustomOptionPrice::PRICE_CODE)
  163. ->willReturn($this->customOptionPriceMock);
  164. $this->customOptionPriceMock->expects($this->once())
  165. ->method('getCustomOptionRange')
  166. ->with(true)
  167. ->willReturn($minOptionPrice);
  168. $this->priceCurrencyMock->expects($this->once())
  169. ->method('convertAndRound')
  170. ->will($this->returnArgument(0));
  171. $this->bundleCalculatorMock->expects($this->once())
  172. ->method('getMinRegularAmount')
  173. ->with($expectedValue, $this->saleableInterfaceMock)
  174. ->will($this->returnValue($expectedValue));
  175. $result = $this->regularPrice->getMinimalPrice();
  176. $this->assertEquals($expectedValue, $result, 'Incorrect amount');
  177. //Calling a second time, should use cached value
  178. $result = $this->regularPrice->getMinimalPrice();
  179. $this->assertEquals($expectedValue, $result, 'Incorrect amount the second time');
  180. }
  181. }