woocommerce-paypal-payments/tests/PHPUnit/ApiClient/Factory/OrderFactoryTest.php

213 lines
7.5 KiB
PHP

<?php
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\ApiClient\Factory;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Order;
use WooCommerce\PayPalCommerce\ApiClient\Entity\OrderStatus;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Payer;
use WooCommerce\PayPalCommerce\ApiClient\Entity\PurchaseUnit;
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
use WooCommerce\PayPalCommerce\TestCase;
use Mockery;
class OrderFactoryTest extends TestCase
{
public function testFromWcOrder()
{
$createTime = new \DateTime();
$updateTime = new \DateTime();
$payer = Mockery::mock(Payer::class);
$status = Mockery::mock(OrderStatus::class);
$order = Mockery::mock(Order::class);
$order->expects('id')->andReturn('id');
$order->expects('status')->andReturn($status);
$order->expects('payer')->andReturn($payer);
$order->expects('intent')->andReturn('intent');
$order->expects('create_time')->andReturn($createTime);
$order->expects('update_time')->andReturn($updateTime);
$order->expects('payment_source')->andReturnNull();
$order->expects('links')->andReturnNull();
$wcOrder = Mockery::mock(\WC_Order::class);
$purchaseUnitFactory = Mockery::mock(PurchaseUnitFactory::class);
$purchaseUnit = Mockery::mock(PurchaseUnit::class);
$purchaseUnitFactory->expects('from_wc_order')->with($wcOrder)->andReturn($purchaseUnit);
$payerFactory = Mockery::mock(PayerFactory::class);
$testee = new OrderFactory(
$purchaseUnitFactory,
$payerFactory
);
$result = $testee->from_wc_order($wcOrder, $order);
$resultPurchaseUnit = current($result->purchase_units());
$this->assertEquals($purchaseUnit, $resultPurchaseUnit);
}
/**
* @dataProvider dataForTestFromPayPalResponseTest
* @param $orderData
*/
public function testFromPayPalResponse($orderData)
{
$purchaseUnitFactory = Mockery::mock(PurchaseUnitFactory::class);
if (count($orderData->purchase_units)) {
$purchaseUnitFactory
->expects('from_paypal_response')
->times(count($orderData->purchase_units))
->andReturn(Mockery::mock(PurchaseUnit::class));
}
$payerFactory = Mockery::mock(PayerFactory::class);
if (isset($orderData->payer)) {
$payerFactory
->expects('from_paypal_response')
->andReturn(Mockery::mock(Payer::class));
}
$testee = new OrderFactory(
$purchaseUnitFactory,
$payerFactory
);
$order = $testee->from_paypal_response($orderData);
$this->assertCount(count($orderData->purchase_units), $order->purchase_units());
$this->assertEquals($orderData->id, $order->id());
$this->assertEquals($orderData->status, $order->status()->name());
$this->assertEquals($orderData->intent, $order->intent());
if (! isset($orderData->create_time)) {
$this->assertNull($order->create_time());
} else {
$this->assertEquals($orderData->create_time, $order->create_time()->format(\DateTime::ISO8601));
}
if (! isset($orderData->payer)) {
$this->assertNull($order->payer());
} else {
$this->assertInstanceOf(Payer::class, $order->payer());
}
if (! isset($orderData->update_time)) {
$this->assertNull($order->update_time());
} else {
$this->assertEquals($orderData->update_time, $order->update_time()->format(\DateTime::ISO8601));
}
if ( isset($orderData->links) ) {
$this->assertEquals($orderData->links, $order->links());
} else {
$this->assertNull($order->links());
}
}
public function dataForTestFromPayPalResponseTest() : array
{
return [
'default' => [
(object) [
'id' => 'id',
'purchase_units' => [new \stdClass(), new \stdClass()],
'status' => OrderStatus::APPROVED,
'intent' => 'CAPTURE',
'create_time' => '2005-08-15T15:52:01+0000',
'update_time' => '2005-09-15T15:52:01+0000',
'payer' => new \stdClass(),
],
],
'no_update_time' => [
(object) [
'id' => 'id',
'purchase_units' => [new \stdClass(), new \stdClass()],
'status' => OrderStatus::APPROVED,
'intent' => 'CAPTURE',
'create_time' => '2005-08-15T15:52:01+0000',
'payer' => new \stdClass(),
],
],
'no_create_time' => [
(object) [
'id' => 'id',
'purchase_units' => [new \stdClass(), new \stdClass()],
'status' => OrderStatus::APPROVED,
'intent' => 'CAPTURE',
'update_time' => '2005-09-15T15:52:01+0000',
'payer' => new \stdClass(),
],
],
'no_payer' => [
(object) [
'id' => 'id',
'purchase_units' => [new \stdClass(), new \stdClass()],
'status' => OrderStatus::APPROVED,
'intent' => 'CAPTURE',
'create_time' => '2005-08-15T15:52:01+0000',
'update_time' => '2005-09-15T15:52:01+0000',
],
],
'with_links' => [
(object) [
'id' => 'id',
'purchase_units' => [new \stdClass(), new \stdClass()],
'status' => OrderStatus::PAYER_ACTION_REQUIRED,
'intent' => 'CAPTURE',
'create_time' => '2005-08-15T15:52:01+0000',
'update_time' => '2005-09-15T15:52:01+0000',
'payer' => new \stdClass(),
'links' => [
(object) ['rel' => 'payer-action', 'href' => 'https://example.com/3ds']
],
],
],
];
}
/**
* @dataProvider dataForTestFromPayPalResponseExceptionsTest
* @param $orderData
*/
public function testFromPayPalResponseExceptions($orderData)
{
$purchaseUnitFactory = Mockery::mock(PurchaseUnitFactory::class);
$payerFactory = Mockery::mock(PayerFactory::class);
$testee = new OrderFactory(
$purchaseUnitFactory,
$payerFactory
);
$this->expectException(RuntimeException::class);
$testee->from_paypal_response($orderData);
}
public function dataForTestFromPayPalResponseExceptionsTest() : array
{
return [
'no_id' => [
(object) [
'purchase_units' => [],
'status' => '',
'intent' => '',
],
],
'no_purchase_units' => [
(object) [
'id' => '',
'status' => '',
'intent' => '',
],
],
'purchase_units_is_not_array' => [
(object) [
'id' => '',
'purchase_units' => 1,
'status' => '',
'intent' => '',
],
],
'no_intent' => [
(object) [
'id' => '',
'purchase_units' => [],
'status' => '',
],
],
];
}
}