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 ;
@@ -196,6 +197,11 @@ class QuoteManagementTest extends TestCase
196
197
*/
197
198
private $ quoteIdMaskFactoryMock ;
198
199
200
+ /**
201
+ * @var LockManagerInterface|MockObject
202
+ */
203
+ private $ lockManagerMock ;
204
+
199
205
/**
200
206
* @inheriDoc
201
207
*
@@ -292,6 +298,9 @@ protected function setUp(): void
292
298
$ this ->addressRepositoryMock = $ this ->getMockBuilder (AddressRepositoryInterface::class)
293
299
->getMockForAbstractClass ();
294
300
301
+ $ this ->lockManagerMock = $ this ->getMockBuilder (LockManagerInterface::class)
302
+ ->getMockForAbstractClass ();
303
+
295
304
$ this ->model = $ objectManager ->getObject (
296
305
QuoteManagement::class,
297
306
[
@@ -315,7 +324,8 @@ protected function setUp(): void
315
324
'customerSession ' => $ this ->customerSessionMock ,
316
325
'accountManagement ' => $ this ->accountManagementMock ,
317
326
'quoteFactory ' => $ this ->quoteFactoryMock ,
318
- 'addressRepository ' => $ this ->addressRepositoryMock
327
+ 'addressRepository ' => $ this ->addressRepositoryMock ,
328
+ 'lockManager ' => $ this ->lockManagerMock
319
329
]
320
330
);
321
331
@@ -761,7 +771,8 @@ public function testSubmit(): void
761
771
$ customerId ,
762
772
$ quoteId ,
763
773
$ quoteItems ,
764
- $ shippingAddress
774
+ $ shippingAddress ,
775
+ false
765
776
);
766
777
767
778
$ this ->submitQuoteValidator ->expects ($ this ->once ())
@@ -826,6 +837,7 @@ public function testSubmit(): void
826
837
['order ' => $ order , 'quote ' => $ quote ]
827
838
]
828
839
);
840
+ $ this ->lockManagerMock ->method ('isLocked ' )->willReturn (false );
829
841
$ this ->quoteRepositoryMock ->expects ($ this ->once ())->method ('save ' )->with ($ quote );
830
842
$ this ->assertEquals ($ order , $ this ->model ->submit ($ quote , $ orderData ));
831
843
}
@@ -1063,7 +1075,8 @@ protected function getQuote(
1063
1075
int $ customerId ,
1064
1076
int $ id ,
1065
1077
array $ quoteItems ,
1066
- Address $ shippingAddress = null
1078
+ Address $ shippingAddress = null ,
1079
+ bool $ setIsActive
1067
1080
): MockObject {
1068
1081
$ quote = $ this ->getMockBuilder (Quote::class)
1069
1082
->addMethods (['getCustomerEmail ' , 'getCustomerId ' ])
@@ -1085,9 +1098,11 @@ protected function getQuote(
1085
1098
)
1086
1099
->disableOriginalConstructor ()
1087
1100
->getMock ();
1088
- $ quote ->expects ($ this ->once ())
1089
- ->method ('setIsActive ' )
1090
- ->with (false );
1101
+ if ($ setIsActive ) {
1102
+ $ quote ->expects ($ this ->once ())
1103
+ ->method ('setIsActive ' )
1104
+ ->with (false );
1105
+ }
1091
1106
$ quote ->expects ($ this ->any ())
1092
1107
->method ('getAllVisibleItems ' )
1093
1108
->willReturn ($ quoteItems );
@@ -1129,7 +1144,7 @@ protected function getQuote(
1129
1144
$ quote ->expects ($ this ->any ())
1130
1145
->method ('getCustomer ' )
1131
1146
->willReturn ($ customer );
1132
- $ quote ->expects ($ this ->once ( ))
1147
+ $ quote ->expects ($ this ->exactly ( 2 ))
1133
1148
->method ('getId ' )
1134
1149
->willReturn ($ id );
1135
1150
$ this ->customerRepositoryMock ->expects ($ this ->any ())->method ('getById ' )->willReturn ($ customer );
@@ -1292,7 +1307,8 @@ public function testSubmitForCustomer(): void
1292
1307
$ customerId ,
1293
1308
$ quoteId ,
1294
1309
$ quoteItems ,
1295
- $ shippingAddress
1310
+ $ shippingAddress ,
1311
+ false
1296
1312
);
1297
1313
1298
1314
$ this ->submitQuoteValidator ->method ('validateQuote ' )
@@ -1386,4 +1402,110 @@ private function createPartialMockForAbstractClass(string $className, array $met
1386
1402
$ methods
1387
1403
);
1388
1404
}
1405
+
1406
+ /**
1407
+ * @return void
1408
+ *
1409
+ * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
1410
+ */
1411
+ public function testSubmitWithLockException (): void
1412
+ {
1413
+ $ orderData = [];
1414
+ $ isGuest = true ;
1415
+ $ isVirtual = false ;
1416
+ $ customerId = 1 ;
1417
+ $ quoteId = 1 ;
1418
+ $ quoteItem = $ this ->createMock (Item::class);
1419
+ $ billingAddress = $ this ->createMock (Address::class);
1420
+ $ shippingAddress = $ this ->getMockBuilder (Address::class)
1421
+ ->addMethods (['getQuoteId ' ])
1422
+ ->onlyMethods (['getShippingMethod ' , 'getId ' ])
1423
+ ->disableOriginalConstructor ()
1424
+ ->getMock ();
1425
+ $ payment = $ this ->createMock (Payment::class);
1426
+ $ baseOrder = $ this ->getMockForAbstractClass (OrderInterface::class);
1427
+ $ convertedBilling = $ this ->createPartialMockForAbstractClass (OrderAddressInterface::class, ['setData ' ]);
1428
+ $ convertedShipping = $ this ->createPartialMockForAbstractClass (OrderAddressInterface::class, ['setData ' ]);
1429
+ $ convertedPayment = $ this ->getMockForAbstractClass (OrderPaymentInterface::class);
1430
+ $ convertedQuoteItem = $ this ->getMockForAbstractClass (OrderItemInterface::class);
1431
+ $ addresses = [$ convertedShipping , $ convertedBilling ];
1432
+ $ quoteItems = [$ quoteItem ];
1433
+ $ convertedItems = [$ convertedQuoteItem ];
1434
+ $ quote = $ this ->getQuote (
1435
+ $ isGuest ,
1436
+ $ isVirtual ,
1437
+ $ billingAddress ,
1438
+ $ payment ,
1439
+ $ customerId ,
1440
+ $ quoteId ,
1441
+ $ quoteItems ,
1442
+ $ shippingAddress ,
1443
+ false
1444
+ );
1445
+
1446
+ $ this ->submitQuoteValidator ->expects ($ this ->once ())
1447
+ ->method ('validateQuote ' )
1448
+ ->with ($ quote );
1449
+ $ this ->quoteAddressToOrder ->expects ($ this ->once ())
1450
+ ->method ('convert ' )
1451
+ ->with ($ shippingAddress , $ orderData )
1452
+ ->willReturn ($ baseOrder );
1453
+ $ this ->quoteAddressToOrderAddress
1454
+ ->method ('convert ' )
1455
+ ->withConsecutive (
1456
+ [
1457
+ $ shippingAddress ,
1458
+ [
1459
+ 'address_type ' => 'shipping ' ,
1460
+ 'email ' => 'customer@example.com '
1461
+ ]
1462
+ ],
1463
+ [
1464
+ $ billingAddress ,
1465
+ [
1466
+ 'address_type ' => 'billing ' ,
1467
+ 'email ' => 'customer@example.com '
1468
+ ]
1469
+ ]
1470
+ )->willReturnOnConsecutiveCalls ($ convertedShipping , $ convertedBilling );
1471
+ $ billingAddress ->expects ($ this ->once ())->method ('getId ' )->willReturn (4 );
1472
+ $ convertedBilling ->expects ($ this ->once ())->method ('setData ' )->with ('quote_address_id ' , 4 );
1473
+
1474
+ $ this ->quoteItemToOrderItem ->expects ($ this ->once ())->method ('convert ' )
1475
+ ->with ($ quoteItem , ['parent_item ' => null ])
1476
+ ->willReturn ($ convertedQuoteItem );
1477
+ $ this ->quotePaymentToOrderPayment ->expects ($ this ->once ())->method ('convert ' )->with ($ payment )
1478
+ ->willReturn ($ convertedPayment );
1479
+ $ shippingAddress ->expects ($ this ->once ())->method ('getShippingMethod ' )->willReturn ('free ' );
1480
+ $ shippingAddress ->expects ($ this ->once ())->method ('getId ' )->willReturn (5 );
1481
+ $ convertedShipping ->expects ($ this ->once ())->method ('setData ' )->with ('quote_address_id ' , 5 );
1482
+ $ order = $ this ->prepareOrderFactory (
1483
+ $ baseOrder ,
1484
+ $ convertedBilling ,
1485
+ $ addresses ,
1486
+ $ convertedPayment ,
1487
+ $ convertedItems ,
1488
+ $ quoteId ,
1489
+ $ convertedShipping
1490
+ );
1491
+
1492
+ $ this ->eventManager
1493
+ ->method ('dispatch ' )
1494
+ ->withConsecutive (
1495
+ [
1496
+ 'sales_model_service_quote_submit_before ' ,
1497
+ ['order ' => $ order , 'quote ' => $ quote ]
1498
+ ],
1499
+ [
1500
+ 'sales_model_service_quote_submit_success ' ,
1501
+ ['order ' => $ order , 'quote ' => $ quote ]
1502
+ ]
1503
+ );
1504
+ $ this ->lockManagerMock ->method ('isLocked ' )->willReturn (true );
1505
+
1506
+ $ this ->expectExceptionMessage (
1507
+ 'A server error stopped your order from being placed. Please try to place your order again. '
1508
+ );
1509
+ $ this ->assertEquals ($ order , $ this ->model ->submit ($ quote , $ orderData ));
1510
+ }
1389
1511
}
0 commit comments