Exemple #1
0
 /**
  * @return array
  */
 public function sourceDataProvider()
 {
     $address1 = new Address();
     $address2 = new Address();
     $badSource = null;
     $region1 = new BAPRegion(self::TEST_REGION_COMBINED_CODE);
     $region1->setName(self::TEST_REGION_NAME);
     $address1->setRegion($region1);
     $address2->setRegionText(self::TEST_REGION_NAME);
     return ['take region from address, found in magento DB, use region ID' => [$address1, true, ['region' => null, 'region_id' => self::TEST_MAGENTO_REGION_ID]], 'take region from address, not found in magento DB, use region name' => [$address1, false, ['region' => self::TEST_REGION_NAME, 'region_id' => null]], 'region not set, use region text' => [$address2, null, ['region' => self::TEST_REGION_NAME, 'region_id' => null]], 'bad type given' => [$badSource, null, null, '\\InvalidArgumentException']];
 }
Exemple #2
0
 /**
  * @param string $key
  * @param Region $entity
  */
 public function fillEntityData($key, $entity)
 {
     $countryRepo = $this->templateManager->getEntityRepository('Oro\\Bundle\\AddressBundle\\Entity\\Country');
     switch ($key) {
         case 'NY':
             $entity->setCode($key);
             $country = $countryRepo->getEntity('US');
             $country->addRegion($entity);
             return;
     }
     parent::fillEntityData($key, $entity);
 }
Exemple #3
0
 /**
  * {@inheritdoc}
  */
 public function denormalize($data, $class, $format = null, array $context = [])
 {
     if (empty($data)) {
         return false;
     }
     /** @var Region $resultObject */
     $resultObject = new $class();
     if (isset($data['region_id'])) {
         $resultObject->setRegionId($data['region_id']);
     }
     if (isset($data['code'])) {
         $code = $data['code'];
         $resultObject->setCode($code);
         // Some magento region codes are already combined
         $countryCode = $data['countryCode'];
         if (strpos($code, $countryCode . BAPRegion::SEPARATOR) === 0) {
             $combinedCode = $code;
         } else {
             $combinedCode = BAPRegion::getRegionCombinedCode($countryCode, $code);
         }
         $resultObject->setCombinedCode($combinedCode);
         $resultObject->setCountryCode($countryCode);
     }
     // magento can bring empty name, region will be skipped in strategy
     if (isset($data['name'])) {
         $resultObject->setName($data['name']);
     }
     return $resultObject;
 }
Exemple #4
0
 /**
  * @param AbstractAddress|BAPRegion $source
  *
  * @return array magento correspondent fields data
  * @throws \InvalidArgumentException
  */
 public function toMagentoData($source)
 {
     $data = ['region' => null, 'region_id' => null];
     if (!$source instanceof AbstractAddress && !$source instanceof BAPRegion) {
         throw new \InvalidArgumentException('Source should be instance of AbstractAddress or Region');
     }
     if ($source instanceof AbstractAddress && !$source->getRegion()) {
         $data['region'] = $source->getRegionText();
     } elseif ($source instanceof AbstractAddress) {
         $source = $source->getRegion();
     }
     if ($source instanceof BAPRegion) {
         $magentoRegion = $this->tryGetMRByCode($source->getCombinedCode());
         if (!$magentoRegion) {
             $data['region'] = $source->getName();
         } else {
             $data['region_id'] = $magentoRegion->getRegionId();
         }
     }
     return $data;
 }
 public function setUp()
 {
     $this->channel = $this->getMock('Oro\\Bundle\\IntegrationBundle\\Entity\\Channel');
     $this->customer = $this->getMock('OroCRM\\Bundle\\MagentoBundle\\Entity\\Customer');
     $this->contact = $this->getMock('OroCRM\\Bundle\\ContactBundle\\Entity\\Contact');
     $this->address = $this->getMock('OroCRM\\Bundle\\MagentoBundle\\Entity\\Address');
     $this->contactAddress = $this->getMock('OroCRM\\Bundle\\ContactBundle\\Entity\\ContactAddress');
     $this->contactPhone = $this->getMock('OroCRM\\Bundle\\ContactBundle\\Entity\\ContactPhone');
     $this->country = $this->getMockBuilder('Oro\\Bundle\\AddressBundle\\Entity\\Country')->disableOriginalConstructor()->getMock();
     $this->region = $this->getMockBuilder('Oro\\Bundle\\AddressBundle\\Entity\\Region')->disableOriginalConstructor()->getMock();
     $collection = $this->getMock('Doctrine\\Common\\Collections\\Collection');
     $collection->expects($this->any())->method('getValues')->will($this->returnValue([$this->address]));
     $this->country->expects($this->any())->method('getIso2Code')->will($this->returnValue('US'));
     $this->region->expects($this->any())->method('getCombinedCode')->will($this->returnValue('US-US'));
     $this->contactAddress->expects($this->any())->method('getCountry')->will($this->returnValue($this->country));
     $this->contactAddress->expects($this->any())->method('getRegion')->will($this->returnValue($this->region));
     $this->customer->expects($this->any())->method('getContact')->will($this->returnValue($this->contact));
     $this->customer->expects($this->any())->method('getChannel')->will($this->returnValue($this->channel));
     $this->address->expects($this->any())->method('getContactAddress')->will($this->returnValue($this->contactAddress));
     $this->address->expects($this->any())->method('getContactPhone')->will($this->returnValue($this->contactPhone));
     $this->address->expects($this->any())->method('getCountry')->will($this->returnValue($this->country));
     $this->address->expects($this->any())->method('getRegion')->will($this->returnValue($this->region));
     $this->customer->expects($this->any())->method('getAddresses')->will($this->returnValue($collection));
 }
Exemple #6
0
 /**
  * @param array $regionData
  *
  * @return null|Region
  */
 protected function getRegion(array $regionData)
 {
     if (strpos($regionData['code'], $regionData['country_id'] . BAPRegion::SEPARATOR) === 0) {
         $combinedCode = $regionData['code'];
     } else {
         $combinedCode = BAPRegion::getRegionCombinedCode($regionData['country_id'], $regionData['code']);
     }
     /** @var $region Region */
     $region = $this->regionRepository->findOneBy(array('combinedCode' => $combinedCode));
     if (!$region) {
         $region = new Region($combinedCode);
         $region->setCode($regionData['code'])->setRegionId($regionData['region_id'])->setCombinedCode($combinedCode)->setCountryCode($regionData['country_id']);
     }
     $region->setName($regionData['default_name']);
     return $region;
 }
 /**
  * @param string $locale
  * @param Country $country
  * @param array $regionData
  * @return null|Region
  */
 protected function getRegion($locale, Country $country, array $regionData)
 {
     if (empty($regionData['combinedCode']) || empty($regionData['code'])) {
         return null;
     }
     /** @var $region Region */
     $region = $this->regionRepository->findOneBy(array('combinedCode' => $regionData['combinedCode']));
     if (!$region) {
         $region = new Region($regionData['combinedCode']);
         $region->setCode($regionData['code'])->setCountry($country);
     }
     $regionName = $this->translate($regionData['combinedCode'], static::REGION_PREFIX, $locale);
     $region->setLocale($locale)->setName($regionName);
     return $region;
 }
 /**
  * @param string $code
  * @param Country $country
  * @return Region
  */
 protected function createRegion($code, Country $country)
 {
     $result = new Region($country->getIso2Code() . '.' . $code);
     $result->setCode($code);
     return $result;
 }
Exemple #9
0
 /**
  * @param string $combinedCode
  * @param string $countryCode
  * @param string $code
  *
  * @return BAPRegion
  */
 public function loadRegionByCode($combinedCode, $countryCode, $code)
 {
     $regionClass = 'Oro\\Bundle\\AddressBundle\\Entity\\Region';
     $countryClass = 'Oro\\Bundle\\AddressBundle\\Entity\\Country';
     // Simply search region by combinedCode
     $region = $this->doctrineHelper->getEntityByCriteria(['combinedCode' => $combinedCode], $regionClass);
     if (!$region) {
         // Some region codes in magento are filled by region names
         $em = $this->doctrineHelper->getEntityManager($countryClass);
         $country = $em->getReference($countryClass, $countryCode);
         $region = $this->doctrineHelper->getEntityByCriteria(['country' => $country, 'name' => $combinedCode], $regionClass);
     }
     if (!$region) {
         // Some numeric regions codes may be padded by 0 in ISO format and not padded in magento
         // As example FR-1 in magento and FR-01 in ISO
         $region = $this->doctrineHelper->getEntityByCriteria(['combinedCode' => BAPRegion::getRegionCombinedCode($countryCode, str_pad($code, 2, '0', STR_PAD_LEFT))], $regionClass);
     }
     return $region;
 }
Exemple #10
0
 public function testToString()
 {
     $obj = new Region('combinedCode');
     $obj->setName('name');
     $this->assertEquals('name', $obj->__toString());
 }