mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-06 12:25:15 +08:00
Introduce unit test for CheckoutPayPalAddressPreset PCP-28
This commit is contained in:
parent
ebb78afe8c
commit
781760a82c
2 changed files with 183 additions and 1 deletions
|
@ -72,7 +72,9 @@ class CheckoutPayPalAddressPreset
|
|||
];
|
||||
$payerMap = [
|
||||
'billing_email' => 'emailAddress',
|
||||
'billing_phone' => 'phone',
|
||||
];
|
||||
$payerPhoneMap = [
|
||||
'billing_phone' => 'nationalNumber',
|
||||
];
|
||||
|
||||
if(array_key_exists($fieldId, $addressMap) && $shipping) {
|
||||
|
@ -87,6 +89,10 @@ class CheckoutPayPalAddressPreset
|
|||
return $payer->{$payerMap[$fieldId]}() ?: null;
|
||||
}
|
||||
|
||||
if(array_key_exists($fieldId, $payerPhoneMap) && $payer && $payer->phone() && $payer->phone()->phone()) {
|
||||
return $payer->phone()->phone()->{$payerPhoneMap[$fieldId]}() ?: null;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,176 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Inpsyde\PayPalCommerce\WcGateway\Checkout;
|
||||
|
||||
use Inpsyde\PayPalCommerce\ApiClient\Entity\Address;
|
||||
use Inpsyde\PayPalCommerce\ApiClient\Entity\Order;
|
||||
use Inpsyde\PayPalCommerce\ApiClient\Entity\Payer;
|
||||
use Inpsyde\PayPalCommerce\ApiClient\Entity\PayerName;
|
||||
use Inpsyde\PayPalCommerce\ApiClient\Entity\Phone;
|
||||
use Inpsyde\PayPalCommerce\ApiClient\Entity\PhoneWithType;
|
||||
use Inpsyde\PayPalCommerce\ApiClient\Entity\PurchaseUnit;
|
||||
use Inpsyde\PayPalCommerce\ApiClient\Entity\Shipping;
|
||||
use Inpsyde\PayPalCommerce\Session\SessionHandler;
|
||||
use Inpsyde\PayPalCommerce\TestCase;
|
||||
use Mockery\MockInterface;
|
||||
|
||||
class CheckoutPayPalAddressPresetTest extends TestCase
|
||||
{
|
||||
private $mocks = [];
|
||||
|
||||
public function tearDown(): void
|
||||
{
|
||||
$this->mocks = [];
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider filterCheckoutFieldData
|
||||
*/
|
||||
public function testFilterCheckoutField(string $fieldId, ?Order $order, ?string $expected): void
|
||||
{
|
||||
// SessionHandler
|
||||
$this->buildTestee()[0]->shouldReceive('order')
|
||||
->andReturn($order);
|
||||
|
||||
/* @var CheckoutPayPalAddressPreset $testee */
|
||||
$testee = $this->buildTestee()[1];
|
||||
|
||||
self::assertSame(
|
||||
$expected,
|
||||
$testee->filterCheckoutFiled(null, $fieldId)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see testFilterCheckoutField
|
||||
*/
|
||||
public function filterCheckoutFieldData(): array
|
||||
{
|
||||
$order = \Mockery::mock(
|
||||
Order::class,
|
||||
[
|
||||
'id' => 'abc123def',
|
||||
'purchaseUnits' => [
|
||||
\Mockery::mock(
|
||||
PurchaseUnit::class,
|
||||
[
|
||||
'shipping' => \Mockery::mock(
|
||||
Shipping::class,
|
||||
[
|
||||
'address' => \Mockery::mock(
|
||||
Address::class,
|
||||
[
|
||||
'addressLine1' => 'Unter den Linden 1',
|
||||
'addressLine2' => '2. Stock Hinterhaus',
|
||||
'postalCode' => '10117',
|
||||
'countryCode' => 'DE',
|
||||
'adminArea1' => 'BE',
|
||||
'adminArea2' => 'Berlin',
|
||||
]
|
||||
),
|
||||
]
|
||||
),
|
||||
]
|
||||
),
|
||||
],
|
||||
'payer' => \Mockery::mock(
|
||||
Payer::class,
|
||||
[
|
||||
'name' => \Mockery::mock(
|
||||
PayerName::class,
|
||||
[
|
||||
'givenName' => 'John',
|
||||
'surname' => 'Doe',
|
||||
]
|
||||
),
|
||||
'emailAddress' => 'mail@domain.tld',
|
||||
'phone' => \Mockery::mock(
|
||||
PhoneWithType::class,
|
||||
[
|
||||
'phone' => \Mockery::mock(
|
||||
Phone::class,
|
||||
[
|
||||
'nationalNumber' => '+4912345678',
|
||||
]
|
||||
),
|
||||
]
|
||||
),
|
||||
]
|
||||
),
|
||||
]
|
||||
);
|
||||
|
||||
return [
|
||||
'Test billing_address_1' => [
|
||||
'fieldId' => 'billing_address_1',
|
||||
'order' => $order,
|
||||
'expected' => 'Unter den Linden 1',
|
||||
],
|
||||
'Test billing_address_2' => [
|
||||
'fieldId' => 'billing_address_2',
|
||||
'order' => $order,
|
||||
'expected' => '2. Stock Hinterhaus',
|
||||
],
|
||||
'Test billing_postcode' => [
|
||||
'fieldId' => 'billing_postcode',
|
||||
'order' => $order,
|
||||
'expected' => '10117',
|
||||
],
|
||||
'Test billing_country' => [
|
||||
'fieldId' => 'billing_country',
|
||||
'order' => $order,
|
||||
'expected' => 'DE',
|
||||
],
|
||||
'Test billing_city' => [
|
||||
'fieldId' => 'billing_city',
|
||||
'order' => $order,
|
||||
'expected' => 'Berlin',
|
||||
],
|
||||
'Test billing_state' => [
|
||||
'fieldId' => 'billing_state',
|
||||
'order' => $order,
|
||||
'expected' => 'BE',
|
||||
],
|
||||
'Test billing_last_name' => [
|
||||
'fieldId' => 'billing_last_name',
|
||||
'order' => $order,
|
||||
'expected' => 'Doe',
|
||||
],
|
||||
'Test billing_first_name' => [
|
||||
'fieldId' => 'billing_first_name',
|
||||
'order' => $order,
|
||||
'expected' => 'John',
|
||||
],
|
||||
'Test billing_email' => [
|
||||
'fieldId' => 'billing_email',
|
||||
'order' => $order,
|
||||
'expected' => 'mail@domain.tld',
|
||||
],
|
||||
'Test billing_phone' => [
|
||||
'fieldId' => 'billing_phone',
|
||||
'order' => $order,
|
||||
'expected' => '+4912345678',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return MockInterface[]
|
||||
*/
|
||||
private function buildTestee(): array
|
||||
{
|
||||
if (! $this->mocks) {
|
||||
$sessionHandler = \Mockery::mock(SessionHandler::class);
|
||||
$testee = new CheckoutPayPalAddressPreset($sessionHandler);
|
||||
$this->mocks = [
|
||||
$sessionHandler,
|
||||
$testee,
|
||||
];
|
||||
}
|
||||
|
||||
return $this->mocks;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue