/** * @dataProvider sourceDataProvider * * @param mixed $source * @param bool $foundInDatabase * @param array $expectedResult * @param string $exception */ public function testToMagentoData($source, $foundInDatabase, $expectedResult, $exception = null) { if ($exception) { $this->setExpectedException($exception); } if ($foundInDatabase === true) { $region = new Region(); $region->setRegionId(self::TEST_MAGENTO_REGION_ID); $this->repository->expects($this->once())->method('findOneBy')->will($this->returnValue($region)); } elseif ($foundInDatabase === false) { $this->repository->expects($this->once())->method('findOneBy')->will($this->returnValue(null)); } // more than one call should not provoke expectation errors $this->assertSame($expectedResult, $this->converter->toMagentoData($source)); $this->assertSame($expectedResult, $this->converter->toMagentoData($source)); }
public function testUpdateAddress() { $transportSetting = $this->getMock('Oro\\Bundle\\IntegrationBundle\\Entity\\Transport'); $channel = new Channel(); $channel->setTransport($transportSetting); $channel->getSynchronizationSettingsReference()->offsetSet('syncPriority', TwoWaySyncConnectorInterface::LOCAL_WINS); $customer = new Customer(); $customer->setOriginId(self::TEST_CUSTOMER_ID); $customer->setChannel($channel); $customer->setFirstName(self::TEST_CUSTOMER_FIRSTNAME); $customer->setLastName(self::TEST_CUSTOMER_LASTNAME); $contactAddress = new ContactAddress(); $address = new Address(); $address->setFirstName(self::TEST_FIRSTNAME); $address->setCountry(new Country(self::TEST_ADDRESS_COUNTRY)); $address->setRegionText(self::TEST_ADDRESS_REGION); $address->setStreet(self::TEST_ADDRESS_STREET); $address->setContactAddress($contactAddress); $address->setOriginId(1); $this->transport->expects($this->once())->method('init'); $this->regionConverter->expects($this->once())->method('toMagentoData')->with($this->identicalTo($address))->will($this->returnValue(['region' => self::TEST_ADDRESS_REGION_RESOLVED, 'region_id' => null])); $this->transport->expects($this->at(2))->method('getCustomerAddresses')->with($this->identicalTo($customer))->will($this->returnValue([(object) ['customer_address_id' => 1, 'telephone' => '911', 'middlename' => 'testMiddleName', 'suffix' => 'testSuffix', 'company' => 'testCompany', 'city' => 'testCity', 'is_default_shipping' => false, 'is_default_billing' => false]])); $this->transport->expects($this->at(3))->method('call')->with($this->equalTo(SoapTransport::ACTION_CUSTOMER_ADDRESS_UPDATE), $this->equalTo(['addressId' => 1, 'addressData' => ['prefix' => null, 'firstname' => 'newName', 'middlename' => null, 'lastname' => 'newLastName', 'suffix' => null, 'company' => null, 'street' => [0 => self::TEST_ADDRESS_STREET, 1 => null], 'city' => null, 'postcode' => null, 'country_id' => self::TEST_ADDRESS_COUNTRY, 'region' => self::TEST_ADDRESS_REGION_RESOLVED, 'region_id' => null, 'created_at' => null, 'updated_at' => null, 'is_default_billing' => false, 'is_default_shipping' => false, 'telephone' => 'no phone']]))->will($this->returnValue(true)); $this->em->expects($this->atLeastOnce())->method('persist'); $this->em->expects($this->once())->method('flush'); $data = []; array_push($data, (object) ['entity' => $customer, 'object' => ['addresses' => [['entity' => $address, 'status' => AbstractReverseProcessor::UPDATE_ENTITY, 'object' => ['firstname' => 'newName', 'lastname' => 'newLastName']]]]]); $this->writer->write($data); }
/** * Process address write to remote instance and to DB * * @param array $addresses * @param bool $isRemoteWins * @param Customer $customer * * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @throws \LogicException */ protected function processAddresses($addresses, $isRemoteWins, Customer $customer) { $remoteAddresses = $this->transport->getCustomerAddresses($customer); $remoteTypesWin = $this->isRemoteAddressesTypesChanged($addresses, $remoteAddresses); foreach ($addresses as $address) { if (empty($address['status']) || empty($address['entity'])) { throw new \LogicException('Unable to process entity modification'); } /** @var ContactAddress|Address $addressEntity */ $addressEntity = $address['entity']; $status = $address['status']; if ($status === AbstractReverseProcessor::UPDATE_ENTITY) { $localChanges = $address['object']; $this->setDefaultData($localChanges, ['firstName' => $customer->getFirstName(), 'lastName' => $customer->getLastName()]); if ($isRemoteWins) { $remoteAddress = $this->getRemoteAddressByOriginId($remoteAddresses, $addressEntity->getOriginId()); if (!$remoteAddress) { continue; } $remoteData = $this->customerSerializer->compareAddresses((array) $remoteAddress, $addressEntity, $remoteTypesWin); $remotePhoneData = $this->customerSerializer->comparePhones((array) $remoteAddress, $addressEntity); $remoteData = array_merge($remoteData, $remotePhoneData); // if on remote side was not changed address types - save local types if (!$remoteTypesWin) { $localChanges = array_merge($localChanges, ['types' => $addressEntity->getContactAddress()->getTypes()]); } $this->setLocalDataChanges($addressEntity, $localChanges); $this->setRemoteDataChanges($addressEntity, $remoteData); } else { $localChanges = array_merge($localChanges, ['types' => $addressEntity->getContactAddress()->getTypes()]); $this->setChangedData($addressEntity, $localChanges); } $dataForSend = array_merge($this->customerSerializer->convertToMagentoAddress($addressEntity), $this->regionConverter->toMagentoData($addressEntity), ['telephone' => $addressEntity->getPhone() ? $addressEntity->getPhone() : 'no phone']); $requestData = ['addressId' => $addressEntity->getOriginId(), 'addressData' => $dataForSend]; try { $this->transport->call(SoapTransport::ACTION_CUSTOMER_ADDRESS_UPDATE, $requestData); $this->em->persist($addressEntity); } catch (\Exception $e) { } } elseif ($status === AbstractReverseProcessor::NEW_ENTITY) { try { $addressData = array_merge(['telephone' => 'no phone'], $this->customerSerializer->convertToMagentoAddress($addressEntity, ['firstname' => $customer->getFirstName(), 'lastname' => $customer->getLastName()]), $this->regionConverter->toMagentoData($addressEntity)); $requestData = ['customerId' => $address['magentoId'], 'addressData' => $addressData]; $result = $this->transport->call(SoapTransport::ACTION_CUSTOMER_ADDRESS_CREATE, $requestData); if ($result) { $newAddress = $this->customerSerializer->convertMageAddressToAddress($addressData, $addressEntity, $result); $newAddress->setOwner($customer); $customer->addAddress($newAddress); $this->em->persist($customer); } } catch (\Exception $e) { } } elseif ($status === AbstractReverseProcessor::DELETE_ENTITY) { try { $shouldBeRemoved = $this->transport->call(SoapTransport::ACTION_CUSTOMER_ADDRESS_DELETE, ['addressId' => $addressEntity->getOriginId()]); } catch (\Exception $e) { // remove from local customer if it's already removed on remote side $errorCode = $this->transport->getErrorCode($e); $shouldBeRemoved = $errorCode === MagentoTransportInterface::TRANSPORT_ERROR_ADDRESS_DOES_NOT_EXIST; } if ($shouldBeRemoved) { $this->em->remove($address['entity']); } } } }