Skip to content

Commit

Permalink
Add missing attribute value factories for Address.phoneNumbers and Ad…
Browse files Browse the repository at this point in the history
…dress.emails (#454)
  • Loading branch information
ashkarpetin authored Mar 3, 2021
1 parent aa84a4b commit eabebc5
Show file tree
Hide file tree
Showing 7 changed files with 232 additions and 11 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand All @@ -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
Expand Down
38 changes: 38 additions & 0 deletions src/Entities/Address.php
Original file line number Diff line number Diff line change
Expand Up @@ -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[]
*/
Expand All @@ -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
);
}
}
11 changes: 4 additions & 7 deletions src/Entities/Customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
}

/**
Expand Down
60 changes: 60 additions & 0 deletions tests/Api/CustomerEntityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => '[email protected]', 'primary' => false],
['label' => 'main', 'value' => '[email protected]', '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('[email protected]', $customer->getPrimaryAddress()->getEmails()[0]->getValue());
self::assertSame('[email protected]', $customer->getPrimaryAddress()->getEmails()[1]->getValue());
self::assertSame('[email protected]', $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' => '[email protected]'],
['label' => 'main', 'value' => '[email protected]', '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('[email protected]', $customer->getPrimaryAddress()->getEmails()[0]->getValue());
self::assertSame('[email protected]', $customer->getPrimaryAddress()->getEmails()[1]->getValue());
self::assertSame('[email protected]', $customer->getEmail());
}
}
116 changes: 116 additions & 0 deletions tests/Entities/AddressTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?php
/**
* This source file is proprietary and part of Rebilly.
*
* (c) Rebilly SRL
* Rebilly Ltd.
* Rebilly Inc.
*
* @see https://www.rebilly.com
*/

declare(strict_types=1);

namespace Rebilly\Tests\Entities;

use Rebilly\Entities\Address;
use Rebilly\Entities\Contact\Email;
use Rebilly\Entities\Contact\PhoneNumber;
use Rebilly\Tests\TestCase;

final class AddressTest extends TestCase
{
/**
* @test
*/
public function buildObjectUsingSetterToSendToServer(): void
{
$data = self::getDefaultData();

$address = new Address();
$address->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' => '[email protected]', 'primary' => true],
['label' => 'other', 'value' => '[email protected]', 'primary' => false],
],
];
}
}
2 changes: 2 additions & 0 deletions tests/Entities/GatewayAccountTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

declare(strict_types=1);

namespace Rebilly\Tests\Entities;

use Rebilly\Entities\GatewayAccount;
use Rebilly\Tests\TestCase;

Expand Down
8 changes: 4 additions & 4 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down

0 comments on commit eabebc5

Please sign in to comment.