25
25
use Magento \Framework \Exception \NoSuchEntityException ;
26
26
use Magento \Framework \Exception \StateException ;
27
27
use Magento \Framework \HTTP \PhpEnvironment \RemoteAddress ;
28
+ use Magento \Framework \Lock \LockManagerInterface ;
28
29
use Magento \Framework \TestFramework \Unit \Helper \ObjectManager ;
29
30
use Magento \Quote \Api \CartRepositoryInterface ;
30
31
use Magento \Quote \Model \CustomerManagement ;
@@ -292,6 +293,9 @@ protected function setUp(): void
292
293
$ this ->addressRepositoryMock = $ this ->getMockBuilder (AddressRepositoryInterface::class)
293
294
->getMockForAbstractClass ();
294
295
296
+ $ this ->lockManagerMock = $ this ->getMockBuilder (LockManagerInterface::class)
297
+ ->getMockForAbstractClass ();
298
+
295
299
$ this ->model = $ objectManager ->getObject (
296
300
QuoteManagement::class,
297
301
[
@@ -315,7 +319,8 @@ protected function setUp(): void
315
319
'customerSession ' => $ this ->customerSessionMock ,
316
320
'accountManagement ' => $ this ->accountManagementMock ,
317
321
'quoteFactory ' => $ this ->quoteFactoryMock ,
318
- 'addressRepository ' => $ this ->addressRepositoryMock
322
+ 'addressRepository ' => $ this ->addressRepositoryMock ,
323
+ 'lockManager ' => $ this ->lockManagerMock
319
324
]
320
325
);
321
326
@@ -761,7 +766,8 @@ public function testSubmit(): void
761
766
$ customerId ,
762
767
$ quoteId ,
763
768
$ quoteItems ,
764
- $ shippingAddress
769
+ $ shippingAddress ,
770
+ false
765
771
);
766
772
767
773
$ this ->submitQuoteValidator ->expects ($ this ->once ())
@@ -826,6 +832,7 @@ public function testSubmit(): void
826
832
['order ' => $ order , 'quote ' => $ quote ]
827
833
]
828
834
);
835
+ $ this ->lockManagerMock ->method ('isLocked ' )->willReturn (false );
829
836
$ this ->quoteRepositoryMock ->expects ($ this ->once ())->method ('save ' )->with ($ quote );
830
837
$ this ->assertEquals ($ order , $ this ->model ->submit ($ quote , $ orderData ));
831
838
}
@@ -1063,7 +1070,8 @@ protected function getQuote(
1063
1070
int $ customerId ,
1064
1071
int $ id ,
1065
1072
array $ quoteItems ,
1066
- Address $ shippingAddress = null
1073
+ Address $ shippingAddress = null ,
1074
+ bool $ setIsActive
1067
1075
): MockObject {
1068
1076
$ quote = $ this ->getMockBuilder (Quote::class)
1069
1077
->addMethods (['getCustomerEmail ' , 'getCustomerId ' ])
@@ -1085,9 +1093,11 @@ protected function getQuote(
1085
1093
)
1086
1094
->disableOriginalConstructor ()
1087
1095
->getMock ();
1088
- $ quote ->expects ($ this ->once ())
1089
- ->method ('setIsActive ' )
1090
- ->with (false );
1096
+ if ($ setIsActive ) {
1097
+ $ quote ->expects ($ this ->once ())
1098
+ ->method ('setIsActive ' )
1099
+ ->with (false );
1100
+ }
1091
1101
$ quote ->expects ($ this ->any ())
1092
1102
->method ('getAllVisibleItems ' )
1093
1103
->willReturn ($ quoteItems );
@@ -1129,7 +1139,7 @@ protected function getQuote(
1129
1139
$ quote ->expects ($ this ->any ())
1130
1140
->method ('getCustomer ' )
1131
1141
->willReturn ($ customer );
1132
- $ quote ->expects ($ this ->once ( ))
1142
+ $ quote ->expects ($ this ->exactly ( 2 ))
1133
1143
->method ('getId ' )
1134
1144
->willReturn ($ id );
1135
1145
$ this ->customerRepositoryMock ->expects ($ this ->any ())->method ('getById ' )->willReturn ($ customer );
@@ -1292,7 +1302,8 @@ public function testSubmitForCustomer(): void
1292
1302
$ customerId ,
1293
1303
$ quoteId ,
1294
1304
$ quoteItems ,
1295
- $ shippingAddress
1305
+ $ shippingAddress ,
1306
+ false
1296
1307
);
1297
1308
1298
1309
$ this ->submitQuoteValidator ->method ('validateQuote ' )
@@ -1386,4 +1397,110 @@ private function createPartialMockForAbstractClass(string $className, array $met
1386
1397
$ methods
1387
1398
);
1388
1399
}
1400
+
1401
+ /**
1402
+ * @return void
1403
+ *
1404
+ * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
1405
+ */
1406
+ public function testSubmitWithLockException (): void
1407
+ {
1408
+ $ orderData = [];
1409
+ $ isGuest = true ;
1410
+ $ isVirtual = false ;
1411
+ $ customerId = 1 ;
1412
+ $ quoteId = 1 ;
1413
+ $ quoteItem = $ this ->createMock (Item::class);
1414
+ $ billingAddress = $ this ->createMock (Address::class);
1415
+ $ shippingAddress = $ this ->getMockBuilder (Address::class)
1416
+ ->addMethods (['getQuoteId ' ])
1417
+ ->onlyMethods (['getShippingMethod ' , 'getId ' ])
1418
+ ->disableOriginalConstructor ()
1419
+ ->getMock ();
1420
+ $ payment = $ this ->createMock (Payment::class);
1421
+ $ baseOrder = $ this ->getMockForAbstractClass (OrderInterface::class);
1422
+ $ convertedBilling = $ this ->createPartialMockForAbstractClass (OrderAddressInterface::class, ['setData ' ]);
1423
+ $ convertedShipping = $ this ->createPartialMockForAbstractClass (OrderAddressInterface::class, ['setData ' ]);
1424
+ $ convertedPayment = $ this ->getMockForAbstractClass (OrderPaymentInterface::class);
1425
+ $ convertedQuoteItem = $ this ->getMockForAbstractClass (OrderItemInterface::class);
1426
+ $ addresses = [$ convertedShipping , $ convertedBilling ];
1427
+ $ quoteItems = [$ quoteItem ];
1428
+ $ convertedItems = [$ convertedQuoteItem ];
1429
+ $ quote = $ this ->getQuote (
1430
+ $ isGuest ,
1431
+ $ isVirtual ,
1432
+ $ billingAddress ,
1433
+ $ payment ,
1434
+ $ customerId ,
1435
+ $ quoteId ,
1436
+ $ quoteItems ,
1437
+ $ shippingAddress ,
1438
+ false
1439
+ );
1440
+
1441
+ $ this ->submitQuoteValidator ->expects ($ this ->once ())
1442
+ ->method ('validateQuote ' )
1443
+ ->with ($ quote );
1444
+ $ this ->quoteAddressToOrder ->expects ($ this ->once ())
1445
+ ->method ('convert ' )
1446
+ ->with ($ shippingAddress , $ orderData )
1447
+ ->willReturn ($ baseOrder );
1448
+ $ this ->quoteAddressToOrderAddress
1449
+ ->method ('convert ' )
1450
+ ->withConsecutive (
1451
+ [
1452
+ $ shippingAddress ,
1453
+ [
1454
+ 'address_type ' => 'shipping ' ,
1455
+ 'email ' => 'customer@example.com '
1456
+ ]
1457
+ ],
1458
+ [
1459
+ $ billingAddress ,
1460
+ [
1461
+ 'address_type ' => 'billing ' ,
1462
+ 'email ' => 'customer@example.com '
1463
+ ]
1464
+ ]
1465
+ )->willReturnOnConsecutiveCalls ($ convertedShipping , $ convertedBilling );
1466
+ $ billingAddress ->expects ($ this ->once ())->method ('getId ' )->willReturn (4 );
1467
+ $ convertedBilling ->expects ($ this ->once ())->method ('setData ' )->with ('quote_address_id ' , 4 );
1468
+
1469
+ $ this ->quoteItemToOrderItem ->expects ($ this ->once ())->method ('convert ' )
1470
+ ->with ($ quoteItem , ['parent_item ' => null ])
1471
+ ->willReturn ($ convertedQuoteItem );
1472
+ $ this ->quotePaymentToOrderPayment ->expects ($ this ->once ())->method ('convert ' )->with ($ payment )
1473
+ ->willReturn ($ convertedPayment );
1474
+ $ shippingAddress ->expects ($ this ->once ())->method ('getShippingMethod ' )->willReturn ('free ' );
1475
+ $ shippingAddress ->expects ($ this ->once ())->method ('getId ' )->willReturn (5 );
1476
+ $ convertedShipping ->expects ($ this ->once ())->method ('setData ' )->with ('quote_address_id ' , 5 );
1477
+ $ order = $ this ->prepareOrderFactory (
1478
+ $ baseOrder ,
1479
+ $ convertedBilling ,
1480
+ $ addresses ,
1481
+ $ convertedPayment ,
1482
+ $ convertedItems ,
1483
+ $ quoteId ,
1484
+ $ convertedShipping
1485
+ );
1486
+
1487
+ $ this ->eventManager
1488
+ ->method ('dispatch ' )
1489
+ ->withConsecutive (
1490
+ [
1491
+ 'sales_model_service_quote_submit_before ' ,
1492
+ ['order ' => $ order , 'quote ' => $ quote ]
1493
+ ],
1494
+ [
1495
+ 'sales_model_service_quote_submit_success ' ,
1496
+ ['order ' => $ order , 'quote ' => $ quote ]
1497
+ ]
1498
+ );
1499
+ $ this ->lockManagerMock ->method ('isLocked ' )->willReturn (true );
1500
+
1501
+ $ this ->expectExceptionMessage (
1502
+ 'A server error stopped your order from being placed. Please try to place your order again. '
1503
+ );
1504
+ $ this ->assertEquals ($ order , $ this ->model ->submit ($ quote , $ orderData ));
1505
+ }
1389
1506
}
0 commit comments