1414use Magento \Framework \TestFramework \Unit \Helper \ObjectManager ;
1515use Magento \Sales \Model \Order \Creditmemo ;
1616use Magento \Sales \Model \Order \Creditmemo \Total \Shipping ;
17+ use Magento \Tax \Model \Calculation as TaxCalculation ;
1718use Magento \Tax \Model \Config ;
1819use PHPUnit \Framework \MockObject \MockObject ;
1920use PHPUnit \Framework \TestCase ;
@@ -104,11 +105,11 @@ public function testCollectException()
104105 ->with ($ allowedShippingAmount , null , false )
105106 ->willReturn ($ allowedShippingAmount );
106107
107- $ order = new DataObject (
108+ $ order = $ this -> getOrderMock (
108109 [
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
112113 ]
113114 );
114115
@@ -126,6 +127,20 @@ public function testCollectException()
126127 $ this ->shippingCollector ->collect ($ this ->creditmemoMock );
127128 }
128129
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+
129144 /**
130145 * situation: The admin user did *not* specify any desired refund amount
131146 *
@@ -154,7 +169,7 @@ public function testCollectNoSpecifiedShippingAmount()
154169
155170 $ this ->taxConfig ->expects ($ this ->any ())->method ('displaySalesShippingInclTax ' )->willReturn (false );
156171
157- $ order = new DataObject (
172+ $ order = $ this -> getOrderMock (
158173 [
159174 'shipping_amount ' => $ orderShippingAmount ,
160175 'shipping_refunded ' => $ orderShippingRefunded ,
@@ -241,7 +256,7 @@ public function testCollectWithSpecifiedShippingAmount($ratio)
241256
242257 $ this ->taxConfig ->expects ($ this ->any ())->method ('displaySalesShippingInclTax ' )->willReturn (false );
243258
244- $ order = new DataObject (
259+ $ order = $ this -> getOrderMock (
245260 [
246261 'shipping_amount ' => $ orderShippingAmount ,
247262 'shipping_refunded ' => $ orderShippingAmountRefunded ,
@@ -346,7 +361,7 @@ public function testCollectUsingTaxInclShippingAmount()
346361 $ expectedGrandTotal = $ grandTotalBefore + $ expectedShippingAmount ;
347362 $ expectedBaseGrandTtoal = $ baseGrandTotalBefore + $ expectedBaseShippingAmount ;
348363
349- $ order = new DataObject (
364+ $ order = $ this -> getOrderMock (
350365 [
351366 'shipping_amount ' => $ orderShippingAmount ,
352367 'base_shipping_amount ' => $ baseOrderShippingAmount ,
@@ -405,6 +420,7 @@ public function testCollectUsingTaxInclShippingAmount()
405420
406421 $ this ->shippingCollector ->collect ($ this ->creditmemoMock );
407422 }
423+
408424 /**
409425 * situation: The admin user did *not* specify any desired refund amount
410426 *
@@ -434,7 +450,7 @@ public function testCollectRefundShippingAmountIncTax()
434450
435451 $ this ->taxConfig ->expects ($ this ->any ())->method ('displaySalesShippingInclTax ' )->willReturn (false );
436452
437- $ order = new DataObject (
453+ $ order = $ this -> getOrderMock (
438454 [
439455 'shipping_amount ' => $ orderShippingAmount ,
440456 'shipping_refunded ' => $ orderShippingRefunded ,
@@ -489,4 +505,102 @@ public function testCollectRefundShippingAmountIncTax()
489505 ->willReturnSelf ();
490506 $ this ->shippingCollector ->collect ($ this ->creditmemoMock );
491507 }
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+ }
492606}
0 commit comments