Skip to content

Commit 1cf357b

Browse files
committed
Merge remote-tracking branch 'origin/ACP2E-4117' into PR_2025_09_08_muntianu
2 parents 0846826 + b63b0dd commit 1cf357b

File tree

2 files changed

+59
-4
lines changed

2 files changed

+59
-4
lines changed

app/code/Magento/Multishipping/Model/Checkout/Type/Multishipping.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Magento\Customer\Api\AddressRepositoryInterface;
1111
use Magento\Framework\Exception\NotFoundException;
1212
use Magento\Framework\Pricing\PriceCurrencyInterface;
13+
use Magento\Quote\Model\CartMutexInterface;
1314
use Magento\Quote\Model\Quote;
1415
use Magento\Quote\Model\Quote\Address;
1516
use Magento\Quote\Model\Quote\Item;
@@ -180,6 +181,11 @@ class Multishipping extends \Magento\Framework\DataObject
180181
*/
181182
private $dataObjectHelper;
182183

184+
/**
185+
* @var CartMutexInterface
186+
*/
187+
private $cartMutex;
188+
183189
/**
184190
* @param Session $checkoutSession
185191
* @param \Magento\Customer\Model\Session $customerSession
@@ -208,6 +214,7 @@ class Multishipping extends \Magento\Framework\DataObject
208214
* @param Multishipping\PlaceOrderFactory|null $placeOrderFactory
209215
* @param LoggerInterface|null $logger
210216
* @param \Magento\Framework\Api\DataObjectHelper|null $dataObjectHelper
217+
* @param CartMutexInterface|null $cartMutex
211218
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
212219
*/
213220
public function __construct(
@@ -237,7 +244,8 @@ public function __construct(
237244
?AllowedCountries $allowedCountryReader = null,
238245
?Multishipping\PlaceOrderFactory $placeOrderFactory = null,
239246
?LoggerInterface $logger = null,
240-
?\Magento\Framework\Api\DataObjectHelper $dataObjectHelper = null
247+
?\Magento\Framework\Api\DataObjectHelper $dataObjectHelper = null,
248+
?CartMutexInterface $cartMutex = null
241249
) {
242250
$this->_eventManager = $eventManager;
243251
$this->_scopeConfig = $scopeConfig;
@@ -270,6 +278,8 @@ public function __construct(
270278
->get(LoggerInterface::class);
271279
$this->dataObjectHelper = $dataObjectHelper ?: ObjectManager::getInstance()
272280
->get(\Magento\Framework\Api\DataObjectHelper::class);
281+
$this->cartMutex = $cartMutex ?: ObjectManager::getInstance()
282+
->get(CartMutexInterface::class);
273283
parent::__construct($data);
274284
$this->_init();
275285
}
@@ -855,6 +865,18 @@ protected function _validate()
855865
* @throws \Exception
856866
*/
857867
public function createOrders()
868+
{
869+
return $this->cartMutex->execute((int)$this->getQuote()->getId(), $this->createOrdersMutexCallback(...));
870+
}
871+
872+
/**
873+
* Mutex callback for createOrders()
874+
*
875+
* @see createOrders
876+
* @return Multishipping
877+
* @throws \Exception
878+
*/
879+
private function createOrdersMutexCallback()
858880
{
859881
$orderIds = [];
860882
$this->_validate();

app/code/Magento/Multishipping/Test/Unit/Model/Checkout/Type/MultishippingTest.php

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2015 Adobe
4+
* All Rights Reserved.
55
*/
66
declare(strict_types=1);
77

@@ -189,6 +189,11 @@ class MultishippingTest extends TestCase
189189
*/
190190
private $scopeConfigMock;
191191

192+
/**
193+
* @var \Magento\Quote\Model\CartMutexInterface::class
194+
*/
195+
private $cartMutex;
196+
192197
/**
193198
* @inheritDoc
194199
*/
@@ -243,6 +248,8 @@ protected function setUp(): void
243248
->getMock();
244249
$logger = $this->createSimpleMock(LoggerInterface::class);
245250

251+
$this->cartMutex = $this->createMock(\Magento\Quote\Model\CartMutexInterface::class);
252+
246253
$this->model = new Multishipping(
247254
$this->checkoutSessionMock,
248255
$this->customerSessionMock,
@@ -270,7 +277,8 @@ protected function setUp(): void
270277
$allowedCountryReaderMock,
271278
$this->placeOrderFactoryMock,
272279
$logger,
273-
$this->dataObjectHelperMock
280+
$this->dataObjectHelperMock,
281+
$this->cartMutex
274282
);
275283

276284
$this->shippingAssignmentProcessorMock = $this->createSimpleMock(ShippingAssignmentProcessor::class);
@@ -593,6 +601,8 @@ function ($arg1, $arg2) use ($orderAddressMock, $billingAddressMock, $shippingAd
593601
$this->placeOrderFactoryMock->method('create')->with($paymentProviderCode)->willReturn($placeOrderServiceMock);
594602
$this->quoteRepositoryMock->method('save')->with($this->quoteMock);
595603

604+
$this->assertMutexIsInvoked();
605+
596606
$this->model->createOrders();
597607
}
598608

@@ -696,6 +706,8 @@ function ($arg1, $arg2) use ($orderAddressMock, $billingAddressMock, $shippingAd
696706

697707
$this->expectExceptionMessage('Quote address for failed order ID "1" not found.');
698708

709+
$this->assertMutexIsInvoked();
710+
699711
$this->model->createOrders();
700712
}
701713

@@ -986,6 +998,8 @@ public function testCreateOrdersCountryNotPresentInAllowedListException(): void
986998
->willReturn([$shippingAddressMock]);
987999
$this->expectExceptionMessage($exceptionMessage);
9881000

1001+
$this->assertMutexIsInvoked();
1002+
9891003
$this->model->createOrders();
9901004
}
9911005

@@ -1213,4 +1227,23 @@ public static function getConfigCreateOrders(): array
12131227
]
12141228
];
12151229
}
1230+
1231+
private function assertMutexIsInvoked(): void
1232+
{
1233+
$this->cartMutex
1234+
->expects($this->once())
1235+
->method('execute')
1236+
->with(
1237+
$this->quoteMock->getId(),
1238+
$this->callback(is_callable(...))
1239+
)
1240+
->willReturnCallback(
1241+
function ($arg1, $callback) {
1242+
if ($arg1 != $this->quoteMock->getId()) {
1243+
throw new \Exception('Quote ID does not match.');
1244+
}
1245+
return $callback();
1246+
}
1247+
);
1248+
}
12161249
}

0 commit comments

Comments
 (0)