From eabebc53eeba4b2c31f1d2e62df444973230ec97 Mon Sep 17 00:00:00 2001 From: Alexander Shkarpetin Date: Thu, 4 Mar 2021 00:34:03 +0600 Subject: [PATCH] Add missing attribute value factories for Address.phoneNumbers and Address.emails (#454) --- CHANGELOG.md | 8 ++ src/Entities/Address.php | 38 +++++++++ src/Entities/Customer.php | 11 +-- tests/Api/CustomerEntityTest.php | 60 +++++++++++++ tests/Entities/AddressTest.php | 116 ++++++++++++++++++++++++++ tests/Entities/GatewayAccountTest.php | 2 + tests/TestCase.php | 8 +- 7 files changed, 232 insertions(+), 11 deletions(-) create mode 100644 tests/Entities/AddressTest.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 2e3f48491..a135e4487 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,10 @@ Security - in case of vulnerabilities. ## [Unreleased] +_TBD_ + +## [2.11.0] 2021-03-03 + ### Added - [x] Added new property to `Plan`: `createdTime` @@ -36,6 +40,10 @@ Security - in case of vulnerabilities. - [x] Removed deprecated `BrowserData` properties: `acceptHeader`, `ipAddress`, `javaEnabled`, `userAgent`, `deviceFingerprintHash` - [x] Removed deprecated `CommonPaymentInstrument`, `BankAccount`, `PaymentCard`, `PayPalAccount` properties: `browserData` +### Fixed + +- [x] Added missing attribute value factories for `Address`: `createPhoneNumbers`, `createEmails` + ## [2.10.0] 2021-01-21 ### Added diff --git a/src/Entities/Address.php b/src/Entities/Address.php index dc3a164b6..0da3fcea8 100644 --- a/src/Entities/Address.php +++ b/src/Entities/Address.php @@ -218,6 +218,25 @@ public function setPhoneNumbers($value) return $this->setAttribute('phoneNumbers', $value); } + /** + * @param array $data + * + * @return PhoneNumber[] + */ + public function createPhoneNumbers(array $data) + { + return array_map( + function ($values) { + if ($values instanceof PhoneNumber) { + return $values; + } + + return PhoneNumber::createFromData((array) $values); + }, + $data + ); + } + /** * @return Email[] */ @@ -235,4 +254,23 @@ public function setEmails($value) { return $this->setAttribute('emails', $value); } + + /** + * @param array $data + * + * @return Email[] + */ + public function createEmails(array $data) + { + return array_map( + function ($values) { + if ($values instanceof Email) { + return $values; + } + + return Email::createFromData((array) $values); + }, + $data + ); + } } diff --git a/src/Entities/Customer.php b/src/Entities/Customer.php index c690d1cb9..f72fce880 100644 --- a/src/Entities/Customer.php +++ b/src/Entities/Customer.php @@ -26,25 +26,22 @@ final class Customer extends Entity */ public function getEmail() { - $primaryAddress = $this->getPrimaryAddress(); - if ($primaryAddress === null) { + if ($this->getPrimaryAddress() === null) { return null; } - $emails = $primaryAddress->getEmails(); + $emails = $this->getPrimaryAddress()->getEmails(); if (empty($emails)) { return null; } foreach ($emails as $email) { - if (is_array($email) && $email['primary'] === true) { + if (($email['primary'] ?? false) === true) { return $email['value']; } - - return $email->getValue(); } - return $emails[0]->getValue(); + return $emails[0]['value']; } /** diff --git a/tests/Api/CustomerEntityTest.php b/tests/Api/CustomerEntityTest.php index 1ff2eef81..6779b40f9 100644 --- a/tests/Api/CustomerEntityTest.php +++ b/tests/Api/CustomerEntityTest.php @@ -84,4 +84,64 @@ public function testDeprecatedMethods() self::assertSame('Doe', $customer->getPrimaryAddress()->getLastName()); self::assertSame('US', $customer->getPrimaryAddress()->getCountry()); } + + public function testDeprecatedMethodsWithMultiplePhoneNumbersAndEmails() + { + $customer = new Customer(); + $customer->setPrimaryAddress(Address::createFromData([ + 'firstName' => 'John', + 'lastName' => 'Doe', + 'country' => 'US', + 'phoneNumbers' => [ + ['label' => 'other', 'value' => '+4567', 'primary' => false], + ['label' => 'main', 'value' => '+1234', 'primary' => true], + ], + 'emails' => [ + ['label' => 'other', 'value' => 'other@mail.com', 'primary' => false], + ['label' => 'main', 'value' => 'main@mail.com', 'primary' => true], + ], + ])); + + self::assertSame('John', $customer->getPrimaryAddress()->getFirstName()); + self::assertSame('Doe', $customer->getPrimaryAddress()->getLastName()); + self::assertSame('John', $customer->getFirstName()); + self::assertSame('Doe', $customer->getLastName()); + self::assertCount(2, $customer->getPrimaryAddress()->getPhoneNumbers()); + self::assertSame('+4567', $customer->getPrimaryAddress()->getPhoneNumbers()[0]->getValue()); + self::assertSame('+1234', $customer->getPrimaryAddress()->getPhoneNumbers()[1]->getValue()); + self::assertCount(2, $customer->getPrimaryAddress()->getEmails()); + self::assertSame('other@mail.com', $customer->getPrimaryAddress()->getEmails()[0]->getValue()); + self::assertSame('main@mail.com', $customer->getPrimaryAddress()->getEmails()[1]->getValue()); + self::assertSame('main@mail.com', $customer->getEmail()); + } + + public function testDeprecatedMethodsWithoutPrimaryEmailAndPhoneNumber() + { + $customer = new Customer(); + $customer->setPrimaryAddress(Address::createFromData([ + 'firstName' => 'John', + 'lastName' => 'Doe', + 'country' => 'US', + 'phoneNumbers' => [ + ['label' => 'other', 'value' => '+4567'], + ['label' => 'main', 'value' => '+1234', 'primary' => false], + ], + 'emails' => [ + ['label' => 'other', 'value' => 'other@mail.com'], + ['label' => 'main', 'value' => 'main@mail.com', 'primary' => false], + ], + ])); + + self::assertSame('John', $customer->getPrimaryAddress()->getFirstName()); + self::assertSame('Doe', $customer->getPrimaryAddress()->getLastName()); + self::assertSame('John', $customer->getFirstName()); + self::assertSame('Doe', $customer->getLastName()); + self::assertCount(2, $customer->getPrimaryAddress()->getPhoneNumbers()); + self::assertSame('+4567', $customer->getPrimaryAddress()->getPhoneNumbers()[0]->getValue()); + self::assertSame('+1234', $customer->getPrimaryAddress()->getPhoneNumbers()[1]->getValue()); + self::assertCount(2, $customer->getPrimaryAddress()->getEmails()); + self::assertSame('other@mail.com', $customer->getPrimaryAddress()->getEmails()[0]->getValue()); + self::assertSame('main@mail.com', $customer->getPrimaryAddress()->getEmails()[1]->getValue()); + self::assertSame('other@mail.com', $customer->getEmail()); + } } diff --git a/tests/Entities/AddressTest.php b/tests/Entities/AddressTest.php new file mode 100644 index 000000000..1ed630fd2 --- /dev/null +++ b/tests/Entities/AddressTest.php @@ -0,0 +1,116 @@ +setFirstName($data['firstName']); + $address->setLastName($data['lastName']); + $address->setOrganization($data['organization']); + $address->setAddress($data['address']); + $address->setAddress2($data['address2']); + $address->setCity($data['city']); + $address->setRegion($data['region']); + $address->setCountry($data['country']); + $address->setPostalCode($data['postalCode']); + $address->setPhoneNumbers( + [ + PhoneNumber::createFromData($data['phoneNumbers'][0]), + $data['phoneNumbers'][1], + ] + ); + $address->setEmails( + [ + Email::createFromData($data['emails'][0]), + $data['emails'][1], + ] + ); + + self::assertSame($data, $address->jsonSerialize()); + } + + /** + * @test + */ + public function populateAddressFromArrayReceivedFromServer(): void + { + $data = self::getDefaultData(); + + $address = new Address($data); + + self::assertSame($data['firstName'], $address->getFirstName()); + self::assertSame($data['lastName'], $address->getLastName()); + self::assertSame($data['organization'], $address->getOrganization()); + self::assertSame($data['address'], $address->getAddress()); + self::assertSame($data['address2'], $address->getAddress2()); + self::assertSame($data['city'], $address->getCity()); + self::assertSame($data['region'], $address->getRegion()); + self::assertSame($data['country'], $address->getCountry()); + self::assertSame($data['postalCode'], $address->getPostalCode()); + + self::assertCount(2, $address->getPhoneNumbers()); + foreach ($data['phoneNumbers'] as $index => $item) { + self::assertArrayHasKey($index, $address->getPhoneNumbers()); + self::assertInstanceOf(PhoneNumber::class, $address->getPhoneNumbers()[$index]); + self::assertSame($item['label'], $address->getPhoneNumbers()[$index]->getLabel()); + self::assertSame($item['value'], $address->getPhoneNumbers()[$index]->getValue()); + self::assertSame($item['primary'], $address->getPhoneNumbers()[$index]->getPrimary()); + } + + self::assertCount(2, $address->getEmails()); + foreach ($data['emails'] as $index => $item) { + self::assertArrayHasKey($index, $address->getEmails()); + self::assertInstanceOf(Email::class, $address->getEmails()[$index]); + self::assertSame($item['label'], $address->getEmails()[$index]->getLabel()); + self::assertSame($item['value'], $address->getEmails()[$index]->getValue()); + self::assertSame($item['primary'], $address->getEmails()[$index]->getPrimary()); + } + } + + private static function getDefaultData(): array + { + return [ + 'firstName' => 'John', + 'lastName' => 'Doe', + 'organization' => 'Rebilly', + 'address' => 'address 1', + 'address2' => 'address 2', + 'city' => 'Austin', + 'region' => 'Texas', + 'country' => 'US', + 'postalCode' => '12345', + 'phoneNumbers' => [ + ['label' => 'main', 'value' => '+1234', 'primary' => true], + ['label' => 'other', 'value' => '+4567', 'primary' => false], + ], + 'emails' => [ + ['label' => 'main', 'value' => 'main@mail.com', 'primary' => true], + ['label' => 'other', 'value' => 'other@mail.com', 'primary' => false], + ], + ]; + } +} diff --git a/tests/Entities/GatewayAccountTest.php b/tests/Entities/GatewayAccountTest.php index 121c9624e..554df5ad5 100644 --- a/tests/Entities/GatewayAccountTest.php +++ b/tests/Entities/GatewayAccountTest.php @@ -11,6 +11,8 @@ declare(strict_types=1); +namespace Rebilly\Tests\Entities; + use Rebilly\Entities\GatewayAccount; use Rebilly\Tests\TestCase; diff --git a/tests/TestCase.php b/tests/TestCase.php index 240954ea4..1982ce5b6 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -283,19 +283,19 @@ protected function getFakeValue($attribute, $class) return random_int(1, 10); case 'phoneNumbers': return [ - new Entities\Contact\PhoneNumber([ + [ 'label' => self::TEST_WORD, 'primary' => true, 'value' => self::TEST_PHONE, - ]), + ], ]; case 'emails': return [ - new Entities\Contact\Email([ + [ 'label' => self::TEST_WORD, 'primary' => true, 'value' => self::TEST_EMAIL, - ]), + ], ]; case 'extension': return self::randomElements(Entities\File::allowedTypes())[0];