AdjustmentTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Weee\Test\Unit\Pricing;
  7. use \Magento\Weee\Pricing\Adjustment;
  8. use Magento\Framework\Pricing\SaleableInterface;
  9. use Magento\Weee\Helper\Data as WeeeHelper;
  10. class AdjustmentTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /**
  13. * @var Adjustment
  14. */
  15. protected $adjustment;
  16. /**
  17. * @var \Magento\Weee\Helper\Data | \PHPUnit_Framework_MockObject_MockObject
  18. */
  19. protected $weeeHelper;
  20. /**
  21. * @var \Magento\Framework\Pricing\PriceCurrencyInterface|\PHPUnit_Framework_MockObject_MockObject
  22. */
  23. protected $priceCurrencyMock;
  24. /**
  25. * @var int
  26. */
  27. protected $sortOrder = 5;
  28. protected function setUp()
  29. {
  30. $this->weeeHelper = $this->createMock(\Magento\Weee\Helper\Data::class);
  31. $this->priceCurrencyMock = $this->createMock(\Magento\Framework\Pricing\PriceCurrencyInterface::class);
  32. $this->priceCurrencyMock->expects($this->any())
  33. ->method('convertAndRound')
  34. ->will(
  35. $this->returnCallback(
  36. function ($arg) {
  37. return round($arg * 0.5, 2);
  38. }
  39. )
  40. );
  41. $this->priceCurrencyMock->expects($this->any())
  42. ->method('convert')
  43. ->will(
  44. $this->returnCallback(
  45. function ($arg) {
  46. return $arg * 0.5;
  47. }
  48. )
  49. );
  50. $this->adjustment = new Adjustment($this->weeeHelper, $this->priceCurrencyMock, $this->sortOrder);
  51. }
  52. public function testGetAdjustmentCode()
  53. {
  54. $this->assertEquals(Adjustment::ADJUSTMENT_CODE, $this->adjustment->getAdjustmentCode());
  55. }
  56. public function testIsIncludedInBasePrice()
  57. {
  58. $this->assertFalse($this->adjustment->isIncludedInBasePrice());
  59. }
  60. /**
  61. * @dataProvider isIncludedInDisplayPriceDataProvider
  62. */
  63. public function testIsIncludedInDisplayPrice($expectedResult)
  64. {
  65. $displayTypes = [
  66. \Magento\Weee\Model\Tax::DISPLAY_INCL,
  67. \Magento\Weee\Model\Tax::DISPLAY_INCL_DESCR,
  68. \Magento\Weee\Model\Tax::DISPLAY_EXCL_DESCR_INCL,
  69. ];
  70. $this->weeeHelper->expects($this->any())
  71. ->method('typeOfDisplay')
  72. ->with($displayTypes)
  73. ->will($this->returnValue($expectedResult));
  74. $this->assertEquals($expectedResult, $this->adjustment->isIncludedInDisplayPrice());
  75. }
  76. /**
  77. * @return array
  78. */
  79. public function isIncludedInDisplayPriceDataProvider()
  80. {
  81. return [[false], [true]];
  82. }
  83. /**
  84. * @param float $amount
  85. * @param float $amountOld
  86. * @param float $expectedResult
  87. * @dataProvider applyAdjustmentDataProvider
  88. */
  89. public function testApplyAdjustment($amount, $amountOld, $expectedResult)
  90. {
  91. $object = $this->getMockForAbstractClass(\Magento\Framework\Pricing\SaleableInterface::class);
  92. $this->weeeHelper->expects($this->any())
  93. ->method('getAmountExclTax')
  94. ->will($this->returnValue($amountOld));
  95. $this->assertEquals($expectedResult, $this->adjustment->applyAdjustment($amount, $object));
  96. }
  97. /**
  98. * @return array
  99. */
  100. public function applyAdjustmentDataProvider()
  101. {
  102. return [
  103. [1.1, 2.4, 2.3],
  104. [0.0, 2.2, 1.1],
  105. [1.1, 0.0, 1.1],
  106. ];
  107. }
  108. /**
  109. * @dataProvider isExcludedWithDataProvider
  110. * @param string $adjustmentCode
  111. * @param bool $expectedResult
  112. */
  113. public function testIsExcludedWith($adjustmentCode, $expectedResult)
  114. {
  115. $this->assertEquals($expectedResult, $this->adjustment->isExcludedWith($adjustmentCode));
  116. }
  117. /**
  118. * @return array
  119. */
  120. public function isExcludedWithDataProvider()
  121. {
  122. return [
  123. ['weee', true],
  124. ['tax', true],
  125. ['not_tax_and_not_weee', false]
  126. ];
  127. }
  128. /**
  129. * @dataProvider getSortOrderProvider
  130. * @param bool $isTaxable
  131. * @param int $expectedResult
  132. */
  133. public function testGetSortOrder($isTaxable, $expectedResult)
  134. {
  135. $this->weeeHelper->expects($this->any())
  136. ->method('isTaxable')
  137. ->will($this->returnValue($isTaxable));
  138. $this->assertEquals($expectedResult, $this->adjustment->getSortOrder());
  139. }
  140. /**
  141. * @return array
  142. */
  143. public function getSortOrderProvider()
  144. {
  145. return [
  146. [true, $this->sortOrder],
  147. [false, $this->sortOrder]
  148. ];
  149. }
  150. }