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 $creditmemo;
31 
35  protected $invoice;
36 
40  protected $weeeData;
41 
42  protected function setUp()
43  {
44  $this->weeeData = $this->getMockBuilder(\Magento\Weee\Helper\Data::class)
45  ->setMethods(
46  [
47  'getRowWeeeTaxInclTax',
48  'getBaseRowWeeeTaxInclTax',
49  'getWeeeAmountInvoiced',
50  'getBaseWeeeAmountInvoiced',
51  'getWeeeAmountRefunded',
52  'getBaseWeeeAmountRefunded',
53  'getWeeeTaxAmountInvoiced',
54  'getBaseWeeeTaxAmountInvoiced',
55  'getWeeeTaxAmountRefunded',
56  'getBaseWeeeTaxAmountRefunded',
57  'getApplied',
58  'setApplied',
59  'includeInSubtotal',
60  ]
61  )->disableOriginalConstructor()
62  ->getMock();
63 
64  $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
65  $serializer = $this->objectManager->getObject(Json::class);
67  $this->model = $this->objectManager->getObject(
68  \Magento\Weee\Model\Total\Creditmemo\Weee::class,
69  [
70  'weeeData' => $this->weeeData,
71  'serializer' => $serializer
72  ]
73  );
74 
75  $this->order = $this->createPartialMock(\Magento\Sales\Model\Order::class, [
76  '__wakeup'
77  ]);
78 
79  $this->creditmemo = $this->createPartialMock(\Magento\Sales\Model\Order\Creditmemo::class, [
80  'getAllItems',
81  'getInvoice',
82  'roundPrice',
83  'getStore',
84  '__wakeup',
85  ]);
86  }
87 
93  public function testCollect($creditmemoData, $expectedResults)
94  {
95  $roundingDelta = [];
96 
97  //Set up weeeData mock
98  $this->weeeData->expects($this->once())
99  ->method('includeInSubtotal')
100  ->will($this->returnValue($creditmemoData['include_in_subtotal']));
101 
102  //Set up invoice mock
104  $creditmemoItems = [];
105  foreach ($creditmemoData['items'] as $itemKey => $creditmemoItemData) {
106  $creditmemoItems[$itemKey] = $this->getInvoiceItem($creditmemoItemData);
107  }
108  $this->creditmemo->expects($this->once())
109  ->method('getAllItems')
110  ->will($this->returnValue($creditmemoItems));
111  foreach ($creditmemoData['data_fields'] as $key => $value) {
112  $this->creditmemo->setData($key, $value);
113  }
114  $this->creditmemo->expects($this->any())
115  ->method('roundPrice')
116  ->will($this->returnCallback(
117  function ($price, $type) use (&$roundingDelta) {
118  if (!isset($roundingDelta[$type])) {
119  $roundingDelta[$type] = 0;
120  }
121  $roundedPrice = round($price + $roundingDelta[$type], 2);
122  $roundingDelta[$type] = $price - $roundedPrice;
123 
124  return $roundedPrice;
125  }
126  ));
127 
128  $this->model->collect($this->creditmemo);
129 
130  //verify invoice data
131  foreach ($expectedResults['creditmemo_data'] as $key => $value) {
132  $this->assertEquals(
133  $value,
134  $this->creditmemo->getData($key),
135  'Creditmemo data field '.$key.' is incorrect'
136  );
137  }
138  //verify invoice item data
139  foreach ($expectedResults['creditmemo_items'] as $itemKey => $itemData) {
140  $creditmemoItem = $creditmemoItems[$itemKey];
141  foreach ($itemData as $key => $value) {
142  if ($key == 'tax_ratio') {
143  $taxRatio = json_decode($creditmemoItem->getData($key), true);
144  $this->assertEquals($value['weee'], $taxRatio['weee'], "Tax ratio is incorrect");
145  } else {
146  $this->assertEquals(
147  $value,
148  $creditmemoItem->getData($key),
149  'Creditmemo item field '.$key.' is incorrect'
150  );
151  }
152  }
153  }
154  }
155 
160  public function collectDataProvider()
161  {
162  $result = [];
163 
164  // scenario 1: 3 item_1, $100 with $weee, 8.25 tax rate, 3 items invoiced, full creditmemo
165  $result['complete_creditmemo'] = [
166  'creditmemo_data' => [
167  'items' => [
168  'item_1' => [
169  'order_item' => [
170  'qty_ordered' => 3,
171  'weee_tax_applied_row_amount' => 30,
172  'base_weee_tax_applied_row_amnt' => 30,
173  'row_weee_tax_incl_tax' => 32.47,
174  'base_row_weee_tax_incl_tax' => 32.47,
175  'weee_amount_invoiced' => 30,
176  'base_weee_amount_invoiced' => 30,
177  'weee_amount_refunded' => 0,
178  'base_weee_amount_refunded' => 0,
179  'weee_tax_amount_invoiced' => 2.47,
180  'base_weee_tax_amount_invoiced' => 2.47,
181  'weee_tax_amount_refunded' => 0,
182  'base_weee_tax_amount_refunded' => 0,
183  'applied_weee' => [
184  [
185  'title' => 'recycling_fee',
186  'base_row_amount' => 30,
187  'row_amount' => 30,
188  'base_row_amount_incl_tax' => 32.47,
189  'row_amount_incl_tax' => 32.47,
190  ],
191  ],
192  'qty_invoiced' => 3,
193  ],
194  'is_last' => true,
195  'data_fields' => [
196  'qty' => 3,
197  'applied_weee' => [
198  [
199  ],
200  ],
201  ],
202  ],
203  ],
204  'include_in_subtotal' => false,
205  'data_fields' => [
206  'grand_total' => 300,
207  'base_grand_total' => 300,
208  'subtotal' => 300,
209  'base_subtotal' => 300,
210  'subtotal_incl_tax' => 324.75,
211  'base_subtotal_incl_tax' => 324.75,
212  'tax_amount' => 0,
213  'base_tax_amount' => 0,
214  ],
215  ],
216  'expected_results' => [
217  'creditmemo_items' => [
218  'item_1' => [
219  'applied_weee' => [
220  [
221  'title' => 'recycling_fee',
222  'base_row_amount' => 30,
223  'row_amount' => 30,
224  'base_row_amount_incl_tax' => 32.47,
225  'row_amount_incl_tax' => 32.47,
226  ],
227  ],
228  'tax_ratio' => ["weee" => 1.0],
229  'weee_tax_applied_row_amount' => 30,
230  'base_weee_tax_applied_row_amount' => 30,
231  ],
232  ],
233  'creditmemo_data' => [
234  'grand_total' => 332.47,
235  'base_grand_total' => 332.47,
236  'tax_amount' => 2.47,
237  'base_tax_amount' => 2.47,
238  'subtotal' => 300,
239  'base_subtotal' => 300,
240  'subtotal_incl_tax' => 357.22,
241  'base_subtotal_incl_tax' => 357.22,
242  ],
243  ],
244  ];
245 
246  // Scenario 2: 3 item_1, $100 with $weee, 8.25 tax rate, 3 items invoiced, 2 item creditmemo
247  $result['partial_creditmemo'] = [
248  'creditmemo_data' => [
249  'items' => [
250  'item_1' => [
251  'order_item' => [
252  'qty_ordered' => 3,
253  'weee_tax_applied_row_amount' => 30,
254  'base_weee_tax_applied_row_amnt' => 30,
255  'row_weee_tax_incl_tax' => 32.47,
256  'base_row_weee_tax_incl_tax' => 32.47,
257  'weee_amount_invoiced' => 30,
258  'base_weee_amount_invoiced' => 30,
259  'weee_amount_refunded' => 0,
260  'base_weee_amount_refunded' => 0,
261  'weee_tax_amount_invoiced' => 2.47,
262  'base_weee_tax_amount_invoiced' => 2.47,
263  'weee_tax_amount_refunded' => 0,
264  'base_weee_tax_amount_refunded' => 0,
265  'applied_weee' => [
266  [
267  'title' => 'recycling_fee',
268  'base_row_amount' => 30,
269  'row_amount' => 30,
270  'base_row_amount_incl_tax' => 32.47,
271  'row_amount_incl_tax' => 32.47,
272  ],
273  ],
274  'qty_invoiced' => 3,
275  ],
276  'is_last' => false,
277  'data_fields' => [
278  'qty' => 2,
279  'applied_weee' => [
280  [
281  ],
282  ],
283  ],
284  ],
285  ],
286  'include_in_subtotal' => false,
287  'data_fields' => [
288  'grand_total' => 200,
289  'base_grand_total' => 200,
290  'subtotal' => 200,
291  'base_subtotal' => 200,
292  'subtotal_incl_tax' => 216.5,
293  'base_subtotal_incl_tax' => 216.5,
294  'tax_amount' => 0,
295  'base_tax_amount' => 0,
296  ],
297  ],
298  'expected_results' => [
299  'creditmemo_items' => [
300  'item_1' => [
301  'applied_weee' => [
302  [
303  'title' => 'recycling_fee',
304  'base_row_amount' => 20,
305  'row_amount' => 20,
306  'base_row_amount_incl_tax' => 21.65,
307  'row_amount_incl_tax' => 21.65,
308  ],
309  ],
310  'tax_ratio' => ['weee' => 1.65 / 2.47],
311  'weee_tax_applied_row_amount' => 20,
312  'base_weee_tax_applied_row_amount' => 20,
313  ],
314  ],
315  'creditmemo_data' => [
316  'grand_total' => 221.65,
317  'base_grand_total' => 221.65,
318  'tax_amount' => 1.65,
319  'base_tax_amount' => 1.65,
320  'subtotal' => 200,
321  'base_subtotal' => 200,
322  'subtotal_incl_tax' => 238.15,
323  'base_subtotal_incl_tax' => 238.15,
324  ],
325  ],
326  ];
327 
328  // Scenario 3: 3 item_1, $100 with $weee, 8.25 tax rate, 3 items invoiced, 2 item returned
329  $result['last_partial_creditmemo'] = [
330  'creditmemo_data' => [
331  'items' => [
332  'item_1' => [
333  'order_item' => [
334  'qty_ordered' => 3,
335  'weee_tax_applied_row_amount' => 30,
336  'base_weee_tax_applied_row_amnt' => 30,
337  'row_weee_tax_incl_tax' => 32.47,
338  'base_row_weee_tax_incl_tax' => 32.47,
339  'weee_amount_invoiced' => 30,
340  'base_weee_amount_invoiced' => 30,
341  'weee_amount_refunded' => 20,
342  'base_weee_amount_refunded' => 20,
343  'weee_tax_amount_invoiced' => 2.47,
344  'base_weee_tax_amount_invoiced' => 2.47,
345  'weee_tax_amount_refunded' => 1.64,
346  'base_weee_tax_amount_refunded' => 1.64,
347  'applied_weee' => [
348  [
349  'title' => 'recycling_fee',
350  'base_row_amount' => 30,
351  'row_amount' => 30,
352  'base_row_amount_incl_tax' => 32.47,
353  'row_amount_incl_tax' => 32.47,
354  ],
355  ],
356  'qty_invoiced' => 3,
357  ],
358  'is_last' => true,
359  'data_fields' => [
360  'qty' => 1,
361  'applied_weee' => [
362  [
363  ],
364  ],
365  ],
366  ],
367  ],
368  'include_in_subtotal' => false,
369  'data_fields' => [
370  'grand_total' => 100,
371  'base_grand_total' => 100,
372  'subtotal' => 100,
373  'base_subtotal' => 100,
374  'subtotal_incl_tax' => 108.25,
375  'base_subtotal_incl_tax' => 108.25,
376  'tax_amount' => 0,
377  'base_tax_amount' => 0,
378  ],
379  ],
380  'expected_results' => [
381  'creditmemo_items' => [
382  'item_1' => [
383  'applied_weee' => [
384  [
385  'title' => 'recycling_fee',
386  'base_row_amount' => 10,
387  'row_amount' => 10,
388  'base_row_amount_incl_tax' => 10.82,
389  'row_amount_incl_tax' => 10.82,
390  ],
391  ],
392  'tax_ratio' => ['weee' => 0.83 / 2.47],
393  'weee_tax_applied_row_amount' => 10,
394  'base_weee_tax_applied_row_amount' => 10,
395  ],
396  ],
397  'creditmemo_data' => [
398  'grand_total' => 110.83,
399  'base_grand_total' => 110.83,
400  'tax_amount' => 0.83,
401  'base_tax_amount' => 0.83,
402  'subtotal' => 100,
403  'base_subtotal' => 100,
404  'subtotal_incl_tax' => 119.07,
405  'base_subtotal_incl_tax' => 119.07,
406  ],
407  ],
408  ];
409 
410  // scenario 4: 3 item_1, $100 with $weee, 8.25 tax rate. Returning qty 0.
411  $result['zero_return'] = [
412  'creditmemo_data' => [
413  'items' => [
414  'item_1' => [
415  'order_item' => [
416  'qty_ordered' => 3,
417  'weee_tax_applied_row_amount' => 30,
418  'base_weee_tax_applied_row_amnt' => 30,
419  'row_weee_tax_incl_tax' => 32.47,
420  'base_row_weee_tax_incl_tax' => 32.47,
421  'weee_amount_invoiced' => 30,
422  'base_weee_amount_invoiced' => 30,
423  'weee_amount_refunded' => 0,
424  'base_weee_amount_refunded' => 0,
425  'weee_tax_amount_invoiced' => 2.47,
426  'base_weee_tax_amount_invoiced' => 2.47,
427  'weee_tax_amount_refunded' => 0,
428  'base_weee_tax_amount_refunded' => 0,
429  'applied_weee' => [
430  [
431  'title' => 'recycling_fee',
432  'base_row_amount' => 30,
433  'row_amount' => 30,
434  'base_row_amount_incl_tax' => 32.47,
435  'row_amount_incl_tax' => 32.47,
436  ],
437  ],
438  'qty_invoiced' => 3,
439  ],
440  'is_last' => true,
441  'data_fields' => [
442  'qty' => 0,
443  'applied_weee' => [
444  [
445  ],
446  ],
447  ],
448  ],
449  ],
450  'include_in_subtotal' => false,
451  'data_fields' => [
452  'grand_total' => 300,
453  'base_grand_total' => 300,
454  'subtotal' => 300,
455  'base_subtotal' => 300,
456  'subtotal_incl_tax' => 324.75,
457  'base_subtotal_incl_tax' => 324.75,
458  'tax_amount' => 0,
459  'base_tax_amount' => 0,
460  ],
461  ],
462  'expected_results' => [
463  'creditmemo_items' => [
464  'item_1' => [
465  'applied_weee' => [
466  [
467  'title' => 'recycling_fee',
468  'base_row_amount' => 0,
469  'row_amount' => 0,
470  'base_row_amount_incl_tax' => 0,
471  'row_amount_incl_tax' => 0,
472  ],
473  ],
474  ],
475  ],
476  'creditmemo_data' => [
477  'subtotal' => 300,
478  'base_subtotal' => 300,
479  ],
480  ],
481  ];
482 
483  return $result;
484  }
485 
490  protected function getInvoiceItem($creditmemoItemData)
491  {
493  $orderItem = $this->createPartialMock(\Magento\Sales\Model\Order\Item::class, [
494  'isDummy',
495  '__wakeup'
496  ]);
497  foreach ($creditmemoItemData['order_item'] as $key => $value) {
498  $orderItem->setData($key, $value);
499  }
500 
501  $this->weeeData->expects($this->once())
502  ->method('getRowWeeeTaxInclTax')
503  ->with($orderItem)
504  ->will($this->returnValue($orderItem->getRowWeeeTaxInclTax()));
505  $this->weeeData->expects($this->once())
506  ->method('getBaseRowWeeeTaxInclTax')
507  ->with($orderItem)
508  ->will($this->returnValue($orderItem->getBaseRowWeeeTaxInclTax()));
509  $this->weeeData->expects($this->once())
510  ->method('getWeeeAmountInvoiced')
511  ->with($orderItem)
512  ->will($this->returnValue($orderItem->getWeeeAmountInvoiced()));
513  $this->weeeData->expects($this->once())
514  ->method('getBaseWeeeAmountInvoiced')
515  ->with($orderItem)
516  ->will($this->returnValue($orderItem->getBaseWeeeAmountInvoiced()));
517  $this->weeeData->expects($this->once())
518  ->method('getWeeeTaxAmountInvoiced')
519  ->with($orderItem)
520  ->will($this->returnValue($orderItem->getWeeeTaxAmountInvoiced()));
521  $this->weeeData->expects($this->once())
522  ->method('getBaseWeeeTaxAmountInvoiced')
523  ->with($orderItem)
524  ->will($this->returnValue($orderItem->getBaseWeeeTaxAmountInvoiced()));
525  $this->weeeData->expects($this->once())
526  ->method('getWeeeAmountRefunded')
527  ->with($orderItem)
528  ->will($this->returnValue($orderItem->getWeeeAmountRefunded()));
529  $this->weeeData->expects($this->once())
530  ->method('getBaseWeeeAmountRefunded')
531  ->with($orderItem)
532  ->will($this->returnValue($orderItem->getBaseWeeeAmountRefunded()));
533  $this->weeeData->expects($this->once())
534  ->method('getWeeeTaxAmountRefunded')
535  ->with($orderItem)
536  ->will($this->returnValue($orderItem->getWeeeTaxAmountRefunded()));
537  $this->weeeData->expects($this->once())
538  ->method('getBaseWeeeTaxAmountRefunded')
539  ->with($orderItem)
540  ->will($this->returnValue($orderItem->getBaseWeeeTaxAmountRefunded()));
541 
543  $invoiceItem = $this->createPartialMock(\Magento\Sales\Model\Order\Invoice\Item::class, [
544  'getOrderItem',
545  'isLast',
546  '__wakeup'
547  ]);
548  $invoiceItem->expects($this->any())->method('getOrderItem')->will($this->returnValue($orderItem));
549  $invoiceItem->expects($this->any())
550  ->method('isLast')
551  ->will($this->returnValue($creditmemoItemData['is_last']));
552  foreach ($creditmemoItemData['data_fields'] as $key => $value) {
553  $invoiceItem->setData($key, $value);
554  }
555 
556  $this->weeeData->expects($this->any())
557  ->method('getApplied')
558  ->will($this->returnCallback(
559  function ($item) {
560  return $item->getAppliedWeee();
561  }
562  ));
563 
564  $this->weeeData->expects($this->any())
565  ->method('setApplied')
566  ->will($this->returnCallback(
567  function ($item, $weee) {
568  return $item->setAppliedWeee($weee);
569  }
570  ));
571 
572  return $invoiceItem;
573  }
574 }
$invoiceItem
$orderItem
Definition: order.php:30
$price
$type
Definition: item.phtml:13
$value
Definition: gender.phtml:16