/** * @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)); }
/** * 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']); } } } }