Skip to content

Commit 95da54e

Browse files
Merge remote-tracking branch 'remotes/github/MAGETWO-91676' into EPAM-PR-35
2 parents c2a36f1 + d17a99f commit 95da54e

File tree

2 files changed

+143
-10
lines changed

2 files changed

+143
-10
lines changed

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

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
use Magento\Sales\Model\ResourceModel\Order\Shipment\Collection as ShipmentCollection;
2424
use Magento\Sales\Model\ResourceModel\Order\Shipment\Track\Collection as TrackCollection;
2525
use Magento\Sales\Model\ResourceModel\Order\Status\History\Collection as HistoryCollection;
26+
use Magento\Sales\Api\OrderItemRepositoryInterface;
27+
use Magento\Framework\Api\SearchCriteriaBuilder;
2628

2729
/**
2830
* Order model
@@ -286,6 +288,16 @@ class Order extends AbstractModel implements EntityInterface, OrderInterface
286288
*/
287289
private $productOption;
288290

291+
/**
292+
* @var OrderItemRepositoryInterface
293+
*/
294+
private $itemRepository;
295+
296+
/**
297+
* @var SearchCriteriaBuilder
298+
*/
299+
private $searchCriteriaBuilder;
300+
289301
/**
290302
* @param \Magento\Framework\Model\Context $context
291303
* @param \Magento\Framework\Registry $registry
@@ -316,6 +328,8 @@ class Order extends AbstractModel implements EntityInterface, OrderInterface
316328
* @param array $data
317329
* @param ResolverInterface $localeResolver
318330
* @param ProductOption|null $productOption
331+
* @param OrderItemRepositoryInterface $itemRepository
332+
* @param SearchCriteriaBuilder $searchCriteriaBuilder
319333
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
320334
*/
321335
public function __construct(
@@ -347,7 +361,9 @@ public function __construct(
347361
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
348362
array $data = [],
349363
ResolverInterface $localeResolver = null,
350-
ProductOption $productOption = null
364+
ProductOption $productOption = null,
365+
OrderItemRepositoryInterface $itemRepository = null,
366+
SearchCriteriaBuilder $searchCriteriaBuilder = null
351367
) {
352368
$this->_storeManager = $storeManager;
353369
$this->_orderConfig = $orderConfig;
@@ -371,6 +387,10 @@ public function __construct(
371387
$this->priceCurrency = $priceCurrency;
372388
$this->localeResolver = $localeResolver ?: ObjectManager::getInstance()->get(ResolverInterface::class);
373389
$this->productOption = $productOption ?: ObjectManager::getInstance()->get(ProductOption::class);
390+
$this->itemRepository = $itemRepository ?: ObjectManager::getInstance()
391+
->get(OrderItemRepositoryInterface::class);
392+
$this->searchCriteriaBuilder = $searchCriteriaBuilder ?: ObjectManager::getInstance()
393+
->get(SearchCriteriaBuilder::class);
374394

375395
parent::__construct(
376396
$context,
@@ -668,7 +688,7 @@ private function canCreditmemoForZeroTotalRefunded($totalRefunded)
668688

669689
return true;
670690
}
671-
691+
672692
/**
673693
* Retrieve credit memo for zero total availability.
674694
*
@@ -2076,9 +2096,12 @@ public function getIncrementId()
20762096
public function getItems()
20772097
{
20782098
if ($this->getData(OrderInterface::ITEMS) == null) {
2099+
$this->searchCriteriaBuilder->addFilter(OrderItemInterface::ORDER_ID, $this->getId());
2100+
2101+
$searchCriteria = $this->searchCriteriaBuilder->create();
20792102
$this->setData(
20802103
OrderInterface::ITEMS,
2081-
$this->getItemsCollection()->getItems()
2104+
$this->itemRepository->getList($searchCriteria)->getItems()
20822105
);
20832106
}
20842107
return $this->getData(OrderInterface::ITEMS);
@@ -2919,7 +2942,7 @@ public function getDiscountTaxCompensationRefunded()
29192942
}
29202943

29212944
/**
2922-
* Return hold_before_state
2945+
* Returns hold_before_state
29232946
*
29242947
* @return string|null
29252948
*/

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

Lines changed: 116 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@
1111
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
1212
use Magento\Sales\Api\Data\OrderInterface;
1313
use Magento\Sales\Model\Order;
14+
use Magento\Sales\Model\ResourceModel\Order\Item\Collection;
1415
use Magento\Sales\Model\ResourceModel\Order\Status\History\CollectionFactory as HistoryCollectionFactory;
16+
use Magento\Sales\Api\OrderItemRepositoryInterface;
17+
use Magento\Framework\Api\SearchCriteriaBuilder;
18+
use Magento\Framework\Api\SearchCriteria;
19+
use Magento\Sales\Api\Data\OrderItemSearchResultInterface;
1520

1621
/**
1722
* Test class for \Magento\Sales\Model\Order
@@ -87,6 +92,16 @@ class OrderTest extends \PHPUnit\Framework\TestCase
8792
*/
8893
private $timezone;
8994

95+
/**
96+
* @var OrderItemRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
97+
*/
98+
private $itemRepository;
99+
100+
/**
101+
* @var SearchCriteriaBuilder|\PHPUnit_Framework_MockObject_MockObject
102+
*/
103+
private $searchCriteriaBuilder;
104+
90105
protected function setUp()
91106
{
92107
$helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
@@ -144,6 +159,15 @@ protected function setUp()
144159
$this->eventManager = $this->createMock(\Magento\Framework\Event\Manager::class);
145160
$context = $this->createPartialMock(\Magento\Framework\Model\Context::class, ['getEventDispatcher']);
146161
$context->expects($this->any())->method('getEventDispatcher')->willReturn($this->eventManager);
162+
163+
$this->itemRepository = $this->getMockBuilder(OrderItemRepositoryInterface::class)
164+
->setMethods(['getList'])
165+
->disableOriginalConstructor()->getMockForAbstractClass();
166+
167+
$this->searchCriteriaBuilder = $this->getMockBuilder(SearchCriteriaBuilder::class)
168+
->setMethods(['addFilter', 'create'])
169+
->disableOriginalConstructor()->getMockForAbstractClass();
170+
147171
$this->order = $helper->getObject(
148172
\Magento\Sales\Model\Order::class,
149173
[
@@ -157,37 +181,80 @@ protected function setUp()
157181
'productListFactory' => $this->productCollectionFactoryMock,
158182
'localeResolver' => $this->localeResolver,
159183
'timezone' => $this->timezone,
184+
'itemRepository' => $this->itemRepository,
185+
'searchCriteriaBuilder' => $this->searchCriteriaBuilder
160186
]
161187
);
162188
}
163189

164-
public function testGetItemById()
190+
/**
191+
* Test testGetItems method.
192+
*/
193+
public function testGetItems()
165194
{
166-
$realOrderItemId = 1;
167-
$fakeOrderItemId = 2;
195+
$orderItems = [$this->item];
196+
197+
$this->searchCriteriaBuilder->expects($this->once())->method('addFilter')->willReturnSelf();
198+
199+
$searchCriteria = $this->getMockBuilder(SearchCriteria::class)
200+
->disableOriginalConstructor()->getMockForAbstractClass();
201+
$this->searchCriteriaBuilder->expects($this->once())->method('create')->willReturn($searchCriteria);
168202

169-
$orderItem = $this->createMock(\Magento\Sales\Model\Order\Item::class);
203+
$itemsCollection = $this->getMockBuilder(OrderItemSearchResultInterface::class)
204+
->setMethods(['getItems'])
205+
->disableOriginalConstructor()->getMockForAbstractClass();
206+
$itemsCollection->expects($this->once())->method('getItems')->willReturn($orderItems);
207+
$this->itemRepository->expects($this->once())->method('getList')->willReturn($itemsCollection);
170208

209+
$this->assertEquals($orderItems, $this->order->getItems());
210+
}
211+
212+
/**
213+
* Prepare order item mock.
214+
*
215+
* @param int $orderId
216+
* @return void
217+
*/
218+
private function prepareOrderItem(int $orderId = 0)
219+
{
171220
$this->order->setData(
172221
\Magento\Sales\Api\Data\OrderInterface::ITEMS,
173222
[
174-
$realOrderItemId => $orderItem
223+
$orderId => $this->item
175224
]
176225
);
226+
}
177227

178-
$this->assertEquals($orderItem, $this->order->getItemById($realOrderItemId));
228+
/**
229+
* Test GetItemById method.
230+
*
231+
* @return void
232+
*/
233+
public function testGetItemById()
234+
{
235+
$realOrderItemId = 1;
236+
$fakeOrderItemId = 2;
237+
238+
$this->prepareOrderItem($realOrderItemId);
239+
240+
$this->assertEquals($this->item, $this->order->getItemById($realOrderItemId));
179241
$this->assertEquals(null, $this->order->getItemById($fakeOrderItemId));
180242
}
181243

182244
/**
245+
* Test GetItemByQuoteItemId method.
246+
*
183247
* @param int|null $gettingQuoteItemId
184248
* @param int|null $quoteItemId
185249
* @param string|null $result
186250
*
187251
* @dataProvider dataProviderGetItemByQuoteItemId
252+
* @return void
188253
*/
189254
public function testGetItemByQuoteItemId($gettingQuoteItemId, $quoteItemId, $result)
190255
{
256+
$this->prepareOrderItem();
257+
191258
$this->item->expects($this->any())
192259
->method('getQuoteItemId')
193260
->willReturn($gettingQuoteItemId);
@@ -212,14 +279,19 @@ public function dataProviderGetItemByQuoteItemId()
212279
}
213280

214281
/**
282+
* Test getAllVisibleItems method.
283+
*
215284
* @param bool $isDeleted
216285
* @param int|null $parentItemId
217286
* @param array $result
218287
*
219288
* @dataProvider dataProviderGetAllVisibleItems
289+
* @return void
220290
*/
221291
public function testGetAllVisibleItems($isDeleted, $parentItemId, array $result)
222292
{
293+
$this->prepareOrderItem();
294+
223295
$this->item->expects($this->once())
224296
->method('isDeleted')
225297
->willReturn($isDeleted);
@@ -263,8 +335,15 @@ public function testCanCancelIsPaymentReview()
263335
$this->assertFalse($this->order->canCancel());
264336
}
265337

338+
/**
339+
* Test CanInvoice method.
340+
*
341+
* @return void
342+
*/
266343
public function testCanInvoice()
267344
{
345+
$this->prepareOrderItem();
346+
268347
$this->item->expects($this->any())
269348
->method('getQtyToInvoice')
270349
->willReturn(42);
@@ -304,8 +383,15 @@ public function testCanNotInvoiceWhenActionInvoiceFlagIsFalse()
304383
$this->assertFalse($this->order->canInvoice());
305384
}
306385

386+
/**
387+
* Test CanNotInvoice method when invoice is locked.
388+
*
389+
* @return void
390+
*/
307391
public function testCanNotInvoiceWhenLockedInvoice()
308392
{
393+
$this->prepareOrderItem();
394+
309395
$this->item->expects($this->any())
310396
->method('getQtyToInvoice')
311397
->willReturn(42);
@@ -315,8 +401,15 @@ public function testCanNotInvoiceWhenLockedInvoice()
315401
$this->assertFalse($this->order->canInvoice());
316402
}
317403

404+
/**
405+
* Test CanNotInvoice method when didn't have qty to invoice.
406+
*
407+
* @return void
408+
*/
318409
public function testCanNotInvoiceWhenDidNotHaveQtyToInvoice()
319410
{
411+
$this->prepareOrderItem();
412+
320413
$this->item->expects($this->any())
321414
->method('getQtyToInvoice')
322415
->willReturn(0);
@@ -329,6 +422,7 @@ public function testCanNotInvoiceWhenDidNotHaveQtyToInvoice()
329422
public function testCanCreditMemo()
330423
{
331424
$totalPaid = 10;
425+
$this->prepareOrderItem();
332426
$this->order->setTotalPaid($totalPaid);
333427
$this->priceCurrency->expects($this->once())->method('round')->with($totalPaid)->willReturnArgument(0);
334428
$this->assertTrue($this->order->canCreditmemo());
@@ -344,6 +438,7 @@ public function testCanCreditMemoForZeroTotal()
344438
$grandTotal = 0;
345439
$totalPaid = 0;
346440
$totalRefunded = 0;
441+
$this->prepareOrderItem();
347442
$this->order->setGrandTotal($grandTotal);
348443
$this->order->setTotalPaid($totalPaid);
349444
$this->assertFalse($this->order->canCreditmemoForZeroTotal($totalRefunded));
@@ -352,6 +447,7 @@ public function testCanCreditMemoForZeroTotal()
352447
public function testCanNotCreditMemoWithTotalNull()
353448
{
354449
$totalPaid = 0;
450+
$this->prepareOrderItem();
355451
$this->order->setTotalPaid($totalPaid);
356452
$this->priceCurrency->expects($this->once())->method('round')->with($totalPaid)->willReturnArgument(0);
357453
$this->assertFalse($this->order->canCreditmemo());
@@ -363,6 +459,7 @@ public function testCanNotCreditMemoWithAdjustmentNegative()
363459
$adjustmentNegative = 10;
364460
$totalRefunded = 90;
365461

462+
$this->prepareOrderItem();
366463
$this->order->setTotalPaid($totalPaid);
367464
$this->order->setTotalRefunded($totalRefunded);
368465
$this->order->setAdjustmentNegative($adjustmentNegative);
@@ -377,6 +474,7 @@ public function testCanCreditMemoWithAdjustmentNegativeLowerThanTotalPaid()
377474
$adjustmentNegative = 9;
378475
$totalRefunded = 90;
379476

477+
$this->prepareOrderItem();
380478
$this->order->setTotalPaid($totalPaid);
381479
$this->order->setTotalRefunded($totalRefunded);
382480
$this->order->setAdjustmentNegative($adjustmentNegative);
@@ -601,8 +699,15 @@ public function testCanCancelCanReviewPayment()
601699
$this->assertFalse($this->order->canCancel());
602700
}
603701

702+
/**
703+
* Test CanCancelAllInvoiced method.
704+
*
705+
* @return void
706+
*/
604707
public function testCanCancelAllInvoiced()
605708
{
709+
$this->prepareOrderItem();
710+
606711
$paymentMock = $this->getMockBuilder(\Magento\Sales\Model\ResourceModel\Order\Payment::class)
607712
->disableOriginalConstructor()
608713
->setMethods(['isDeleted', 'canReviewPayment', 'canFetchTransactionInfo', '__wakeUp'])
@@ -662,11 +767,16 @@ public function testCanCancelState()
662767
}
663768

664769
/**
770+
* Test CanCancelActionFlag method.
771+
*
665772
* @param bool $cancelActionFlag
666773
* @dataProvider dataProviderActionFlag
774+
* @return void
667775
*/
668776
public function testCanCancelActionFlag($cancelActionFlag)
669777
{
778+
$this->prepareOrderItem();
779+
670780
$paymentMock = $this->getMockBuilder(\Magento\Sales\Model\ResourceModel\Order\Payment::class)
671781
->disableOriginalConstructor()
672782
->setMethods(['isDeleted', 'canReviewPayment', 'canFetchTransactionInfo', '__wakeUp'])

0 commit comments

Comments
 (0)