Skip to content

Commit 76c7aa4

Browse files
committed
ACP2E-2213: Loading order through repository loads the order multiple times from DB
1 parent eb81692 commit 76c7aa4

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

app/code/Magento/Sales/Model/OrderRepository.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ class OrderRepository implements \Magento\Sales\Api\OrderRepositoryInterface, Re
9393
* @param OrderTaxManagementInterface|null $orderTaxManagement
9494
* @param PaymentAdditionalInfoInterfaceFactory|null $paymentAdditionalInfoFactory
9595
* @param JsonSerializer|null $serializer
96-
* @param JoinProcessorInterface $extensionAttributesJoinProcessor
96+
* @param JoinProcessorInterface|null $extensionAttributesJoinProcessor
97+
* @param ShippingAssignmentBuilder|null $shippingAssignmentBuilder
9798
*/
9899
public function __construct(
99100
Metadata $metadata,
@@ -103,7 +104,8 @@ public function __construct(
103104
OrderTaxManagementInterface $orderTaxManagement = null,
104105
PaymentAdditionalInfoInterfaceFactory $paymentAdditionalInfoFactory = null,
105106
JsonSerializer $serializer = null,
106-
JoinProcessorInterface $extensionAttributesJoinProcessor = null
107+
JoinProcessorInterface $extensionAttributesJoinProcessor = null,
108+
ShippingAssignmentBuilder $shippingAssignmentBuilder = null
107109
) {
108110
$this->metadata = $metadata;
109111
$this->searchResultFactory = $searchResultFactory;
@@ -119,6 +121,8 @@ public function __construct(
119121
->get(JsonSerializer::class);
120122
$this->extensionAttributesJoinProcessor = $extensionAttributesJoinProcessor
121123
?: ObjectManager::getInstance()->get(JoinProcessorInterface::class);
124+
$this->shippingAssignmentBuilder = $shippingAssignmentBuilder
125+
?: ObjectManager::getInstance()->get(ShippingAssignmentBuilder::class);
122126
}
123127

124128
/**

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

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use Magento\Sales\Model\Order\Shipping;
2323
use Magento\Sales\Model\Order\ShippingAssignment;
2424
use Magento\Sales\Model\Order\ShippingAssignmentBuilder;
25+
use Magento\Sales\Api\Data\OrderAddressInterface;
2526
use Magento\Sales\Model\OrderRepository;
2627
use Magento\Sales\Model\ResourceModel\Metadata;
2728
use Magento\Sales\Model\ResourceModel\Order as OrderResource;
@@ -77,6 +78,11 @@ class OrderRepositoryTest extends TestCase
7778
*/
7879
private $orderExtensionFactoryMock;
7980

81+
/**
82+
* @var ShippingAssignmentBuilder|MockObject
83+
*/
84+
private $shippingAssignmentBuilder;
85+
8086
/**
8187
* Setup the test
8288
*
@@ -104,6 +110,9 @@ protected function setUp(): void
104110
$this->paymentAdditionalInfoFactory = $this->getMockBuilder(PaymentAdditionalInfoInterfaceFactory::class)
105111
->disableOriginalConstructor()
106112
->setMethods(['create'])->getMockForAbstractClass();
113+
$this->shippingAssignmentBuilder = $this->getMockBuilder(ShippingAssignmentBuilder::class)
114+
->disableOriginalConstructor()
115+
->getMock();
107116
$this->orderRepository = $this->objectManager->getObject(
108117
OrderRepository::class,
109118
[
@@ -113,6 +122,7 @@ protected function setUp(): void
113122
'orderExtensionFactory' => $this->orderExtensionFactoryMock,
114123
'orderTaxManagement' => $this->orderTaxManagementMock,
115124
'paymentAdditionalInfoFactory' => $this->paymentAdditionalInfoFactory,
125+
'shippingAssignmentBuilder' => $this->shippingAssignmentBuilder
116126
]
117127
);
118128
}
@@ -194,7 +204,10 @@ public function testSave()
194204
->method('getShippingAssignments')
195205
->willReturn([$shippingAssignment]);
196206
$shippingAssignment->expects($this->once())->method('getShipping')->willReturn($shippingMock);
197-
$shippingMock->expects($this->once())->method('getAddress');
207+
$shippingAddressMock = $this->getMockBuilder(OrderAddressInterface::class)
208+
->disableOriginalConstructor()
209+
->getMock();
210+
$shippingMock->expects($this->once())->method('getAddress')->willReturn($shippingAddressMock);
198211
$shippingMock->expects($this->once())->method('getMethod');
199212
$this->metadata->expects($this->once())->method('getMapper')->willReturn($mapperMock);
200213
$mapperMock->expects($this->once())->method('save');
@@ -219,7 +232,7 @@ public function testGet()
219232
->disableOriginalConstructor()->getMockForAbstractClass();
220233
$paymentMock->expects($this->once())->method('getAdditionalInformation')->willReturn($paymentInfo);
221234
$orderExtension = $this->getOrderExtensionMock();
222-
$orderExtension->expects($this->once())->method('getShippingAssignments')->willReturn(true);
235+
$orderExtension->expects($this->once())->method('getShippingAssignments')->willReturn(null);
223236
$orderExtension->expects($this->once())->method('setAppliedTaxes')->with($appliedTaxes);
224237
$orderExtension->expects($this->once())->method('setConvertingFromQuote')->with(true);
225238
$orderExtension->expects($this->once())->method('setItemAppliedTaxes')->with($items);
@@ -228,7 +241,7 @@ public function testGet()
228241
$orderEntity->expects($this->once())->method('load')->with($orderId)->willReturn($orderEntity);
229242
$orderEntity->expects($this->exactly(2))->method('getEntityId')->willReturn($orderId);
230243
$orderEntity->expects($this->once())->method('getPayment')->willReturn($paymentMock);
231-
$orderEntity->expects($this->exactly(2))->method('setExtensionAttributes')->with($orderExtension);
244+
$orderEntity->expects($this->exactly(3))->method('setExtensionAttributes')->with($orderExtension);
232245
$orderEntity->expects($this->exactly(3))
233246
->method('getExtensionAttributes')
234247
->willReturnOnConsecutiveCalls(null, $orderExtension, $orderExtension);
@@ -240,6 +253,7 @@ public function testGet()
240253
$orderTaxDetailsMock->expects($this->once())->method('getItems')->willReturn($items);
241254
$this->orderTaxManagementMock->expects($this->atLeastOnce())->method('getOrderTaxDetails')
242255
->willReturn($orderTaxDetailsMock);
256+
$this->shippingAssignmentBuilder->expects($this->once())->method('setOrder')->with($orderEntity);
243257

244258
$this->orderRepository->get($orderId);
245259
}
@@ -254,7 +268,7 @@ private function getOrderExtensionMock(): MockObject
254268
$mockBuilder = $this->getMockBuilder(OrderExtensionInterface::class)->disableOriginalConstructor();
255269
try {
256270
$mockBuilder
257-
->addMethods(
271+
->onlyMethods(
258272
[
259273
'getShippingAssignments',
260274
'setAppliedTaxes',

0 commit comments

Comments
 (0)