2525use Magento \Framework \Exception \NoSuchEntityException ;
2626use Magento \Framework \Exception \StateException ;
2727use Magento \Framework \HTTP \PhpEnvironment \RemoteAddress ;
28+ use Magento \Framework \Lock \LockManagerInterface ;
2829use Magento \Framework \TestFramework \Unit \Helper \ObjectManager ;
2930use Magento \Quote \Api \CartRepositoryInterface ;
3031use Magento \Quote \Model \CustomerManagement ;
@@ -292,6 +293,9 @@ protected function setUp(): void
292293 $ this ->addressRepositoryMock = $ this ->getMockBuilder (AddressRepositoryInterface::class)
293294 ->getMockForAbstractClass ();
294295
296+ $ this ->lockManagerMock = $ this ->getMockBuilder (LockManagerInterface::class)
297+ ->getMockForAbstractClass ();
298+
295299 $ this ->model = $ objectManager ->getObject (
296300 QuoteManagement::class,
297301 [
@@ -315,7 +319,8 @@ protected function setUp(): void
315319 'customerSession ' => $ this ->customerSessionMock ,
316320 'accountManagement ' => $ this ->accountManagementMock ,
317321 'quoteFactory ' => $ this ->quoteFactoryMock ,
318- 'addressRepository ' => $ this ->addressRepositoryMock
322+ 'addressRepository ' => $ this ->addressRepositoryMock ,
323+ 'lockManager ' => $ this ->lockManagerMock
319324 ]
320325 );
321326
@@ -761,7 +766,8 @@ public function testSubmit(): void
761766 $ customerId ,
762767 $ quoteId ,
763768 $ quoteItems ,
764- $ shippingAddress
769+ $ shippingAddress ,
770+ false
765771 );
766772
767773 $ this ->submitQuoteValidator ->expects ($ this ->once ())
@@ -826,6 +832,7 @@ public function testSubmit(): void
826832 ['order ' => $ order , 'quote ' => $ quote ]
827833 ]
828834 );
835+ $ this ->lockManagerMock ->method ('isLocked ' )->willReturn (false );
829836 $ this ->quoteRepositoryMock ->expects ($ this ->once ())->method ('save ' )->with ($ quote );
830837 $ this ->assertEquals ($ order , $ this ->model ->submit ($ quote , $ orderData ));
831838 }
@@ -1063,7 +1070,8 @@ protected function getQuote(
10631070 int $ customerId ,
10641071 int $ id ,
10651072 array $ quoteItems ,
1066- Address $ shippingAddress = null
1073+ Address $ shippingAddress = null ,
1074+ bool $ setIsActive
10671075 ): MockObject {
10681076 $ quote = $ this ->getMockBuilder (Quote::class)
10691077 ->addMethods (['getCustomerEmail ' , 'getCustomerId ' ])
@@ -1085,9 +1093,11 @@ protected function getQuote(
10851093 )
10861094 ->disableOriginalConstructor ()
10871095 ->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+ }
10911101 $ quote ->expects ($ this ->any ())
10921102 ->method ('getAllVisibleItems ' )
10931103 ->willReturn ($ quoteItems );
@@ -1129,7 +1139,7 @@ protected function getQuote(
11291139 $ quote ->expects ($ this ->any ())
11301140 ->method ('getCustomer ' )
11311141 ->willReturn ($ customer );
1132- $ quote ->expects ($ this ->once ( ))
1142+ $ quote ->expects ($ this ->exactly ( 2 ))
11331143 ->method ('getId ' )
11341144 ->willReturn ($ id );
11351145 $ this ->customerRepositoryMock ->expects ($ this ->any ())->method ('getById ' )->willReturn ($ customer );
@@ -1292,7 +1302,8 @@ public function testSubmitForCustomer(): void
12921302 $ customerId ,
12931303 $ quoteId ,
12941304 $ quoteItems ,
1295- $ shippingAddress
1305+ $ shippingAddress ,
1306+ false
12961307 );
12971308
12981309 $ this ->submitQuoteValidator ->method ('validateQuote ' )
@@ -1386,4 +1397,110 @@ private function createPartialMockForAbstractClass(string $className, array $met
13861397 $ methods
13871398 );
13881399 }
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+ }
13891506}
0 commit comments