55 */
66namespace Magento \Sales \Test \Unit \Model \Service ;
77
8+ use Magento \Framework \Pricing \PriceCurrencyInterface ;
89use Magento \Framework \TestFramework \Unit \Helper \ObjectManager as ObjectManagerHelper ;
10+ use Magento \Sales \Api \Data \CreditmemoInterface ;
11+ use Magento \Sales \Model \Order ;
12+ use Magento \Sales \Model \Order \Creditmemo ;
13+ use Magento \Sales \Model \Order \Creditmemo \Item ;
914
1015/**
1116 * Class CreditmemoServiceTest
@@ -37,6 +42,11 @@ class CreditmemoServiceTest extends \PHPUnit_Framework_TestCase
3742 */
3843 protected $ creditmemoNotifierMock ;
3944
45+ /**
46+ * @var PriceCurrencyInterface|\PHPUnit_Framework_MockObject_MockObject
47+ */
48+ private $ priceCurrencyMock ;
49+
4050 /**
4151 * @var \Magento\Sales\Model\Service\CreditmemoService
4252 */
@@ -82,6 +92,7 @@ protected function setUp()
8292 '' ,
8393 false
8494 );
95+ $ this ->priceCurrencyMock = $ this ->getMockBuilder (PriceCurrencyInterface::class)->getMockForAbstractClass ();
8596
8697 $ this ->creditmemoService = $ objectManager ->getObject (
8798 'Magento\Sales\Model\Service\CreditmemoService ' ,
@@ -90,7 +101,8 @@ protected function setUp()
90101 'creditmemoCommentRepository ' => $ this ->creditmemoCommentRepositoryMock ,
91102 'searchCriteriaBuilder ' => $ this ->searchCriteriaBuilderMock ,
92103 'filterBuilder ' => $ this ->filterBuilderMock ,
93- 'creditmemoNotifier ' => $ this ->creditmemoNotifierMock
104+ 'creditmemoNotifier ' => $ this ->creditmemoNotifierMock ,
105+ 'priceCurrency ' => $ this ->priceCurrencyMock
94106 ]
95107 );
96108 }
@@ -183,4 +195,69 @@ public function testNotify()
183195
184196 $ this ->assertEquals ($ returnValue , $ this ->creditmemoService ->notify ($ id ));
185197 }
198+
199+ public function testRefund ()
200+ {
201+ $ creditMemoMock = $ this ->getMockBuilder (Creditmemo::class)
202+ ->setMethods (['getId ' , 'getOrder ' , 'getBaseGrandTotal ' , 'getAllItems ' , 'setDoTransaction ' ])
203+ ->disableOriginalConstructor ()
204+ ->getMock ();
205+ $ creditMemoMock ->expects ($ this ->once ())->method ('getId ' )->willReturn (null );
206+ $ orderMock = $ this ->getMockBuilder (Order::class)->disableOriginalConstructor ()->getMock ();
207+ $ creditMemoMock ->expects ($ this ->atLeastOnce ())->method ('getOrder ' )->willReturn ($ orderMock );
208+ $ itemMock = $ this ->getMockBuilder (
209+ Item::class
210+ )->disableOriginalConstructor ()->getMock ();
211+ $ creditMemoMock ->expects ($ this ->once ())->method ('getAllItems ' )->willReturn ([$ itemMock ]);
212+ $ itemMock ->expects ($ this ->once ()) -> method ('setCreditMemo ' )->with ($ creditMemoMock );
213+ $ itemMock ->expects ($ this ->once ()) -> method ('getQty ' )->willReturn (1 );
214+ $ itemMock ->expects ($ this ->once ()) -> method ('register ' );
215+ $ creditMemoMock ->expects ($ this ->once ())->method ('setDoTransaction ' )->with (false );
216+ $ this ->assertSame ($ creditMemoMock , $ this ->creditmemoService ->refund ($ creditMemoMock , true ));
217+ }
218+
219+ /**
220+ * @expectedExceptionMessage The most money available to refund is 1.
221+ * @expectedException \Magento\Framework\Exception\LocalizedException
222+ */
223+ public function testRefundExpectsMoneyAvailableToReturn ()
224+ {
225+ $ baseGrandTotal = 10 ;
226+ $ baseTotalRefunded = 9 ;
227+ $ baseTotalPaid = 10 ;
228+ $ creditMemoMock = $ this ->getMockBuilder (CreditmemoInterface::class)
229+ ->setMethods (['getId ' , 'getOrder ' , 'getBaseGrandTotal ' , 'formatBasePrice ' ])
230+ ->getMockForAbstractClass ();
231+ $ creditMemoMock ->expects ($ this ->once ())->method ('getId ' )->willReturn (null );
232+ $ orderMock = $ this ->getMockBuilder (Order::class)->disableOriginalConstructor ()->getMock ();
233+ $ creditMemoMock ->expects ($ this ->atLeastOnce ())->method ('getOrder ' )->willReturn ($ orderMock );
234+ $ creditMemoMock ->expects ($ this ->once ())->method ('getBaseGrandTotal ' )->willReturn ($ baseGrandTotal );
235+ $ orderMock ->expects ($ this ->atLeastOnce ())->method ('getBaseTotalRefunded ' )->willReturn ($ baseTotalRefunded );
236+ $ this ->priceCurrencyMock ->expects ($ this ->exactly (2 ))->method ('round ' )->withConsecutive (
237+ [$ baseTotalRefunded + $ baseGrandTotal ],
238+ [$ baseTotalPaid ]
239+ )->willReturnOnConsecutiveCalls (
240+ $ baseTotalRefunded + $ baseGrandTotal ,
241+ $ baseTotalPaid
242+ );
243+ $ orderMock ->expects ($ this ->atLeastOnce ())->method ('getBaseTotalPaid ' )->willReturn ($ baseTotalPaid );
244+ $ baseAvailableRefund = $ baseTotalPaid - $ baseTotalRefunded ;
245+ $ orderMock ->expects ($ this ->once ())->method ('formatBasePrice ' )->with (
246+ $ baseAvailableRefund
247+ )->willReturn ($ baseAvailableRefund );
248+ $ this ->creditmemoService ->refund ($ creditMemoMock , true );
249+ }
250+
251+ /**
252+ * @expectedExceptionMessage We cannot register an existing credit memo.
253+ * @expectedException \Magento\Framework\Exception\LocalizedException
254+ */
255+ public function testRefundDoNotExpectsId ()
256+ {
257+ $ creditMemoMock = $ this ->getMockBuilder (CreditmemoInterface::class)
258+ ->setMethods (['getId ' ])
259+ ->getMockForAbstractClass ();
260+ $ creditMemoMock ->expects ($ this ->once ())->method ('getId ' )->willReturn (444 );
261+ $ this ->creditmemoService ->refund ($ creditMemoMock , true );
262+ }
186263}
0 commit comments