Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
OrderTest.php
Go to the documentation of this file.
1 <?php
7 
9 use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory as ProductCollectionFactory;
14 use Magento\Sales\Model\ResourceModel\Order\Status\History\CollectionFactory as HistoryCollectionFactory;
15 
23 class OrderTest extends \PHPUnit\Framework\TestCase
24 {
29 
34 
38  protected $order;
39 
43  protected $eventManager;
44 
48  protected $incrementId;
49 
53  protected $item;
54 
59 
63  protected $priceCurrency;
64 
69 
74 
79 
83  private $localeResolver;
84 
88  private $timezone;
89 
90  protected function setUp()
91  {
92  $helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
93  $this->paymentCollectionFactoryMock = $this->createPartialMock(
94  \Magento\Sales\Model\ResourceModel\Order\Payment\CollectionFactory::class,
95  ['create']
96  );
97  $this->orderItemCollectionFactoryMock = $this->createPartialMock(
98  \Magento\Sales\Model\ResourceModel\Order\Item\CollectionFactory::class,
99  ['create']
100  );
101  $this->historyCollectionFactoryMock = $this->createPartialMock(
102  \Magento\Sales\Model\ResourceModel\Order\Status\History\CollectionFactory::class,
103  ['create']
104  );
105  $this->productCollectionFactoryMock = $this->createPartialMock(
106  \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory::class,
107  ['create']
108  );
109  $this->salesOrderCollectionFactoryMock = $this->createPartialMock(
110  \Magento\Sales\Model\ResourceModel\Order\CollectionFactory::class,
111  ['create']
112  );
113  $this->item = $this->createPartialMock(\Magento\Sales\Model\ResourceModel\Order\Item::class, [
114  'isDeleted',
115  'getQtyToInvoice',
116  'getParentItemId',
117  'getQuoteItemId',
118  'getLockedDoInvoice',
119  'getProductId',
120  ]);
121  $this->salesOrderCollectionMock = $this->getMockBuilder(
122  \Magento\Sales\Model\ResourceModel\Order\Collection::class
123  )->disableOriginalConstructor()
124  ->setMethods(['addFieldToFilter', 'load', 'getFirstItem'])
125  ->getMock();
126  $collection = $this->createMock(\Magento\Sales\Model\ResourceModel\Order\Item\Collection::class);
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);
131 
132  $this->priceCurrency = $this->getMockForAbstractClass(
133  \Magento\Framework\Pricing\PriceCurrencyInterface::class,
134  [],
135  '',
136  false,
137  false,
138  true,
139  ['round']
140  );
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,
149  [
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,
160  ]
161  );
162  }
163 
164  public function testGetItemById()
165  {
166  $realOrderItemId = 1;
167  $fakeOrderItemId = 2;
168 
169  $orderItem = $this->createMock(\Magento\Sales\Model\Order\Item::class);
170 
171  $this->order->setData(
173  [
174  $realOrderItemId => $orderItem
175  ]
176  );
177 
178  $this->assertEquals($orderItem, $this->order->getItemById($realOrderItemId));
179  $this->assertEquals(null, $this->order->getItemById($fakeOrderItemId));
180  }
181 
189  public function testGetItemByQuoteItemId($gettingQuoteItemId, $quoteItemId, $result)
190  {
191  $this->item->expects($this->any())
192  ->method('getQuoteItemId')
193  ->willReturn($gettingQuoteItemId);
194 
195  if ($result !== null) {
197  }
198 
199  $this->assertEquals($result, $this->order->getItemByQuoteItemId($quoteItemId));
200  }
201 
206  {
207  return [
208  [10, 10, 'replace-me'],
209  [10, 88, null],
210  [88, 10, null],
211  ];
212  }
213 
221  public function testGetAllVisibleItems($isDeleted, $parentItemId, array $result)
222  {
223  $this->item->expects($this->once())
224  ->method('isDeleted')
225  ->willReturn($isDeleted);
226 
227  $this->item->expects($this->any())
228  ->method('getParentItemId')
229  ->willReturn($parentItemId);
230 
231  if (!empty($result)) {
232  $result = [$this->item];
233  }
234 
235  $this->assertEquals($result, $this->order->getAllVisibleItems());
236  }
237 
242  {
243  return [
244  [false, null, ['replace-me']],
245  [true, null, []],
246  [true, 10, []],
247  [false, 10, []],
248  [true, null, []],
249  ];
250  }
251 
252  public function testCanCancelCanUnhold()
253  {
254  $this->order->setActionFlag(\Magento\Sales\Model\Order::ACTION_FLAG_UNHOLD, true);
255  $this->order->setState(\Magento\Sales\Model\Order::STATE_PAYMENT_REVIEW);
256  $this->assertFalse($this->order->canCancel());
257  }
258 
260  {
261  $this->order->setActionFlag(\Magento\Sales\Model\Order::ACTION_FLAG_UNHOLD, false);
262  $this->order->setState(\Magento\Sales\Model\Order::STATE_PAYMENT_REVIEW);
263  $this->assertFalse($this->order->canCancel());
264  }
265 
266  public function testCanInvoice()
267  {
268  $this->item->expects($this->any())
269  ->method('getQtyToInvoice')
270  ->willReturn(42);
271  $this->item->expects($this->any())
272  ->method('getLockedDoInvoice')
273  ->willReturn(false);
274 
275  $this->assertTrue($this->order->canInvoice());
276  }
277 
284  {
285  $this->item->expects($this->any())
286  ->method('getQtyToInvoice')
287  ->willReturn(42);
288  $this->item->expects($this->any())
289  ->method('getLockedDoInvoice')
290  ->willReturn(false);
291  $this->order->setData('state', $status);
292  $this->assertFalse($this->order->canInvoice());
293  }
294 
296  {
297  $this->item->expects($this->any())
298  ->method('getQtyToInvoice')
299  ->willReturn(42);
300  $this->item->expects($this->any())
301  ->method('getLockedDoInvoice')
302  ->willReturn(false);
303  $this->order->setActionFlag(\Magento\Sales\Model\Order::ACTION_FLAG_INVOICE, false);
304  $this->assertFalse($this->order->canInvoice());
305  }
306 
308  {
309  $this->item->expects($this->any())
310  ->method('getQtyToInvoice')
311  ->willReturn(42);
312  $this->item->expects($this->any())
313  ->method('getLockedDoInvoice')
314  ->willReturn(true);
315  $this->assertFalse($this->order->canInvoice());
316  }
317 
319  {
320  $this->item->expects($this->any())
321  ->method('getQtyToInvoice')
322  ->willReturn(0);
323  $this->item->expects($this->any())
324  ->method('getLockedDoInvoice')
325  ->willReturn(false);
326  $this->assertFalse($this->order->canInvoice());
327  }
328 
329  public function testCanCreditMemo()
330  {
331  $totalPaid = 10;
332  $this->order->setTotalPaid($totalPaid);
333  $this->priceCurrency->expects($this->once())->method('round')->with($totalPaid)->willReturnArgument(0);
334  $this->assertTrue($this->order->canCreditmemo());
335  }
336 
338  {
339  $totalPaid = 0;
340  $this->order->setTotalPaid($totalPaid);
341  $this->priceCurrency->expects($this->once())->method('round')->with($totalPaid)->willReturnArgument(0);
342  $this->assertFalse($this->order->canCreditmemo());
343  }
344 
346  {
347  $totalPaid = 100;
348  $adjustmentNegative = 10;
349  $totalRefunded = 90;
350 
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);
355 
356  $this->assertFalse($this->order->canCreditmemo());
357  }
358 
360  {
361  $totalPaid = 100;
362  $adjustmentNegative = 9;
363  $totalRefunded = 90;
364 
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);
369 
370  $this->assertTrue($this->order->canCreditmemo());
371  }
372 
378  public function testCanNotCreditMemoWithSomeStates($state)
379  {
380  $this->order->setData('state', $state);
381  $this->assertFalse($this->order->canCreditmemo());
382  }
383 
385  {
386  $this->order->setData('forced_can_creditmemo', true);
387  $this->assertTrue($this->order->canCreditmemo());
388  }
389 
390  public function testCanEditIfHasInvoices()
391  {
392  $invoiceCollection = $this->getMockBuilder(\Magento\Sales\Model\ResourceModel\Order\Invoice\Collection::class)
393  ->disableOriginalConstructor()
394  ->setMethods(['count'])
395  ->getMock();
396 
397  $invoiceCollection->expects($this->once())
398  ->method('count')
399  ->willReturn(2);
400 
401  $this->order->setInvoiceCollection($invoiceCollection);
402  $this->order->setState(\Magento\Sales\Model\Order::STATE_PROCESSING);
403 
404  $this->assertFalse($this->order->canEdit());
405  }
406 
410  public function testCanReorder()
411  {
412  $productId = 1;
413 
414  $this->order->setState(Order::STATE_PROCESSING);
415  $this->order->setActionFlag(Order::ACTION_FLAG_REORDER, true);
416 
417  $this->item->expects($this->any())
418  ->method('getProductId')
419  ->willReturn($productId);
420 
421  $product = $this->getMockBuilder(ProductInterface::class)
422  ->setMethods(['isSalable'])
423  ->getMockForAbstractClass();
424  $product->expects(static::once())
425  ->method('isSalable')
426  ->willReturn(true);
427 
428  $productCollection = $this->getMockBuilder(\Magento\Catalog\Model\ResourceModel\Product\Collection::class)
429  ->disableOriginalConstructor()
430  ->setMethods(['setStoreId', 'addIdFilter', 'load', 'getItemById', 'addAttributeToSelect'])
431  ->getMock();
432  $productCollection->expects($this->once())
433  ->method('setStoreId')
434  ->willReturnSelf();
435  $productCollection->expects($this->once())
436  ->method('addIdFilter')
437  ->willReturnSelf();
438  $productCollection->expects($this->once())
439  ->method('addAttributeToSelect')
440  ->willReturnSelf();
441  $productCollection->expects($this->once())
442  ->method('load')
443  ->willReturnSelf();
444  $productCollection->expects($this->once())
445  ->method('getItemById')
446  ->with($productId)
447  ->willReturn($product);
448  $this->productCollectionFactoryMock->expects($this->once())
449  ->method('create')
450  ->willReturn($productCollection);
451 
452  $this->assertTrue($this->order->canReorder());
453  }
454 
459  {
460  $this->order->setState(Order::STATE_PAYMENT_REVIEW);
461 
462  $this->assertFalse($this->order->canReorder());
463  }
464 
469  {
470  $this->order->setState(Order::STATE_PROCESSING);
471  $this->order->setActionFlag(Order::ACTION_FLAG_REORDER, false);
472 
473  $this->assertFalse($this->order->canReorder());
474  }
475 
480  {
481  $productId = 1;
482 
483  $this->order->setState(Order::STATE_PROCESSING);
484  $this->order->setActionFlag(Order::ACTION_FLAG_REORDER, true);
485 
486  $this->item->expects($this->any())
487  ->method('getProductId')
488  ->willReturn($productId);
489 
490  $product = $this->getMockBuilder(ProductInterface::class)
491  ->setMethods(['isSalable'])
492  ->getMockForAbstractClass();
493  $product->expects(static::never())
494  ->method('isSalable');
495 
496  $productCollection = $this->getMockBuilder(\Magento\Catalog\Model\ResourceModel\Product\Collection::class)
497  ->disableOriginalConstructor()
498  ->setMethods(['setStoreId', 'addIdFilter', 'load', 'getItemById', 'addAttributeToSelect'])
499  ->getMock();
500  $productCollection->expects($this->once())
501  ->method('setStoreId')
502  ->willReturnSelf();
503  $productCollection->expects($this->once())
504  ->method('addIdFilter')
505  ->willReturnSelf();
506  $productCollection->expects($this->once())
507  ->method('load')
508  ->willReturnSelf();
509  $productCollection->expects($this->once())
510  ->method('getItemById')
511  ->with($productId)
512  ->willReturn(null);
513  $this->productCollectionFactoryMock->expects($this->once())
514  ->method('create')
515  ->willReturn($productCollection);
516 
517  $productCollection->expects($this->once())
518  ->method('addAttributeToSelect')
519  ->willReturnSelf();
520  $this->assertFalse($this->order->canReorder());
521  }
522 
527  {
528  $productId = 1;
529 
530  $this->order->setState(Order::STATE_PROCESSING);
531  $this->order->setActionFlag(Order::ACTION_FLAG_REORDER, true);
532 
533  $this->item->expects($this->any())
534  ->method('getProductId')
535  ->willReturn($productId);
536 
537  $product = $this->getMockBuilder(ProductInterface::class)
538  ->setMethods(['isSalable'])
539  ->getMockForAbstractClass();
540  $product->expects(static::once())
541  ->method('isSalable')
542  ->willReturn(false);
543 
544  $productCollection = $this->getMockBuilder(\Magento\Catalog\Model\ResourceModel\Product\Collection::class)
545  ->disableOriginalConstructor()
546  ->setMethods(['setStoreId', 'addIdFilter', 'load', 'getItemById', 'addAttributeToSelect'])
547  ->getMock();
548  $productCollection->expects($this->once())
549  ->method('setStoreId')
550  ->willReturnSelf();
551  $productCollection->expects($this->once())
552  ->method('addIdFilter')
553  ->willReturnSelf();
554  $productCollection->expects($this->once())
555  ->method('load')
556  ->willReturnSelf();
557  $productCollection->expects($this->once())
558  ->method('getItemById')
559  ->with($productId)
560  ->willReturn($product);
561  $this->productCollectionFactoryMock->expects($this->once())
562  ->method('create')
563  ->willReturn($productCollection);
564 
565  $productCollection->expects($this->once())
566  ->method('addAttributeToSelect')
567  ->willReturnSelf();
568  $this->assertFalse($this->order->canReorder());
569  }
570 
572  {
573  $paymentMock = $this->getMockBuilder(\Magento\Sales\Model\ResourceModel\Order\Payment::class)
574  ->disableOriginalConstructor()
575  ->setMethods(['isDeleted', 'canReviewPayment', 'canFetchTransactionInfo', '__wakeUp'])
576  ->getMock();
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));
583  $this->preparePaymentMock($paymentMock);
584  $this->order->setActionFlag(\Magento\Sales\Model\Order::ACTION_FLAG_UNHOLD, false);
585  $this->order->setState(\Magento\Sales\Model\Order::STATE_PAYMENT_REVIEW);
586  $this->assertFalse($this->order->canCancel());
587  }
588 
589  public function testCanCancelAllInvoiced()
590  {
591  $paymentMock = $this->getMockBuilder(\Magento\Sales\Model\ResourceModel\Order\Payment::class)
592  ->disableOriginalConstructor()
593  ->setMethods(['isDeleted', 'canReviewPayment', 'canFetchTransactionInfo', '__wakeUp'])
594  ->getMock();
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(
602  \Magento\Sales\Model\ResourceModel\Order\Item\Collection::class,
603  ['getItems', 'setOrderFilter']
604  );
605  $this->orderItemCollectionFactoryMock->expects($this->any())
606  ->method('create')
607  ->will($this->returnValue($collectionMock));
608  $collectionMock->expects($this->any())
609  ->method('setOrderFilter')
610  ->willReturnSelf();
611  $this->preparePaymentMock($paymentMock);
612 
613  $this->prepareItemMock(0);
614 
615  $this->order->setActionFlag(\Magento\Sales\Model\Order::ACTION_FLAG_UNHOLD, false);
616  $this->order->setState(\Magento\Sales\Model\Order::STATE_NEW);
617 
618  $this->item->expects($this->any())
619  ->method('isDeleted')
620  ->willReturn(false);
621  $this->item->expects($this->any())
622  ->method('getQtyToInvoice')
623  ->willReturn(0);
624 
625  $this->assertFalse($this->order->canCancel());
626  }
627 
628  public function testCanCancelState()
629  {
630  $paymentMock = $this->getMockBuilder(\Magento\Sales\Model\ResourceModel\Order\Payment::class)
631  ->disableOriginalConstructor()
632  ->setMethods(['isDeleted', 'canReviewPayment', 'canFetchTransactionInfo', '__wakeUp'])
633  ->getMock();
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));
640 
641  $this->preparePaymentMock($paymentMock);
642 
643  $this->prepareItemMock(1);
644  $this->order->setActionFlag(\Magento\Sales\Model\Order::ACTION_FLAG_UNHOLD, false);
645  $this->order->setState(\Magento\Sales\Model\Order::STATE_CANCELED);
646  $this->assertFalse($this->order->canCancel());
647  }
648 
653  public function testCanCancelActionFlag($cancelActionFlag)
654  {
655  $paymentMock = $this->getMockBuilder(\Magento\Sales\Model\ResourceModel\Order\Payment::class)
656  ->disableOriginalConstructor()
657  ->setMethods(['isDeleted', 'canReviewPayment', 'canFetchTransactionInfo', '__wakeUp'])
658  ->getMock();
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));
665 
666  $this->preparePaymentMock($paymentMock);
667 
668  $this->prepareItemMock(1);
669 
670  $actionFlags = [
673  ];
674  foreach ($actionFlags as $action => $flag) {
675  $this->order->setActionFlag($action, $flag);
676  }
677  $this->order->setData('state', \Magento\Sales\Model\Order::STATE_NEW);
678 
679  $this->item->expects($this->any())
680  ->method('isDeleted')
681  ->willReturn(false);
682  $this->item->expects($this->any())
683  ->method('getQtyToInvoice')
684  ->willReturn(42);
685 
686  $this->assertEquals($cancelActionFlag, $this->order->canCancel());
687  }
688 
694  public function testCanVoidPayment($actionFlags, $orderState)
695  {
696  $helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
698  $order = $helper->getObject(\Magento\Sales\Model\Order::class);
699  foreach ($actionFlags as $action => $flag) {
700  $order->setActionFlag($action, $flag);
701  }
702  $order->setData('state', $orderState);
704  $canVoidOrder = true;
705 
706  if ($orderState == \Magento\Sales\Model\Order::STATE_CANCELED) {
707  $canVoidOrder = false;
708  }
709 
710  if ($orderState == \Magento\Sales\Model\Order::STATE_PAYMENT_REVIEW) {
711  $canVoidOrder = false;
712  }
713  if ($orderState == \Magento\Sales\Model\Order::STATE_HOLDED && (!isset(
714  $actionFlags[\Magento\Sales\Model\Order::ACTION_FLAG_UNHOLD]
715  ) || $actionFlags[\Magento\Sales\Model\Order::ACTION_FLAG_UNHOLD] !== false)
716  ) {
717  $canVoidOrder = false;
718  }
719 
720  $expected = false;
721  if ($canVoidOrder) {
722  $expected = 'some value';
723  $payment->expects(
724  $this->any()
725  )->method(
726  'canVoid'
727  )->will(
728  $this->returnValue($expected)
729  );
730  } else {
731  $payment->expects($this->never())->method('canVoid');
732  }
733  $this->assertEquals($expected, $order->canVoidPayment());
734  }
735 
739  protected function preparePaymentMock($paymentMock)
740  {
741  $iterator = new \ArrayIterator([$paymentMock]);
742 
743  $collectionMock = $this->getMockBuilder(\Magento\Sales\Model\ResourceModel\Order\Payment\Collection::class)
744  ->disableOriginalConstructor()
745  ->setMethods(['setOrderFilter', 'getIterator'])
746  ->getMock();
747  $collectionMock->expects($this->any())
748  ->method('getIterator')
749  ->will($this->returnValue($iterator));
750  $collectionMock->expects($this->any())
751  ->method('setOrderFilter')
752  ->will($this->returnSelf());
753 
754  $this->paymentCollectionFactoryMock->expects($this->any())
755  ->method('create')
756  ->will($this->returnValue($collectionMock));
757  }
758 
766  protected function _prepareOrderPayment($order, $mockedMethods = [])
767  {
768  $payment = $this->getMockBuilder(
769  \Magento\Sales\Model\Order\Payment::class
770  )->disableOriginalConstructor()->getMock();
771  foreach ($mockedMethods as $method => $value) {
772  $payment->expects($this->any())->method($method)->will($this->returnValue($value));
773  }
774  $payment->expects($this->any())->method('isDeleted')->will($this->returnValue(false));
775 
776  $order->setData(\Magento\Sales\Api\Data\OrderInterface::PAYMENT, $payment);
777 
778  return $payment;
779  }
780 
785  protected function _getActionFlagsValues()
786  {
787  return [
788  [],
789  [
792  ],
793  [
796  ]
797  ];
798  }
799 
805  protected function _getOrderStatuses()
806  {
807  return [
814  ];
815  }
816 
821  protected function prepareItemMock($qtyInvoiced)
822  {
823  $itemMock = $this->getMockBuilder(\Magento\Sales\Model\ResourceModel\Order\Item::class)
824  ->disableOriginalConstructor()
825  ->setMethods(['isDeleted', 'filterByTypes', 'filterByParent', 'getQtyToInvoice', '__wakeUp'])
826  ->getMock();
827 
828  $itemMock->expects($this->any())
829  ->method('getQtyToInvoice')
830  ->will($this->returnValue($qtyInvoiced));
831 
832  $iterator = new \ArrayIterator([$itemMock]);
833 
834  $itemCollectionMock = $this->getMockBuilder(\Magento\Sales\Model\ResourceModel\Order\Item\Collection::class)
835  ->disableOriginalConstructor()
836  ->setMethods(['setOrderFilter', 'getIterator', 'getItems'])
837  ->getMock();
838  $itemCollectionMock->expects($this->any())
839  ->method('getIterator')
840  ->will($this->returnValue($iterator));
841  $itemCollectionMock->expects($this->any())
842  ->method('setOrderFilter')
843  ->will($this->returnSelf());
844 
845  $this->orderItemCollectionFactoryMock->expects($this->any())
846  ->method('create')
847  ->will($this->returnValue($itemCollectionMock));
848  }
849 
853  public function canVoidPaymentDataProvider()
854  {
855  $data = [];
856  foreach ($this->_getActionFlagsValues() as $actionFlags) {
857  foreach ($this->_getOrderStatuses() as $status) {
858  $data[] = [$actionFlags, $status];
859  }
860  }
861  return $data;
862  }
863 
867  public function dataProviderActionFlag()
868  {
869  return [
870  [false],
871  [true]
872  ];
873  }
874 
878  public function testGetIncrementId()
879  {
880  $this->assertEquals($this->incrementId, $this->order->getIncrementId());
881  }
882 
883  public function testGetEntityType()
884  {
885  $this->assertEquals('order', $this->order->getEntityType());
886  }
887 
893  public function testGetStatusHistories()
894  {
895  $itemMock = $this->getMockForAbstractClass(
896  \Magento\Sales\Api\Data\OrderStatusHistoryInterface::class,
897  [],
898  '',
899  false,
900  true,
901  true,
902  ['setOrder']
903  );
904  $dbMock = $this->getMockBuilder(\Magento\Framework\Data\Collection\AbstractDb::class)
905  ->setMethods(['setOrder'])
906  ->disableOriginalConstructor()
907  ->getMockForAbstractClass();
908  $collectionMock = $this->createPartialMock(
909  \Magento\Sales\Model\ResourceModel\Order\Status\History\Collection::class,
910  [
911  'setOrderFilter',
912  'setOrder',
913  'getItems',
914  'getIterator',
915  'toOptionArray',
916  'count',
917  'load'
918  ]
919  );
920 
921  $collectionItems = [$itemMock];
922 
923  $collectionMock->expects($this->once())
924  ->method('setOrderFilter')
925  ->with($this->order)
926  ->willReturnSelf();
927  $collectionMock->expects($this->once())
928  ->method('setOrder')
929  ->with('created_at', 'desc')
930  ->willReturn($dbMock);
931  $dbMock->expects($this->once())
932  ->method('setOrder')
933  ->with('entity_id', 'desc')
934  ->willReturn($collectionMock);
935  $collectionMock->expects($this->once())
936  ->method('getItems')
937  ->willReturn($collectionItems);
938 
939  $this->historyCollectionFactoryMock->expects($this->once())
940  ->method('create')
941  ->willReturn($collectionMock);
942 
943  for ($i = 10; --$i;) {
944  $this->assertEquals($collectionItems, $this->order->getStatusHistories());
945  }
946  }
947 
949  {
950  $incrementId = '000000001';
951  $storeId = '2';
952  $this->salesOrderCollectionFactoryMock
953  ->expects($this->once())
954  ->method('create')
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));
960  }
961 
962  public function testSetPaymentWithId()
963  {
964  $this->order->setId(123);
965  $payment = $this->getMockBuilder(\Magento\Sales\Model\Order\Payment::class)
966  ->disableOriginalConstructor()
967  ->getMock();
968  $this->order->setData(OrderInterface::PAYMENT, $payment);
969  $this->order->setDataChanges(false);
970 
971  $payment->expects($this->once())
972  ->method('setOrder')
973  ->with($this->order)
974  ->willReturnSelf();
975 
976  $payment->expects($this->once())
977  ->method('setParentId')
978  ->with(123)
979  ->willReturnSelf();
980 
981  $payment->expects($this->any())
982  ->method('getId')
983  ->willReturn(1);
984 
985  $this->order->setPayment($payment);
986 
987  $this->assertEquals(
988  $this->order->getData(
990  ),
991  $payment
992  );
993 
994  $this->assertFalse(
995  $this->order->hasDataChanges()
996  );
997  }
998 
999  public function testSetPaymentNoId()
1000  {
1001  $this->order->setId(123);
1002  $this->order->setDataChanges(false);
1003 
1004  $payment = $this->getMockBuilder(\Magento\Sales\Model\Order\Payment::class)
1005  ->disableOriginalConstructor()
1006  ->getMock();
1007 
1008  $payment->expects($this->once())
1009  ->method('setOrder')
1010  ->with($this->order)
1011  ->willReturnSelf();
1012 
1013  $payment->expects($this->once())
1014  ->method('setParentId')
1015  ->with(123)
1016  ->willReturnSelf();
1017 
1018  $payment->expects($this->any())
1019  ->method('getId')
1020  ->willReturn(null);
1021 
1022  $this->order->setPayment($payment);
1023 
1024  $this->assertEquals(
1025  $this->order->getData(
1027  ),
1028  $payment
1029  );
1030 
1031  $this->assertTrue(
1032  $this->order->hasDataChanges()
1033  );
1034  }
1035 
1036  public function testSetPaymentNull()
1037  {
1038  $this->assertEquals(null, $this->order->setPayment(null));
1039 
1040  $this->assertEquals(
1041  $this->order->getData(
1043  ),
1044  null
1045  );
1046 
1047  $this->assertTrue(
1048  $this->order->hasDataChanges()
1049  );
1050  }
1051 
1053  {
1054  $payment = $this->getMockBuilder(\Magento\Sales\Model\Order\Payment::class)
1055  ->disableOriginalConstructor()
1056  ->getMock();
1057  $this->order->setData(OrderInterface::PAYMENT, $payment);
1058  $this->order->reset();
1059  $this->assertEquals(
1060  $this->order->getData(
1062  ),
1063  null
1064  );
1065 
1066  $this->assertTrue(
1067  $this->order->hasDataChanges()
1068  );
1069  }
1070 
1072  {
1073  $localeCode = 'nl_NL';
1074 
1075  $this->localeResolver->expects($this->once())->method('getDefaultLocale')->willReturn($localeCode);
1076  $this->timezone->expects($this->once())->method('formatDateTime')
1077  ->with(
1078  $this->anything(),
1079  $this->anything(),
1080  $this->anything(),
1081  $localeCode
1082  );
1083 
1084  $this->order->getCreatedAtFormatted(\IntlDateFormatter::SHORT);
1085  }
1086 
1087  public function notInvoicingStatesProvider()
1088  {
1089  return [
1093  ];
1094  }
1095 
1097  {
1098  return [
1103  ];
1104  }
1105 }
testGetAllVisibleItems($isDeleted, $parentItemId, array $result)
Definition: OrderTest.php:221
$helper
Definition: iframe.phtml:13
$orderItem
Definition: order.php:30
return false
Definition: gallery.phtml:36
$payment
Definition: order.php:17
$value
Definition: gender.phtml:16
_prepareOrderPayment($order, $mockedMethods=[])
Definition: OrderTest.php:766
$status
Definition: order_status.php:8
testGetItemByQuoteItemId($gettingQuoteItemId, $quoteItemId, $result)
Definition: OrderTest.php:189
$method
Definition: info.phtml:13
$quoteItemId
Definition: cart.php:17
testCanCancelActionFlag($cancelActionFlag)
Definition: OrderTest.php:653
$i
Definition: gallery.phtml:31