10 class WeeeTest extends \PHPUnit\Framework\TestCase
37 protected function setUp()
39 $this->weeeData = $this->getMockBuilder(\
Magento\Weee\Helper\Data::class)
42 'getRowWeeeTaxInclTax',
43 'getBaseRowWeeeTaxInclTax',
44 'getWeeeAmountInvoiced',
45 'getBaseWeeeAmountInvoiced',
46 'getWeeeTaxAmountInvoiced',
47 'getBaseWeeeTaxAmountInvoiced',
52 )->disableOriginalConstructor()
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,
61 'weeeData' => $this->weeeData,
66 $this->order = $this->createPartialMock(\
Magento\Sales\Model\Order::class, [
70 $this->invoice = $this->createPartialMock(\
Magento\Sales\Model\Order\Invoice::class, [
78 $this->invoice->expects($this->atLeastOnce())->method(
'getOrder')->will($this->returnValue($this->order));
84 private function setupOrder($orderData)
87 foreach ($orderData[
'data_fields'] as $key =>
$value) {
88 $this->order->setData($key,
$value);
98 public function testCollect($orderData, $invoiceData, $expectedResults)
102 $this->setupOrder($orderData);
105 $this->weeeData->expects($this->once())
106 ->method(
'includeInSubtotal')
107 ->will($this->returnValue($invoiceData[
'include_in_subtotal']));
112 foreach ($invoiceData[
'items'] as $itemKey => $invoiceItemData) {
113 $invoiceItems[$itemKey] = $this->getInvoiceItem($invoiceItemData);
115 $this->invoice->expects($this->once())
116 ->method(
'getAllItems')
117 ->will($this->returnValue($invoiceItems));
118 $this->invoice->expects($this->once())
120 ->will($this->returnValue($invoiceData[
'is_last']));
121 foreach ($invoiceData[
'data_fields'] as $key =>
$value) {
122 $this->invoice->setData($key,
$value);
124 $this->invoice->expects($this->any())
125 ->method(
'roundPrice')
126 ->will($this->returnCallback(
128 if (!isset($roundingDelta[
$type])) {
129 $roundingDelta[
$type] = 0;
131 $roundedPrice = round(
$price + $roundingDelta[
$type], 2);
134 return $roundedPrice;
138 $this->model->collect($this->invoice);
141 foreach ($expectedResults[
'invoice_data'] as $key =>
$value) {
142 $this->assertEquals(
$value, $this->invoice->getData($key),
'Invoice data field '.$key.
' is incorrect');
145 foreach ($expectedResults[
'invoice_items'] as $itemKey => $itemData) {
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");
155 'Invoice item field '.$key.
' is incorrect' 171 $result[
'complete_invoice'] = [
173 'previous_invoices' => [
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,
182 'base_tax_amount' => 16.09,
183 'base_tax_amount_invoiced' => 0,
185 'base_subtotal' =>
'300',
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,
203 'title' =>
'recycling_fee',
204 'base_row_amount' => 30,
206 'base_row_amount_incl_tax' => 32.47,
207 'row_amount_incl_tax' => 32.47,
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,
229 'include_in_subtotal' =>
false,
231 'grand_total' => 181.09,
232 'base_grand_total' => 181.09,
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,
241 'expected_results' => [
246 'title' =>
'recycling_fee',
247 'base_row_amount' => 30,
249 'base_row_amount_incl_tax' => 32.47,
250 'row_amount_incl_tax' => 32.47,
253 'weee_tax_applied_row_amount' => 30,
254 'base_weee_tax_applied_row_amount' => 30,
255 'tax_ratio' => [
"weee" => 1.0],
259 'grand_total' => 211.09,
260 'base_grand_total' => 211.09,
261 'tax_amount' => 16.09,
262 'base_tax_amount' => 16.09,
264 'base_subtotal' => 300,
265 'subtotal_incl_tax' => 344.85,
266 'base_subtotal_incl_tax' => 344.85,
274 'previous_invoices' => [
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,
283 'base_tax_amount' => 16.09,
284 'base_tax_amount_invoiced' => 0,
286 'base_subtotal' =>
'300',
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,
304 'title' =>
'recycling_fee',
305 'base_row_amount' => 30,
307 'base_row_amount_incl_tax' => 32.47,
308 'row_amount_incl_tax' => 32.47,
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,
330 'include_in_subtotal' =>
false,
332 'grand_total' => 124.49,
333 'base_grand_total' => 124.49,
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,
342 'expected_results' => [
347 'title' =>
'recycling_fee',
348 'base_row_amount' => 20,
350 'base_row_amount_incl_tax' => 21.65,
351 'row_amount_incl_tax' => 21.65,
354 'tax_ratio' => [
'weee' => 1.65 / 2.47],
355 'weee_tax_applied_row_amount' => 20,
356 'base_weee_tax_applied_row_amount' => 20,
360 'grand_total' => 146.14,
361 'base_grand_total' => 146.14,
362 'tax_amount' => 11.14,
363 'base_tax_amount' => 11.14,
365 'base_subtotal' => 200,
366 'subtotal_incl_tax' => 238.15,
367 'base_subtotal_incl_tax' => 238.15,
374 $result[
'second_partial_invoice'] = [
376 'previous_invoices' => [
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,
385 'base_tax_amount' => 16.09,
386 'base_tax_amount_invoiced' => 0,
388 'base_subtotal' =>
'300',
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,
406 'title' =>
'recycling_fee',
407 'base_row_amount' => 30,
409 'base_row_amount_incl_tax' => 32.47,
410 'row_amount_incl_tax' => 32.47,
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,
432 'include_in_subtotal' =>
false,
434 'grand_total' => 54.13,
435 'base_grand_total' => 54.13,
436 'tax_amount' => 4.13,
437 'base_tax_amount' => 4.13,
439 'base_subtotal' => 100,
440 'subtotal_incl_tax' => 108.25,
441 'base_subtotal_incl_tax' => 108.25,
444 'expected_results' => [
449 'title' =>
'recycling_fee',
450 'base_row_amount' => 10,
452 'base_row_amount_incl_tax' => 10.82,
453 'row_amount_incl_tax' => 10.82,
456 'tax_ratio' => [
'weee' => 0.82 / 2.47],
457 'weee_tax_applied_row_amount' => 10,
458 'base_weee_tax_applied_row_amount' => 10,
462 'grand_total' => 64.95,
463 'base_grand_total' => 64.95,
464 'tax_amount' => 4.95,
465 'base_tax_amount' => 4.95,
467 'base_subtotal' => 100,
468 'subtotal_incl_tax' => 119.07,
469 'base_subtotal_incl_tax' => 119.07,
476 $result[
'last_partial_invoice'] = [
478 'previous_invoices' => [
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,
490 'base_subtotal' =>
'300',
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,
508 'title' =>
'recycling_fee',
509 'base_row_amount' => 30,
511 'base_row_amount_incl_tax' => 32.47,
512 'row_amount_incl_tax' => 32.47,
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,
534 'include_in_subtotal' =>
false,
536 'grand_total' => 54.95,
537 'base_grand_total' => 54.95,
538 'tax_amount' => 4.95,
539 'base_tax_amount' => 4.95,
541 'base_subtotal' => 100,
542 'subtotal_incl_tax' => 104.95,
543 'base_subtotal_incl_tax' => 104.95,
546 'expected_results' => [
551 'title' =>
'recycling_fee',
552 'base_row_amount' => 10,
554 'base_row_amount_incl_tax' => 10.82,
555 'row_amount_incl_tax' => 10.82,
558 'tax_ratio' => [
'weee' => 0.83 / 2.47],
559 'weee_tax_applied_row_amount' => 10,
560 'base_weee_tax_applied_row_amount' => 10,
565 'grand_total' => 64.95,
566 'base_grand_total' => 64.95,
567 'tax_amount' => 4.95,
568 'base_tax_amount' => 4.95,
570 'base_subtotal' => 100,
571 'subtotal_incl_tax' => 114.95,
572 'base_subtotal_incl_tax' => 114.95,
580 'previous_invoices' => [
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,
589 'base_tax_amount' => 16.09,
590 'base_tax_amount_invoiced' => 0,
592 'base_subtotal' =>
'300',
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,
610 'title' =>
'recycling_fee',
611 'base_row_amount' => 30,
613 'base_row_amount_incl_tax' => 32.47,
614 'row_amount_incl_tax' => 32.47,
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,
636 'include_in_subtotal' =>
false,
638 'grand_total' => 181.09,
639 'base_grand_total' => 181.09,
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,
648 'expected_results' => [
653 'title' =>
'recycling_fee',
654 'base_row_amount' => 0,
656 'base_row_amount_incl_tax' => 0,
657 'row_amount_incl_tax' => 0,
664 'base_subtotal' => 300,
676 protected function getInvoiceItem($invoiceItemData)
683 foreach ($invoiceItemData[
'order_item'] as $key =>
$value) {
687 $this->weeeData->expects($this->once())
688 ->method(
'getRowWeeeTaxInclTax')
690 ->will($this->returnValue(
$orderItem->getRowWeeeTaxInclTax()));
691 $this->weeeData->expects($this->once())
692 ->method(
'getBaseRowWeeeTaxInclTax')
694 ->will($this->returnValue(
$orderItem->getBaseRowWeeeTaxInclTax()));
695 if ($invoiceItemData[
'is_last']) {
696 $this->weeeData->expects($this->once())
697 ->method(
'getWeeeAmountInvoiced')
699 ->will($this->returnValue(
$orderItem->getWeeeAmountInvoiced()));
700 $this->weeeData->expects($this->once())
701 ->method(
'getBaseWeeeAmountInvoiced')
703 ->will($this->returnValue(
$orderItem->getBaseWeeeAmountInvoiced()));
704 $this->weeeData->expects($this->once())
705 ->method(
'getWeeeTaxAmountInvoiced')
707 ->will($this->returnValue(
$orderItem->getWeeeTaxAmountInvoiced()));
708 $this->weeeData->expects($this->once())
709 ->method(
'getBaseWeeeTaxAmountInvoiced')
711 ->will($this->returnValue(
$orderItem->getBaseWeeeTaxAmountInvoiced()));
722 ->will($this->returnValue($invoiceItemData[
'is_last']));
723 foreach ($invoiceItemData[
'data_fields'] as $key =>
$value) {
727 $this->weeeData->expects($this->any())
728 ->method(
'getApplied')
729 ->will($this->returnCallback(
731 return $item->getAppliedWeee();
735 $this->weeeData->expects($this->any())
736 ->method(
'setApplied')
737 ->will($this->returnCallback(
738 function (
$item, $weee) {
739 return $item->setAppliedWeee($weee);