2020-05-26 12:25:53 +03:00
|
|
|
<?php
|
2021-12-06 14:37:43 +03:00
|
|
|
/**
|
|
|
|
* MIT License
|
|
|
|
*
|
|
|
|
* Copyright (c) 2021 DIGITAL RETAIL TECHNOLOGIES SL
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
* SOFTWARE.
|
|
|
|
*
|
|
|
|
* DISCLAIMER
|
|
|
|
*
|
|
|
|
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
|
|
|
|
* versions in the future. If you wish to customize PrestaShop for your
|
|
|
|
* needs please refer to http://www.prestashop.com for more information.
|
|
|
|
*
|
|
|
|
* @author DIGITAL RETAIL TECHNOLOGIES SL <mail@simlachat.com>
|
|
|
|
* @copyright 2021 DIGITAL RETAIL TECHNOLOGIES SL
|
|
|
|
* @license https://opensource.org/licenses/MIT The MIT License
|
|
|
|
*
|
|
|
|
* Don't forget to prefix your containers with your own identifier
|
|
|
|
* to avoid any conflicts with others containers.
|
|
|
|
*/
|
2020-05-26 12:25:53 +03:00
|
|
|
|
|
|
|
class RetailcrmAddressBuilderTest extends RetailcrmTestCase
|
|
|
|
{
|
2021-11-03 16:19:39 +07:00
|
|
|
/** @var \AddressCore|Address */
|
2020-05-26 12:25:53 +03:00
|
|
|
protected $address;
|
|
|
|
|
2021-11-03 16:19:39 +07:00
|
|
|
/** @var int */
|
2020-05-26 12:25:53 +03:00
|
|
|
protected $defaultLang;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* setUp test
|
|
|
|
*/
|
2021-11-03 16:19:39 +07:00
|
|
|
protected function setUp()
|
2020-05-26 12:25:53 +03:00
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->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()
|
2021-11-03 16:19:39 +07:00
|
|
|
->getDataArray()
|
|
|
|
;
|
2020-05-26 12:25:53 +03:00
|
|
|
|
|
|
|
$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()
|
2021-11-03 16:19:39 +07:00
|
|
|
->getDataArray()
|
|
|
|
;
|
2020-05-26 12:25:53 +03:00
|
|
|
|
|
|
|
$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()
|
2021-11-03 16:19:39 +07:00
|
|
|
->getDataArray()
|
|
|
|
;
|
2020-05-26 12:25:53 +03:00
|
|
|
|
|
|
|
$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']);
|
2021-11-03 16:19:39 +07:00
|
|
|
$this->assertFieldsNotEmpty($result['delivery']['address'], ['countryIso']);
|
2020-05-26 12:25:53 +03:00
|
|
|
}
|
|
|
|
|
2021-07-07 13:14:06 +03:00
|
|
|
/**
|
|
|
|
* @dataProvider getAddressLines
|
|
|
|
*/
|
|
|
|
public function testAddressLineConcatenation($addressLine1, $addressLine2, $addressText)
|
|
|
|
{
|
|
|
|
$address = $this->address;
|
|
|
|
$address->address1 = $addressLine1;
|
|
|
|
$address->address2 = $addressLine2;
|
|
|
|
|
|
|
|
$builder = new RetailcrmAddressBuilder();
|
|
|
|
$result = $builder
|
|
|
|
->setMode(RetailcrmAddressBuilder::MODE_ORDER_DELIVERY)
|
|
|
|
->setAddress($this->address)
|
|
|
|
->setIsMain(true)
|
|
|
|
->setWithExternalId(true)
|
|
|
|
->setExternalIdSuffix('suffix')
|
|
|
|
->build()
|
2021-11-03 16:19:39 +07:00
|
|
|
->getDataArray()
|
|
|
|
;
|
2021-07-07 13:14:06 +03:00
|
|
|
|
|
|
|
$this->assertNotEmpty($result);
|
|
|
|
$this->assertArrayHasKey('delivery', $result);
|
|
|
|
$this->assertArrayHasKey('address', $result['delivery']);
|
|
|
|
$this->assertArrayHasKey('text', $result['delivery']['address']);
|
|
|
|
$this->assertEquals($result['delivery']['address']['text'], $addressText);
|
|
|
|
}
|
|
|
|
|
2020-05-26 12:25:53 +03:00
|
|
|
/**
|
|
|
|
* Asserts address fields
|
|
|
|
*
|
|
|
|
* @param array $address
|
|
|
|
*/
|
2021-11-03 16:19:39 +07:00
|
|
|
private function assertFieldsNotEmpty($address, $skip = [])
|
2020-05-26 12:25:53 +03:00
|
|
|
{
|
|
|
|
foreach (array_diff($this->getCheckableFields(), $skip) as $field) {
|
|
|
|
$this->assertArrayHasKey($field, $address);
|
|
|
|
$this->assertNotEmpty($address[$field]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-07 13:14:06 +03:00
|
|
|
public function getAddressLines()
|
|
|
|
{
|
2021-11-03 16:19:39 +07:00
|
|
|
return [
|
|
|
|
[
|
2021-07-07 13:14:06 +03:00
|
|
|
'addressline 1',
|
|
|
|
'addressline 2',
|
2021-11-03 16:19:39 +07:00
|
|
|
'addressline 1' . RetailcrmAddressBuilder::ADDRESS_LINE_DIVIDER . 'addressline 2',
|
|
|
|
],
|
|
|
|
[
|
2021-07-07 13:14:06 +03:00
|
|
|
'addressline 1',
|
|
|
|
'',
|
2021-11-03 16:19:39 +07:00
|
|
|
'addressline 1',
|
|
|
|
],
|
|
|
|
[
|
2021-07-07 13:14:06 +03:00
|
|
|
'',
|
|
|
|
'addressline 2',
|
2021-11-03 16:19:39 +07:00
|
|
|
RetailcrmAddressBuilder::ADDRESS_LINE_DIVIDER . 'addressline 2',
|
|
|
|
],
|
|
|
|
];
|
2021-07-07 13:14:06 +03:00
|
|
|
}
|
|
|
|
|
2020-05-26 12:25:53 +03:00
|
|
|
/**
|
|
|
|
* Returns address fields names
|
|
|
|
*
|
|
|
|
* @return string[]
|
|
|
|
*/
|
|
|
|
private function getCheckableFields()
|
|
|
|
{
|
2021-11-03 16:19:39 +07:00
|
|
|
return ['index', 'city', 'countryIso', 'text', 'region'];
|
2020-05-26 12:25:53 +03:00
|
|
|
}
|
|
|
|
}
|