protected function setUp() { $this->_objectManager = Bootstrap::getObjectManager(); $this->_customerAccountService = $this->_objectManager->create('Magento\\Customer\\Service\\V1\\CustomerAccountServiceInterface'); $this->_customerAddressService = $this->_objectManager->create('Magento\\Customer\\Service\\V1\\CustomerAddressServiceInterface'); $this->_addressBuilder = $this->_objectManager->create('Magento\\Customer\\Service\\V1\\Data\\AddressBuilder'); $this->_customerBuilder = $this->_objectManager->create('Magento\\Customer\\Service\\V1\\Data\\CustomerBuilder'); $this->_customerDetailsBuilder = $this->_objectManager->create('Magento\\Customer\\Service\\V1\\Data\\CustomerDetailsBuilder'); $regionBuilder = $this->_objectManager->create('Magento\\Customer\\Service\\V1\\Data\\RegionBuilder'); $this->_addressBuilder->setId(1)->setCountryId('US')->setCustomerId(1)->setDefaultBilling(true)->setDefaultShipping(true)->setPostcode('75477')->setRegion($regionBuilder->setRegionCode('AL')->setRegion('Alabama')->setRegionId(1)->create())->setStreet(['Green str, 67'])->setTelephone('3468676')->setCity('CityM')->setFirstname('John')->setLastname('Smith'); $address = $this->_addressBuilder->create(); $this->_addressBuilder->setId(2)->setCountryId('US')->setCustomerId(1)->setDefaultBilling(false)->setDefaultShipping(false)->setPostcode('47676')->setRegion($regionBuilder->setRegionCode('AL')->setRegion('Alabama')->setRegionId(1)->create())->setStreet(['Black str, 48'])->setCity('CityX')->setTelephone('3234676')->setFirstname('John')->setLastname('Smith'); $address2 = $this->_addressBuilder->create(); $this->_expectedAddresses = [$address, $address2]; }
protected function setUp() { $this->_objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); $this->_service = $this->_objectManager->create('Magento\\Customer\\Service\\V1\\CustomerAddressServiceInterface'); $this->_addressBuilder = $this->_objectManager->create('Magento\\Customer\\Service\\V1\\Data\\AddressBuilder'); $this->_customerBuilder = $this->_objectManager->create('Magento\\Customer\\Service\\V1\\Data\\CustomerBuilder'); $builder = $this->_objectManager->create('\\Magento\\Customer\\Service\\V1\\Data\\RegionBuilder'); $region = $builder->setRegionCode('AL')->setRegion('Alabama')->setRegionId(1)->create(); $this->_addressBuilder->setId(1)->setCountryId('US')->setCustomerId(1)->setDefaultBilling(true)->setDefaultShipping(true)->setPostcode('75477')->setRegion($region)->setStreet(array('Green str, 67'))->setTelephone('3468676')->setCity('CityM')->setFirstname('John')->setLastname('Smith')->setCompany('CompanyName'); $address = $this->_addressBuilder->create(); /* XXX: would it be better to have a clear method for this? */ $this->_addressBuilder->setId(2)->setCountryId('US')->setCustomerId(1)->setDefaultBilling(false)->setDefaultShipping(false)->setPostcode('47676')->setRegion($region)->setStreet(array('Black str, 48'))->setCity('CityX')->setTelephone('3234676')->setFirstname('John')->setLastname('Smith'); $address2 = $this->_addressBuilder->create(); $this->_expectedAddresses = array($address, $address2); }
/** * Make address Data Object out of an address model * * @param AbstractAddress $addressModel * @param int $defaultBillingId * @param int $defaultShippingId * @return Address */ public function createAddressFromModel(AbstractAddress $addressModel, $defaultBillingId, $defaultShippingId) { $addressId = $addressModel->getId(); $attributes = $this->_metadataService->getAllAddressAttributeMetadata(); $addressData = array(); foreach ($attributes as $attribute) { $code = $attribute->getAttributeCode(); if (!is_null($addressModel->getData($code))) { $addressData[$code] = $addressModel->getData($code); } } $isDefaultBilling = $addressModel->getData('is_default_billing') === null && intval($addressId) ? $addressId === $defaultBillingId : $addressModel->getData('is_default_billing'); $isDefaultShipping = $addressModel->getData('is_default_shipping') === null && intval($addressId) ? $addressId === $defaultShippingId : $addressModel->getData('is_default_shipping'); $this->_addressBuilder->populateWithArray(array_merge($addressData, array(Address::KEY_STREET => $addressModel->getStreet(), Address::KEY_DEFAULT_BILLING => $isDefaultBilling, Address::KEY_DEFAULT_SHIPPING => $isDefaultShipping, Address::KEY_REGION => array(Region::KEY_REGION => $addressModel->getRegion(), Region::KEY_REGION_ID => $addressModel->getRegionId(), Region::KEY_REGION_CODE => $addressModel->getRegionCode())))); if ($addressId) { $this->_addressBuilder->setId($addressId); } if ($addressModel->getCustomerId() || $addressModel->getParentId()) { $customerId = $addressModel->getCustomerId() ?: $addressModel->getParentId(); $this->_addressBuilder->setCustomerId($customerId); } $addressDataObject = $this->_addressBuilder->create(); return $addressDataObject; }
public function testSaveAddressesIdSetButNotAlreadyExisting() { // Setup Customer mock $this->_customerRegistryMock->expects($this->once())->method('retrieve')->with(1)->will($this->returnValue($this->_customerModelMock)); $this->_customerModelMock->expects($this->any())->method('load')->will($this->returnSelf()); $this->_customerModelMock->expects($this->any())->method('getId')->will($this->returnValue(1)); $this->_customerModelMock->expects($this->any())->method('getAddresses')->will($this->returnValue(array())); $this->_customerModelMock->expects($this->any())->method('getAddressItemById')->with(1)->will($this->returnValue(null)); // Setup address mock $mockAddress = $this->_createAddress(1, 'John'); $mockAddress->expects($this->once())->method('save'); $mockAddress->expects($this->any())->method('setData'); $this->_addressConverterMock->expects($this->once())->method('createAddressModel')->will($this->returnValue($mockAddress)); $customerService = $this->_createService(); $this->_addressBuilder->setId(1)->setFirstname('John')->setLastname(self::LASTNAME)->setRegion($this->objectManagerHelper->getObject('\\Magento\\Customer\\Service\\V1\\Data\\RegionBuilder')->setRegionId(self::REGION_ID)->setRegion(self::REGION)->create())->setStreet(array(self::STREET))->setTelephone(self::TELEPHONE)->setCity(self::CITY)->setCountryId(self::COUNTRY_ID)->setPostcode(self::POSTCODE); $ids = $customerService->saveAddresses(1, array($this->_addressBuilder->create())); $this->assertEquals(array(1), $ids); }