Merge branch 'master' into feature/create-order-intent-authorize

This commit is contained in:
Mészáros Róbert 2020-04-15 17:27:25 +03:00
commit 8376b2e5e7
4 changed files with 289 additions and 3 deletions

View file

@ -90,8 +90,7 @@ class Order
'id' => $this->id(),
'intent' => $this->intent(),
'status' => $this->status()->name(),
'purchase_units' => array_map(
function (PurchaseUnit $unit) : array {
'purchase_units' => array_map(function (PurchaseUnit $unit) : array {
return $unit->toArray();
},
$this->purchaseUnits()

View file

@ -40,7 +40,7 @@ class AddressFactory
public function fromPayPalRequest(\stdClass $data) : Address
{
if (! isset($data->country_code)) {
new RuntimeException(__('No country given for address.', 'woocommerce-paypal-commerce-gateway'));
throw new RuntimeException(__('No country given for address.', 'woocommerce-paypal-commerce-gateway'));
}
return new Address(
$data->country_code,

View file

@ -0,0 +1,86 @@
<?php
declare(strict_types=1);
namespace Inpsyde\PayPalCommerce\ApiClient\Endpoint;
use Inpsyde\PayPalCommerce\ApiClient\Entity\Order;
use Inpsyde\PayPalCommerce\ApiClient\Entity\OrderStatus;
use Inpsyde\PayPalCommerce\ApiClient\Entity\Payer;
use Inpsyde\PayPalCommerce\ApiClient\Entity\PurchaseUnit;
use Inpsyde\PayPalCommerce\ApiClient\TestCase;
use Mockery;
class OrderTest extends TestCase
{
public function testOrder() {
$id = 'id';
$createTime = new \DateTime();
$updateTime = new \DateTime();
$unit = Mockery::mock(PurchaseUnit::class);
$unit->expects('toArray')->andReturn([1]);
$status = Mockery::mock(OrderStatus::class);
$status->expects('name')->andReturn('CREATED');
$payer = Mockery::mock(Payer::class);
$payer
->expects('toArray')->andReturn(['payer']);
$intent = 'AUTHORIZE';
$testee = new Order(
$id,
[$unit],
$status,
$payer,
$intent,
$createTime,
$updateTime
);
$this->assertEquals($id, $testee->id());
$this->assertEquals($createTime, $testee->createTime());
$this->assertEquals($updateTime, $testee->updateTime());
$this->assertEquals([$unit], $testee->purchaseUnits());
$this->assertEquals($payer, $testee->payer());
$this->assertEquals($intent, $testee->intent());
$this->assertEquals($status, $testee->status());
$expected = [
'id' => $id,
'intent' => $intent,
'status' => 'CREATED',
'purchase_units' => [
[1],
],
'create_time' => $createTime->format(\DateTimeInterface::ISO8601),
'update_time' => $updateTime->format(\DateTimeInterface::ISO8601),
'payer' => ['payer'],
];
$this->assertEquals($expected, $testee->toArray());
}
public function testOrderNoDatesOrPayer() {
$id = 'id';
$unit = Mockery::mock(PurchaseUnit::class);
$unit->expects('toArray')->andReturn([1]);
$status = Mockery::mock(OrderStatus::class);
$status->expects('name')->andReturn('CREATED');
$testee = new Order(
$id,
[$unit],
$status
);
$this->assertEquals(null, $testee->createTime());
$this->assertEquals(null, $testee->updateTime());
$this->assertEquals(null, $testee->payer());
$this->assertEquals('CAPTURE', $testee->intent());
$array = $testee->toArray();
$this->assertFalse(array_key_exists('payer', $array));
$this->assertFalse(array_key_exists('create_time', $array));
$this->assertFalse(array_key_exists('update_time', $array));
}
}

View file

@ -0,0 +1,201 @@
<?php
declare(strict_types=1);
namespace Inpsyde\PayPalCommerce\ApiClient\Factory;
use Inpsyde\PayPalCommerce\ApiClient\Exception\RuntimeException;
use Inpsyde\PayPalCommerce\ApiClient\TestCase;
use Mockery;
class AddressFactoryTest extends TestCase
{
public function testFromWcCustomer() {
$testee = new AddressFactory();
$customer = Mockery::mock(\WC_Customer::class);
$customer
->expects('get_shipping_country')
->andReturn('shipping_country');
$customer
->expects('get_shipping_address_1')
->andReturn('shipping_address_1');
$customer
->expects('get_shipping_address_2')
->andReturn('shipping_address_2');
$customer
->expects('get_shipping_state')
->andReturn('shipping_state');
$customer
->expects('get_shipping_city')
->andReturn('shipping_city');
$customer
->expects('get_shipping_postcode')
->andReturn('shipping_postcode');
$result = $testee->fromWcCustomer($customer);
$this->assertEquals('shipping_country', $result->countryCode());
$this->assertEquals('shipping_address_1', $result->addressLine1());
$this->assertEquals('shipping_address_2', $result->addressLine2());
$this->assertEquals('shipping_state', $result->adminArea1());
$this->assertEquals('shipping_city', $result->adminArea2());
$this->assertEquals('shipping_postcode', $result->postalCode());
}
public function testFromWcCustomersBillingAddress() {
$testee = new AddressFactory();
$customer = Mockery::mock(\WC_Customer::class);
$customer
->expects('get_billing_country')
->andReturn('billing_country');
$customer
->expects('get_billing_address_1')
->andReturn('billing_address_1');
$customer
->expects('get_billing_address_2')
->andReturn('billing_address_2');
$customer
->expects('get_billing_state')
->andReturn('billing_state');
$customer
->expects('get_billing_city')
->andReturn('billing_city');
$customer
->expects('get_billing_postcode')
->andReturn('billing_postcode');
$result = $testee->fromWcCustomer($customer, 'billing');
$this->assertEquals('billing_country', $result->countryCode());
$this->assertEquals('billing_address_1', $result->addressLine1());
$this->assertEquals('billing_address_2', $result->addressLine2());
$this->assertEquals('billing_state', $result->adminArea1());
$this->assertEquals('billing_city', $result->adminArea2());
$this->assertEquals('billing_postcode', $result->postalCode());
}
public function testFromWcOrder() {
$testee = new AddressFactory();
$order = Mockery::mock(\WC_Order::class);
$order
->expects('get_shipping_country')
->andReturn('shipping_country');
$order
->expects('get_shipping_address_1')
->andReturn('shipping_address_1');
$order
->expects('get_shipping_address_2')
->andReturn('shipping_address_2');
$order
->expects('get_shipping_state')
->andReturn('shipping_state');
$order
->expects('get_shipping_city')
->andReturn('shipping_city');
$order
->expects('get_shipping_postcode')
->andReturn('shipping_postcode');
$result = $testee->fromWcOrder($order);
$this->assertEquals('shipping_country', $result->countryCode());
$this->assertEquals('shipping_address_1', $result->addressLine1());
$this->assertEquals('shipping_address_2', $result->addressLine2());
$this->assertEquals('shipping_state', $result->adminArea1());
$this->assertEquals('shipping_city', $result->adminArea2());
$this->assertEquals('shipping_postcode', $result->postalCode());
}
/**
* @dataProvider dataFromPayPalRequest
*/
public function testFromPayPalRequest($data) {
$testee = new AddressFactory();
$result = $testee->fromPayPalRequest($data);
$expectedAddressLine1 = (isset($data->address_line_1)) ? $data->address_line_1 : '';
$expectedAddressLine2 = (isset($data->address_line_2)) ? $data->address_line_2 : '';
$expectedAdminArea1 = (isset($data->admin_area_1)) ? $data->admin_area_1 : '';
$expectedAdminArea2 = (isset($data->admin_area_2)) ? $data->admin_area_2 : '';
$expectedPostalCode = (isset($data->postal_code)) ? $data->postal_code : '';
$this->assertEquals($data->country_code, $result->countryCode());
$this->assertEquals($expectedAddressLine1, $result->addressLine1());
$this->assertEquals($expectedAddressLine2, $result->addressLine2());
$this->assertEquals($expectedAdminArea1, $result->adminArea1());
$this->assertEquals($expectedAdminArea2, $result->adminArea2());
$this->assertEquals($expectedPostalCode, $result->postalCode());
}
public function testFromPayPalRequestThrowsError() {
$testee = new AddressFactory();
$data = (object) [
'address_line_1' => 'shipping_address_1',
'address_line_2' => 'shipping_address_2',
'admin_area_1' => 'shipping_admin_area_1',
'admin_area_2' => 'shipping_admin_area_2',
'postal_code' => 'shipping_postcode',
];
$this->expectException(RuntimeException::class);
$testee->fromPayPalRequest($data);
}
public function dataFromPayPalRequest() : array {
return [
'default' => [
(object) [
'country_code' => 'shipping_country',
'address_line_1' => 'shipping_address_1',
'address_line_2' => 'shipping_address_2',
'admin_area_1' => 'shipping_admin_area_1',
'admin_area_2' => 'shipping_admin_area_2',
'postal_code' => 'shipping_postcode',
]
],
'no_admin_area_2' => [
(object) [
'country_code' => 'shipping_country',
'address_line_1' => 'shipping_address_1',
'address_line_2' => 'shipping_address_2',
'admin_area_1' => 'shipping_admin_area_1',
'postal_code' => 'shipping_postcode',
]
],
'no_postal_code' => [
(object) [
'country_code' => 'shipping_country',
'address_line_1' => 'shipping_address_1',
'address_line_2' => 'shipping_address_2',
'admin_area_1' => 'shipping_admin_area_1',
'admin_area_2' => 'shipping_admin_area_2',
]
],
'no_admin_area_1' => [
(object) [
'country_code' => 'shipping_country',
'address_line_1' => 'shipping_address_1',
'address_line_2' => 'shipping_address_2',
'admin_area_2' => 'shipping_admin_area_2',
'postal_code' => 'shipping_postcode',
]
],
'no_address_line_1' => [
(object) [
'country_code' => 'shipping_country',
'address_line_2' => 'shipping_address_2',
'admin_area_1' => 'shipping_admin_area_1',
'admin_area_2' => 'shipping_admin_area_2',
'postal_code' => 'shipping_postcode',
]
],
'no_address_line_2' => [
(object) [
'country_code' => 'shipping_country',
'address_line_1' => 'shipping_address_1',
'admin_area_1' => 'shipping_admin_area_1',
'admin_area_2' => 'shipping_admin_area_2',
'postal_code' => 'shipping_postcode',
]
],
];
}
}