FinalPriceTest.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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\Bundle\Pricing\Price\BundleOptionPrice;
  8. use Magento\Catalog\Pricing\Price\CustomOptionPrice;
  9. use Magento\Bundle\Model\Product\Price;
  10. use Magento\Catalog\Api\ProductCustomOptionRepositoryInterface;
  11. use Magento\Framework\Pricing\PriceCurrencyInterface;
  12. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
  13. /**
  14. * @SuppressWarnings(PHPMD)
  15. */
  16. class FinalPriceTest extends \PHPUnit\Framework\TestCase
  17. {
  18. /** @var \Magento\Bundle\Pricing\Price\FinalPrice */
  19. protected $finalPrice;
  20. /** @var ObjectManagerHelper */
  21. protected $objectManagerHelper;
  22. /** @var \Magento\Framework\Pricing\SaleableInterface|\PHPUnit_Framework_MockObject_MockObject */
  23. protected $saleableInterfaceMock;
  24. /** @var float */
  25. protected $quantity = 1.;
  26. /** @var float*/
  27. protected $baseAmount;
  28. /** @var \Magento\Bundle\Pricing\Adjustment\BundleCalculatorInterface|\PHPUnit_Framework_MockObject_MockObject */
  29. protected $bundleCalculatorMock;
  30. /** @var \Magento\Framework\Pricing\PriceInfo\Base |\PHPUnit_Framework_MockObject_MockObject */
  31. protected $priceInfoMock;
  32. /** @var \Magento\Catalog\Pricing\Price\BasePrice|\PHPUnit_Framework_MockObject_MockObject */
  33. protected $basePriceMock;
  34. /** @var BundleOptionPrice|\PHPUnit_Framework_MockObject_MockObject */
  35. protected $bundleOptionMock;
  36. /** @var CustomOptionPrice|\PHPUnit_Framework_MockObject_MockObject */
  37. protected $customOptionPriceMock;
  38. /**
  39. * @var PriceCurrencyInterface|\PHPUnit_Framework_MockObject_MockObject
  40. */
  41. protected $priceCurrencyMock;
  42. /**
  43. * @var ProductCustomOptionRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
  44. */
  45. private $productOptionRepositoryMock;
  46. /**
  47. * @return void
  48. */
  49. protected function prepareMock()
  50. {
  51. $this->saleableInterfaceMock = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
  52. ->disableOriginalConstructor()
  53. ->setMethods(['getPriceType', 'getPriceInfo'])
  54. ->getMock();
  55. $this->bundleCalculatorMock = $this->createMock(
  56. \Magento\Bundle\Pricing\Adjustment\BundleCalculatorInterface::class
  57. );
  58. $this->basePriceMock = $this->createMock(\Magento\Catalog\Pricing\Price\BasePrice::class);
  59. $this->basePriceMock->expects($this->any())
  60. ->method('getValue')
  61. ->will($this->returnValue($this->baseAmount));
  62. $this->bundleOptionMock = $this->getMockBuilder(\Magento\Bundle\Pricing\Price\BundleOptionPrice::class)
  63. ->disableOriginalConstructor()
  64. ->getMock();
  65. $this->customOptionPriceMock = $this->getMockBuilder(\Magento\Catalog\Pricing\Price\CustomOptionPrice::class)
  66. ->disableOriginalConstructor()
  67. ->getMock();
  68. $this->priceInfoMock = $this->createMock(\Magento\Framework\Pricing\PriceInfo\Base::class);
  69. $this->priceInfoMock->expects($this->atLeastOnce())
  70. ->method('getPrice')
  71. ->will($this->returnValueMap([
  72. [\Magento\Catalog\Pricing\Price\BasePrice::PRICE_CODE, $this->basePriceMock],
  73. [BundleOptionPrice::PRICE_CODE, $this->bundleOptionMock],
  74. [CustomOptionPrice::PRICE_CODE, $this->customOptionPriceMock],
  75. ]));
  76. $this->saleableInterfaceMock->expects($this->once())
  77. ->method('getPriceInfo')
  78. ->will($this->returnValue($this->priceInfoMock));
  79. $this->priceCurrencyMock = $this->createMock(\Magento\Framework\Pricing\PriceCurrencyInterface::class);
  80. $this->objectManagerHelper = new ObjectManagerHelper($this);
  81. $this->finalPrice = new \Magento\Bundle\Pricing\Price\FinalPrice(
  82. $this->saleableInterfaceMock,
  83. $this->quantity,
  84. $this->bundleCalculatorMock,
  85. $this->priceCurrencyMock
  86. );
  87. $this->productOptionRepositoryMock = $this->getMockForAbstractClass(
  88. ProductCustomOptionRepositoryInterface::class
  89. );
  90. $reflection = new \ReflectionClass(get_class($this->finalPrice));
  91. $reflectionProperty = $reflection->getProperty('productOptionRepository');
  92. $reflectionProperty->setAccessible(true);
  93. $reflectionProperty->setValue($this->finalPrice, $this->productOptionRepositoryMock);
  94. }
  95. /**
  96. * @dataProvider getValueDataProvider
  97. */
  98. public function testGetValue($baseAmount, $optionsValue, $result)
  99. {
  100. $this->baseAmount = $baseAmount;
  101. $this->prepareMock();
  102. $this->bundleOptionMock->expects($this->once())
  103. ->method('getValue')
  104. ->will($this->returnValue($optionsValue));
  105. $this->assertSame($result, $this->finalPrice->getValue());
  106. }
  107. /**
  108. * @return array
  109. */
  110. public function getValueDataProvider()
  111. {
  112. return [
  113. [false, false, 0],
  114. [0, 1.2, 1.2],
  115. [1, 2, 3]
  116. ];
  117. }
  118. /**
  119. * @dataProvider getValueDataProvider
  120. */
  121. public function testGetMaximalPrice($baseAmount)
  122. {
  123. $result = 3;
  124. $this->baseAmount = $baseAmount;
  125. $this->prepareMock();
  126. $this->bundleCalculatorMock->expects($this->once())
  127. ->method('getMaxAmount')
  128. ->with($this->equalTo($this->baseAmount), $this->equalTo($this->saleableInterfaceMock))
  129. ->will($this->returnValue($result));
  130. $this->assertSame($result, $this->finalPrice->getMaximalPrice());
  131. //The second call should use cached value
  132. $this->assertSame($result, $this->finalPrice->getMaximalPrice());
  133. }
  134. public function testGetMaximalPriceFixedBundleWithOption()
  135. {
  136. $optionMaxPrice = 2;
  137. $this->baseAmount = 5;
  138. $result = 7;
  139. $this->prepareMock();
  140. $this->saleableInterfaceMock->expects($this->once())
  141. ->method('getPriceType')
  142. ->willReturn(Price::PRICE_TYPE_FIXED);
  143. $this->customOptionPriceMock->expects($this->once())
  144. ->method('getCustomOptionRange')
  145. ->with(false)
  146. ->willReturn($optionMaxPrice);
  147. $this->bundleCalculatorMock->expects($this->once())
  148. ->method('getMaxAmount')
  149. ->with($this->equalTo($this->baseAmount + $optionMaxPrice), $this->equalTo($this->saleableInterfaceMock))
  150. ->will($this->returnValue($result));
  151. $this->assertSame($result, $this->finalPrice->getMaximalPrice());
  152. //The second call should use cached value
  153. $this->assertSame($result, $this->finalPrice->getMaximalPrice());
  154. }
  155. public function testGetMinimalPriceFixedBundleWithOption()
  156. {
  157. $optionMaxPrice = 2;
  158. $this->baseAmount = 5;
  159. $result = 7;
  160. $this->prepareMock();
  161. $customOptions = [
  162. $this->getMockBuilder(\Magento\Catalog\Api\Data\ProductCustomOptionInterface::class)
  163. ->setMethods(['setProduct'])
  164. ->getMockForAbstractClass()
  165. ];
  166. $this->productOptionRepositoryMock->expects(static::once())
  167. ->method('getProductOptions')
  168. ->with($this->saleableInterfaceMock)
  169. ->willReturn($customOptions);
  170. $this->saleableInterfaceMock->expects($this->once())
  171. ->method('getPriceType')
  172. ->willReturn(Price::PRICE_TYPE_FIXED);
  173. $this->customOptionPriceMock->expects($this->once())
  174. ->method('getCustomOptionRange')
  175. ->with(true)
  176. ->willReturn($optionMaxPrice);
  177. $this->bundleCalculatorMock->expects($this->once())
  178. ->method('getAmount')
  179. ->with($this->equalTo($this->baseAmount + $optionMaxPrice), $this->equalTo($this->saleableInterfaceMock))
  180. ->will($this->returnValue($result));
  181. $this->assertSame($result, $this->finalPrice->getMinimalPrice());
  182. //The second call should use cached value
  183. $this->assertSame($result, $this->finalPrice->getMinimalPrice());
  184. }
  185. /**
  186. * @dataProvider getValueDataProvider
  187. */
  188. public function testGetMinimalPrice($baseAmount)
  189. {
  190. $result = 5;
  191. $this->baseAmount = $baseAmount;
  192. $this->prepareMock();
  193. $this->bundleCalculatorMock->expects($this->once())
  194. ->method('getAmount')
  195. ->with($this->equalTo($this->baseAmount), $this->equalTo($this->saleableInterfaceMock))
  196. ->will($this->returnValue($result));
  197. $this->assertSame($result, $this->finalPrice->getMinimalPrice());
  198. //The second call should use cached value
  199. $this->assertSame($result, $this->finalPrice->getMinimalPrice());
  200. }
  201. public function testGetPriceWithoutOption()
  202. {
  203. $result = 5;
  204. $this->prepareMock();
  205. $this->bundleCalculatorMock->expects($this->once())
  206. ->method('getAmountWithoutOption')
  207. ->with($this->equalTo($this->baseAmount), $this->equalTo($this->saleableInterfaceMock))
  208. ->will($this->returnValue($result));
  209. $this->assertSame($result, $this->finalPrice->getPriceWithoutOption());
  210. //The second call should use cached value
  211. $this->assertSame($result, $this->finalPrice->getPriceWithoutOption());
  212. }
  213. }