Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
TaxCalculationTest.php
Go to the documentation of this file.
1 <?php
8 
10 
14 class TaxCalculationTest extends \PHPUnit\Framework\TestCase
15 {
19  private $taxCalculationService;
20 
24  private $taxDetailsItemDataObjectFactory;
25 
29  private $taxDetailsDataObjectFactory;
30 
34  private $storeManager;
35 
39  private $calculatorFactory;
40 
44  private $calculationTool;
45 
49  private $configMock;
50 
54  private $taxClassManagementMock;
55 
59  private $dataObjectHelperMock;
60 
61  protected function setUp()
62  {
63  $this->calculationTool = $this->createMock(\Magento\Tax\Model\Calculation::class);
64  $this->calculatorFactory = $this->createMock(\Magento\Tax\Model\Calculation\CalculatorFactory::class);
65  $this->configMock = $this->createMock(\Magento\Tax\Model\Config::class);
66  $this->taxDetailsDataObjectFactory = $this->createPartialMock(
67  \Magento\Tax\Api\Data\TaxDetailsInterfaceFactory::class,
68  ['create']
69  );
70  $this->taxDetailsItemDataObjectFactory = $this->createMock(
71  \Magento\Tax\Api\Data\TaxDetailsItemInterfaceFactory::class
72  );
73  $this->storeManager = $this->createMock(\Magento\Store\Model\StoreManagerInterface::class);
74  $this->dataObjectHelperMock = $this->getMockBuilder(\Magento\Framework\Api\DataObjectHelper::class)
75  ->disableOriginalConstructor()
76  ->getMock();
77  $this->taxClassManagementMock = $this->createMock(\Magento\Tax\Api\TaxClassManagementInterface::class);
78 
79  $objectManager = new ObjectManager($this);
80  $this->taxCalculationService = $objectManager->getObject(
81  \Magento\Tax\Model\TaxCalculation::class,
82  [
83  'calculation' => $this->calculationTool,
84  'calculatorFactory' => $this->calculatorFactory,
85  'config' => $this->configMock,
86  'taxDetailsDataObjectFactory' => $this->taxDetailsDataObjectFactory,
87  'taxDetailsItemDataObjectFactory' => $this->taxDetailsItemDataObjectFactory,
88  'storeManager' => $this->storeManager,
89  'taxClassManagement' => $this->taxClassManagementMock,
90  'dataObjectHelper' => $this->dataObjectHelperMock,
91  ]
92  );
93  }
94 
95  public function testGetCalculatedRate()
96  {
97  $productTaxClassId = 1;
98  $customerId = 2;
99  $storeId = 3;
100  $rate = 0.5;
101 
102  $storeMock = $this->createPartialMock(\Magento\Store\Model\Store::class, ['getStoreId']);
103  $this->storeManager->expects($this->once())->method('getStore')->willReturn($storeMock);
104  $storeMock->expects($this->once())->method('getStoreId')->willReturn($storeId);
105 
106  $rateRequestMock = $this->createPartialMock(\Magento\Framework\DataObject::class, ['setProductClassId']);
107  $this->calculationTool->expects($this->once())
108  ->method('getRateRequest')
109  ->with(null, null, null, $storeId, $customerId)
110  ->willReturn($rateRequestMock);
111 
112  $rateRequestMock->expects($this->once())
113  ->method('setProductClassId')
114  ->with($productTaxClassId)
115  ->willReturnSelf();
116 
117  $this->calculationTool->expects($this->once())->method('getRate')->with($rateRequestMock)->willReturn($rate);
118  $this->assertEquals(
119  $rate,
120  $this->taxCalculationService->getCalculatedRate($productTaxClassId, $customerId, null)
121  );
122  }
123 
125  {
126  $productTaxClassId = 1;
127  $customerId = 2;
128  $storeId = 3;
129  $rate = 0.5;
130 
131  $storeMock = $this->createPartialMock(\Magento\Store\Model\Store::class, ['getStoreId']);
132  $this->storeManager->expects($this->once())->method('getStore')->willReturn($storeMock);
133  $storeMock->expects($this->once())->method('getStoreId')->willReturn($storeId);
134 
135  $rateRequestMock = $this->createPartialMock(\Magento\Framework\DataObject::class, ['setProductClassId']);
136  $this->calculationTool->expects($this->once())
137  ->method('getDefaultRateRequest')
138  ->with($storeId, $customerId)
139  ->willReturn($rateRequestMock);
140 
141  $rateRequestMock->expects($this->once())
142  ->method('setProductClassId')
143  ->with($productTaxClassId)
144  ->willReturnSelf();
145 
146  $this->calculationTool->expects($this->once())->method('getRate')->with($rateRequestMock)->willReturn($rate);
147  $this->assertEquals(
148  $rate,
149  $this->taxCalculationService->getDefaultCalculatedRate($productTaxClassId, $customerId, null)
150  );
151  }
152 
154  {
155  $storeId = 3;
156  $quoteDetailsMock = $this->createMock(\Magento\Tax\Api\Data\QuoteDetailsInterface::class);
157 
158  $storeMock = $this->createPartialMock(\Magento\Store\Model\Store::class, ['getStoreId']);
159  $this->storeManager->expects($this->once())->method('getStore')->willReturn($storeMock);
160  $storeMock->expects($this->once())->method('getStoreId')->willReturn($storeId);
161 
162  $quoteDetailsMock->expects($this->once())->method('getItems')->willReturn(null);
163 
164  $taxDetailsMock = $this->createMock(\Magento\Tax\Api\Data\TaxDetailsInterface::class);
165  $taxDetailsMock->expects($this->once())
166  ->method('setSubtotal')
167  ->willReturnSelf();
168  $taxDetailsMock->expects($this->once())
169  ->method('setTaxAmount')
170  ->willReturnSelf();
171  $taxDetailsMock->expects($this->once())
172  ->method('setDiscountTaxCompensationAmount')
173  ->willReturnSelf();
174  $taxDetailsMock->expects($this->once())
175  ->method('setAppliedTaxes')
176  ->willReturnSelf();
177  $taxDetailsMock->expects($this->once())
178  ->method('setItems')
179  ->willReturnSelf();
180  $this->taxDetailsDataObjectFactory->expects($this->once())->method('create')->willReturn($taxDetailsMock);
181 
182  $this->assertEquals($taxDetailsMock, $this->taxCalculationService->calculateTax($quoteDetailsMock));
183  }
184 
185  public function testCalculateTax()
186  {
187  $storeId = 3;
188  $algorithm = 'algorithm';
189  $customerId = 100;
190  $taxClassId = 200;
191  $taxDetailsData = [
197  ];
198 
199  $quoteDetailsMock = $this->createMock(\Magento\Tax\Api\Data\QuoteDetailsInterface::class);
200 
201  $storeMock = $this->createPartialMock(\Magento\Store\Model\Store::class, ['getStoreId']);
202  $this->storeManager->expects($this->once())->method('getStore')->willReturn($storeMock);
203  $storeMock->expects($this->once())->method('getStoreId')->willReturn($storeId);
204 
205  $billAddressMock = $this->createMock(\Magento\Customer\Api\Data\AddressInterface::class);
206  $shipAddressMock = $this->createMock(\Magento\Customer\Api\Data\AddressInterface::class);
207  $taxClassKeyMock = $this->createMock(\Magento\Tax\Api\Data\TaxClassKeyInterface::class);
208 
209  $quoteDetailsItemMock = $this->createMock(\Magento\Tax\Api\Data\QuoteDetailsItemInterface::class);
210  $quoteDetailsMock->expects($this->once())->method('getItems')->willReturn([$quoteDetailsItemMock]);
211  $quoteDetailsMock->expects($this->once())->method('getBillingAddress')->willReturn($billAddressMock);
212  $quoteDetailsMock->expects($this->once())->method('getShippingAddress')->willReturn($shipAddressMock);
213  $quoteDetailsMock->expects($this->once())->method('getCustomerId')->willReturn($customerId);
214  $quoteDetailsMock->expects($this->once())->method('getCustomerTaxClassKey')->willReturn($taxClassKeyMock);
215 
216  $this->configMock->expects($this->once())->method('getAlgorithm')->with($storeId)->willReturn($algorithm);
217  $this->taxClassManagementMock->expects($this->once())
218  ->method('getTaxClassId')
219  ->with($taxClassKeyMock, 'customer')
220  ->willReturn($taxClassId);
221 
222  $calculatorMock = $this->createMock(\Magento\Tax\Model\Calculation\TotalBaseCalculator::class);
223  $this->calculatorFactory->expects($this->once())
224  ->method('create')
225  ->with($algorithm, $storeId, $billAddressMock, $shipAddressMock, $taxClassId, $customerId)
226  ->willReturn($calculatorMock);
227 
228  $taxDetailsMock = $this->createMock(\Magento\Tax\Api\Data\TaxDetailsItemInterface::class);
229  $calculatorMock->expects($this->once())->method('calculate')->willReturn($taxDetailsMock);
230 
231  $taxDetailsMock = $this->createMock(\Magento\Tax\Api\Data\TaxDetailsInterface::class);
232  $this->taxDetailsDataObjectFactory->expects($this->once())->method('create')->willReturn($taxDetailsMock);
233  $this->dataObjectHelperMock->expects($this->once())
234  ->method('populateWithArray')
235  ->with($taxDetailsMock, $taxDetailsData)
236  ->willReturnSelf();
237 
238  $this->assertEquals($taxDetailsMock, $this->taxCalculationService->calculateTax($quoteDetailsMock));
239  }
240 }
$objectManager
Definition: bootstrap.php:17