From c90adf1913e9e1868515ed36f159b803d36be9c1 Mon Sep 17 00:00:00 2001 From: Neur0toxine Date: Tue, 26 May 2020 12:25:53 +0300 Subject: [PATCH] Use state field in address * use state in address * make tests independent --- retailcrm/lib/RetailcrmAddressBuilder.php | 17 ++- tests/lib/RetailcrmAddressBuilderTest.php | 124 +++++++++++++++++++ tests/{ => lib}/RetailcrmCatalogTest.php | 0 tests/{ => lib}/RetailcrmHistoryTest.php | 0 tests/{ => lib}/RetailcrmInventoriesTest.php | 0 tests/{ => lib}/RetailcrmReferencesTest.php | 0 6 files changed, 138 insertions(+), 3 deletions(-) create mode 100644 tests/lib/RetailcrmAddressBuilderTest.php rename tests/{ => lib}/RetailcrmCatalogTest.php (100%) rename tests/{ => lib}/RetailcrmHistoryTest.php (100%) rename tests/{ => lib}/RetailcrmInventoriesTest.php (100%) rename tests/{ => lib}/RetailcrmReferencesTest.php (100%) diff --git a/retailcrm/lib/RetailcrmAddressBuilder.php b/retailcrm/lib/RetailcrmAddressBuilder.php index f1facb1..5a63f19 100644 --- a/retailcrm/lib/RetailcrmAddressBuilder.php +++ b/retailcrm/lib/RetailcrmAddressBuilder.php @@ -221,12 +221,23 @@ class RetailcrmAddressBuilder extends RetailcrmAbstractDataBuilder */ private function parseAddress() { - return array( + $state = null; + + if (!empty($this->address->id_state)) { + $stateName = State::getNameById($this->address->id_state); + + if (!empty($stateName)) { + $state = $stateName; + } + } + + return array_filter(array( 'index' => $this->address->postcode, 'city' => $this->address->city, 'countryIso' => Country::getIsoById($this->address->id_country), - 'text' => sprintf("%s %s", $this->address->address1, $this->address->address2) - ); + 'text' => sprintf("%s %s", $this->address->address1, $this->address->address2), + 'region' => $state + )); } /** diff --git a/tests/lib/RetailcrmAddressBuilderTest.php b/tests/lib/RetailcrmAddressBuilderTest.php new file mode 100644 index 0000000..5cdba27 --- /dev/null +++ b/tests/lib/RetailcrmAddressBuilderTest.php @@ -0,0 +1,124 @@ +defaultLang = (int) Configuration::get('PS_LANG_DEFAULT'); + $address = new Address(); + $address->id_state = State::getIdByName('Alabama'); + $address->address1 = 'address1'; + $address->address2 = 'address2'; + $address->alias = 'test_address'; + $address->city = 'Montgomery'; + $address->id_country = Country::getByIso('us'); + $address->country = Country::getNameById($this->defaultLang, $address->id_country); + $address->firstname = 'FirstName'; + $address->lastname = 'LastName'; + $address->phone = '123'; + $address->phone_mobile = '123'; + $address->postcode = '333000'; + $this->address = $address; + } + + public function testBuildRegular() + { + $builder = new RetailcrmAddressBuilder(); + $result = $builder + ->setMode(RetailcrmAddressBuilder::MODE_CUSTOMER) + ->setAddress($this->address) + ->setIsMain(true) + ->setWithExternalId(true) + ->setExternalIdSuffix('suffix') + ->build() + ->getDataArray(); + + $this->assertNotEmpty($result); + $this->assertArrayHasKey('address', $result); + $this->assertFalse(array_key_exists('externalId', $result['address'])); + $this->assertFalse(array_key_exists('isMain', $result)); + $this->assertFalse(array_key_exists('isMain', $result['address'])); + $this->assertArrayHasKey('phones', $result); + $this->assertNotEmpty($result['phones']); + $this->assertFieldsNotEmpty($result['address']); + } + + public function testBuildCorporate() + { + $builder = new RetailcrmAddressBuilder(); + $result = $builder + ->setMode(RetailcrmAddressBuilder::MODE_CORPORATE_CUSTOMER) + ->setAddress($this->address) + ->setIsMain(true) + ->setWithExternalId(true) + ->setExternalIdSuffix('suffix') + ->build() + ->getDataArray(); + + $this->assertNotEmpty($result); + $this->assertNotEmpty($result['externalId']); + $this->assertTrue($result['isMain']); + $this->assertFieldsNotEmpty($result); + } + + public function testBuildOrder() + { + $builder = new RetailcrmAddressBuilder(); + $result = $builder + ->setMode(RetailcrmAddressBuilder::MODE_ORDER_DELIVERY) + ->setAddress($this->address) + ->setIsMain(true) + ->setWithExternalId(true) + ->setExternalIdSuffix('suffix') + ->build() + ->getDataArray(); + + $this->assertNotEmpty($result); + $this->assertArrayHasKey('delivery', $result); + $this->assertArrayHasKey('address', $result['delivery']); + $this->assertFalse(array_key_exists('externalId', $result['delivery']['address'])); + $this->assertFalse(array_key_exists('isMain', $result)); + $this->assertFalse(array_key_exists('isMain', $result['delivery']['address'])); + $this->assertArrayHasKey('countryIso', $result); + $this->assertNotEmpty($result['countryIso']); + $this->assertArrayHasKey('phone', $result); + $this->assertArrayHasKey('additionalPhone', $result); + $this->assertNotEmpty($result['phone']); + $this->assertNotEmpty($result['additionalPhone']); + $this->assertFieldsNotEmpty($result['delivery']['address'], array('countryIso')); + } + + /** + * Asserts address fields + * + * @param array $address + */ + private function assertFieldsNotEmpty($address, $skip = array()) + { + foreach (array_diff($this->getCheckableFields(), $skip) as $field) { + $this->assertArrayHasKey($field, $address); + $this->assertNotEmpty($address[$field]); + } + } + + /** + * Returns address fields names + * + * @return string[] + */ + private function getCheckableFields() + { + return array('index', 'city', 'countryIso', 'text', 'region'); + } +} diff --git a/tests/RetailcrmCatalogTest.php b/tests/lib/RetailcrmCatalogTest.php similarity index 100% rename from tests/RetailcrmCatalogTest.php rename to tests/lib/RetailcrmCatalogTest.php diff --git a/tests/RetailcrmHistoryTest.php b/tests/lib/RetailcrmHistoryTest.php similarity index 100% rename from tests/RetailcrmHistoryTest.php rename to tests/lib/RetailcrmHistoryTest.php diff --git a/tests/RetailcrmInventoriesTest.php b/tests/lib/RetailcrmInventoriesTest.php similarity index 100% rename from tests/RetailcrmInventoriesTest.php rename to tests/lib/RetailcrmInventoriesTest.php diff --git a/tests/RetailcrmReferencesTest.php b/tests/lib/RetailcrmReferencesTest.php similarity index 100% rename from tests/RetailcrmReferencesTest.php rename to tests/lib/RetailcrmReferencesTest.php