9 use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory as ProductCollectionFactory;
14 use Magento\Sales\Model\ResourceModel\Order\Status\History\CollectionFactory as HistoryCollectionFactory;
83 private $localeResolver;
92 $helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
93 $this->paymentCollectionFactoryMock = $this->createPartialMock(
97 $this->orderItemCollectionFactoryMock = $this->createPartialMock(
101 $this->historyCollectionFactoryMock = $this->createPartialMock(
105 $this->productCollectionFactoryMock = $this->createPartialMock(
109 $this->salesOrderCollectionFactoryMock = $this->createPartialMock(
118 'getLockedDoInvoice',
121 $this->salesOrderCollectionMock = $this->getMockBuilder(
123 )->disableOriginalConstructor()
124 ->setMethods([
'addFieldToFilter',
'load',
'getFirstItem'])
127 $collection->expects($this->any())->method(
'setOrderFilter')->willReturnSelf();
128 $collection->expects($this->any())->method(
'getItems')->willReturn([$this->item]);
129 $collection->expects($this->any())->method(
'getIterator')->willReturn(
new \ArrayIterator([$this->item]));
130 $this->orderItemCollectionFactoryMock->expects($this->any())->method(
'create')->willReturn(
$collection);
132 $this->priceCurrency = $this->getMockForAbstractClass(
133 \
Magento\Framework\Pricing\PriceCurrencyInterface::class,
141 $this->localeResolver = $this->createMock(ResolverInterface::class);
142 $this->timezone = $this->createMock(TimezoneInterface::class);
143 $this->incrementId =
'#00000001';
144 $this->eventManager = $this->createMock(\
Magento\Framework\Event\Manager::class);
145 $context = $this->createPartialMock(\
Magento\Framework\Model\Context::class, [
'getEventDispatcher']);
146 $context->expects($this->any())->method(
'getEventDispatcher')->willReturn($this->eventManager);
147 $this->order =
$helper->getObject(
148 \
Magento\Sales\Model\Order::class,
150 'paymentCollectionFactory' => $this->paymentCollectionFactoryMock,
151 'orderItemCollectionFactory' => $this->orderItemCollectionFactoryMock,
152 'data' => [
'increment_id' => $this->incrementId],
153 'context' => $context,
154 'historyCollectionFactory' => $this->historyCollectionFactoryMock,
155 'salesOrderCollectionFactory' => $this->salesOrderCollectionFactoryMock,
156 'priceCurrency' => $this->priceCurrency,
157 'productListFactory' => $this->productCollectionFactoryMock,
158 'localeResolver' => $this->localeResolver,
159 'timezone' => $this->timezone,
166 $realOrderItemId = 1;
167 $fakeOrderItemId = 2;
171 $this->order->setData(
178 $this->assertEquals(
$orderItem, $this->order->getItemById($realOrderItemId));
179 $this->assertEquals(
null, $this->order->getItemById($fakeOrderItemId));
191 $this->item->expects($this->any())
192 ->method(
'getQuoteItemId')
193 ->willReturn($gettingQuoteItemId);
208 [10, 10,
'replace-me'],
223 $this->item->expects($this->once())
224 ->method(
'isDeleted')
225 ->willReturn($isDeleted);
227 $this->item->expects($this->any())
228 ->method(
'getParentItemId')
229 ->willReturn($parentItemId);
235 $this->assertEquals(
$result, $this->order->getAllVisibleItems());
244 [
false,
null, [
'replace-me']],
256 $this->assertFalse($this->order->canCancel());
263 $this->assertFalse($this->order->canCancel());
268 $this->item->expects($this->any())
269 ->method(
'getQtyToInvoice')
271 $this->item->expects($this->any())
272 ->method(
'getLockedDoInvoice')
275 $this->assertTrue($this->order->canInvoice());
285 $this->item->expects($this->any())
286 ->method(
'getQtyToInvoice')
288 $this->item->expects($this->any())
289 ->method(
'getLockedDoInvoice')
291 $this->order->setData(
'state',
$status);
292 $this->assertFalse($this->order->canInvoice());
297 $this->item->expects($this->any())
298 ->method(
'getQtyToInvoice')
300 $this->item->expects($this->any())
301 ->method(
'getLockedDoInvoice')
304 $this->assertFalse($this->order->canInvoice());
309 $this->item->expects($this->any())
310 ->method(
'getQtyToInvoice')
312 $this->item->expects($this->any())
313 ->method(
'getLockedDoInvoice')
315 $this->assertFalse($this->order->canInvoice());
320 $this->item->expects($this->any())
321 ->method(
'getQtyToInvoice')
323 $this->item->expects($this->any())
324 ->method(
'getLockedDoInvoice')
326 $this->assertFalse($this->order->canInvoice());
332 $this->order->setTotalPaid($totalPaid);
333 $this->priceCurrency->expects($this->once())->method(
'round')->with($totalPaid)->willReturnArgument(0);
334 $this->assertTrue($this->order->canCreditmemo());
340 $this->order->setTotalPaid($totalPaid);
341 $this->priceCurrency->expects($this->once())->method(
'round')->with($totalPaid)->willReturnArgument(0);
342 $this->assertFalse($this->order->canCreditmemo());
348 $adjustmentNegative = 10;
351 $this->order->setTotalPaid($totalPaid);
352 $this->order->setTotalRefunded($totalRefunded);
353 $this->order->setAdjustmentNegative($adjustmentNegative);
354 $this->priceCurrency->expects($this->once())->method(
'round')->with($totalPaid)->willReturnArgument(0);
356 $this->assertFalse($this->order->canCreditmemo());
362 $adjustmentNegative = 9;
365 $this->order->setTotalPaid($totalPaid);
366 $this->order->setTotalRefunded($totalRefunded);
367 $this->order->setAdjustmentNegative($adjustmentNegative);
368 $this->priceCurrency->expects($this->once())->method(
'round')->with($totalPaid)->willReturnArgument(0);
370 $this->assertTrue($this->order->canCreditmemo());
380 $this->order->setData(
'state', $state);
381 $this->assertFalse($this->order->canCreditmemo());
386 $this->order->setData(
'forced_can_creditmemo',
true);
387 $this->assertTrue($this->order->canCreditmemo());
393 ->disableOriginalConstructor()
394 ->setMethods([
'count'])
397 $invoiceCollection->expects($this->once())
401 $this->order->setInvoiceCollection($invoiceCollection);
404 $this->assertFalse($this->order->canEdit());
417 $this->item->expects($this->any())
418 ->method(
'getProductId')
421 $product = $this->getMockBuilder(ProductInterface::class)
422 ->setMethods([
'isSalable'])
423 ->getMockForAbstractClass();
425 ->method(
'isSalable')
429 ->disableOriginalConstructor()
430 ->setMethods([
'setStoreId',
'addIdFilter',
'load',
'getItemById',
'addAttributeToSelect'])
433 ->method(
'setStoreId')
436 ->method(
'addIdFilter')
439 ->method(
'addAttributeToSelect')
445 ->method(
'getItemById')
448 $this->productCollectionFactoryMock->expects($this->once())
452 $this->assertTrue($this->order->canReorder());
462 $this->assertFalse($this->order->canReorder());
473 $this->assertFalse($this->order->canReorder());
486 $this->item->expects($this->any())
487 ->method(
'getProductId')
490 $product = $this->getMockBuilder(ProductInterface::class)
491 ->setMethods([
'isSalable'])
492 ->getMockForAbstractClass();
494 ->method(
'isSalable');
497 ->disableOriginalConstructor()
498 ->setMethods([
'setStoreId',
'addIdFilter',
'load',
'getItemById',
'addAttributeToSelect'])
501 ->method(
'setStoreId')
504 ->method(
'addIdFilter')
510 ->method(
'getItemById')
513 $this->productCollectionFactoryMock->expects($this->once())
518 ->method(
'addAttributeToSelect')
520 $this->assertFalse($this->order->canReorder());
533 $this->item->expects($this->any())
534 ->method(
'getProductId')
537 $product = $this->getMockBuilder(ProductInterface::class)
538 ->setMethods([
'isSalable'])
539 ->getMockForAbstractClass();
541 ->method(
'isSalable')
545 ->disableOriginalConstructor()
546 ->setMethods([
'setStoreId',
'addIdFilter',
'load',
'getItemById',
'addAttributeToSelect'])
549 ->method(
'setStoreId')
552 ->method(
'addIdFilter')
558 ->method(
'getItemById')
561 $this->productCollectionFactoryMock->expects($this->once())
566 ->method(
'addAttributeToSelect')
568 $this->assertFalse($this->order->canReorder());
574 ->disableOriginalConstructor()
575 ->setMethods([
'isDeleted',
'canReviewPayment',
'canFetchTransactionInfo',
'__wakeUp'])
577 $paymentMock->expects($this->any())
578 ->method(
'canReviewPayment')
579 ->will($this->returnValue(
false));
580 $paymentMock->expects($this->any())
581 ->method(
'canFetchTransactionInfo')
582 ->will($this->returnValue(
true));
586 $this->assertFalse($this->order->canCancel());
592 ->disableOriginalConstructor()
593 ->setMethods([
'isDeleted',
'canReviewPayment',
'canFetchTransactionInfo',
'__wakeUp'])
595 $paymentMock->expects($this->any())
596 ->method(
'canReviewPayment')
597 ->will($this->returnValue(
false));
598 $paymentMock->expects($this->any())
599 ->method(
'canFetchTransactionInfo')
600 ->will($this->returnValue(
false));
601 $collectionMock = $this->createPartialMock(
603 [
'getItems',
'setOrderFilter']
605 $this->orderItemCollectionFactoryMock->expects($this->any())
607 ->will($this->returnValue($collectionMock));
608 $collectionMock->expects($this->any())
609 ->method(
'setOrderFilter')
618 $this->item->expects($this->any())
619 ->method(
'isDeleted')
621 $this->item->expects($this->any())
622 ->method(
'getQtyToInvoice')
625 $this->assertFalse($this->order->canCancel());
631 ->disableOriginalConstructor()
632 ->setMethods([
'isDeleted',
'canReviewPayment',
'canFetchTransactionInfo',
'__wakeUp'])
634 $paymentMock->expects($this->any())
635 ->method(
'canReviewPayment')
636 ->will($this->returnValue(
false));
637 $paymentMock->expects($this->any())
638 ->method(
'canFetchTransactionInfo')
639 ->will($this->returnValue(
false));
646 $this->assertFalse($this->order->canCancel());
656 ->disableOriginalConstructor()
657 ->setMethods([
'isDeleted',
'canReviewPayment',
'canFetchTransactionInfo',
'__wakeUp'])
659 $paymentMock->expects($this->any())
660 ->method(
'canReviewPayment')
661 ->will($this->returnValue(
false));
662 $paymentMock->expects($this->any())
663 ->method(
'canFetchTransactionInfo')
664 ->will($this->returnValue(
false));
674 foreach ($actionFlags as $action => $flag) {
675 $this->order->setActionFlag($action, $flag);
679 $this->item->expects($this->any())
680 ->method(
'isDeleted')
682 $this->item->expects($this->any())
683 ->method(
'getQtyToInvoice')
686 $this->assertEquals($cancelActionFlag, $this->order->canCancel());
694 public function testCanVoidPayment($actionFlags, $orderState)
696 $helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
699 foreach ($actionFlags as $action => $flag) {
700 $order->setActionFlag($action, $flag);
702 $order->setData(
'state', $orderState);
704 $canVoidOrder =
true;
707 $canVoidOrder =
false;
711 $canVoidOrder =
false;
717 $canVoidOrder =
false;
722 $expected =
'some value';
728 $this->returnValue($expected)
731 $payment->expects($this->never())->method(
'canVoid');
733 $this->assertEquals($expected,
$order->canVoidPayment());
741 $iterator = new \ArrayIterator([$paymentMock]);
744 ->disableOriginalConstructor()
745 ->setMethods([
'setOrderFilter',
'getIterator'])
747 $collectionMock->expects($this->any())
748 ->method(
'getIterator')
750 $collectionMock->expects($this->any())
751 ->method(
'setOrderFilter')
752 ->will($this->returnSelf());
754 $this->paymentCollectionFactoryMock->expects($this->any())
756 ->will($this->returnValue($collectionMock));
770 )->disableOriginalConstructor()->getMock();
774 $payment->expects($this->any())->method(
'isDeleted')->will($this->returnValue(
false));
824 ->disableOriginalConstructor()
825 ->setMethods([
'isDeleted',
'filterByTypes',
'filterByParent',
'getQtyToInvoice',
'__wakeUp'])
828 $itemMock->expects($this->any())
829 ->method(
'getQtyToInvoice')
830 ->will($this->returnValue($qtyInvoiced));
832 $iterator = new \ArrayIterator([$itemMock]);
835 ->disableOriginalConstructor()
836 ->setMethods([
'setOrderFilter',
'getIterator',
'getItems'])
838 $itemCollectionMock->expects($this->any())
839 ->method(
'getIterator')
841 $itemCollectionMock->expects($this->any())
842 ->method(
'setOrderFilter')
843 ->will($this->returnSelf());
845 $this->orderItemCollectionFactoryMock->expects($this->any())
847 ->will($this->returnValue($itemCollectionMock));
880 $this->assertEquals($this->incrementId, $this->order->getIncrementId());
885 $this->assertEquals(
'order', $this->order->getEntityType());
895 $itemMock = $this->getMockForAbstractClass(
896 \
Magento\Sales\Api\
Data\OrderStatusHistoryInterface::class,
905 ->setMethods([
'setOrder'])
906 ->disableOriginalConstructor()
907 ->getMockForAbstractClass();
908 $collectionMock = $this->createPartialMock(
921 $collectionItems = [$itemMock];
923 $collectionMock->expects($this->once())
924 ->method(
'setOrderFilter')
927 $collectionMock->expects($this->once())
929 ->with(
'created_at',
'desc')
930 ->willReturn($dbMock);
931 $dbMock->expects($this->once())
933 ->with(
'entity_id',
'desc')
934 ->willReturn($collectionMock);
935 $collectionMock->expects($this->once())
937 ->willReturn($collectionItems);
939 $this->historyCollectionFactoryMock->expects($this->once())
941 ->willReturn($collectionMock);
943 for (
$i = 10; --
$i;) {
944 $this->assertEquals($collectionItems, $this->order->getStatusHistories());
952 $this->salesOrderCollectionFactoryMock
953 ->expects($this->once())
955 ->willReturn($this->salesOrderCollectionMock);
956 $this->salesOrderCollectionMock->expects($this->any())->method(
'addFieldToFilter')->willReturnSelf();
957 $this->salesOrderCollectionMock->expects($this->once())->method(
'load')->willReturnSelf();
958 $this->salesOrderCollectionMock->expects($this->once())->method(
'getFirstItem')->willReturn($this->order);
959 $this->assertSame($this->order, $this->order->loadByIncrementIdAndStoreId(
$incrementId,
$storeId));
964 $this->order->setId(123);
966 ->disableOriginalConstructor()
969 $this->order->setDataChanges(
false);
977 ->method(
'setParentId')
988 $this->order->getData(
995 $this->order->hasDataChanges()
1001 $this->order->setId(123);
1002 $this->order->setDataChanges(
false);
1005 ->disableOriginalConstructor()
1009 ->method(
'setOrder')
1010 ->with($this->order)
1014 ->method(
'setParentId')
1022 $this->order->setPayment(
$payment);
1024 $this->assertEquals(
1025 $this->order->getData(
1032 $this->order->hasDataChanges()
1038 $this->assertEquals(
null, $this->order->setPayment(
null));
1040 $this->assertEquals(
1041 $this->order->getData(
1048 $this->order->hasDataChanges()
1055 ->disableOriginalConstructor()
1058 $this->order->reset();
1059 $this->assertEquals(
1060 $this->order->getData(
1067 $this->order->hasDataChanges()
1073 $localeCode =
'nl_NL';
1075 $this->localeResolver->expects($this->once())->method(
'getDefaultLocale')->willReturn($localeCode);
1076 $this->timezone->expects($this->once())->method(
'formatDateTime')
1084 $this->order->getCreatedAtFormatted(\IntlDateFormatter::SHORT);
notInvoicingStatesProvider()
testCanReorderFlagReorderFalse()
testLoadByIncrementIdAndStoreId()
testGetAllVisibleItems($isDeleted, $parentItemId, array $result)
$historyCollectionFactoryMock
testGetCreatedAtFormattedUsesCorrectLocale()
testCanNotCreditMemoWithForced()
testCanCancelCanReviewPayment()
testCanCreditMemoWithAdjustmentNegativeLowerThanTotalPaid()
testCanReorderProductNotExists()
$salesOrderCollectionFactoryMock
testCanNotCreditMemoWithTotalNull()
const ACTION_FLAG_REORDER
dataProviderGetAllVisibleItems()
preparePaymentMock($paymentMock)
prepareItemMock($qtyInvoiced)
testResetOrderWillResetPayment()
dataProviderGetItemByQuoteItemId()
$productCollectionFactoryMock
canVoidPaymentDataProvider()
$orderItemCollectionFactoryMock
$salesOrderCollectionMock
_prepareOrderPayment($order, $mockedMethods=[])
testCanCancelAllInvoiced()
testCanNotInvoiceInSomeStates($status)
$paymentCollectionFactoryMock
testCanReorderIsPaymentReview()
testGetItemByQuoteItemId($gettingQuoteItemId, $quoteItemId, $result)
const ACTION_FLAG_INVOICE
testCanNotCreditMemoWithSomeStates($state)
testCanEditIfHasInvoices()
const STATE_PAYMENT_REVIEW
testCanNotCreditMemoWithAdjustmentNegative()
testCanNotInvoiceWhenActionInvoiceFlagIsFalse()
testCanCancelActionFlag($cancelActionFlag)
testCanNotInvoiceWhenLockedInvoice()
canNotCreditMemoStatesProvider()
testCanCancelIsPaymentReview()
testCanNotInvoiceWhenDidNotHaveQtyToInvoice()
testCanReorderProductNotSalable()