TaxCalculationTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\Tax\Test\Unit\Model;
  8. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  9. /**
  10. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  11. */
  12. class TaxCalculationTest extends \PHPUnit\Framework\TestCase
  13. {
  14. /**
  15. * @var \Magento\Tax\Api\TaxCalculationInterface
  16. */
  17. private $taxCalculationService;
  18. /**
  19. * @var \PHPUnit_Framework_MockObject_MockObject
  20. */
  21. private $taxDetailsItemDataObjectFactory;
  22. /**
  23. * @var \PHPUnit_Framework_MockObject_MockObject
  24. */
  25. private $taxDetailsDataObjectFactory;
  26. /**
  27. * @var \PHPUnit_Framework_MockObject_MockObject
  28. */
  29. private $storeManager;
  30. /**
  31. * @var \PHPUnit_Framework_MockObject_MockObject
  32. */
  33. private $calculatorFactory;
  34. /**
  35. * @var \PHPUnit_Framework_MockObject_MockObject
  36. */
  37. private $calculationTool;
  38. /**
  39. * @var \PHPUnit_Framework_MockObject_MockObject
  40. */
  41. private $configMock;
  42. /**
  43. * @var \PHPUnit_Framework_MockObject_MockObject
  44. */
  45. private $taxClassManagementMock;
  46. /**
  47. * @var \Magento\Framework\Api\DataObjectHelper|\PHPUnit_Framework_MockObject_MockObject
  48. */
  49. private $dataObjectHelperMock;
  50. protected function setUp()
  51. {
  52. $this->calculationTool = $this->createMock(\Magento\Tax\Model\Calculation::class);
  53. $this->calculatorFactory = $this->createMock(\Magento\Tax\Model\Calculation\CalculatorFactory::class);
  54. $this->configMock = $this->createMock(\Magento\Tax\Model\Config::class);
  55. $this->taxDetailsDataObjectFactory = $this->createPartialMock(
  56. \Magento\Tax\Api\Data\TaxDetailsInterfaceFactory::class,
  57. ['create']
  58. );
  59. $this->taxDetailsItemDataObjectFactory = $this->createMock(
  60. \Magento\Tax\Api\Data\TaxDetailsItemInterfaceFactory::class
  61. );
  62. $this->storeManager = $this->createMock(\Magento\Store\Model\StoreManagerInterface::class);
  63. $this->dataObjectHelperMock = $this->getMockBuilder(\Magento\Framework\Api\DataObjectHelper::class)
  64. ->disableOriginalConstructor()
  65. ->getMock();
  66. $this->taxClassManagementMock = $this->createMock(\Magento\Tax\Api\TaxClassManagementInterface::class);
  67. $objectManager = new ObjectManager($this);
  68. $this->taxCalculationService = $objectManager->getObject(
  69. \Magento\Tax\Model\TaxCalculation::class,
  70. [
  71. 'calculation' => $this->calculationTool,
  72. 'calculatorFactory' => $this->calculatorFactory,
  73. 'config' => $this->configMock,
  74. 'taxDetailsDataObjectFactory' => $this->taxDetailsDataObjectFactory,
  75. 'taxDetailsItemDataObjectFactory' => $this->taxDetailsItemDataObjectFactory,
  76. 'storeManager' => $this->storeManager,
  77. 'taxClassManagement' => $this->taxClassManagementMock,
  78. 'dataObjectHelper' => $this->dataObjectHelperMock,
  79. ]
  80. );
  81. }
  82. public function testGetCalculatedRate()
  83. {
  84. $productTaxClassId = 1;
  85. $customerId = 2;
  86. $storeId = 3;
  87. $rate = 0.5;
  88. $storeMock = $this->createPartialMock(\Magento\Store\Model\Store::class, ['getStoreId']);
  89. $this->storeManager->expects($this->once())->method('getStore')->willReturn($storeMock);
  90. $storeMock->expects($this->once())->method('getStoreId')->willReturn($storeId);
  91. $rateRequestMock = $this->createPartialMock(\Magento\Framework\DataObject::class, ['setProductClassId']);
  92. $this->calculationTool->expects($this->once())
  93. ->method('getRateRequest')
  94. ->with(null, null, null, $storeId, $customerId)
  95. ->willReturn($rateRequestMock);
  96. $rateRequestMock->expects($this->once())
  97. ->method('setProductClassId')
  98. ->with($productTaxClassId)
  99. ->willReturnSelf();
  100. $this->calculationTool->expects($this->once())->method('getRate')->with($rateRequestMock)->willReturn($rate);
  101. $this->assertEquals(
  102. $rate,
  103. $this->taxCalculationService->getCalculatedRate($productTaxClassId, $customerId, null)
  104. );
  105. }
  106. public function testGetDefaultCalculatedRate()
  107. {
  108. $productTaxClassId = 1;
  109. $customerId = 2;
  110. $storeId = 3;
  111. $rate = 0.5;
  112. $storeMock = $this->createPartialMock(\Magento\Store\Model\Store::class, ['getStoreId']);
  113. $this->storeManager->expects($this->once())->method('getStore')->willReturn($storeMock);
  114. $storeMock->expects($this->once())->method('getStoreId')->willReturn($storeId);
  115. $rateRequestMock = $this->createPartialMock(\Magento\Framework\DataObject::class, ['setProductClassId']);
  116. $this->calculationTool->expects($this->once())
  117. ->method('getDefaultRateRequest')
  118. ->with($storeId, $customerId)
  119. ->willReturn($rateRequestMock);
  120. $rateRequestMock->expects($this->once())
  121. ->method('setProductClassId')
  122. ->with($productTaxClassId)
  123. ->willReturnSelf();
  124. $this->calculationTool->expects($this->once())->method('getRate')->with($rateRequestMock)->willReturn($rate);
  125. $this->assertEquals(
  126. $rate,
  127. $this->taxCalculationService->getDefaultCalculatedRate($productTaxClassId, $customerId, null)
  128. );
  129. }
  130. public function testCalculateTaxIfNoItemsInQuote()
  131. {
  132. $storeId = 3;
  133. $quoteDetailsMock = $this->createMock(\Magento\Tax\Api\Data\QuoteDetailsInterface::class);
  134. $storeMock = $this->createPartialMock(\Magento\Store\Model\Store::class, ['getStoreId']);
  135. $this->storeManager->expects($this->once())->method('getStore')->willReturn($storeMock);
  136. $storeMock->expects($this->once())->method('getStoreId')->willReturn($storeId);
  137. $quoteDetailsMock->expects($this->once())->method('getItems')->willReturn(null);
  138. $taxDetailsMock = $this->createMock(\Magento\Tax\Api\Data\TaxDetailsInterface::class);
  139. $taxDetailsMock->expects($this->once())
  140. ->method('setSubtotal')
  141. ->willReturnSelf();
  142. $taxDetailsMock->expects($this->once())
  143. ->method('setTaxAmount')
  144. ->willReturnSelf();
  145. $taxDetailsMock->expects($this->once())
  146. ->method('setDiscountTaxCompensationAmount')
  147. ->willReturnSelf();
  148. $taxDetailsMock->expects($this->once())
  149. ->method('setAppliedTaxes')
  150. ->willReturnSelf();
  151. $taxDetailsMock->expects($this->once())
  152. ->method('setItems')
  153. ->willReturnSelf();
  154. $this->taxDetailsDataObjectFactory->expects($this->once())->method('create')->willReturn($taxDetailsMock);
  155. $this->assertEquals($taxDetailsMock, $this->taxCalculationService->calculateTax($quoteDetailsMock));
  156. }
  157. public function testCalculateTax()
  158. {
  159. $storeId = 3;
  160. $algorithm = 'algorithm';
  161. $customerId = 100;
  162. $taxClassId = 200;
  163. $taxDetailsData = [
  164. \Magento\Tax\Model\TaxDetails\TaxDetails::KEY_SUBTOTAL => 0.0,
  165. \Magento\Tax\Model\TaxDetails\TaxDetails::KEY_TAX_AMOUNT => 0.0,
  166. \Magento\Tax\Model\TaxDetails\TaxDetails::KEY_DISCOUNT_TAX_COMPENSATION_AMOUNT => 0.0,
  167. \Magento\Tax\Model\TaxDetails\TaxDetails::KEY_APPLIED_TAXES => [],
  168. \Magento\Tax\Model\TaxDetails\TaxDetails::KEY_ITEMS => [],
  169. ];
  170. $quoteDetailsMock = $this->createMock(\Magento\Tax\Api\Data\QuoteDetailsInterface::class);
  171. $storeMock = $this->createPartialMock(\Magento\Store\Model\Store::class, ['getStoreId']);
  172. $this->storeManager->expects($this->once())->method('getStore')->willReturn($storeMock);
  173. $storeMock->expects($this->once())->method('getStoreId')->willReturn($storeId);
  174. $billAddressMock = $this->createMock(\Magento\Customer\Api\Data\AddressInterface::class);
  175. $shipAddressMock = $this->createMock(\Magento\Customer\Api\Data\AddressInterface::class);
  176. $taxClassKeyMock = $this->createMock(\Magento\Tax\Api\Data\TaxClassKeyInterface::class);
  177. $quoteDetailsItemMock = $this->createMock(\Magento\Tax\Api\Data\QuoteDetailsItemInterface::class);
  178. $quoteDetailsMock->expects($this->once())->method('getItems')->willReturn([$quoteDetailsItemMock]);
  179. $quoteDetailsMock->expects($this->once())->method('getBillingAddress')->willReturn($billAddressMock);
  180. $quoteDetailsMock->expects($this->once())->method('getShippingAddress')->willReturn($shipAddressMock);
  181. $quoteDetailsMock->expects($this->once())->method('getCustomerId')->willReturn($customerId);
  182. $quoteDetailsMock->expects($this->once())->method('getCustomerTaxClassKey')->willReturn($taxClassKeyMock);
  183. $this->configMock->expects($this->once())->method('getAlgorithm')->with($storeId)->willReturn($algorithm);
  184. $this->taxClassManagementMock->expects($this->once())
  185. ->method('getTaxClassId')
  186. ->with($taxClassKeyMock, 'customer')
  187. ->willReturn($taxClassId);
  188. $calculatorMock = $this->createMock(\Magento\Tax\Model\Calculation\TotalBaseCalculator::class);
  189. $this->calculatorFactory->expects($this->once())
  190. ->method('create')
  191. ->with($algorithm, $storeId, $billAddressMock, $shipAddressMock, $taxClassId, $customerId)
  192. ->willReturn($calculatorMock);
  193. $taxDetailsMock = $this->createMock(\Magento\Tax\Api\Data\TaxDetailsItemInterface::class);
  194. $calculatorMock->expects($this->once())->method('calculate')->willReturn($taxDetailsMock);
  195. $taxDetailsMock = $this->createMock(\Magento\Tax\Api\Data\TaxDetailsInterface::class);
  196. $this->taxDetailsDataObjectFactory->expects($this->once())->method('create')->willReturn($taxDetailsMock);
  197. $this->dataObjectHelperMock->expects($this->once())
  198. ->method('populateWithArray')
  199. ->with($taxDetailsMock, $taxDetailsData)
  200. ->willReturnSelf();
  201. $this->assertEquals($taxDetailsMock, $this->taxCalculationService->calculateTax($quoteDetailsMock));
  202. }
  203. }