14
14
use Magento \Framework \TestFramework \Unit \Helper \ObjectManager ;
15
15
use Magento \Sales \Model \Order \Creditmemo ;
16
16
use Magento \Sales \Model \Order \Creditmemo \Total \Shipping ;
17
+ use Magento \Tax \Model \Calculation as TaxCalculation ;
17
18
use Magento \Tax \Model \Config ;
18
19
use PHPUnit \Framework \MockObject \MockObject ;
19
20
use PHPUnit \Framework \TestCase ;
@@ -104,11 +105,11 @@ public function testCollectException()
104
105
->with ($ allowedShippingAmount , null , false )
105
106
->willReturn ($ allowedShippingAmount );
106
107
107
- $ order = new DataObject (
108
+ $ order = $ this -> getOrderMock (
108
109
[
109
- 'base_shipping_amount ' => $ orderShippingAmount ,
110
- 'base_shipping_refunded ' => $ orderShippingRefunded ,
111
- 'base_currency ' => $ currencyMock,
110
+ 'base_shipping_amount ' => $ orderShippingAmount ,
111
+ 'base_shipping_refunded ' => $ orderShippingRefunded ,
112
+ 'base_currency ' => $ currencyMock
112
113
]
113
114
);
114
115
@@ -126,6 +127,20 @@ public function testCollectException()
126
127
$ this ->shippingCollector ->collect ($ this ->creditmemoMock );
127
128
}
128
129
130
+ private function getOrderMock ($ data )
131
+ {
132
+ $ orderMock = $ this ->getMockBuilder (\Magento \Sales \Model \Order::class)
133
+ ->disableOriginalConstructor ()
134
+ ->getMock ();
135
+
136
+ foreach ($ data as $ method => $ returnValue ) {
137
+ $ orderMock
138
+ ->method ('get ' . str_replace ('_ ' , '' , ucwords ($ method , '_ ' )))
139
+ ->willReturn ($ returnValue );
140
+ }
141
+ return $ orderMock ;
142
+ }
143
+
129
144
/**
130
145
* situation: The admin user did *not* specify any desired refund amount
131
146
*
@@ -154,7 +169,7 @@ public function testCollectNoSpecifiedShippingAmount()
154
169
155
170
$ this ->taxConfig ->expects ($ this ->any ())->method ('displaySalesShippingInclTax ' )->willReturn (false );
156
171
157
- $ order = new DataObject (
172
+ $ order = $ this -> getOrderMock (
158
173
[
159
174
'shipping_amount ' => $ orderShippingAmount ,
160
175
'shipping_refunded ' => $ orderShippingRefunded ,
@@ -241,7 +256,7 @@ public function testCollectWithSpecifiedShippingAmount($ratio)
241
256
242
257
$ this ->taxConfig ->expects ($ this ->any ())->method ('displaySalesShippingInclTax ' )->willReturn (false );
243
258
244
- $ order = new DataObject (
259
+ $ order = $ this -> getOrderMock (
245
260
[
246
261
'shipping_amount ' => $ orderShippingAmount ,
247
262
'shipping_refunded ' => $ orderShippingAmountRefunded ,
@@ -346,7 +361,7 @@ public function testCollectUsingTaxInclShippingAmount()
346
361
$ expectedGrandTotal = $ grandTotalBefore + $ expectedShippingAmount ;
347
362
$ expectedBaseGrandTtoal = $ baseGrandTotalBefore + $ expectedBaseShippingAmount ;
348
363
349
- $ order = new DataObject (
364
+ $ order = $ this -> getOrderMock (
350
365
[
351
366
'shipping_amount ' => $ orderShippingAmount ,
352
367
'base_shipping_amount ' => $ baseOrderShippingAmount ,
@@ -405,6 +420,7 @@ public function testCollectUsingTaxInclShippingAmount()
405
420
406
421
$ this ->shippingCollector ->collect ($ this ->creditmemoMock );
407
422
}
423
+
408
424
/**
409
425
* situation: The admin user did *not* specify any desired refund amount
410
426
*
@@ -434,7 +450,7 @@ public function testCollectRefundShippingAmountIncTax()
434
450
435
451
$ this ->taxConfig ->expects ($ this ->any ())->method ('displaySalesShippingInclTax ' )->willReturn (false );
436
452
437
- $ order = new DataObject (
453
+ $ order = $ this -> getOrderMock (
438
454
[
439
455
'shipping_amount ' => $ orderShippingAmount ,
440
456
'shipping_refunded ' => $ orderShippingRefunded ,
@@ -489,4 +505,102 @@ public function testCollectRefundShippingAmountIncTax()
489
505
->willReturnSelf ();
490
506
$ this ->shippingCollector ->collect ($ this ->creditmemoMock );
491
507
}
508
+
509
+ /**
510
+ * situation: The admin user specified the desired refund amount that has taxes and discount embedded within it
511
+ *
512
+ * @throws LocalizedException
513
+ */
514
+ public function testCollectUsingShippingInclTaxAndDiscountOnExclBeforeTax ()
515
+ {
516
+ $ this ->taxConfig ->expects ($ this ->any ())->method ('displaySalesShippingInclTax ' )->willReturn (true );
517
+ $ this ->taxConfig ->expects ($ this ->any ())
518
+ ->method ('getCalculationSequence ' )
519
+ ->willReturn (TaxCalculation::CALC_TAX_AFTER_DISCOUNT_ON_EXCL );
520
+
521
+ $ orderShippingAmount = 14.55 ;
522
+ $ shippingTaxAmount = 0.45 ;
523
+ $ shippingDiscountAmount = 10 ;
524
+ $ orderShippingInclTax = 15 ;
525
+ $ orderShippingAmountRefunded = 7.27 ;
526
+ $ orderShippingAmountInclTaxRefunded = 8 ;
527
+ $ shippingTaxRefunded = 0.24 ;
528
+
529
+ $ currencyMultiple = 2 ;
530
+ $ baseOrderShippingAmount = $ orderShippingAmount * $ currencyMultiple ;
531
+ $ baseShippingTaxAmount = $ shippingTaxAmount * $ currencyMultiple ;
532
+ $ baseOrderShippingInclTax = $ orderShippingInclTax * $ currencyMultiple ;
533
+ $ baseOrderShippingAmountRefunded = $ orderShippingAmountRefunded * $ currencyMultiple ;
534
+ $ baseShippingTaxRefunded = $ shippingTaxRefunded * $ currencyMultiple ;
535
+
536
+ //determine expected amounts
537
+ $ expectedShippingAmount = $ orderShippingAmount - $ orderShippingAmountRefunded ;
538
+ $ expectedShippingAmountInclTax = $ orderShippingInclTax - $ orderShippingAmountInclTaxRefunded ;
539
+
540
+ $ expectedBaseShippingAmount = $ expectedShippingAmount * $ currencyMultiple ;
541
+ $ expectedBaseShippingAmountInclTax = $ expectedShippingAmountInclTax * $ currencyMultiple ;
542
+
543
+ $ grandTotalBefore = 27 ;
544
+ $ baseGrandTotalBefore = $ grandTotalBefore * $ currencyMultiple ;
545
+ $ expectedGrandTotal = $ grandTotalBefore + $ expectedShippingAmount ;
546
+ $ expectedBaseGrandTotal = $ baseGrandTotalBefore + $ expectedBaseShippingAmount ;
547
+
548
+ $ order = $ this ->getOrderMock (
549
+ [
550
+ 'shipping_amount ' => $ orderShippingAmount ,
551
+ 'base_shipping_amount ' => $ baseOrderShippingAmount ,
552
+ 'shipping_refunded ' => $ orderShippingAmountRefunded ,
553
+ 'base_shipping_refunded ' => $ baseOrderShippingAmountRefunded ,
554
+ 'shipping_incl_tax ' => $ orderShippingInclTax ,
555
+ 'base_shipping_incl_tax ' => $ baseOrderShippingInclTax ,
556
+ 'shipping_tax_amount ' => $ shippingTaxAmount ,
557
+ 'shipping_tax_refunded ' => $ shippingTaxRefunded ,
558
+ 'base_shipping_tax_amount ' => $ baseShippingTaxAmount ,
559
+ 'base_shipping_tax_refunded ' => $ baseShippingTaxRefunded ,
560
+ 'shipping_discount_amount ' => $ shippingDiscountAmount
561
+ ]
562
+ );
563
+ $ orderCreditMemo = $ this ->createMock (Creditmemo::class);
564
+ $ orderCreditMemo ->expects ($ this ->atLeastOnce ())
565
+ ->method ('getShippingInclTax ' )
566
+ ->willReturn ($ orderShippingAmountInclTaxRefunded );
567
+ $ orderCreditMemo ->expects ($ this ->atLeastOnce ())
568
+ ->method ('getBaseShippingInclTax ' )
569
+ ->willReturn ($ orderShippingAmountInclTaxRefunded * $ currencyMultiple );
570
+ $ order ->expects ($ this ->atLeastOnce ())
571
+ ->method ('getCreditmemosCollection ' )
572
+ ->willReturn ([$ orderCreditMemo ]);
573
+
574
+ $ this ->creditmemoMock ->expects ($ this ->once ())->method ('getOrder ' )->willReturn ($ order );
575
+ $ this ->creditmemoMock ->expects ($ this ->once ())->method ('hasBaseShippingAmount ' )->willReturn (false );
576
+ $ this ->creditmemoMock ->expects ($ this ->once ())->method ('getGrandTotal ' )->willReturn ($ grandTotalBefore );
577
+ $ this ->creditmemoMock ->expects ($ this ->once ())->method ('getBaseGrandTotal ' )->willReturn ($ baseGrandTotalBefore );
578
+
579
+ //verify
580
+ $ this ->creditmemoMock ->expects ($ this ->once ())
581
+ ->method ('setShippingAmount ' )
582
+ ->with ($ expectedShippingAmount )
583
+ ->willReturnSelf ();
584
+ $ this ->creditmemoMock ->expects ($ this ->once ())
585
+ ->method ('setBaseShippingAmount ' )
586
+ ->with ($ expectedBaseShippingAmount )
587
+ ->willReturnSelf ();
588
+ $ this ->creditmemoMock ->expects ($ this ->once ())
589
+ ->method ('setShippingInclTax ' )
590
+ ->with ($ expectedShippingAmountInclTax )
591
+ ->willReturnSelf ();
592
+ $ this ->creditmemoMock ->expects ($ this ->once ())
593
+ ->method ('setBaseShippingInclTax ' )
594
+ ->with ($ expectedBaseShippingAmountInclTax )
595
+ ->willReturnSelf ();
596
+ $ this ->creditmemoMock ->expects ($ this ->once ())
597
+ ->method ('setGrandTotal ' )
598
+ ->with ($ expectedGrandTotal )
599
+ ->willReturnSelf ();
600
+ $ this ->creditmemoMock ->expects ($ this ->once ())
601
+ ->method ('setBaseGrandTotal ' )
602
+ ->with ($ expectedBaseGrandTotal )
603
+ ->willReturnSelf ();
604
+ $ this ->shippingCollector ->collect ($ this ->creditmemoMock );
605
+ }
492
606
}
0 commit comments