protected function setUp() { $helper = $this->getMockBuilder('Mage_Customer_Helper_Data')->getMock(); $helper->expects($this->any())->method('__')->will($this->returnArgument(0)); $this->_customerFactory = $this->getMockBuilder('Mage_Customer_Model_Customer_Factory')->disableOriginalConstructor()->setMethods(array('create'))->getMock(); $this->_addressFactory = $this->getMockBuilder('Mage_Customer_Model_Address_Factory')->disableOriginalConstructor()->setMethods(array('create'))->getMock(); $this->_customer = $this->getMockBuilder('Mage_Customer_Model_Customer')->setMethods(array('save', 'generatePassword', 'getOrigData', 'sendNewAccountEmail', 'getConfirmation', 'getPrimaryAddress', 'getAddresses', 'getAdditionalAddresses', 'load', 'getId', 'changePassword', 'sendPasswordReminderEmail', 'addAddress', 'getAddressItemById', 'getAddressesCollection'))->disableOriginalConstructor()->getMock(); $this->_customerFactory->expects($this->any())->method('create')->will($this->returnValue($this->_customer)); $this->_address = $this->_createAddress(true, null); $this->_addressFactory->expects($this->any())->method('create')->will($this->returnValue($this->_address)); $this->_service = new Mage_Customer_Service_Customer($helper, $this->_customerFactory, $this->_addressFactory); }
/** * Save customer addresses. * * @param Mage_Customer_Model_Customer $customer * @param array $addressesData * @throws Mage_Core_Exception */ protected function _prepareCustomerAddressesForSave($customer, array $addressesData) { $hasChanges = $customer->hasDataChanges(); $actualAddressesIds = array(); foreach ($addressesData as $addressData) { $addressId = null; if (array_key_exists('entity_id', $addressData)) { $addressId = $addressData['entity_id']; unset($addressData['entity_id']); } if (null !== $addressId) { $address = $customer->getAddressItemById($addressId); if (!$address || !$address->getId()) { throw new Mage_Core_Exception($this->_translateHelper->__('The address with the specified ID not found.')); } } else { $address = $this->_addressFactory->create(); $address->setCustomerId($customer->getId()); // Add customer address into addresses collection $customer->addAddress($address); } $address->addData($addressData); $hasChanges = $hasChanges || $address->hasDataChanges(); // Set post_index for detect default billing and shipping addresses $address->setPostIndex($addressId); $actualAddressesIds[] = $address->getId(); } /** @var Mage_Customer_Model_Address $address */ foreach ($customer->getAddressesCollection() as $address) { if (!in_array($address->getId(), $actualAddressesIds)) { $address->setData('_deleted', true); $hasChanges = true; } } $customer->setDataChanges($hasChanges); }