TierPriceTest.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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\TierPrice;
  8. use Magento\Customer\Model\Group;
  9. /**
  10. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  11. */
  12. class TierPriceTest extends \PHPUnit\Framework\TestCase
  13. {
  14. /**
  15. * @var \PHPUnit_Framework_MockObject_MockObject
  16. */
  17. protected $priceInfo;
  18. /**
  19. * @var \Magento\Catalog\Model\Product|\PHPUnit_Framework_MockObject_MockObject
  20. */
  21. protected $product;
  22. /**
  23. * @var \PHPUnit_Framework_MockObject_MockObject
  24. */
  25. protected $calculator;
  26. /**
  27. * @var TierPrice
  28. */
  29. protected $model;
  30. /**
  31. * @var \Magento\Framework\Pricing\PriceCurrencyInterface|\PHPUnit_Framework_MockObject_MockObject
  32. */
  33. protected $priceCurrencyMock;
  34. /**
  35. * @var \PHPUnit_Framework_MockObject_MockObject
  36. */
  37. protected $groupManagement;
  38. /**
  39. * Initialize base dependencies
  40. */
  41. protected function setUp()
  42. {
  43. $this->priceInfo = $this->createMock(\Magento\Framework\Pricing\PriceInfo\Base::class);
  44. $this->product = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
  45. ->setMethods(['getPriceInfo', 'hasCustomerGroupId', 'getCustomerGroupId', 'getResource', '__wakeup'])
  46. ->disableOriginalConstructor()
  47. ->getMock();
  48. $this->product->expects($this->any())
  49. ->method('getPriceInfo')
  50. ->will($this->returnValue($this->priceInfo));
  51. $this->calculator = $this->createMock(\Magento\Framework\Pricing\Adjustment\Calculator::class);
  52. $this->groupManagement = $this
  53. ->createMock(\Magento\Customer\Api\GroupManagementInterface::class);
  54. $this->priceCurrencyMock = $this->createMock(\Magento\Framework\Pricing\PriceCurrencyInterface::class);
  55. $objectHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  56. $this->model = $objectHelper->getObject(
  57. \Magento\Bundle\Pricing\Price\TierPrice::class,
  58. [
  59. 'saleableItem' => $this->product,
  60. 'calculator' => $this->calculator,
  61. 'priceCurrency' => $this->priceCurrencyMock,
  62. 'groupManagement' => $this->groupManagement
  63. ]
  64. );
  65. }
  66. /**
  67. * @covers \Magento\Bundle\Pricing\Price\TierPrice::isFirstPriceBetter
  68. * @dataProvider providerForGetterTierPriceList
  69. */
  70. public function testGetterTierPriceList($tierPrices, $basePrice, $expectedResult)
  71. {
  72. $this->product->setData(TierPrice::PRICE_CODE, $tierPrices);
  73. $price = $this->createMock(\Magento\Framework\Pricing\Price\PriceInterface::class);
  74. $price->expects($this->any())
  75. ->method('getValue')
  76. ->will($this->returnValue($basePrice));
  77. $this->priceInfo->expects($this->any())
  78. ->method('getPrice')
  79. ->will($this->returnValue($price));
  80. $this->calculator->expects($this->atLeastOnce())
  81. ->method('getAmount')
  82. ->will($this->returnArgument(0));
  83. $this->priceCurrencyMock->expects($this->never())->method('convertAndRound');
  84. $group = $this->createMock(\Magento\Customer\Model\Data\Group::class);
  85. $group->expects($this->any())
  86. ->method('getId')
  87. ->willReturn(\Magento\Customer\Model\GroupManagement::CUST_GROUP_ALL);
  88. $this->groupManagement->expects($this->any())->method('getAllCustomersGroup')
  89. ->will($this->returnValue($group));
  90. $this->assertEquals($expectedResult, $this->model->getTierPriceList());
  91. $this->assertEquals(count($expectedResult), $this->model->getTierPriceCount());
  92. }
  93. /**
  94. * @return array
  95. */
  96. public function providerForGetterTierPriceList()
  97. {
  98. return [
  99. 'base case' => [
  100. 'tierPrices' => [
  101. // will be ignored due to customer group
  102. [
  103. 'price' => '1.3',
  104. 'website_price' => '1.3',
  105. 'price_qty' => '1.',
  106. 'cust_group' => 999
  107. ],
  108. [
  109. 'price' => '50.',
  110. 'website_price' => '50.',
  111. 'price_qty' => '2.',
  112. 'cust_group' => Group::CUST_GROUP_ALL
  113. ],
  114. [
  115. 'price' => '25.',
  116. 'website_price' => '25.',
  117. 'price_qty' => '5.',
  118. 'cust_group' => Group::CUST_GROUP_ALL
  119. ],
  120. [
  121. 'price' => '15.',
  122. 'website_price' => '15.',
  123. 'price_qty' => '5.',
  124. 'cust_group' => Group::CUST_GROUP_ALL
  125. ],
  126. [
  127. 'price' => '30.',
  128. 'website_price' => '30.',
  129. 'price_qty' => '5.',
  130. 'cust_group' => Group::CUST_GROUP_ALL
  131. ],
  132. [
  133. 'price' => '8.',
  134. 'website_price' => '8.',
  135. 'price_qty' => '11.',
  136. 'cust_group' => Group::CUST_GROUP_ALL
  137. ],
  138. ],
  139. 'basePrice' => 20.,
  140. 'expectedResult' => [
  141. [
  142. 'price' => '15.',
  143. 'website_price' => '15.',
  144. 'price_qty' => '5.',
  145. 'cust_group' => Group::CUST_GROUP_ALL
  146. ],
  147. [
  148. 'price' => '8.',
  149. 'website_price' => '8.',
  150. 'price_qty' => '11.',
  151. 'cust_group' => Group::CUST_GROUP_ALL
  152. ],
  153. ],
  154. ]
  155. ];
  156. }
  157. /**
  158. * @dataProvider providerForTestGetSavePercent
  159. */
  160. public function testGetSavePercent($baseAmount, $tierPrice, $savePercent)
  161. {
  162. /** @var \Magento\Framework\Pricing\Amount\AmountInterface|\PHPUnit_Framework_MockObject_MockObject $amount */
  163. $amount = $this->getMockForAbstractClass(\Magento\Framework\Pricing\Amount\AmountInterface::class);
  164. $amount->expects($this->any())
  165. ->method('getValue')
  166. ->will($this->returnValue($tierPrice));
  167. $priceAmount = $this->getMockForAbstractClass(\Magento\Framework\Pricing\Amount\AmountInterface::class);
  168. $priceAmount->expects($this->any())
  169. ->method('getValue')
  170. ->will($this->returnValue($baseAmount));
  171. $price = $this->createMock(\Magento\Framework\Pricing\Price\PriceInterface::class);
  172. $price->expects($this->any())
  173. ->method('getAmount')
  174. ->will($this->returnValue($priceAmount));
  175. $this->priceInfo->expects($this->any())
  176. ->method('getPrice')
  177. ->will($this->returnValue($price));
  178. $this->assertEquals($savePercent, $this->model->getSavePercent($amount));
  179. }
  180. /**
  181. * @return array
  182. */
  183. public function providerForTestGetSavePercent()
  184. {
  185. return [
  186. 'no fraction' => [9.0000, 8.1, 10],
  187. 'lower half' => [9.1234, 8.3, 9],
  188. ];
  189. }
  190. }