From 654741261bce818bb2f501e345b722976b8c1a79 Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Sat, 23 Jan 2021 15:11:56 +0200 Subject: [PATCH] Address formatter --- .../Classes/AddressFormatters/Formatter1.php | 89 ++++++++++ .../Classes/AddressFormatters/Formatter2.php | 91 ++++++++++ .../Classes/AddressFormatters/Formatter3.php | 89 ++++++++++ .../Classes/AddressFormatters/Formatter4.php | 89 ++++++++++ .../Espo/Core/FieldUtils/Address/Address.php | 156 +++++++++++++++++ .../FieldUtils/Address/AddressBuilder.php | 104 ++++++++++++ .../FieldUtils/Address/AddressFormatter.php | 38 +++++ .../Address/AddressFormatterFactory.php | 74 ++++++++ .../AddressFormatterMetadataProvider.php | 49 ++++++ .../metadata/app/addressFormats.json | 14 ++ .../Espo/Resources/metadata/app/metadata.json | 1 + .../Espo/Tools/Export/Formats/Xlsx.php | 104 ++++-------- .../Address/AddressFormatterTest.php | 62 +++++++ .../Address/AddressFormattersTest.php | 159 ++++++++++++++++++ .../Core/FieldUtils/Address/AddressTest.php | 122 ++++++++++++++ 15 files changed, 1169 insertions(+), 72 deletions(-) create mode 100644 application/Espo/Classes/AddressFormatters/Formatter1.php create mode 100644 application/Espo/Classes/AddressFormatters/Formatter2.php create mode 100644 application/Espo/Classes/AddressFormatters/Formatter3.php create mode 100644 application/Espo/Classes/AddressFormatters/Formatter4.php create mode 100644 application/Espo/Core/FieldUtils/Address/Address.php create mode 100644 application/Espo/Core/FieldUtils/Address/AddressBuilder.php create mode 100644 application/Espo/Core/FieldUtils/Address/AddressFormatter.php create mode 100644 application/Espo/Core/FieldUtils/Address/AddressFormatterFactory.php create mode 100644 application/Espo/Core/FieldUtils/Address/AddressFormatterMetadataProvider.php create mode 100644 application/Espo/Resources/metadata/app/addressFormats.json create mode 100644 tests/integration/Espo/Core/FieldUtils/Address/AddressFormatterTest.php create mode 100644 tests/unit/Espo/Core/FieldUtils/Address/AddressFormattersTest.php create mode 100644 tests/unit/Espo/Core/FieldUtils/Address/AddressTest.php diff --git a/application/Espo/Classes/AddressFormatters/Formatter1.php b/application/Espo/Classes/AddressFormatters/Formatter1.php new file mode 100644 index 0000000000..2a1050e15e --- /dev/null +++ b/application/Espo/Classes/AddressFormatters/Formatter1.php @@ -0,0 +1,89 @@ +getStreet(); + $city = $address->getCity(); + $country = $address->getCountry(); + $state = $address->getState(); + $postalCode = $address->getPostalCode(); + + if ($street) { + $result .= $street; + } + + if ($city || $state || $postalCode) { + if ($result) { + $result .= "\n"; + } + + if ($city) { + $result .= $city; + } + + if ($state && $city) { + $result .= ', '; + } + + if ($state) { + $result .= $state; + } + + if ($postalCode && ($state || $city)) { + $result .= ' '; + } + + if ($postalCode) { + $result .= $postalCode; + } + } + + if ($country) { + if ($result) { + $result .= "\n"; + } + + $result .= $country; + } + + return $result; + } +} diff --git a/application/Espo/Classes/AddressFormatters/Formatter2.php b/application/Espo/Classes/AddressFormatters/Formatter2.php new file mode 100644 index 0000000000..e9093c9a63 --- /dev/null +++ b/application/Espo/Classes/AddressFormatters/Formatter2.php @@ -0,0 +1,91 @@ +getStreet(); + $city = $address->getCity(); + $country = $address->getCountry(); + $state = $address->getState(); + $postalCode = $address->getPostalCode(); + + if ($street) { + $result .= $street; + } + + if ($city || $postalCode) { + if ($result) { + $result .= "\n"; + } + + if ($postalCode) { + $result .= $postalCode; + } + + if ($postalCode && $city) { + $result .= ' '; + } + + if ($city) { + $result .= $city; + } + } + + if ($state || $country) { + if ($result) { + $result .= "\n"; + } + + if ($state) { + $result .= $state; + } + + if ($state && $country) { + $result .= ' '; + } + + if ($country) { + $result .= $country; + } + } + + return $result; + } +} diff --git a/application/Espo/Classes/AddressFormatters/Formatter3.php b/application/Espo/Classes/AddressFormatters/Formatter3.php new file mode 100644 index 0000000000..0514a03ac7 --- /dev/null +++ b/application/Espo/Classes/AddressFormatters/Formatter3.php @@ -0,0 +1,89 @@ +getStreet(); + $city = $address->getCity(); + $country = $address->getCountry(); + $state = $address->getState(); + $postalCode = $address->getPostalCode(); + + if ($country) { + $result .= $country; + } + + if ($city || $state || $postalCode) { + if ($result) { + $result .= "\n"; + } + + if ($state) { + $result .= $state; + } + + if ($state && $postalCode) { + $result .= ' '; + } + + if ($postalCode) { + $result .= $postalCode; + } + + if ($city && ($state || $postalCode)) { + $result .= ' '; + } + + if ($city) { + $result .= $city; + } + } + + if ($street) { + if ($result) { + $result .= "\n"; + } + + $result .= $street; + } + + return $result; + } +} diff --git a/application/Espo/Classes/AddressFormatters/Formatter4.php b/application/Espo/Classes/AddressFormatters/Formatter4.php new file mode 100644 index 0000000000..9282179012 --- /dev/null +++ b/application/Espo/Classes/AddressFormatters/Formatter4.php @@ -0,0 +1,89 @@ +getStreet(); + $city = $address->getCity(); + $country = $address->getCountry(); + $state = $address->getState(); + $postalCode = $address->getPostalCode(); + + if ($street) { + $result .= $street; + } + + if ($city) { + if ($result) { + $result .= "\n"; + } + + $result .= $city; + } + + if ($country || $state || $postalCode) { + if ($result) { + $result .= "\n"; + } + + if ($country) { + $result .= $country; + } + + if ($state && $country) { + $result .= ' - '; + } + + if ($state) { + $result .= $state; + } + + if ($postalCode && ($state || $country)) { + $result .= ' '; + } + + if ($postalCode) { + $result .= $postalCode; + } + } + + return $result; + } +} diff --git a/application/Espo/Core/FieldUtils/Address/Address.php b/application/Espo/Core/FieldUtils/Address/Address.php new file mode 100644 index 0000000000..f4d1dad7a4 --- /dev/null +++ b/application/Espo/Core/FieldUtils/Address/Address.php @@ -0,0 +1,156 @@ +street; + } + + public function getCity() : ?string + { + return $this->city; + } + + public function getCountry() : ?string + { + return $this->country; + } + + public function getState() : ?string + { + return $this->state; + } + + public function getPostalCode() : ?string + { + return $this->postalCode; + } + + public function withStreet(?string $street) : self + { + $newAddress = self::createBuilder() + ->clone($this) + ->setStreet($street) + ->build(); + + return $newAddress; + } + + public function withCity(?string $city) : self + { + $newAddress = self::createBuilder() + ->clone($this) + ->setCity($city) + ->build(); + + return $newAddress; + } + + public function withCountry(?string $country) : self + { + $newAddress = self::createBuilder() + ->clone($this) + ->setCountry($country) + ->build(); + + return $newAddress; + } + + public function withState(?string $state) : self + { + $newAddress = self::createBuilder() + ->clone($this) + ->setState($state) + ->build(); + + return $newAddress; + } + + public function withPostalCode(?string $postalCode) : self + { + $newAddress = self::createBuilder() + ->clone($this) + ->setPostalCode($postalCode) + ->build(); + + return $newAddress; + } + + public static function fromEntity(Entity $entity, string $field) : self + { + $obj = new self(); + + $obj->street = $entity->get($field . 'Street'); + $obj->city = $entity->get($field . 'City'); + $obj->country = $entity->get($field . 'Country'); + $obj->state = $entity->get($field . 'State'); + $obj->postalCode = $entity->get($field . 'PostalCode'); + + return $obj; + } + + public static function fromRaw(array $raw) : self + { + $obj = new self(); + + $obj->street = $raw['street']; + $obj->city = $raw['city']; + $obj->country = $raw['country']; + $obj->state = $raw['state']; + $obj->postalCode = $raw['postalCode']; + + return $obj; + } + + public static function createBuilder() : AddressBuilder + { + return new AddressBuilder(); + } +} diff --git a/application/Espo/Core/FieldUtils/Address/AddressBuilder.php b/application/Espo/Core/FieldUtils/Address/AddressBuilder.php new file mode 100644 index 0000000000..de93d94e99 --- /dev/null +++ b/application/Espo/Core/FieldUtils/Address/AddressBuilder.php @@ -0,0 +1,104 @@ +setStreet($address->getStreet()); + $this->setCity($address->getCity()); + $this->setCountry($address->getCountry()); + $this->setState($address->getState()); + $this->setPostalCode($address->getPostalCode()); + + return $this; + } + + public function setStreet(?string $street) : self + { + $this->street = $street; + + return $this; + } + + public function setCity(?string $city) : self + { + $this->city = $city; + + return $this; + } + + public function setCountry(?string $country) : self + { + $this->country = $country; + + return $this; + } + + public function setState(?string $state) : self + { + $this->state = $state; + + return $this; + } + + public function setPostalCode(?string $postalCode) : self + { + $this->postalCode = $postalCode; + + return $this; + } + + public function build() : Address + { + return Address::fromRaw([ + 'street' => $this->street, + 'city' => $this->city, + 'country' => $this->country, + 'state' => $this->state, + 'postalCode' => $this->postalCode, + ]); + } + +} diff --git a/application/Espo/Core/FieldUtils/Address/AddressFormatter.php b/application/Espo/Core/FieldUtils/Address/AddressFormatter.php new file mode 100644 index 0000000000..afcfc8e104 --- /dev/null +++ b/application/Espo/Core/FieldUtils/Address/AddressFormatter.php @@ -0,0 +1,38 @@ +metadataProvider = $metadataProvider; + $this->injectableFactory = $injectableFactory; + $this->config = $config; + } + + public function create(int $format) : AddressFormatter + { + $className = $this->metadataProvider->getFormatterClassName($format); + + if (!$className) { + throw new RuntimeException("Unknown address format '{$format}'."); + } + + return $this->injectableFactory->create($className); + } + + public function createDefault() : AddressFormatter + { + $format = $this->config->get('addressFormat') ?? 1; + + return $this->create($format); + } +} diff --git a/application/Espo/Core/FieldUtils/Address/AddressFormatterMetadataProvider.php b/application/Espo/Core/FieldUtils/Address/AddressFormatterMetadataProvider.php new file mode 100644 index 0000000000..0ccd53743e --- /dev/null +++ b/application/Espo/Core/FieldUtils/Address/AddressFormatterMetadataProvider.php @@ -0,0 +1,49 @@ +metadata = $metadata; + } + + public function getFormatterClassName(int $format) : ?string + { + return $this->metadata->get([ + 'app', 'addressFormats', strval($format), 'formatterClassName', + ]); + } +} diff --git a/application/Espo/Resources/metadata/app/addressFormats.json b/application/Espo/Resources/metadata/app/addressFormats.json new file mode 100644 index 0000000000..b64b5bba2b --- /dev/null +++ b/application/Espo/Resources/metadata/app/addressFormats.json @@ -0,0 +1,14 @@ +{ + "1": { + "formatterClassName": "Espo\\Classes\\AddressFormatters\\Formatter1" + }, + "2": { + "formatterClassName": "Espo\\Classes\\AddressFormatters\\Formatter2" + }, + "3": { + "formatterClassName": "Espo\\Classes\\AddressFormatters\\Formatter3" + }, + "4": { + "formatterClassName": "Espo\\Classes\\AddressFormatters\\Formatter4" + } +} \ No newline at end of file diff --git a/application/Espo/Resources/metadata/app/metadata.json b/application/Espo/Resources/metadata/app/metadata.json index 4b24c62ec2..7cd2818246 100644 --- a/application/Espo/Resources/metadata/app/metadata.json +++ b/application/Espo/Resources/metadata/app/metadata.json @@ -14,6 +14,7 @@ ["app", "appParams"], ["app", "cleanup"], ["app", "pdfEngines", "__ANY__", "implementationClassNameMap"], + ["app", "addressFormats", "__ANY__", "formatterClassName"], ["app", "auth2FAMethods", "__ANY__", "implementationClassName"], ["app", "auth2FAMethods", "__ANY__", "implementationUserClassName"], ["authenticationMethods", "__ANY__", "implementationClassName"] diff --git a/application/Espo/Tools/Export/Formats/Xlsx.php b/application/Espo/Tools/Export/Formats/Xlsx.php index c089f9cdcc..e8a841649e 100644 --- a/application/Espo/Tools/Export/Formats/Xlsx.php +++ b/application/Espo/Tools/Export/Formats/Xlsx.php @@ -40,6 +40,8 @@ use Espo\Core\{ FileStorage\Manager as FileStorageManager, Utils\File\Manager as FileManager, ORM\EntityManager, + FieldUtils\Address\Address, + FieldUtils\Address\AddressFormatterFactory, }; use PhpOffice\PhpSpreadsheet\Cell\DataType; @@ -62,6 +64,7 @@ class Xlsx protected $entityManager; protected $fileStorageManager; protected $fileManager; + protected $addressFormatterFactory; public function __construct( Config $config, @@ -70,7 +73,8 @@ class Xlsx DateTimeUtil $dateTime, EntityManager $entityManager, FileStorageManager $fileStorageManager, - FileManager $fileManager + FileManager $fileManager, + AddressFormatterFactory $addressFormatterFactory ) { $this->config = $config; $this->metadata = $metadata; @@ -79,21 +83,7 @@ class Xlsx $this->entityManager = $entityManager; $this->fileStorageManager = $fileStorageManager; $this->fileManager = $fileManager; - } - - protected function getConfig() - { - return $this->config; - } - - protected function getMetadata() - { - return $this->metadata; - } - - protected function getEntityManager() - { - return $this->entityManager; + $this->addressFormatterFactory = $addressFormatterFactory; } public function loadAdditionalFields(Entity $entity, $fieldList) @@ -124,7 +114,7 @@ class Xlsx } } foreach ($fieldList as $field) { - $fieldType = $this->getMetadata()->get(['entityDefs', $entity->getEntityType(), 'fields', $field, 'type']); + $fieldType = $this->metadata->get(['entityDefs', $entity->getEntityType(), 'fields', $field, 'type']); if ($fieldType === 'linkMultiple' || $fieldType === 'attachmentMultiple') { if (!$entity->has($field . 'Ids')) { @@ -138,7 +128,7 @@ class Xlsx { if ($exportAllFields) { foreach ($fieldList as $i => $field) { - $type = $this->getMetadata()->get(['entityDefs', $entityType, 'fields', $field, 'type']); + $type = $this->metadata->get(['entityDefs', $entityType, 'fields', $field, 'type']); if (in_array($type, ['linkMultiple', 'attachmentMultiple'])) { unset($fieldList[$i]); } @@ -156,7 +146,7 @@ class Xlsx $attributeList[] = 'id'; } - $linkDefs = $this->getMetadata()->get(['entityDefs', $entityType, 'links']); + $linkDefs = $this->metadata->get(['entityDefs', $entityType, 'links']); if (is_array($linkDefs)) { foreach ($linkDefs as $link => $defs) { @@ -168,7 +158,7 @@ class Xlsx $linkList[] = $link; } else if ($defs['type'] === 'belongsTo' && !empty($defs['noJoin'])) { - if ($this->getMetadata()->get(['entityDefs', $entityType, 'fields', $link])) { + if ($this->metadata->get(['entityDefs', $entityType, 'fields', $link])) { $linkList[] = $link; } } @@ -182,7 +172,7 @@ class Xlsx } foreach ($fieldList as $field) { - $type = $this->getMetadata()->get(['entityDefs', $entityType, 'fields', $field, 'type']); + $type = $this->metadata->get(['entityDefs', $entityType, 'fields', $field, 'type']); if ($type === 'currencyConverted') { if (!in_array($field, $attributeList)) { @@ -402,7 +392,7 @@ class Xlsx if (array_key_exists($name.'Currency', $row) && array_key_exists($name, $row)) { $sheet->setCellValue("$col$rowNumber", $row[$name] ? $row[$name] : ''); - $currency = $row[$name . 'Currency'] ?? $this->getConfig()->get('defaultCurrency'); + $currency = $row[$name . 'Currency'] ?? $this->config->get('defaultCurrency'); $sheet->getStyle("$col$rowNumber") ->getNumberFormat() @@ -413,7 +403,7 @@ class Xlsx } else if ($type == 'currencyConverted') { if (array_key_exists($name, $row)) { - $currency = $this->getConfig()->get('defaultCurrency'); + $currency = $this->config->get('defaultCurrency'); $sheet->getStyle("$col$rowNumber") ->getNumberFormat() @@ -481,7 +471,7 @@ class Xlsx } else if ($type == 'image') { if (isset($row[$name . 'Id']) && $row[$name . 'Id']) { - $attachment = $this->getEntityManager()->getEntity('Attachment', $row[$name . 'Id']); + $attachment = $this->entityManager->getEntity('Attachment', $row[$name . 'Id']); if ($attachment) { $objDrawing = new Drawing(); @@ -533,47 +523,17 @@ class Xlsx } } else if ($type == 'address') { - $value = ''; + $address = Address::createBuilder() + ->setStreet($row[$name . 'Street'] ?? null) + ->setCity($row[$name . 'City'] ?? null) + ->setState($row[$name . 'State'] ?? null) + ->setCountry($row[$name . 'Country'] ?? null) + ->setPostalCode($row[$name . 'PostalCode'] ?? null) + ->build(); - if (!empty($row[$name . 'Street'])) { - $value = $value .= $row[$name.'Street']; - } + $formatter = $this->addressFormatterFactory->createDefault(); - if (!empty($row[$name.'City']) || !empty($row[$name.'State']) || !empty($row[$name.'PostalCode'])) { - if ($value) { - $value .= "\n"; - } - - if (!empty($row[$name.'City'])) { - $value .= $row[$name.'City']; - - if ( - !empty($row[$name.'State']) || !empty($row[$name.'PostalCode']) - ) { - $value .= ', '; - } - } - - if (!empty($row[$name.'State'])) { - $value .= $row[$name.'State']; - - if (!empty($row[$name.'PostalCode'])) { - $value .= ' '; - } - } - - if (!empty($row[$name.'PostalCode'])) { - $value .= $row[$name.'PostalCode']; - } - } - - if (!empty($row[$name.'Country'])) { - if ($value) { - $value .= "\n"; - } - - $value .= $row[$name.'Country']; - } + $value = $formatter->format($address); $sheet->setCellValue("$col$rowNumber", $value); } @@ -656,7 +616,7 @@ class Xlsx if ($name == 'name') { if (array_key_exists('id', $row)) { - $link = $this->getConfig()->getSiteUrl() . "/#".$entityType . "/view/" . $row['id']; + $link = $this->config->getSiteUrl() . "/#".$entityType . "/view/" . $row['id']; } } else if ($type == 'url') { @@ -669,33 +629,33 @@ class Xlsx $foreignEntity = null; if (!$isForeign) { - $foreignEntity = $this->getMetadata()->get( + $foreignEntity = $this->metadata->get( ['entityDefs', $entityType, 'links', $name, 'entity'] ); } else { - $foreignEntity1 = $this->getMetadata()->get( + $foreignEntity1 = $this->metadata->get( ['entityDefs', $entityType, 'links', $foreignLink, 'entity'] ); - $foreignEntity = $this->getMetadata()->get( + $foreignEntity = $this->metadata->get( ['entityDefs', $foreignEntity1, 'links', $foreignField, 'entity'] ); } if ($foreignEntity) { - $link = $this->getConfig()->getSiteUrl() . "/#" . $foreignEntity. "/view/". $row[$name.'Id']; + $link = $this->config->getSiteUrl() . "/#" . $foreignEntity. "/view/". $row[$name.'Id']; } } } else if ($type == 'file') { if (array_key_exists($name.'Id', $row)) { - $link = $this->getConfig()->getSiteUrl() . "/?entryPoint=download&id=" . $row[$name.'Id']; + $link = $this->config->getSiteUrl() . "/?entryPoint=download&id=" . $row[$name.'Id']; } } else if ($type == 'linkParent') { if (array_key_exists($name.'Id', $row) && array_key_exists($name.'Type', $row)) { - $link = $this->getConfig()->getSiteUrl() . "/#".$row[$name.'Type']."/view/". $row[$name.'Id']; + $link = $this->config->getSiteUrl() . "/#".$row[$name.'Type']."/view/". $row[$name.'Id']; } } else if ($type == 'phone') { @@ -809,9 +769,9 @@ class Xlsx protected function getCurrencyFormatCode(string $currency) : string { - $currencySymbol = $this->getMetadata()->get(['app', 'currency', 'symbolMap', $currency], ''); + $currencySymbol = $this->metadata->get(['app', 'currency', 'symbolMap', $currency], ''); - $currencyFormat = $this->getConfig()->get('currencyFormat') ?? 2; + $currencyFormat = $this->config->get('currencyFormat') ?? 2; if ($currencyFormat == 3) { return '#,##0.00_-"' . $currencySymbol . '"'; diff --git a/tests/integration/Espo/Core/FieldUtils/Address/AddressFormatterTest.php b/tests/integration/Espo/Core/FieldUtils/Address/AddressFormatterTest.php new file mode 100644 index 0000000000..fed9def420 --- /dev/null +++ b/tests/integration/Espo/Core/FieldUtils/Address/AddressFormatterTest.php @@ -0,0 +1,62 @@ +getContainer()->get('injectableFactory')->create(AddressFormatterFactory::class); + + $formatter = $formatterFactory->create(1); + + $address = Address::createBuilder() + ->setStreet('street') + ->setCity('city') + ->setCountry('country') + ->setState('state') + ->setPostalCode('postalCode') + ->build(); + + $expected = + "street\n" . + "city, state postalCode\n" . + "country"; + + $result = $formatter->format($address); + + $this->assertEquals($expected, $result); + } +} diff --git a/tests/unit/Espo/Core/FieldUtils/Address/AddressFormattersTest.php b/tests/unit/Espo/Core/FieldUtils/Address/AddressFormattersTest.php new file mode 100644 index 0000000000..ee436c9134 --- /dev/null +++ b/tests/unit/Espo/Core/FieldUtils/Address/AddressFormattersTest.php @@ -0,0 +1,159 @@ +setStreet('street') + ->setCity('city') + ->setCountry('country') + ->setState('state') + ->setPostalCode('postalCode') + ->build(); + + $formatter = new Formatter1(); + + $expected = + "street\n" . + "city, state postalCode\n" . + "country"; + + $result = $formatter->format($address); + + $this->assertEquals($expected, $result); + } + + public function testFormat1NoState() + { + $address = Address::createBuilder() + ->setStreet('street') + ->setCity('city') + ->setCountry('country') + ->setState(null) + ->setPostalCode('postalCode') + ->build(); + + $formatter = new Formatter1(); + + $expected = + "street\n" . + "city postalCode\n" . + "country"; + + $result = $formatter->format($address); + + $this->assertEquals($expected, $result); + } + + public function testFormat2All() + { + $address = Address::createBuilder() + ->setStreet('street') + ->setCity('city') + ->setCountry('country') + ->setState('state') + ->setPostalCode('postalCode') + ->build(); + + $formatter = new Formatter2(); + + $expected = + "street\n" . + "postalCode city\n" . + "state country"; + + $result = $formatter->format($address); + + $this->assertEquals($expected, $result); + } + + public function testFormat3All() + { + $address = Address::createBuilder() + ->setStreet('street') + ->setCity('city') + ->setCountry('country') + ->setState('state') + ->setPostalCode('postalCode') + ->build(); + + $formatter = new Formatter3(); + + $expected = + "country\n" . + "state postalCode city\n" . + "street"; + + $result = $formatter->format($address); + + $this->assertEquals($expected, $result); + } + + public function testFormat4All() + { + $address = Address::createBuilder() + ->setStreet('street') + ->setCity('city') + ->setCountry('country') + ->setState('state') + ->setPostalCode('postalCode') + ->build(); + + $formatter = new Formatter4(); + + $expected = + "street\n" . + "city\n" . + "country - state postalCode"; + + $result = $formatter->format($address); + + $this->assertEquals($expected, $result); + } +} diff --git a/tests/unit/Espo/Core/FieldUtils/Address/AddressTest.php b/tests/unit/Espo/Core/FieldUtils/Address/AddressTest.php new file mode 100644 index 0000000000..7694dcc070 --- /dev/null +++ b/tests/unit/Espo/Core/FieldUtils/Address/AddressTest.php @@ -0,0 +1,122 @@ +setStreet('street') + ->setCity('city') + ->setCountry('country') + ->setState('state') + ->setPostalCode('postalCode') + ->build(); + + $this->assertEquals('street', $address->getStreet()); + $this->assertEquals('city', $address->getCity()); + $this->assertEquals('country', $address->getCountry()); + $this->assertEquals('state', $address->getState()); + $this->assertEquals('postalCode', $address->getPostalCode()); + } + + public function testBuilderClone() + { + $addressOriginal = Address::createBuilder() + ->setStreet('street') + ->setCity('city') + ->setCountry('country') + ->setState('state') + ->setPostalCode('postalCode') + ->build(); + + $address = Address::createBuilder() + ->clone($addressOriginal) + ->build(); + + $this->assertEquals('street', $address->getStreet()); + $this->assertEquals('city', $address->getCity()); + $this->assertEquals('country', $address->getCountry()); + $this->assertEquals('state', $address->getState()); + $this->assertEquals('postalCode', $address->getPostalCode()); + } + + public function testAddressWith() + { + $addressOriginal = Address::createBuilder() + ->setStreet('street') + ->setCity('city') + ->setCountry('country') + ->setState('state') + ->setPostalCode('postalCode') + ->build(); + + $address = $addressOriginal->withStreet('new street'); + + $this->assertEquals('new street', $address->getStreet()); + $this->assertEquals('city', $address->getCity()); + } + + public function testFromEntity() + { + $entity = $this->createMock(Entity::class); + + $entity + ->expects($this->any()) + ->method('get') + ->willReturnMap([ + ['addressStreet', 'street'], + ['addressCity', 'city'], + ['addressCountry', 'country'], + ['addressState', null], + ['addressPostalCode', null], + ]); + + $address = Address::fromEntity($entity, 'address'); + + $this->assertEquals('street', $address->getStreet()); + $this->assertEquals('city', $address->getCity()); + $this->assertEquals('country', $address->getCountry()); + $this->assertEquals(null, $address->getState()); + $this->assertEquals(null, $address->getPostalCode()); + } +}