public function testUpdateAddressModelWithAttributes() { $addressModelMock = $this->getAddressModelMock(); $addressModelMock->expects($this->once())->method('getAttributeSetId')->will($this->returnValue(true)); $addressModelMock->expects($this->never())->method('setAttributeSetId'); $attributes = array('custom_attributes' => array(array(AttributeValue::ATTRIBUTE_CODE => 'code_01', AttributeValue::VALUE => 'value_01'), array(AttributeValue::ATTRIBUTE_CODE => 'code_02', AttributeValue::VALUE => 'value_02'), array(AttributeValue::ATTRIBUTE_CODE => 'code_03', AttributeValue::VALUE => 'value_03')), 'attributes_01' => array('some_value_01', 'some_value_02', 'some_value_03'), 'attributes_02' => 'some_value_04', \Magento\Customer\Service\V1\Data\Address::KEY_REGION => 'some_region'); $regionMock = $this->getMock('Magento\\Customer\\Service\\V1\\Data\\Region', array('getRegion', 'getRegionCode', 'getRegionId'), array(), '', false); $regionMock->expects($this->once())->method('getRegion'); $regionMock->expects($this->once())->method('getRegionCode'); $regionMock->expects($this->once())->method('getRegionId'); $addressMock = $this->getMock('Magento\\Customer\\Service\\V1\\Data\\Address', array(), array(), '', false); $addressMock->expects($this->once())->method('__toArray')->will($this->returnValue($attributes)); $addressMock->expects($this->exactly(4))->method('getRegion')->will($this->returnValue($regionMock)); $this->model->updateAddressModel($addressModelMock, $addressMock); }
/** * {@inheritdoc} */ public function saveAddresses($customerId, $addresses) { $customerModel = $this->customerRegistry->retrieve($customerId); $addressModels = []; $inputException = new InputException(); for ($i = 0; $i < count($addresses); $i++) { $address = $addresses[$i]; $addressModel = null; if ($address->getId()) { $addressModel = $customerModel->getAddressItemById($address->getId()); } if (is_null($addressModel)) { $addressModel = $this->addressConverter->createAddressModel($address); $addressModel->setCustomer($customerModel); } else { $this->addressConverter->updateAddressModel($addressModel, $address); } $inputException = $this->_validate($addressModel, $inputException, $i); $addressModels[] = $addressModel; } $this->customerRegistry->remove($customerId); if ($inputException->wasErrorAdded()) { throw $inputException; } $addressIds = array(); /** @var \Magento\Customer\Model\Address $addressModel */ foreach ($addressModels as $addressModel) { $addressModel->save(); $this->addressRegistry->remove($addressModel->getId()); $addressIds[] = $addressModel->getId(); } return $addressIds; }