Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
WeeeTest.php
Go to the documentation of this file.
1 <?php
7 
9 
10 class WeeeTest extends \PHPUnit\Framework\TestCase
11 {
15  protected $model;
16 
20  protected $order;
21 
25  protected $objectManager;
26 
30  protected $invoice;
31 
35  protected $weeeData;
36 
37  protected function setUp()
38  {
39  $this->weeeData = $this->getMockBuilder(\Magento\Weee\Helper\Data::class)
40  ->setMethods(
41  [
42  'getRowWeeeTaxInclTax',
43  'getBaseRowWeeeTaxInclTax',
44  'getWeeeAmountInvoiced',
45  'getBaseWeeeAmountInvoiced',
46  'getWeeeTaxAmountInvoiced',
47  'getBaseWeeeTaxAmountInvoiced',
48  'getApplied',
49  'setApplied',
50  'includeInSubtotal',
51  ]
52  )->disableOriginalConstructor()
53  ->getMock();
54 
55  $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
56  $serializer = $this->objectManager->getObject(Json::class);
58  $this->model = $this->objectManager->getObject(
59  \Magento\Weee\Model\Total\Invoice\Weee::class,
60  [
61  'weeeData' => $this->weeeData,
62  'serializer' => $serializer
63  ]
64  );
65 
66  $this->order = $this->createPartialMock(\Magento\Sales\Model\Order::class, [
67  '__wakeup'
68  ]);
69 
70  $this->invoice = $this->createPartialMock(\Magento\Sales\Model\Order\Invoice::class, [
71  'getAllItems',
72  'getOrder',
73  'roundPrice',
74  'isLast',
75  'getStore',
76  '__wakeup',
77  ]);
78  $this->invoice->expects($this->atLeastOnce())->method('getOrder')->will($this->returnValue($this->order));
79  }
80 
84  private function setupOrder($orderData)
85  {
86  //Set up order mock
87  foreach ($orderData['data_fields'] as $key => $value) {
88  $this->order->setData($key, $value);
89  }
90  }
91 
98  public function testCollect($orderData, $invoiceData, $expectedResults)
99  {
100  $roundingDelta = [];
101 
102  $this->setupOrder($orderData);
103 
104  //Set up weeeData mock
105  $this->weeeData->expects($this->once())
106  ->method('includeInSubtotal')
107  ->will($this->returnValue($invoiceData['include_in_subtotal']));
108 
109  //Set up invoice mock
111  $invoiceItems = [];
112  foreach ($invoiceData['items'] as $itemKey => $invoiceItemData) {
113  $invoiceItems[$itemKey] = $this->getInvoiceItem($invoiceItemData);
114  }
115  $this->invoice->expects($this->once())
116  ->method('getAllItems')
117  ->will($this->returnValue($invoiceItems));
118  $this->invoice->expects($this->once())
119  ->method('isLast')
120  ->will($this->returnValue($invoiceData['is_last']));
121  foreach ($invoiceData['data_fields'] as $key => $value) {
122  $this->invoice->setData($key, $value);
123  }
124  $this->invoice->expects($this->any())
125  ->method('roundPrice')
126  ->will($this->returnCallback(
127  function ($price, $type) use (&$roundingDelta) {
128  if (!isset($roundingDelta[$type])) {
129  $roundingDelta[$type] = 0;
130  }
131  $roundedPrice = round($price + $roundingDelta[$type], 2);
132  $roundingDelta[$type] = $price - $roundedPrice;
133 
134  return $roundedPrice;
135  }
136  ));
137 
138  $this->model->collect($this->invoice);
139 
140  //verify invoice data
141  foreach ($expectedResults['invoice_data'] as $key => $value) {
142  $this->assertEquals($value, $this->invoice->getData($key), 'Invoice data field '.$key.' is incorrect');
143  }
144  //verify invoice item data
145  foreach ($expectedResults['invoice_items'] as $itemKey => $itemData) {
146  $invoiceItem = $invoiceItems[$itemKey];
147  foreach ($itemData as $key => $value) {
148  if ($key == 'tax_ratio') {
149  $taxRatio = json_decode($invoiceItem->getData($key), true);
150  $this->assertEquals($value['weee'], $taxRatio['weee'], "Tax ratio is incorrect");
151  } else {
152  $this->assertEquals(
153  $value,
154  $invoiceItem->getData($key),
155  'Invoice item field '.$key.' is incorrect'
156  );
157  }
158  }
159  }
160  }
161 
166  public function collectDataProvider()
167  {
168  $result = [];
169 
170  // 3 item_1, $100 with $weee, 8.25 tax rate, full invoice
171  $result['complete_invoice'] = [
172  'order_data' => [
173  'previous_invoices' => [
174  ],
175  'data_fields' => [
176  'shipping_tax_amount' => 1.24,
177  'base_shipping_tax_amount' => 1.24,
178  'shipping_discount_tax_compensation_amount' => 0,
179  'base_shipping_discount_tax_compensation_amount' => 0,
180  'tax_amount' => 16.09,
181  'tax_invoiced' => 0,
182  'base_tax_amount' => 16.09,
183  'base_tax_amount_invoiced' => 0,
184  'subtotal' => '300',
185  'base_subtotal' => '300',
186  ],
187  ],
188  'invoice_data' => [
189  'items' => [
190  'item_1' => [
191  'order_item' => [
192  'qty_ordered' => 3,
193  'weee_tax_applied_row_amount' => 30,
194  'base_weee_tax_applied_row_amnt' => 30,
195  'row_weee_tax_incl_tax' => 32.47,
196  'base_row_weee_tax_incl_tax' => 32.47,
197  'weee_amount_invoiced' => 0,
198  'base_weee_amount_invoiced' => 0,
199  'weee_tax_amount_invoiced' => 0,
200  'base_weee_tax_amount_invoiced' => 0,
201  'applied_weee' => [
202  [
203  'title' => 'recycling_fee',
204  'base_row_amount' => 30,
205  'row_amount' => 30,
206  'base_row_amount_incl_tax' => 32.47,
207  'row_amount_incl_tax' => 32.47,
208  ],
209  ],
210  'applied_weee_updated' => [
211  'base_row_amount_invoiced' => 30,
212  'row_amount_invoiced' => 30,
213  'base_tax_amount_invoiced' => 2.47,
214  'tax_amount_invoiced' => 2.47,
215  ],
216  'qty_invoiced' => 0,
217  ],
218  'is_last' => true,
219  'data_fields' => [
220  'qty' => 3,
221  'applied_weee' => [
222  [
223  ],
224  ],
225  ],
226  ],
227  ],
228  'is_last' => true,
229  'include_in_subtotal' => false,
230  'data_fields' => [
231  'grand_total' => 181.09,
232  'base_grand_total' => 181.09,
233  'subtotal' => 300,
234  'base_subtotal' => 300,
235  'subtotal_incl_tax' => 314.85,
236  'base_subtotal_incl_tax' => 314.85,
237  'tax_amount' => 16.09,
238  'base_tax_amount' => 16.09,
239  ],
240  ],
241  'expected_results' => [
242  'invoice_items' => [
243  'item_1' => [
244  'applied_weee' => [
245  [
246  'title' => 'recycling_fee',
247  'base_row_amount' => 30,
248  'row_amount' => 30,
249  'base_row_amount_incl_tax' => 32.47,
250  'row_amount_incl_tax' => 32.47,
251  ],
252  ],
253  'weee_tax_applied_row_amount' => 30,
254  'base_weee_tax_applied_row_amount' => 30,
255  'tax_ratio' => ["weee" => 1.0],
256  ],
257  ],
258  'invoice_data' => [
259  'grand_total' => 211.09,
260  'base_grand_total' => 211.09,
261  'tax_amount' => 16.09,
262  'base_tax_amount' => 16.09,
263  'subtotal' => 300,
264  'base_subtotal' => 300,
265  'subtotal_incl_tax' => 344.85,
266  'base_subtotal_incl_tax' => 344.85,
267  ],
268  ],
269  ];
270 
271  // 3 item_1, $100 with $weee, 8.25 tax rate, partial invoice, invoice qty=2
272  $result['partial_invoice'] = [
273  'order_data' => [
274  'previous_invoices' => [
275  ],
276  'data_fields' => [
277  'shipping_tax_amount' => 1.24,
278  'base_shipping_tax_amount' => 1.24,
279  'shipping_discount_tax_compensation_amount' => 0,
280  'base_shipping_discount_tax_compensation_amount' => 0,
281  'tax_amount' => 16.09,
282  'tax_invoiced' => 0,
283  'base_tax_amount' => 16.09,
284  'base_tax_amount_invoiced' => 0,
285  'subtotal' => '300',
286  'base_subtotal' => '300',
287  ],
288  ],
289  'invoice_data' => [
290  'items' => [
291  'item_1' => [
292  'order_item' => [
293  'qty_ordered' => 3,
294  'weee_tax_applied_row_amount' => 30,
295  'base_weee_tax_applied_row_amnt' => 30,
296  'row_weee_tax_incl_tax' => 32.47,
297  'base_row_weee_tax_incl_tax' => 32.47,
298  'weee_amount_invoiced' => 0,
299  'base_weee_amount_invoiced' => 0,
300  'weee_tax_amount_invoiced' => 0,
301  'base_weee_tax_amount_invoiced' => 0,
302  'applied_weee' => [
303  [
304  'title' => 'recycling_fee',
305  'base_row_amount' => 30,
306  'row_amount' => 30,
307  'base_row_amount_incl_tax' => 32.47,
308  'row_amount_incl_tax' => 32.47,
309  ],
310  ],
311  'applied_weee_updated' => [
312  'base_row_amount_invoiced' => 30,
313  'row_amount_invoiced' => 30,
314  'base_tax_amount_invoiced' => 2.47,
315  'tax_amount_invoiced' => 2.47,
316  ],
317  'qty_invoiced' => 0,
318  ],
319  'is_last' => false,
320  'data_fields' => [
321  'qty' => 2,
322  'applied_weee' => [
323  [
324  ],
325  ],
326  ],
327  ],
328  ],
329  'is_last' => false,
330  'include_in_subtotal' => false,
331  'data_fields' => [
332  'grand_total' => 124.49,
333  'base_grand_total' => 124.49,
334  'subtotal' => 200,
335  'base_subtotal' => 200,
336  'subtotal_incl_tax' => 216.5,
337  'base_subtotal_incl_tax' => 216.5,
338  'tax_amount' => 9.49,
339  'base_tax_amount' => 9.49,
340  ],
341  ],
342  'expected_results' => [
343  'invoice_items' => [
344  'item_1' => [
345  'applied_weee' => [
346  [
347  'title' => 'recycling_fee',
348  'base_row_amount' => 20,
349  'row_amount' => 20,
350  'base_row_amount_incl_tax' => 21.65,
351  'row_amount_incl_tax' => 21.65,
352  ],
353  ],
354  'tax_ratio' => ['weee' => 1.65 / 2.47],
355  'weee_tax_applied_row_amount' => 20,
356  'base_weee_tax_applied_row_amount' => 20,
357  ],
358  ],
359  'invoice_data' => [
360  'grand_total' => 146.14,
361  'base_grand_total' => 146.14,
362  'tax_amount' => 11.14,
363  'base_tax_amount' => 11.14,
364  'subtotal' => 200,
365  'base_subtotal' => 200,
366  'subtotal_incl_tax' => 238.15,
367  'base_subtotal_incl_tax' => 238.15,
368  ],
369  ],
370  ];
371 
372  // 3 item_1, $100 with $weee, 8.25 tax rate, partial invoice: one item invoiced
373  // invoice another item
374  $result['second_partial_invoice'] = [
375  'order_data' => [
376  'previous_invoices' => [
377  ],
378  'data_fields' => [
379  'shipping_tax_amount' => 1.24,
380  'base_shipping_tax_amount' => 1.24,
381  'shipping_discount_tax_compensation_amount' => 0,
382  'base_shipping_discount_tax_compensation_amount' => 0,
383  'tax_amount' => 16.09,
384  'tax_invoiced' => 0,
385  'base_tax_amount' => 16.09,
386  'base_tax_amount_invoiced' => 0,
387  'subtotal' => '300',
388  'base_subtotal' => '300',
389  ],
390  ],
391  'invoice_data' => [
392  'items' => [
393  'item_1' => [
394  'order_item' => [
395  'qty_ordered' => 3,
396  'weee_tax_applied_row_amount' => 30,
397  'base_weee_tax_applied_row_amnt' => 30,
398  'row_weee_tax_incl_tax' => 32.47,
399  'base_row_weee_tax_incl_tax' => 32.47,
400  'weee_amount_invoiced' => 0,
401  'base_weee_amount_invoiced' => 0,
402  'weee_tax_amount_invoiced' => 0,
403  'base_weee_tax_amount_invoiced' => 0,
404  'applied_weee' => [
405  [
406  'title' => 'recycling_fee',
407  'base_row_amount' => 30,
408  'row_amount' => 30,
409  'base_row_amount_incl_tax' => 32.47,
410  'row_amount_incl_tax' => 32.47,
411  ],
412  ],
413  'applied_weee_updated' => [
414  'base_row_amount_invoiced' => 30,
415  'row_amount_invoiced' => 30,
416  'base_tax_amount_invoiced' => 2.47,
417  'tax_amount_invoiced' => 2.47,
418  ],
419  'qty_invoiced' => 1,
420  ],
421  'is_last' => false,
422  'data_fields' => [
423  'qty' => 1,
424  'applied_weee' => [
425  [
426  ],
427  ],
428  ],
429  ],
430  ],
431  'is_last' => false,
432  'include_in_subtotal' => false,
433  'data_fields' => [
434  'grand_total' => 54.13,
435  'base_grand_total' => 54.13,
436  'tax_amount' => 4.13,
437  'base_tax_amount' => 4.13,
438  'subtotal' => 100,
439  'base_subtotal' => 100,
440  'subtotal_incl_tax' => 108.25,
441  'base_subtotal_incl_tax' => 108.25,
442  ],
443  ],
444  'expected_results' => [
445  'invoice_items' => [
446  'item_1' => [
447  'applied_weee' => [
448  [
449  'title' => 'recycling_fee',
450  'base_row_amount' => 10,
451  'row_amount' => 10,
452  'base_row_amount_incl_tax' => 10.82,
453  'row_amount_incl_tax' => 10.82,
454  ],
455  ],
456  'tax_ratio' => ['weee' => 0.82 / 2.47],
457  'weee_tax_applied_row_amount' => 10,
458  'base_weee_tax_applied_row_amount' => 10,
459  ],
460  ],
461  'invoice_data' => [
462  'grand_total' => 64.95,
463  'base_grand_total' => 64.95,
464  'tax_amount' => 4.95,
465  'base_tax_amount' => 4.95,
466  'subtotal' => 100,
467  'base_subtotal' => 100,
468  'subtotal_incl_tax' => 119.07,
469  'base_subtotal_incl_tax' => 119.07,
470  ],
471  ],
472  ];
473 
474  // 3 item_1, $100 with $weee, 8.25 tax rate, partial invoice: two item invoiced
475  // invoice another item
476  $result['last_partial_invoice'] = [
477  'order_data' => [
478  'previous_invoices' => [
479  ],
480  'data_fields' => [
481  'shipping_tax_amount' => 1.24,
482  'base_shipping_tax_amount' => 1.24,
483  'shipping_discount_tax_compensation_amount' => 0,
484  'base_shipping_discount_tax_compensation_amount' => 0,
485  'tax_amount' => 16.09,
486  'tax_invoiced' => 11.14,
487  'base_tax_amount' => 16.09,
488  'base_tax_invoiced' => 11.14,
489  'subtotal' => '300',
490  'base_subtotal' => '300',
491  ],
492  ],
493  'invoice_data' => [
494  'items' => [
495  'item_1' => [
496  'order_item' => [
497  'qty_ordered' => 3,
498  'weee_tax_applied_row_amount' => 30,
499  'base_weee_tax_applied_row_amnt' => 30,
500  'row_weee_tax_incl_tax' => 32.47,
501  'base_row_weee_tax_incl_tax' => 32.47,
502  'weee_amount_invoiced' => 20,
503  'base_weee_amount_invoiced' => 20,
504  'weee_tax_amount_invoiced' => 1.64,
505  'base_weee_tax_amount_invoiced' => 1.64,
506  'applied_weee' => [
507  [
508  'title' => 'recycling_fee',
509  'base_row_amount' => 30,
510  'row_amount' => 30,
511  'base_row_amount_incl_tax' => 32.47,
512  'row_amount_incl_tax' => 32.47,
513  ],
514  ],
515  'applied_weee_updated' => [
516  'base_row_amount_invoiced' => 30,
517  'row_amount_invoiced' => 30,
518  'base_tax_amount_invoiced' => 2.47,
519  'tax_amount_invoiced' => 2.47,
520  ],
521  'qty_invoiced' => 2,
522  ],
523  'is_last' => true,
524  'data_fields' => [
525  'qty' => 1,
526  'applied_weee' => [
527  [
528  ],
529  ],
530  ],
531  ],
532  ],
533  'is_last' => true,
534  'include_in_subtotal' => false,
535  'data_fields' => [
536  'grand_total' => 54.95,
537  'base_grand_total' => 54.95,
538  'tax_amount' => 4.95,
539  'base_tax_amount' => 4.95,
540  'subtotal' => 100,
541  'base_subtotal' => 100,
542  'subtotal_incl_tax' => 104.95,
543  'base_subtotal_incl_tax' => 104.95,
544  ],
545  ],
546  'expected_results' => [
547  'invoice_items' => [
548  'item_1' => [
549  'applied_weee' => [
550  [
551  'title' => 'recycling_fee',
552  'base_row_amount' => 10,
553  'row_amount' => 10,
554  'base_row_amount_incl_tax' => 10.82,
555  'row_amount_incl_tax' => 10.82,
556  ],
557  ],
558  'tax_ratio' => ['weee' => 0.83 / 2.47],
559  'weee_tax_applied_row_amount' => 10,
560  'base_weee_tax_applied_row_amount' => 10,
561 
562  ],
563  ],
564  'invoice_data' => [
565  'grand_total' => 64.95,
566  'base_grand_total' => 64.95,
567  'tax_amount' => 4.95,
568  'base_tax_amount' => 4.95,
569  'subtotal' => 100,
570  'base_subtotal' => 100,
571  'subtotal_incl_tax' => 114.95,
572  'base_subtotal_incl_tax' => 114.95,
573  ],
574  ],
575  ];
576 
577  // 3 item_1, $100 with $weee, 8.25 tax rate. Invoicing qty 0.
578  $result['zero_invoice'] = [
579  'order_data' => [
580  'previous_invoices' => [
581  ],
582  'data_fields' => [
583  'shipping_tax_amount' => 1.24,
584  'base_shipping_tax_amount' => 1.24,
585  'shipping_discount_tax_compensation_amount' => 0,
586  'base_shipping_discount_tax_compensation_amount' => 0,
587  'tax_amount' => 16.09,
588  'tax_invoiced' => 0,
589  'base_tax_amount' => 16.09,
590  'base_tax_amount_invoiced' => 0,
591  'subtotal' => '300',
592  'base_subtotal' => '300',
593  ],
594  ],
595  'invoice_data' => [
596  'items' => [
597  'item_1' => [
598  'order_item' => [
599  'qty_ordered' => 3,
600  'weee_tax_applied_row_amount' => 30,
601  'base_weee_tax_applied_row_amnt' => 30,
602  'row_weee_tax_incl_tax' => 32.47,
603  'base_row_weee_tax_incl_tax' => 32.47,
604  'weee_amount_invoiced' => 0,
605  'base_weee_amount_invoiced' => 0,
606  'weee_tax_amount_invoiced' => 0,
607  'base_weee_tax_amount_invoiced' => 0,
608  'applied_weee' => [
609  [
610  'title' => 'recycling_fee',
611  'base_row_amount' => 30,
612  'row_amount' => 30,
613  'base_row_amount_incl_tax' => 32.47,
614  'row_amount_incl_tax' => 32.47,
615  ],
616  ],
617  'applied_weee_updated' => [
618  'base_row_amount_invoiced' => 30,
619  'row_amount_invoiced' => 30,
620  'base_tax_amount_invoiced' => 2.47,
621  'tax_amount_invoiced' => 2.47,
622  ],
623  'qty_invoiced' => 0,
624  ],
625  'is_last' => true,
626  'data_fields' => [
627  'qty' => 0,
628  'applied_weee' => [
629  [
630  ],
631  ],
632  ],
633  ],
634  ],
635  'is_last' => true,
636  'include_in_subtotal' => false,
637  'data_fields' => [
638  'grand_total' => 181.09,
639  'base_grand_total' => 181.09,
640  'subtotal' => 300,
641  'base_subtotal' => 300,
642  'subtotal_incl_tax' => 314.85,
643  'base_subtotal_incl_tax' => 314.85,
644  'tax_amount' => 16.09,
645  'base_tax_amount' => 16.09,
646  ],
647  ],
648  'expected_results' => [
649  'invoice_items' => [
650  'item_1' => [
651  'applied_weee' => [
652  [
653  'title' => 'recycling_fee',
654  'base_row_amount' => 0,
655  'row_amount' => 0,
656  'base_row_amount_incl_tax' => 0,
657  'row_amount_incl_tax' => 0,
658  ],
659  ],
660  ],
661  ],
662  'invoice_data' => [
663  'subtotal' => 300,
664  'base_subtotal' => 300,
665  ],
666  ],
667  ];
668 
669  return $result;
670  }
671 
676  protected function getInvoiceItem($invoiceItemData)
677  {
679  $orderItem = $this->createPartialMock(\Magento\Sales\Model\Order\Item::class, [
680  'isDummy',
681  '__wakeup'
682  ]);
683  foreach ($invoiceItemData['order_item'] as $key => $value) {
684  $orderItem->setData($key, $value);
685  }
686 
687  $this->weeeData->expects($this->once())
688  ->method('getRowWeeeTaxInclTax')
689  ->with($orderItem)
690  ->will($this->returnValue($orderItem->getRowWeeeTaxInclTax()));
691  $this->weeeData->expects($this->once())
692  ->method('getBaseRowWeeeTaxInclTax')
693  ->with($orderItem)
694  ->will($this->returnValue($orderItem->getBaseRowWeeeTaxInclTax()));
695  if ($invoiceItemData['is_last']) {
696  $this->weeeData->expects($this->once())
697  ->method('getWeeeAmountInvoiced')
698  ->with($orderItem)
699  ->will($this->returnValue($orderItem->getWeeeAmountInvoiced()));
700  $this->weeeData->expects($this->once())
701  ->method('getBaseWeeeAmountInvoiced')
702  ->with($orderItem)
703  ->will($this->returnValue($orderItem->getBaseWeeeAmountInvoiced()));
704  $this->weeeData->expects($this->once())
705  ->method('getWeeeTaxAmountInvoiced')
706  ->with($orderItem)
707  ->will($this->returnValue($orderItem->getWeeeTaxAmountInvoiced()));
708  $this->weeeData->expects($this->once())
709  ->method('getBaseWeeeTaxAmountInvoiced')
710  ->with($orderItem)
711  ->will($this->returnValue($orderItem->getBaseWeeeTaxAmountInvoiced()));
712  }
714  $invoiceItem = $this->createPartialMock(\Magento\Sales\Model\Order\Invoice\Item::class, [
715  'getOrderItem',
716  'isLast',
717  '__wakeup'
718  ]);
719  $invoiceItem->expects($this->any())->method('getOrderItem')->will($this->returnValue($orderItem));
720  $invoiceItem->expects($this->any())
721  ->method('isLast')
722  ->will($this->returnValue($invoiceItemData['is_last']));
723  foreach ($invoiceItemData['data_fields'] as $key => $value) {
724  $invoiceItem->setData($key, $value);
725  }
726 
727  $this->weeeData->expects($this->any())
728  ->method('getApplied')
729  ->will($this->returnCallback(
730  function ($item) {
731  return $item->getAppliedWeee();
732  }
733  ));
734 
735  $this->weeeData->expects($this->any())
736  ->method('setApplied')
737  ->will($this->returnCallback(
738  function ($item, $weee) {
739  return $item->setAppliedWeee($weee);
740  }
741  ));
742 
743  return $invoiceItem;
744  }
745 }
$invoiceItem
$orderItem
Definition: order.php:30
$price
$type
Definition: item.phtml:13
$value
Definition: gender.phtml:16