Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
DataTest.php
Go to the documentation of this file.
1 <?php
8 
10 
16 class DataTest extends \PHPUnit\Framework\TestCase
17 {
21  protected $helper;
22 
25 
27  protected $priceCurrencyMock;
28 
30  protected $taxConfigMock;
31 
33  protected $serializer;
34 
35  protected function setUp()
36  {
37  $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
38 
39  $this->orderTaxManagementMock = $this->getMockBuilder(\Magento\Tax\Api\OrderTaxManagementInterface::class)
40  ->disableOriginalConstructor()
41  ->getMock();
42  $this->priceCurrencyMock = $this->getMockBuilder(\Magento\Framework\Pricing\PriceCurrencyInterface::class)
43  ->disableOriginalConstructor()
44  ->getMock();
45  $this->taxConfigMock = $this->getMockBuilder(\Magento\Tax\Model\Config::class)
46  ->disableOriginalConstructor()
47  ->getMock();
48  $this->serializer = $this->getMockBuilder(\Magento\Framework\Serialize\Serializer\Json::class)
49  ->disableOriginalConstructor()
50  ->getMock();
51  $this->serializer->expects($this->any())
52  ->method('serialize')
53  ->willReturnCallback(
54  function ($value) {
55  return json_encode($value);
56  }
57  );
58 
59  $this->serializer->expects($this->any())
60  ->method('unserialize')
61  ->willReturnCallback(
62  function ($value) {
63  return json_decode($value, true);
64  }
65  );
66  $this->helper = $objectManager->getObject(
67  \Magento\Tax\Helper\Data::class,
68  [
69  'orderTaxManagement' => $this->orderTaxManagementMock,
70  'priceCurrency' => $this->priceCurrencyMock,
71  'taxConfig' => $this->taxConfigMock,
72  'serializer' => $this->serializer
73  ]
74  );
75  }
76 
78  {
79  $source = null;
80  $this->assertEquals([], $this->helper->getCalculatedTaxes($source));
81  }
82 
84  {
85  $orderId = 1;
86  $itemCode = 'test_code';
87  $itemAmount = 2;
88  $itemBaseAmount = 3;
89  $itemTitle = 'Test title';
90  $itemPercent = 0.1;
91 
92  $expectedAmount = $itemAmount + 1;
93  $expectedBaseAmount = $itemBaseAmount + 1;
94 
95  $orderDetailsItem = $this->getMockBuilder(\Magento\Tax\Api\Data\OrderTaxDetailsAppliedTaxInterface::class)
96  ->disableOriginalConstructor()
97  ->getMock();
98  $orderDetailsItem->expects($this->once())
99  ->method('getCode')
100  ->willReturn($itemCode);
101  $orderDetailsItem->expects($this->once())
102  ->method('getAmount')
103  ->willReturn($itemAmount);
104  $orderDetailsItem->expects($this->once())
105  ->method('getBaseAmount')
106  ->willReturn($itemBaseAmount);
107  $orderDetailsItem->expects($this->once())
108  ->method('getTitle')
109  ->willReturn($itemTitle);
110  $orderDetailsItem->expects($this->once())
111  ->method('getPercent')
112  ->willReturn($itemPercent);
113 
114  $roundValues = [
115  [$itemAmount, $expectedAmount],
116  [$itemBaseAmount, $expectedBaseAmount],
117  ];
118  $this->priceCurrencyMock->expects($this->exactly(2))
119  ->method('round')
120  ->will($this->returnValueMap($roundValues));
121 
122  $appliedTaxes = [$orderDetailsItem];
123 
124  $orderDetails = $this->getMockBuilder(\Magento\Tax\Api\Data\OrderTaxDetailsInterface::class)
125  ->disableOriginalConstructor()
126  ->getMock();
127  $orderDetails->expects($this->once())
128  ->method('getAppliedTaxes')
129  ->willReturn($appliedTaxes);
130  $this->orderTaxManagementMock->expects($this->once())
131  ->method('getOrderTaxDetails')
132  ->with($orderId)
133  ->willReturn($orderDetails);
134 
135  $orderMock = $this->getMockBuilder(\Magento\Sales\Model\Order::class)
136  ->disableOriginalConstructor()
137  ->getMock();
138  $orderMock->expects($this->once())
139  ->method('getId')
140  ->willReturn($orderId);
141 
142  $result = $this->helper->getCalculatedTaxes($orderMock);
143  $this->assertCount(1, $result);
144  $this->assertEquals($expectedAmount, $result[0]['tax_amount']);
145  $this->assertEquals($expectedBaseAmount, $result[0]['base_tax_amount']);
146  $this->assertEquals($itemTitle, $result[0]['title']);
147  $this->assertEquals($itemPercent, $result[0]['percent']);
148  }
149 
157  protected function mapOrderTaxItemDetail($inputArray)
158  {
159  $orderTaxItemDetailsMock = $this->getMockBuilder(\Magento\Tax\Api\Data\OrderTaxDetailsInterface::class)
160  ->getMock();
161  $itemMocks = [];
162  foreach ($inputArray['items'] as $orderTaxDetailsItemData) {
163  $itemId = isset($orderTaxDetailsItemData['item_id']) ? $orderTaxDetailsItemData['item_id'] : null;
164  $associatedItemId = isset($orderTaxDetailsItemData['associated_item_id'])
165  ? $orderTaxDetailsItemData['associated_item_id']
166  : null;
167  $itemType = isset($orderTaxDetailsItemData['type']) ? $orderTaxDetailsItemData['type'] : null;
168  $appliedTaxesData = $orderTaxDetailsItemData['applied_taxes'];
169  $appliedTaxesMocks = [];
170  foreach ($appliedTaxesData as $appliedTaxData) {
171  $appliedTaxesMock = $this->getMockBuilder(
172  \Magento\Tax\Api\Data\OrderTaxDetailsAppliedTaxInterface::class
173  )->getMock();
174  $appliedTaxesMock->expects($this->any())
175  ->method('getAmount')
176  ->will($this->returnValue($appliedTaxData['amount']));
177  $appliedTaxesMock->expects($this->any())
178  ->method('getBaseAmount')
179  ->will($this->returnValue($appliedTaxData['base_amount']));
180  $appliedTaxesMock->expects($this->any())
181  ->method('getCode')
182  ->will($this->returnValue($appliedTaxData['code']));
183  $appliedTaxesMock->expects($this->any())
184  ->method('getTitle')
185  ->will($this->returnValue($appliedTaxData['title']));
186  $appliedTaxesMock->expects($this->any())
187  ->method('getPercent')
188  ->will($this->returnValue($appliedTaxData['percent']));
189  $appliedTaxesMocks[] = $appliedTaxesMock;
190  }
191  $orderTaxDetailsItemMock = $this->getMockBuilder(\Magento\Tax\Api\Data\OrderTaxDetailsItemInterface::class)
192  ->getMock();
193  $orderTaxDetailsItemMock->expects($this->any())
194  ->method('getItemId')
195  ->will($this->returnValue($itemId));
196  $orderTaxDetailsItemMock->expects($this->any())
197  ->method('getAssociatedItemId')
198  ->will($this->returnValue($associatedItemId));
199  $orderTaxDetailsItemMock->expects($this->any())
200  ->method('getType')
201  ->will($this->returnValue($itemType));
202  $orderTaxDetailsItemMock->expects($this->any())
203  ->method('getAppliedTaxes')
204  ->will($this->returnValue($appliedTaxesMocks));
205 
206  $itemMocks[] = $orderTaxDetailsItemMock;
207  }
208  $orderTaxItemDetailsMock->expects($this->any())
209  ->method('getItems')
210  ->will($this->returnValue($itemMocks));
211 
212  return $orderTaxItemDetailsMock;
213  }
214 
218  public function testGetCalculatedTaxesForOrderItems($orderData, $invoiceData, $expectedResults)
219  {
220  $orderId = $orderData['order_id'];
221  $orderShippingTaxAmount = isset($orderData['shipping_tax_amount']) ? $orderData['shipping_tax_amount'] : 0;
222  $orderTaxDetails = $orderData['order_tax_details'];
223 
225  $orderMock = $this->getMockBuilder(\Magento\Sales\Model\Order::class)
226  ->disableOriginalConstructor()
227  ->getMock();
228  $orderMock->expects($this->once())
229  ->method('getId')
230  ->willReturn($orderId);
231  $orderMock->expects($this->once())
232  ->method('getShippingTaxAmount')
233  ->willReturn($orderShippingTaxAmount);
234 
235  $orderTaxDetailsMock = $this->mapOrderTaxItemDetail($orderTaxDetails);
236  $this->orderTaxManagementMock->expects($this->any())
237  ->method('getOrderTaxDetails')
238  ->with($orderId)
239  ->will($this->returnValue($orderTaxDetailsMock));
240 
241  $invoiceShippingTaxAmount =
242  isset($invoiceData['shipping_tax_amount']) ? $invoiceData['shipping_tax_amount'] : 0;
243  $invoiceItems = $invoiceData['invoice_items'];
245  $source = $this->getMockBuilder(\Magento\Sales\Model\Order\Invoice::class)
246  ->disableOriginalConstructor()
247  ->getMock();
248  $source->expects($this->once())
249  ->method('getOrder')
250  ->willReturn($orderMock);
251  $source->expects($this->once())
252  ->method('getShippingTaxAmount')
253  ->willReturn($invoiceShippingTaxAmount);
254  $source->expects($this->once())
255  ->method('getItems')
256  ->willReturn($invoiceItems);
257 
258  $this->priceCurrencyMock->expects($this->any())
259  ->method('round')
260  ->will(
261  $this->returnCallback(
262  function ($arg) {
263  return round($arg, 2);
264  }
265  )
266  );
267 
268  $result = $this->helper->getCalculatedTaxes($source);
269  foreach ($result as $index => $appliedTax) {
270  $expectedTax = $expectedResults[$index];
271  foreach ($appliedTax as $attr => $value) {
272  $this->assertEquals($expectedTax[$attr], $value, "The ".$attr." of tax does not match");
273  }
274  }
275  }
276 
282  {
283  $data = [
284  //Scenario 1: two items, one item with 0 tax
285  'two_items_with_one_zero_tax' => [
286  'order' => [
287  'order_id' => 1,
288  'shipping_tax_amount' => 0,
289  'order_tax_details' => [
290  'items' => [
291  'itemTax1' => [
292  'item_id' => 1,
293  'applied_taxes' => [
294  [
295  'amount' => 5.0,
296  'base_amount' => 5.0,
297  'code' => 'US-CA',
298  'title' => 'US-CA-Sales-Tax',
299  'percent' => 20.0,
300  ],
301  ],
302  ],
303  ],
304  ],
305  ],
306  'invoice' => [
307  'invoice_items' => [
308  'item1' => new MagentoObject(
309  [
310  'order_item' => new MagentoObject(
311  [
312  'id' => 1,
313  'tax_amount' => 5.00,
314  ]
315  ),
316  'tax_amount' => 2.50,
317  ]
318  ),
319  'item2' => new MagentoObject(
320  [
321  'order_item' => new MagentoObject(
322  [
323  'id' => 2,
324  'tax_amount' => 0.0,
325  ]
326  ),
327  'tax_amount' => 0.0,
328  ]
329  ),
330  ],
331  ],
332  'expected_results' => [
333  [
334  'title' => 'US-CA-Sales-Tax',
335  'percent' => 20.0,
336  'tax_amount' => 2.5,
337  'base_tax_amount' => 2.5,
338  ],
339  ],
340  ],
341  //Scenario 2: one item with associated weee tax
342  'item_with_weee_tax_partial_invoice' => [
343  'order' => [
344  'order_id' => 1,
345  'shipping_tax_amount' => 0,
346  'order_tax_details' => [
347  'items' => [
348  'itemTax1' => [
349  'item_id' => 1,
350  'applied_taxes' => [
351  [
352  'amount' => 5.0,
353  'base_amount' => 5.0,
354  'code' => 'US-CA',
355  'title' => 'US-CA-Sales-Tax',
356  'percent' => 20.0,
357  ],
358  ],
359  ],
360  'weeeTax1' => [
361  'associated_item_id' => 1,
362  'type' => 'weee',
363  'applied_taxes' => [
364  [
365  'amount' => 3.0,
366  'base_amount' => 3.0,
367  'code' => 'US-CA',
368  'title' => 'US-CA-Sales-Tax',
369  'percent' => 20.0,
370  ],
371  ],
372  ],
373  ],
374  ],
375  ],
376  'invoice' => [
377  'invoice_items' => [
378  'item1' => new MagentoObject(
379  [
380  'order_item' => new MagentoObject(
381  [
382  'id' => 1,
383  'tax_amount' => 5.00,
384  ]
385  ),
386  'tax_amount' => 5.0,
387  //half of weee tax is invoiced
388  'tax_ratio' => json_encode(['weee' => 0.5]),
389  ]
390  ),
391  ],
392  ],
393  'expected_results' => [
394  [
395  'title' => 'US-CA-Sales-Tax',
396  'percent' => 20.0,
397  'tax_amount' => 6.5,
398  'base_tax_amount' => 6.5,
399  ],
400  ],
401  ],
402  //Scenario 3: one item, with both shipping and product taxes
403  // note that 'shipping tax' is listed before 'product tax'
404  'one_item_with_both_shipping_and_product_taxes' => [
405  'order' => [
406  'order_id' => 1,
407  'shipping_tax_amount' => 2,
408  'order_tax_details' => [
409  'items' => [
410  'shippingTax1' => [
411  'item_id' => null,
412  'type' => 'shipping',
413  'applied_taxes' => [
414  [
415  'amount' => 2.0,
416  'base_amount' => 2.0,
417  'code' => 'US-CA-Ship',
418  'title' => 'US-CA-Sales-Tax-Ship',
419  'percent' => 10.0,
420  ],
421  ],
422  ],
423  'itemTax1' => [
424  'item_id' => 1,
425  'applied_taxes' => [
426  [
427  'amount' => 5.0,
428  'base_amount' => 5.0,
429  'code' => 'US-CA',
430  'title' => 'US-CA-Sales-Tax',
431  'percent' => 20.0,
432  ],
433  ],
434  ],
435  ],
436  ],
437  ],
438  'invoice' => [
439  'shipping_tax_amount' => 2,
440  'invoice_items' => [
441  'item1' => new MagentoObject(
442  [
443  'order_item' => new MagentoObject(
444  [
445  'id' => 1,
446  'tax_amount' => 5.00,
447  ]
448  ),
449  'tax_amount' => 5.00,
450  ]
451  ),
452  ],
453  ],
454  // note that 'shipping tax' is now listed after 'product tax'
455  'expected_results' => [
456  [
457  'title' => 'US-CA-Sales-Tax',
458  'percent' => 20.0,
459  'tax_amount' => 5.00,
460  'base_tax_amount' => 5.00,
461  ],
462  [
463  'title' => 'US-CA-Sales-Tax-Ship',
464  'percent' => 10.0,
465  'tax_amount' => 2.00,
466  'base_tax_amount' => 2.00,
467  ],
468  ],
469  ],
470  ];
471 
472  return $data;
473  }
474 
484  $expected,
486  $priceIncludesTax,
487  $isCrossBorderTradeEnabled,
488  $displayPriceIncludingTax
489  ) {
490  if ($displayBothPrices == true) {
491  $this->taxConfigMock->expects($this->at(0))
492  ->method('getPriceDisplayType')
493  ->willReturn(3);
494  } else {
495  $this->taxConfigMock->expects($this->at(0))
496  ->method('getPriceDisplayType')
497  ->willReturn(2);
498 
499  $this->taxConfigMock->expects($this->any())
500  ->method('priceIncludesTax')
501  ->willReturn($priceIncludesTax);
502 
503  $this->taxConfigMock->expects($this->any())
504  ->method('crossBorderTradeEnabled')
505  ->willReturn($isCrossBorderTradeEnabled);
506 
507  if ($displayPriceIncludingTax == true) {
508  $this->taxConfigMock->expects($this->at(3))
509  ->method('getPriceDisplayType')
510  ->willReturn(2);
511  } else {
512  $this->taxConfigMock->expects($this->at(2))
513  ->method('getPriceDisplayType')
514  ->willReturn(1);
515  }
516  }
517 
518  $this->assertSame($expected, $this->helper->isCatalogPriceDisplayAffectedByTax(null));
519  }
520 
525  {
526  return [
527  [true , true, false, false, false],
528  [true , false, true, true, false],
529  [true , false, true, false, true],
530  [false , false, true, true, true],
531  [true , false, false, true, true],
532  [false , false, false, true, false]
533  ];
534  }
535 }
$objectManager
Definition: bootstrap.php:17
return false
Definition: gallery.phtml:36
$attr
Definition: text.phtml:8
testIsCatalogPriceDisplayAffectedByTax( $expected, $displayBothPrices, $priceIncludesTax, $isCrossBorderTradeEnabled, $displayPriceIncludingTax)
Definition: DataTest.php:483
$source
Definition: source.php:23
$displayBothPrices
Definition: overview.phtml:101
$value
Definition: gender.phtml:16
$index
Definition: list.phtml:44