/** * Save relations for Order * * @param \Magento\Framework\Model\AbstractModel $object * @return void * @throws \Exception */ public function processRelation(\Magento\Framework\Model\AbstractModel $object) { /** @var \Magento\Sales\Model\Order $object */ if (null !== $object->getItems()) { /** @var \Magento\Sales\Model\Order\Item $item */ foreach ($object->getItems() as $item) { $item->setOrderId($object->getId()); $item->setOrder($object); $this->orderItemRepository->save($item); } } if (null !== $object->getPayment()) { $payment = $object->getPayment(); $payment->setParentId($object->getId()); $payment->setOrder($object); $this->orderPaymentResource->save($payment); } if (null !== $object->getStatusHistories()) { /** @var \Magento\Sales\Model\Order\Status\History $statusHistory */ foreach ($object->getStatusHistories() as $statusHistory) { $statusHistory->setParentId($object->getId()); $statusHistory->setOrder($object); $this->orderStatusHistoryResource->save($statusHistory); } } if (null !== $object->getRelatedObjects()) { foreach ($object->getRelatedObjects() as $relatedObject) { $relatedObject->setOrder($object); $relatedObject->save(); } } $this->addressHandler->removeEmptyAddresses($object); $this->addressHandler->process($object); }
/** * test _beforeSaveMethod via save() * @expectedException \Magento\Framework\Exception\LocalizedException * @expectedExceptionMessage Cannot save comment: */ public function testValidate() { $historyMock = $this->getMock('Magento\\Sales\\Model\\Order\\Status\\History', [], [], '', false); $this->entitySnapshotMock->expects($this->once())->method('isModified')->with($historyMock)->willReturn(true); $historyMock->expects($this->any())->method('isSaveAllowed')->will($this->returnValue(true)); $this->validatorMock->expects($this->once())->method('validate')->with($historyMock)->will($this->returnValue(['Some warnings'])); $this->assertEquals($this->historyResource, $this->historyResource->save($historyMock)); }
public function testProcessRelation() { $this->addressHandlerMock->expects($this->once())->method('removeEmptyAddresses')->with($this->orderMock)->willReturnSelf(); $this->addressHandlerMock->expects($this->once())->method('process')->with($this->orderMock)->willReturnSelf(); $this->orderMock->expects($this->exactly(2))->method('getItems')->willReturn([$this->orderItemMock]); $this->orderMock->expects($this->exactly(3))->method('getId')->willReturn('order-id-value'); $this->orderItemMock->expects($this->once())->method('setOrderId')->with('order-id-value')->willReturnSelf(); $this->orderItemMock->expects($this->once())->method('setOrder')->with($this->orderMock)->willReturnSelf(); $this->orderItemRepositoryMock->expects($this->once())->method('save')->with($this->orderItemMock)->willReturnSelf(); $this->orderMock->expects($this->exactly(2))->method('getPayment')->willReturn($this->orderPaymentMock); $this->orderPaymentMock->expects($this->once())->method('setParentId')->with('order-id-value')->willReturnSelf(); $this->orderPaymentMock->expects($this->once())->method('setOrder')->with($this->orderMock)->willReturnSelf(); $this->orderPaymentResourceMock->expects($this->once())->method('save')->with($this->orderPaymentMock)->willReturnSelf(); $this->orderMock->expects($this->exactly(2))->method('getStatusHistories')->willReturn([$this->orderStatusHistoryMock]); $this->orderStatusHistoryMock->expects($this->once())->method('setParentId')->with('order-id-value')->willReturnSelf(); $this->orderStatusHistoryMock->expects($this->once())->method('setOrder')->with($this->orderMock)->willReturnSelf(); $this->statusHistoryResource->expects($this->once())->method('save')->with($this->orderStatusHistoryMock)->willReturnSelf(); $this->orderMock->expects($this->exactly(2))->method('getRelatedObjects')->willReturn([$this->orderInvoiceMock]); $this->orderInvoiceMock->expects($this->once())->method('setOrder')->with($this->orderMock)->willReturnSelf(); $this->orderInvoiceMock->expects($this->once())->method('save')->willReturnSelf(); $this->relationProcessor->processRelation($this->orderMock); }