Skip to content

Commit bb3a526

Browse files
committed
ACP2E-2910: Order Rest API call is taking a long time to execute
1 parent f1fc8c1 commit bb3a526

File tree

4 files changed

+63
-145
lines changed

4 files changed

+63
-145
lines changed

app/code/Magento/GiftMessage/Model/Plugin/OrderGet.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,7 @@ protected function getOrderItemGiftMessage(\Magento\Sales\Api\Data\OrderInterfac
9090
if (null !== $orderItems) {
9191
foreach ($orderItems as $orderItem) {
9292
$extensionAttributes = $orderItem->getExtensionAttributes();
93-
if (
94-
($extensionAttributes && $extensionAttributes->getGiftMessage()) ||
93+
if (($extensionAttributes && $extensionAttributes->getGiftMessage()) ||
9594
!$orderItem->getGiftMessageId()
9695
) {
9796
continue;

app/code/Magento/GiftMessage/Test/Unit/Model/Plugin/OrderGetTest.php

Lines changed: 40 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,16 @@
77

88
namespace Magento\GiftMessage\Test\Unit\Model\Plugin;
99

10-
use Magento\Framework\Exception\NoSuchEntityException;
1110
use Magento\GiftMessage\Api\Data\MessageInterface;
1211
use Magento\GiftMessage\Api\OrderItemRepositoryInterface;
1312
use Magento\GiftMessage\Api\OrderRepositoryInterface;
1413
use Magento\GiftMessage\Model\Plugin\OrderGet;
1514
use Magento\Sales\Api\Data\OrderExtension;
16-
use Magento\Sales\Api\Data\OrderExtensionFactory;
1715
use Magento\Sales\Api\Data\OrderInterface;
1816
use Magento\Sales\Api\Data\OrderItemExtension;
19-
use Magento\Sales\Api\Data\OrderItemExtensionFactory;
2017
use Magento\Sales\Api\Data\OrderItemInterface;
2118
use Magento\Sales\Model\ResourceModel\Order\Collection;
2219
use PHPUnit\Framework\MockObject\MockObject;
23-
use PHPUnit\Framework\MockObject\RuntimeException;
2420
use PHPUnit\Framework\TestCase;
2521

2622
/**
@@ -43,16 +39,6 @@ class OrderGetTest extends TestCase
4339
*/
4440
private $giftMessageOrderItemRepositoryMock;
4541

46-
/**
47-
* @var MockObject
48-
*/
49-
private $orderExtensionFactoryMock;
50-
51-
/**
52-
* @var MockObject
53-
*/
54-
private $orderItemExtensionFactoryMock;
55-
5642
/**
5743
* @var MockObject
5844
*/
@@ -96,25 +82,19 @@ protected function setUp(): void
9682
$this->giftMessageOrderItemRepositoryMock = $this->createMock(
9783
OrderItemRepositoryInterface::class
9884
);
99-
$this->orderExtensionFactoryMock = $this->createPartialMock(
100-
OrderExtensionFactory::class,
101-
['create']
102-
);
103-
$this->orderItemExtensionFactoryMock = $this->createPartialMock(
104-
OrderItemExtensionFactory::class,
105-
['create']
106-
);
107-
$this->orderMock = $this->createMock(
108-
OrderInterface::class
109-
);
110-
$this->orderExtensionMock = $this->getOrderExtensionMock();
85+
$this->orderMock = $this->getMockBuilder(OrderInterface::class)
86+
->disableOriginalConstructor()
87+
->addMethods(['getGiftMessageId'])
88+
->getMockForAbstractClass();
89+
$this->orderExtensionMock = $this->createMock(OrderExtension::class);
11190
$this->giftMessageMock = $this->createMock(
11291
MessageInterface::class
11392
);
114-
$this->orderItemMock = $this->createMock(
115-
OrderItemInterface::class
116-
);
117-
$this->orderItemExtensionMock = $this->getOrderItemExtensionMock();
93+
$this->orderItemMock = $this->getMockBuilder(OrderItemInterface::class)
94+
->disableOriginalConstructor()
95+
->addMethods(['getGiftMessageId'])
96+
->getMockForAbstractClass();
97+
$this->orderItemExtensionMock = $this->createMock(OrderItemExtension::class);
11898
$this->orderRepositoryMock = $this->createMock(
11999
\Magento\Sales\Api\OrderRepositoryInterface::class
120100
);
@@ -123,16 +103,19 @@ protected function setUp(): void
123103

124104
$this->plugin = new OrderGet(
125105
$this->giftMessageOrderRepositoryMock,
126-
$this->giftMessageOrderItemRepositoryMock,
127-
$this->orderExtensionFactoryMock,
128-
$this->orderItemExtensionFactoryMock
106+
$this->giftMessageOrderItemRepositoryMock
129107
);
130108
}
131109

132-
public function testAfterGetGiftMessageOnOrderLevel()
110+
/**
111+
* @return void
112+
*/
113+
public function testAfterGetGiftMessageOnOrderLevel(): void
133114
{
134115
//set Gift Message for Order
135116
$orderId = 1;
117+
$messageId = 1;
118+
$this->orderMock->expects($this->once())->method('getGiftMessageId')->willReturn($messageId);
136119
$this->orderMock->expects($this->once())->method('getEntityId')->willReturn($orderId);
137120
$this->orderMock
138121
->expects($this->once())
@@ -160,12 +143,17 @@ public function testAfterGetGiftMessageOnOrderLevel()
160143
$this->plugin->afterGet($this->orderRepositoryMock, $this->orderMock);
161144
}
162145

163-
public function testAfterGetGiftMessageOnItemLevel()
146+
/**
147+
* @return void
148+
*/
149+
public function testAfterGetGiftMessageOnItemLevel(): void
164150
{
165151
//set Gift Message for Order
166152
$orderId = 1;
167153
$orderItemId = 2;
154+
$messageId = 1;
168155
$this->orderItemMock->expects($this->once())->method('getItemId')->willReturn($orderItemId);
156+
$this->orderItemMock->expects($this->once())->method('getGiftMessageId')->willReturn($messageId);
169157
$this->orderMock->expects($this->once())->method('getEntityId')->willReturn($orderId);
170158
$this->orderMock
171159
->expects($this->once())
@@ -198,23 +186,25 @@ public function testAfterGetGiftMessageOnItemLevel()
198186
$this->plugin->afterGet($this->orderRepositoryMock, $this->orderMock);
199187
}
200188

201-
public function testGetAfterWhenMessagesAreNotSet()
189+
/**
190+
* @return void
191+
*/
192+
public function testGetAfterWhenMessagesAreNotSet(): void
202193
{
203194
$orderId = 1;
204195
$orderItemId = 2;
205196
//set Gift Message for Order
206-
$this->orderMock->expects($this->exactly(2))->method('getEntityId')->willReturn($orderId);
207-
$this->orderItemMock->expects($this->once())->method('getItemId')->willReturn($orderItemId);
197+
$this->orderMock->expects($this->never())->method('getEntityId');
198+
$this->orderItemMock->expects($this->never())->method('getItemId');
199+
$this->orderItemMock->expects($this->once())->method('getGiftMessageId')->willReturn(null);
208200
$this->orderMock
209201
->expects($this->once())
210202
->method('getExtensionAttributes')
211203
->willReturn($this->orderExtensionMock);
212204
$this->orderExtensionMock->expects($this->once())->method('getGiftMessage')->willReturn([]);
213205
$this->giftMessageOrderRepositoryMock
214-
->expects($this->once())
215-
->method('get')
216-
->with($orderId)
217-
->willThrowException(new NoSuchEntityException());
206+
->expects($this->never())
207+
->method('get');
218208
$this->orderExtensionMock
219209
->expects($this->never())
220210
->method('setGiftMessage');
@@ -227,22 +217,25 @@ public function testGetAfterWhenMessagesAreNotSet()
227217
->willReturn($this->orderItemExtensionMock);
228218
$this->orderItemExtensionMock->expects($this->once())->method('getGiftMessage')->willReturn([]);
229219
$this->giftMessageOrderItemRepositoryMock
230-
->expects($this->once())
231-
->method('get')
232-
->with($orderId, $orderItemId)
233-
->willThrowException(new NoSuchEntityException());
220+
->expects($this->never())
221+
->method('get');
234222
$this->orderItemExtensionMock
235223
->expects($this->never())
236224
->method('setGiftMessage');
237225

238226
$this->plugin->afterGet($this->orderRepositoryMock, $this->orderMock);
239227
}
240228

241-
public function testAfterGetList()
229+
/**
230+
* @return void
231+
*/
232+
public function testAfterGetList(): void
242233
{
243234
//set Gift Message List for Order
244235
$orderId = 1;
236+
$messageId = 1;
245237
$this->orderMock->expects($this->once())->method('getEntityId')->willReturn($orderId);
238+
$this->orderMock->expects($this->once())->method('getGiftMessageId')->willReturn($messageId);
246239
$this->orderMock
247240
->expects($this->once())
248241
->method('getExtensionAttributes')
@@ -269,38 +262,4 @@ public function testAfterGetList()
269262
$this->collectionMock->expects($this->once())->method('getItems')->willReturn([$this->orderMock]);
270263
$this->plugin->afterGetList($this->orderRepositoryMock, $this->collectionMock);
271264
}
272-
273-
/**
274-
* Build order extension mock.
275-
*
276-
* @return MockObject
277-
*/
278-
private function getOrderExtensionMock(): MockObject
279-
{
280-
$mockObject = $this->getMockBuilder(OrderExtension::class);
281-
try {
282-
$mockObject->addMethods(['getGiftMessage', 'setGiftMessage']);
283-
} catch (RuntimeException $e) {
284-
// Order extension already generated.
285-
}
286-
287-
return $mockObject->getMock();
288-
}
289-
290-
/**
291-
* Build order item extension mock.
292-
*
293-
* @return MockObject
294-
*/
295-
private function getOrderItemExtensionMock(): MockObject
296-
{
297-
$mockObject = $this->getMockBuilder(OrderItemExtension::class);
298-
try {
299-
$mockObject->addMethods(['getGiftMessage', 'setGiftMessage']);
300-
} catch (RuntimeException $e) {
301-
// Order extension already generated.
302-
}
303-
304-
return $mockObject->getMock();
305-
}
306265
}

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

Lines changed: 19 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
1414
use Magento\Payment\Api\Data\PaymentAdditionalInfoInterface;
1515
use Magento\Payment\Api\Data\PaymentAdditionalInfoInterfaceFactory;
16-
use Magento\Sales\Api\Data\OrderExtensionFactory;
1716
use Magento\Sales\Api\Data\OrderExtensionInterface;
1817
use Magento\Sales\Api\Data\OrderInterface;
1918
use Magento\Sales\Api\Data\OrderPaymentInterface;
@@ -30,7 +29,6 @@
3029
use Magento\Tax\Api\Data\OrderTaxDetailsInterface;
3130
use Magento\Tax\Api\OrderTaxManagementInterface;
3231
use PHPUnit\Framework\MockObject\MockObject;
33-
use PHPUnit\Framework\MockObject\RuntimeException;
3432
use PHPUnit\Framework\TestCase;
3533

3634
/**
@@ -73,11 +71,6 @@ class OrderRepositoryTest extends TestCase
7371
*/
7472
private $paymentAdditionalInfoFactory;
7573

76-
/**
77-
* @var OrderExtensionFactory|\MockObject
78-
*/
79-
private $orderExtensionFactoryMock;
80-
8174
/**
8275
* @var ShippingAssignmentBuilder|MockObject
8376
*/
@@ -101,9 +94,6 @@ protected function setUp(): void
10194
$this->collectionProcessor = $this->createMock(
10295
CollectionProcessorInterface::class
10396
);
104-
$this->orderExtensionFactoryMock = $this->getMockBuilder(OrderExtensionFactory::class)
105-
->disableOriginalConstructor()
106-
->getMock();
10797
$this->orderTaxManagementMock = $this->getMockBuilder(OrderTaxManagementInterface::class)
10898
->disableOriginalConstructor()
10999
->getMockForAbstractClass();
@@ -119,7 +109,6 @@ protected function setUp(): void
119109
'metadata' => $this->metadata,
120110
'searchResultFactory' => $this->searchResultFactory,
121111
'collectionProcessor' => $this->collectionProcessor,
122-
'orderExtensionFactory' => $this->orderExtensionFactoryMock,
123112
'orderTaxManagement' => $this->orderTaxManagementMock,
124113
'paymentAdditionalInfoFactory' => $this->paymentAdditionalInfoFactory,
125114
'shippingAssignmentBuilder' => $this->shippingAssignmentBuilder
@@ -149,7 +138,9 @@ public function testGetList()
149138
->disableOriginalConstructor()
150139
->setMethods(['setKey', 'setValue'])->getMockForAbstractClass();
151140

152-
$extensionAttributes = $this->getOrderExtensionMock();
141+
$extensionAttributes = $this->getMockBuilder(OrderExtensionInterface::class)
142+
->disableOriginalConstructor()
143+
->getMockForAbstractClass();
153144
$shippingAssignmentBuilder = $this->createMock(
154145
ShippingAssignmentBuilder::class
155146
);
@@ -181,14 +172,19 @@ public function testGetList()
181172
* Test for method save.
182173
*
183174
* @return void
175+
* @throws \Magento\Framework\Exception\AlreadyExistsException
176+
* @throws \Magento\Framework\Exception\InputException
177+
* @throws \Magento\Framework\Exception\NoSuchEntityException
184178
*/
185179
public function testSave()
186180
{
187181
$mapperMock = $this->getMockBuilder(OrderResource::class)
188182
->disableOriginalConstructor()
189183
->getMock();
190184
$orderEntity = $this->createMock(Order::class);
191-
$extensionAttributes = $this->getOrderExtensionMock();
185+
$extensionAttributes = $this->getMockBuilder(OrderExtensionInterface::class)
186+
->disableOriginalConstructor()
187+
->getMockForAbstractClass();
192188
$shippingAssignment = $this->getMockBuilder(ShippingAssignment::class)
193189
->disableOriginalConstructor()
194190
->setMethods(['getShipping'])
@@ -219,6 +215,8 @@ public function testSave()
219215
* Test for method get.
220216
*
221217
* @return void
218+
* @throws \Magento\Framework\Exception\InputException
219+
* @throws \Magento\Framework\Exception\NoSuchEntityException
222220
*/
223221
public function testGet()
224222
{
@@ -227,24 +225,29 @@ public function testGet()
227225
$items = 'items';
228226
$paymentInfo = [];
229227

230-
$orderEntity = $this->createMock(Order::class);
231228
$paymentMock = $this->getMockBuilder(OrderPaymentInterface::class)
232229
->disableOriginalConstructor()->getMockForAbstractClass();
233230
$paymentMock->expects($this->once())->method('getAdditionalInformation')->willReturn($paymentInfo);
234-
$orderExtension = $this->getOrderExtensionMock();
231+
232+
$orderExtension = $this->getMockBuilder(OrderExtensionInterface::class)
233+
->disableOriginalConstructor()
234+
->getMockForAbstractClass();
235235
$orderExtension->expects($this->once())->method('getShippingAssignments')->willReturn(null);
236236
$orderExtension->expects($this->once())->method('setAppliedTaxes')->with($appliedTaxes);
237237
$orderExtension->expects($this->once())->method('setConvertingFromQuote')->with(false);
238238
$orderExtension->expects($this->once())->method('setItemAppliedTaxes')->with($items);
239239
$orderExtension->expects($this->once())->method('setPaymentAdditionalInfo')->with($paymentInfo);
240-
$this->orderExtensionFactoryMock->expects($this->once())->method('create')->willReturn($orderExtension);
240+
241+
$orderEntity = $this->createMock(Order::class);
242+
$orderEntity->expects($this->exactly(3))->method('getExtensionAttributes')->willReturn($orderExtension);
241243
$orderEntity->expects($this->once())->method('load')->with($orderId)->willReturn($orderEntity);
242244
$orderEntity->expects($this->exactly(2))->method('getEntityId')->willReturn($orderId);
243245
$orderEntity->expects($this->once())->method('getPayment')->willReturn($paymentMock);
244246
$orderEntity->expects($this->exactly(3))->method('setExtensionAttributes')->with($orderExtension);
245247
$orderEntity->expects($this->exactly(3))
246248
->method('getExtensionAttributes')
247249
->willReturnOnConsecutiveCalls(null, $orderExtension, $orderExtension);
250+
248251
$this->metadata->expects($this->once())->method('getNewInstance')->willReturn($orderEntity);
249252
$orderTaxDetailsMock = $this->getMockBuilder(OrderTaxDetailsInterface::class)
250253
->disableOriginalConstructor()
@@ -257,31 +260,4 @@ public function testGet()
257260

258261
$this->orderRepository->get($orderId);
259262
}
260-
261-
/**
262-
* Buld order extension mock.
263-
*
264-
* @return MockObject
265-
*/
266-
private function getOrderExtensionMock(): MockObject
267-
{
268-
$mockBuilder = $this->getMockBuilder(OrderExtensionInterface::class)->disableOriginalConstructor();
269-
try {
270-
$mockBuilder
271-
->addMethods(
272-
[
273-
'setShippingAssignments',
274-
'getShippingAssignments',
275-
'setAppliedTaxes',
276-
'setConvertingFromQuote',
277-
'setItemAppliedTaxes',
278-
'setPaymentAdditionalInfo'
279-
]
280-
);
281-
} catch (RuntimeException $e) {
282-
// Order extension already generated.
283-
}
284-
285-
return $mockBuilder->getMockForAbstractClass();
286-
}
287263
}

0 commit comments

Comments
 (0)