9
9
10
10
use Magento \Framework \App \ResourceConnection ;
11
11
use Magento \Framework \DB \Adapter \AdapterInterface ;
12
+ use Magento \Framework \DB \Select ;
12
13
use Magento \Sales \Api \CreditmemoRepositoryInterface ;
13
14
use Magento \Sales \Api \Data \CreditmemoCommentCreationInterface ;
14
15
use Magento \Sales \Api \Data \CreditmemoCreationArgumentsInterface ;
28
29
use Magento \Sales \Model \Order \OrderStateResolverInterface ;
29
30
use Magento \Sales \Model \Order \RefundAdapterInterface ;
30
31
use Magento \Sales \Model \Order \Validation \RefundInvoiceInterface ;
32
+ use Magento \Sales \Model \OrderMutex ;
31
33
use Magento \Sales \Model \RefundInvoice ;
32
34
use Magento \Sales \Model \ValidatorResultInterface ;
33
35
use PHPUnit \Framework \MockObject \MockObject ;
@@ -222,7 +224,8 @@ protected function setUp(): void
222
224
$ this ->creditmemoDocumentFactoryMock ,
223
225
$ this ->notifierMock ,
224
226
$ this ->configMock ,
225
- $ this ->loggerMock
227
+ $ this ->loggerMock ,
228
+ new OrderMutex ($ this ->resourceConnectionMock )
226
229
);
227
230
}
228
231
@@ -238,14 +241,17 @@ protected function setUp(): void
238
241
*/
239
242
public function testOrderCreditmemo ($ invoiceId , $ isOnline , $ items , $ notify , $ appendComment )
240
243
{
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 ))
246
252
->method ('get ' )
247
253
->willReturn ($ this ->invoiceMock );
248
- $ this ->orderRepositoryMock ->expects ($ this ->once ( ))
254
+ $ this ->orderRepositoryMock ->expects ($ this ->exactly ( 2 ))
249
255
->method ('get ' )
250
256
->willReturn ($ this ->orderMock );
251
257
$ this ->creditmemoDocumentFactoryMock ->expects ($ this ->once ())
@@ -341,11 +347,17 @@ public function testDocumentValidationException()
341
347
$ appendComment = true ;
342
348
$ isOnline = false ;
343
349
$ 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 ))
346
358
->method ('get ' )
347
359
->willReturn ($ this ->invoiceMock );
348
- $ this ->orderRepositoryMock ->expects ($ this ->once ( ))
360
+ $ this ->orderRepositoryMock ->expects ($ this ->exactly ( 2 ))
349
361
->method ('get ' )
350
362
->willReturn ($ this ->orderMock );
351
363
@@ -401,15 +413,18 @@ public function testCouldNotCreditmemoException()
401
413
$ notify = true ;
402
414
$ appendComment = true ;
403
415
$ isOnline = false ;
404
- $ this ->resourceConnectionMock ->expects ($ this ->once ())
405
- ->method ('getConnection ' )
406
- ->with ('sales ' )
407
- ->willReturn ($ this ->adapterInterface );
408
416
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 ))
410
425
->method ('get ' )
411
426
->willReturn ($ this ->invoiceMock );
412
- $ this ->orderRepositoryMock ->expects ($ this ->once ( ))
427
+ $ this ->orderRepositoryMock ->expects ($ this ->exactly ( 2 ))
413
428
->method ('get ' )
414
429
->willReturn ($ this ->orderMock );
415
430
@@ -479,4 +494,31 @@ public function dataProvider()
479
494
'TestWithNotifyFalse ' => [1 , true , [1 => $ creditmemoItemCreationMock ], false , true ],
480
495
];
481
496
}
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
+ }
482
524
}
0 commit comments