Skip to content

Commit 0abdb48

Browse files
committed
ACP2E-694: [Magento Cloud] REST API intermittently over writes the results of shipping results
1 parent cac6fcd commit 0abdb48

File tree

2 files changed

+74
-20
lines changed

2 files changed

+74
-20
lines changed

app/code/Magento/Sales/Test/Unit/Model/RefundOrderTest.php

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
use Magento\Framework\App\ResourceConnection;
1111
use Magento\Framework\DB\Adapter\AdapterInterface;
12+
use Magento\Framework\DB\Select;
1213
use Magento\Sales\Api\CreditmemoRepositoryInterface;
1314
use Magento\Sales\Api\Data\CreditmemoCommentCreationInterface;
1415
use Magento\Sales\Api\Data\CreditmemoCreationArgumentsInterface;
@@ -26,6 +27,7 @@
2627
use Magento\Sales\Model\Order\OrderStateResolverInterface;
2728
use Magento\Sales\Model\Order\RefundAdapterInterface;
2829
use Magento\Sales\Model\Order\Validation\RefundOrderInterface;
30+
use Magento\Sales\Model\OrderMutex;
2931
use Magento\Sales\Model\RefundOrder;
3032
use Magento\Sales\Model\ValidatorResultInterface;
3133
use PHPUnit\Framework\MockObject\MockObject;
@@ -193,7 +195,8 @@ protected function setUp(): void
193195
$this->refundOrderValidatorMock,
194196
$this->notifierMock,
195197
$this->configMock,
196-
$this->loggerMock
198+
$this->loggerMock,
199+
new OrderMutex($this->resourceConnectionMock)
197200
);
198201
}
199202

@@ -208,10 +211,7 @@ protected function setUp(): void
208211
public function testOrderCreditmemo($orderId, $notify, $appendComment)
209212
{
210213
$items = [$this->creditmemoItemCreationMock];
211-
$this->resourceConnectionMock->expects($this->once())
212-
->method('getConnection')
213-
->with('sales')
214-
->willReturn($this->adapterInterface);
214+
$this->mockConnection($orderId);
215215
$this->orderRepositoryMock->expects($this->once())
216216
->method('get')
217217
->willReturn($this->orderMock);
@@ -304,7 +304,7 @@ public function testDocumentValidationException()
304304
$notify = true;
305305
$appendComment = true;
306306
$errorMessages = ['error1', 'error2'];
307-
307+
$this->mockConnection($orderId);
308308
$this->orderRepositoryMock->expects($this->once())
309309
->method('get')
310310
->willReturn($this->orderMock);
@@ -357,10 +357,7 @@ public function testCouldNotCreditmemoException()
357357
$items = [$this->creditmemoItemCreationMock];
358358
$notify = true;
359359
$appendComment = true;
360-
$this->resourceConnectionMock->expects($this->once())
361-
->method('getConnection')
362-
->with('sales')
363-
->willReturn($this->adapterInterface);
360+
$this->mockConnection($orderId);
364361
$this->orderRepositoryMock->expects($this->once())
365362
->method('get')
366363
->willReturn($this->orderMock);
@@ -419,4 +416,34 @@ public function dataProvider()
419416
'TestWithNotifyFalse' => [1, false, true],
420417
];
421418
}
419+
420+
/**
421+
* @param int $orderId
422+
*/
423+
private function mockConnection(int $orderId): void
424+
{
425+
$select = $this->createMock(Select::class);
426+
$select->expects($this->once())
427+
->method('from')
428+
->with('sales_order', 'entity_id')
429+
->willReturnSelf();
430+
$select->expects($this->once())
431+
->method('where')
432+
->with('entity_id = ?', $orderId)
433+
->willReturnSelf();
434+
$select->expects($this->once())
435+
->method('forUpdate')
436+
->with(true)
437+
->willReturnSelf();
438+
$this->adapterInterface->expects($this->once())
439+
->method('select')
440+
->willReturn($select);
441+
$this->resourceConnectionMock->expects($this->once())
442+
->method('getConnection')
443+
->with('sales')
444+
->willReturn($this->adapterInterface);
445+
$this->resourceConnectionMock->expects($this->once())
446+
->method('getTableName')
447+
->willReturnArgument(0);
448+
}
422449
}

app/code/Magento/Sales/Test/Unit/Model/ShipOrderTest.php

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
use Magento\Framework\App\ResourceConnection;
1111
use Magento\Framework\DB\Adapter\AdapterInterface;
12+
use Magento\Framework\DB\Select;
1213
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
1314
use Magento\Sales\Api\Data\OrderInterface;
1415
use Magento\Sales\Api\Data\ShipmentCommentCreationInterface;
@@ -27,6 +28,7 @@
2728
use Magento\Sales\Model\Order\Shipment\OrderRegistrarInterface;
2829
use Magento\Sales\Model\Order\ShipmentDocumentFactory;
2930
use Magento\Sales\Model\Order\Validation\ShipOrderInterface;
31+
use Magento\Sales\Model\OrderMutex;
3032
use Magento\Sales\Model\ShipOrder;
3133
use Magento\Sales\Model\ValidatorResultInterface;
3234
use PHPUnit\Framework\MockObject\MockObject;
@@ -208,7 +210,8 @@ protected function setUp(): void
208210
'shipOrderValidator' => $this->shipOrderValidatorMock,
209211
'notifierInterface' => $this->notifierInterfaceMock,
210212
'logger' => $this->loggerMock,
211-
'orderRegistrar' => $this->orderRegistrarMock
213+
'orderRegistrar' => $this->orderRegistrarMock,
214+
'orderMutex' => new OrderMutex($this->resourceConnectionMock)
212215
]
213216
);
214217
}
@@ -224,10 +227,7 @@ protected function setUp(): void
224227
*/
225228
public function testExecute($orderId, $items, $notify, $appendComment)
226229
{
227-
$this->resourceConnectionMock->expects($this->once())
228-
->method('getConnection')
229-
->with('sales')
230-
->willReturn($this->adapterMock);
230+
$this->mockConnection($orderId);
231231
$this->orderRepositoryMock->expects($this->once())
232232
->method('get')
233233
->willReturn($this->orderMock);
@@ -320,7 +320,7 @@ public function testDocumentValidationException()
320320
$notify = true;
321321
$appendComment = true;
322322
$errorMessages = ['error1', 'error2'];
323-
323+
$this->mockConnection($orderId);
324324
$this->orderRepositoryMock->expects($this->once())
325325
->method('get')
326326
->willReturn($this->orderMock);
@@ -375,10 +375,7 @@ public function testCouldNotShipException()
375375
$items = [1 => 2];
376376
$notify = true;
377377
$appendComment = true;
378-
$this->resourceConnectionMock->expects($this->once())
379-
->method('getConnection')
380-
->with('sales')
381-
->willReturn($this->adapterMock);
378+
$this->mockConnection($orderId);
382379

383380
$this->orderRepositoryMock->expects($this->once())
384381
->method('get')
@@ -447,4 +444,34 @@ public function dataProvider()
447444
'TestWithNotifyFalse' => [1, [1 => 2], false, true],
448445
];
449446
}
447+
448+
/**
449+
* @param int $orderId
450+
*/
451+
private function mockConnection(int $orderId): void
452+
{
453+
$select = $this->createMock(Select::class);
454+
$select->expects($this->once())
455+
->method('from')
456+
->with('sales_order', 'entity_id')
457+
->willReturnSelf();
458+
$select->expects($this->once())
459+
->method('where')
460+
->with('entity_id = ?', $orderId)
461+
->willReturnSelf();
462+
$select->expects($this->once())
463+
->method('forUpdate')
464+
->with(true)
465+
->willReturnSelf();
466+
$this->adapterMock->expects($this->once())
467+
->method('select')
468+
->willReturn($select);
469+
$this->resourceConnectionMock->expects($this->once())
470+
->method('getConnection')
471+
->with('sales')
472+
->willReturn($this->adapterMock);
473+
$this->resourceConnectionMock->expects($this->once())
474+
->method('getTableName')
475+
->willReturnArgument(0);
476+
}
450477
}

0 commit comments

Comments
 (0)