99
1010use Magento \Framework \App \ResourceConnection ;
1111use Magento \Framework \DB \Adapter \AdapterInterface ;
12+ use Magento \Framework \DB \Select ;
1213use Magento \Sales \Api \CreditmemoRepositoryInterface ;
1314use Magento \Sales \Api \Data \CreditmemoCommentCreationInterface ;
1415use Magento \Sales \Api \Data \CreditmemoCreationArgumentsInterface ;
2829use Magento \Sales \Model \Order \OrderStateResolverInterface ;
2930use Magento \Sales \Model \Order \RefundAdapterInterface ;
3031use Magento \Sales \Model \Order \Validation \RefundInvoiceInterface ;
32+ use Magento \Sales \Model \OrderMutex ;
3133use Magento \Sales \Model \RefundInvoice ;
3234use Magento \Sales \Model \ValidatorResultInterface ;
3335use PHPUnit \Framework \MockObject \MockObject ;
@@ -222,7 +224,8 @@ protected function setUp(): void
222224 $ this ->creditmemoDocumentFactoryMock ,
223225 $ this ->notifierMock ,
224226 $ this ->configMock ,
225- $ this ->loggerMock
227+ $ this ->loggerMock ,
228+ new OrderMutex ($ this ->resourceConnectionMock )
226229 );
227230 }
228231
@@ -238,14 +241,17 @@ protected function setUp(): void
238241 */
239242 public function testOrderCreditmemo ($ invoiceId , $ isOnline , $ items , $ notify , $ appendComment )
240243 {
241- $ this ->resourceConnectionMock ->expects ($ this ->once ())
242- ->method ('getConnection ' )
243- ->with ('sales ' )
244- ->willReturn ($ this ->adapterInterface );
245- $ this ->invoiceRepositoryMock ->expects ($ this ->once ())
244+ $ this ->mockConnection ($ invoiceId );
245+ $ this ->invoiceMock ->expects ($ this ->exactly (2 ))
246+ ->method ('getOrderId ' )
247+ ->willReturn ($ invoiceId );
248+ $ this ->orderMock ->expects ($ this ->once ())
249+ ->method ('getEntityId ' )
250+ ->willReturn ($ invoiceId );
251+ $ this ->invoiceRepositoryMock ->expects ($ this ->exactly (2 ))
246252 ->method ('get ' )
247253 ->willReturn ($ this ->invoiceMock );
248- $ this ->orderRepositoryMock ->expects ($ this ->once ( ))
254+ $ this ->orderRepositoryMock ->expects ($ this ->exactly ( 2 ))
249255 ->method ('get ' )
250256 ->willReturn ($ this ->orderMock );
251257 $ this ->creditmemoDocumentFactoryMock ->expects ($ this ->once ())
@@ -341,11 +347,17 @@ public function testDocumentValidationException()
341347 $ appendComment = true ;
342348 $ isOnline = false ;
343349 $ errorMessages = ['error1 ' , 'error2 ' ];
344-
345- $ this ->invoiceRepositoryMock ->expects ($ this ->once ())
350+ $ this ->mockConnection ($ invoiceId );
351+ $ this ->invoiceMock ->expects ($ this ->exactly (2 ))
352+ ->method ('getOrderId ' )
353+ ->willReturn ($ invoiceId );
354+ $ this ->orderMock ->expects ($ this ->once ())
355+ ->method ('getEntityId ' )
356+ ->willReturn ($ invoiceId );
357+ $ this ->invoiceRepositoryMock ->expects ($ this ->exactly (2 ))
346358 ->method ('get ' )
347359 ->willReturn ($ this ->invoiceMock );
348- $ this ->orderRepositoryMock ->expects ($ this ->once ( ))
360+ $ this ->orderRepositoryMock ->expects ($ this ->exactly ( 2 ))
349361 ->method ('get ' )
350362 ->willReturn ($ this ->orderMock );
351363
@@ -401,15 +413,18 @@ public function testCouldNotCreditmemoException()
401413 $ notify = true ;
402414 $ appendComment = true ;
403415 $ isOnline = false ;
404- $ this ->resourceConnectionMock ->expects ($ this ->once ())
405- ->method ('getConnection ' )
406- ->with ('sales ' )
407- ->willReturn ($ this ->adapterInterface );
408416
409- $ this ->invoiceRepositoryMock ->expects ($ this ->once ())
417+ $ this ->mockConnection ($ invoiceId );
418+ $ this ->invoiceMock ->expects ($ this ->exactly (2 ))
419+ ->method ('getOrderId ' )
420+ ->willReturn ($ invoiceId );
421+ $ this ->orderMock ->expects ($ this ->once ())
422+ ->method ('getEntityId ' )
423+ ->willReturn ($ invoiceId );
424+ $ this ->invoiceRepositoryMock ->expects ($ this ->exactly (2 ))
410425 ->method ('get ' )
411426 ->willReturn ($ this ->invoiceMock );
412- $ this ->orderRepositoryMock ->expects ($ this ->once ( ))
427+ $ this ->orderRepositoryMock ->expects ($ this ->exactly ( 2 ))
413428 ->method ('get ' )
414429 ->willReturn ($ this ->orderMock );
415430
@@ -479,4 +494,31 @@ public function dataProvider()
479494 'TestWithNotifyFalse ' => [1 , true , [1 => $ creditmemoItemCreationMock ], false , true ],
480495 ];
481496 }
497+
498+ private function mockConnection (int $ orderId )
499+ {
500+ $ select = $ this ->createMock (Select::class);
501+ $ select ->expects ($ this ->once ())
502+ ->method ('from ' )
503+ ->with ('sales_order ' , 'entity_id ' )
504+ ->willReturnSelf ();
505+ $ select ->expects ($ this ->once ())
506+ ->method ('where ' )
507+ ->with ('entity_id = ? ' , $ orderId )
508+ ->willReturnSelf ();
509+ $ select ->expects ($ this ->once ())
510+ ->method ('forUpdate ' )
511+ ->with (true )
512+ ->willReturnSelf ();
513+ $ this ->adapterInterface ->expects ($ this ->once ())
514+ ->method ('select ' )
515+ ->willReturn ($ select );
516+ $ this ->resourceConnectionMock ->expects ($ this ->once ())
517+ ->method ('getConnection ' )
518+ ->with ('sales ' )
519+ ->willReturn ($ this ->adapterInterface );
520+ $ this ->resourceConnectionMock ->expects ($ this ->once ())
521+ ->method ('getTableName ' )
522+ ->willReturnArgument (0 );
523+ }
482524}
0 commit comments