/** * Process addresses saving * * @param Order $order * @return $this * @throws \Exception */ public function process(Order $order) { if (null !== $order->getAddresses()) { /** @var \Magento\Sales\Model\Order\Address $address */ foreach ($order->getAddresses() as $address) { $address->setParentId($order->getId()); $address->setOrder($order); $address->save(); } $billingAddress = $order->getBillingAddress(); $attributesForSave = []; if ($billingAddress && $order->getBillingAddressId() != $billingAddress->getId()) { $order->setBillingAddressId($billingAddress->getId()); $attributesForSave[] = 'billing_address_id'; } $shippingAddress = $order->getShippingAddress(); if ($shippingAddress && $order->getShippigAddressId() != $shippingAddress->getId()) { $order->setShippingAddressId($shippingAddress->getId()); $attributesForSave[] = 'shipping_address_id'; } if (!empty($attributesForSave)) { $this->attribute->saveAttribute($order, $attributesForSave); } } return $this; }
/** * Test process method with shipping_address */ public function testProcessShippingAddress() { $this->orderMock->expects($this->exactly(2))->method('getAddresses')->willReturn([$this->addressMock]); $this->addressMock->expects($this->once())->method('save')->will($this->returnSelf()); $this->orderMock->expects($this->once())->method('getBillingAddress')->will($this->returnValue(null)); $this->orderMock->expects($this->once())->method('getShippingAddress')->will($this->returnValue($this->addressMock)); $this->addressMock->expects($this->exactly(2))->method('getId')->will($this->returnValue(2)); $this->orderMock->expects($this->once())->method('setShippingAddressId')->will($this->returnSelf()); $this->attributeMock->expects($this->once())->method('saveAttribute')->with($this->orderMock, ['shipping_address_id'])->will($this->returnSelf()); $this->assertEquals($this->address, $this->address->process($this->orderMock)); }
/** * @expectedException \Exception * @expectedExceptionMessage Expected Exception * @throws \Exception */ public function testSaveFailed() { $this->modelMock->expects($this->any())->method('getEventPrefix')->will($this->returnValue('event_prefix')); $this->modelMock->expects($this->any())->method('getEventObject')->will($this->returnValue('event_object')); $this->appResourceMock->expects($this->once())->method('getConnection')->will($this->returnValue($this->connectionMock)); $exception = new \Exception('Expected Exception'); $this->modelMock->expects($this->any())->method('getId')->will($this->throwException($exception)); $this->connectionMock->expects($this->once())->method('beginTransaction'); $this->connectionMock->expects($this->once())->method('rollback'); $this->eventManagerMock->expects($this->once())->method('dispatch')->with('event_prefix_save_attribute_before', ['event_object' => $this->attribute, 'object' => $this->modelMock, 'attribute' => ['attribute']]); $this->attribute->saveAttribute($this->modelMock, 'attribute'); }
/** * Perform actions after object save * * @param \Magento\Framework\Model\AbstractModel $object * @param string $attribute * @return $this * @throws \Exception */ public function saveAttribute(\Magento\Framework\Model\AbstractModel $object, $attribute) { $this->attribute->saveAttribute($object, $attribute); return $this; }