AdjustmentTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Tax\Test\Unit\Pricing;
  7. use \Magento\Tax\Pricing\Adjustment;
  8. use Magento\Framework\Pricing\SaleableInterface;
  9. class AdjustmentTest extends \PHPUnit\Framework\TestCase
  10. {
  11. /**
  12. * @var Adjustment
  13. */
  14. protected $adjustment;
  15. /**
  16. * @var \Magento\Tax\Helper\Data | \PHPUnit_Framework_MockObject_MockObject
  17. */
  18. protected $taxHelper;
  19. /**
  20. * @var \Magento\Catalog\Helper\Data | \PHPUnit_Framework_MockObject_MockObject
  21. */
  22. protected $catalogHelper;
  23. /**
  24. * @var int
  25. */
  26. protected $sortOrder = 5;
  27. protected function setUp()
  28. {
  29. $this->taxHelper = $this->createMock(\Magento\Tax\Helper\Data::class);
  30. $this->catalogHelper = $this->createMock(\Magento\Catalog\Helper\Data::class);
  31. $this->adjustment = new Adjustment($this->taxHelper, $this->catalogHelper, $this->sortOrder);
  32. }
  33. public function testGetAdjustmentCode()
  34. {
  35. $this->assertEquals(Adjustment::ADJUSTMENT_CODE, $this->adjustment->getAdjustmentCode());
  36. }
  37. /**
  38. * @param bool $expectedResult
  39. * @dataProvider isIncludedInBasePriceDataProvider
  40. */
  41. public function testIsIncludedInBasePrice($expectedResult)
  42. {
  43. $this->taxHelper->expects($this->once())
  44. ->method('priceIncludesTax')
  45. ->will($this->returnValue($expectedResult));
  46. $this->assertEquals($expectedResult, $this->adjustment->isIncludedInBasePrice());
  47. }
  48. /**
  49. * @return array
  50. */
  51. public function isIncludedInBasePriceDataProvider()
  52. {
  53. return [[true], [false]];
  54. }
  55. /**
  56. * @dataProvider isIncludedInDisplayPriceDataProvider
  57. */
  58. public function testIsIncludedInDisplayPrice($displayPriceIncludingTax, $displayBothPrices, $expectedResult)
  59. {
  60. $this->taxHelper->expects($this->once())
  61. ->method('displayPriceIncludingTax')
  62. ->will($this->returnValue($displayPriceIncludingTax));
  63. if (!$displayPriceIncludingTax) {
  64. $this->taxHelper->expects($this->once())
  65. ->method('displayBothPrices')
  66. ->will($this->returnValue($displayBothPrices));
  67. }
  68. $this->assertEquals($expectedResult, $this->adjustment->isIncludedInDisplayPrice());
  69. }
  70. /**
  71. * @return array
  72. */
  73. public function isIncludedInDisplayPriceDataProvider()
  74. {
  75. return [
  76. [false, false, false],
  77. [false, true, true],
  78. [true, false, true],
  79. [true, true, true],
  80. ];
  81. }
  82. /**
  83. * @param float $amount
  84. * @param bool $isPriceIncludesTax
  85. * @param float $price
  86. * @param float $expectedResult
  87. * @dataProvider extractAdjustmentDataProvider
  88. */
  89. public function testExtractAdjustment($isPriceIncludesTax, $amount, $price, $expectedResult)
  90. {
  91. $object = $this->getMockForAbstractClass(\Magento\Framework\Pricing\SaleableInterface::class);
  92. $this->taxHelper->expects($this->any())
  93. ->method('priceIncludesTax')
  94. ->will($this->returnValue($isPriceIncludesTax));
  95. $this->catalogHelper->expects($this->any())
  96. ->method('getTaxPrice')
  97. ->with($object, $amount)
  98. ->will($this->returnValue($price));
  99. $this->assertEquals($expectedResult, $this->adjustment->extractAdjustment($amount, $object));
  100. }
  101. /**
  102. * @return array
  103. */
  104. public function extractAdjustmentDataProvider()
  105. {
  106. return [
  107. [false, 'not_important', 'not_important', 0.00],
  108. [true, 10.1, 0.2, 9.9],
  109. [true, 10.1, 20.3, -10.2],
  110. [true, 0.0, 0.0, 0],
  111. ];
  112. }
  113. /**
  114. * @param bool $isPriceIncludesTax
  115. * @param float $amount
  116. * @param float $price
  117. * @param $expectedResult
  118. * @dataProvider applyAdjustmentDataProvider
  119. */
  120. public function testApplyAdjustment($amount, $price, $expectedResult)
  121. {
  122. $object = $this->getMockBuilder(\Magento\Framework\Pricing\SaleableInterface::class)->getMock();
  123. $this->catalogHelper->expects($this->any())
  124. ->method('getTaxPrice')
  125. ->with($object, $amount, true)
  126. ->will($this->returnValue($price));
  127. $this->assertEquals($expectedResult, $this->adjustment->applyAdjustment($amount, $object));
  128. }
  129. /**
  130. * @return array
  131. */
  132. public function applyAdjustmentDataProvider()
  133. {
  134. return [
  135. [1.1, 2.2, 2.2],
  136. [0.0, 2.2, 2.2],
  137. [1.1, 0.0, 0.0],
  138. ];
  139. }
  140. /**
  141. * @dataProvider isExcludedWithDataProvider
  142. * @param string $adjustmentCode
  143. * @param bool $expectedResult
  144. */
  145. public function testIsExcludedWith($adjustmentCode, $expectedResult)
  146. {
  147. $this->assertEquals($expectedResult, $this->adjustment->isExcludedWith($adjustmentCode));
  148. }
  149. /**
  150. * @return array
  151. */
  152. public function isExcludedWithDataProvider()
  153. {
  154. return [
  155. [Adjustment::ADJUSTMENT_CODE, true],
  156. ['not_tax', false]
  157. ];
  158. }
  159. public function testGetSortOrder()
  160. {
  161. $this->assertEquals($this->sortOrder, $this->adjustment->getSortOrder());
  162. }
  163. }