mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-06 13:44:42 +08:00
add unit tests
This commit is contained in:
parent
af492f57ac
commit
c9d5417435
8 changed files with 503 additions and 3 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1,5 +1,6 @@
|
|||
/vendor/
|
||||
/modules/
|
||||
/build/
|
||||
node_modules
|
||||
phpunit.xml
|
||||
.phpunit.result.cache
|
||||
|
|
|
@ -36,19 +36,27 @@
|
|||
"dhii/wp-containers": "^0.1.0@alpha"
|
||||
},
|
||||
"require-dev": {
|
||||
"inpsyde/php-coding-standards": "^1"
|
||||
"inpsyde/php-coding-standards": "^1",
|
||||
"phpunit/phpunit": "^9.1",
|
||||
"brain/monkey": "^2.4"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Inpsyde\\PayPalCommerce\\": "src/"
|
||||
}
|
||||
},
|
||||
"autoload-dev": {
|
||||
"psr-4": {
|
||||
"Inpsyde\\PayPalCommerce\\": "tests/PHPUnit/"
|
||||
}
|
||||
},
|
||||
"minimum-stability": "dev",
|
||||
"prefer-stable": true,
|
||||
"scripts": {
|
||||
"ci": [
|
||||
"vendor/bin/phpcs"
|
||||
]
|
||||
],
|
||||
"unit": "./vendor/bin/phpunit --coverage-html build/coverage-report"
|
||||
},
|
||||
"extra": {
|
||||
"installer-types": [
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<phpunit
|
||||
bootstrap="vendor/autoload.php"
|
||||
bootstrap="tests/PHPUnit/bootstrap.php"
|
||||
backupGlobals="false"
|
||||
colors="true"
|
||||
convertErrorsToExceptions="true"
|
||||
|
@ -10,6 +10,7 @@
|
|||
<filter>
|
||||
<whitelist processUncoveredFilesFromWhitelist="true">
|
||||
<directory suffix=".php">./src</directory>
|
||||
<directory suffix=".php">./modules.local/</directory>
|
||||
</whitelist>
|
||||
</filter>
|
||||
<testsuites>
|
||||
|
|
28
tests/PHPUnit/TestCase.php
Normal file
28
tests/PHPUnit/TestCase.php
Normal file
|
@ -0,0 +1,28 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Inpsyde\PayPalCommerce;
|
||||
|
||||
use function Brain\Monkey\setUp;
|
||||
use function Brain\Monkey\tearDown;
|
||||
use function Brain\Monkey\Functions\expect;
|
||||
use Mockery;
|
||||
|
||||
class TestCase extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
public function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
expect('__')->andReturnUsing(function (string $text) {
|
||||
return $text;
|
||||
});
|
||||
setUp();
|
||||
}
|
||||
|
||||
public function tearDown(): void
|
||||
{
|
||||
tearDown();
|
||||
Mockery::close();
|
||||
parent::tearDown();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,210 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Inpsyde\PayPalCommerce\WcGateway\Processor;
|
||||
|
||||
|
||||
use Inpsyde\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint;
|
||||
use Inpsyde\PayPalCommerce\ApiClient\Endpoint\PaymentsEndpoint;
|
||||
use Inpsyde\PayPalCommerce\ApiClient\Entity\Authorization;
|
||||
use Inpsyde\PayPalCommerce\ApiClient\Entity\AuthorizationStatus;
|
||||
use Inpsyde\PayPalCommerce\ApiClient\Entity\Order;
|
||||
use Inpsyde\PayPalCommerce\ApiClient\Entity\Payments;
|
||||
use Inpsyde\PayPalCommerce\ApiClient\Entity\PurchaseUnit;
|
||||
use Inpsyde\PayPalCommerce\ApiClient\Exception\RuntimeException;
|
||||
use Inpsyde\PayPalCommerce\TestCase;
|
||||
use Inpsyde\PayPalCommerce\WcGateway\Gateway\WcGateway;
|
||||
use Mockery;
|
||||
class AuthorizedPaymentsProcessorTest extends TestCase
|
||||
{
|
||||
|
||||
public function testDefault() {
|
||||
$orderId = 'abc';
|
||||
$authorizationId = 'def';
|
||||
$authorizationStatus = Mockery::mock(AuthorizationStatus::class);
|
||||
$authorizationStatus
|
||||
->shouldReceive('is')
|
||||
->with(AuthorizationStatus::CAPTURED)
|
||||
->andReturn(false);
|
||||
$authorizationStatus
|
||||
->shouldReceive('is')
|
||||
->with(AuthorizationStatus::CREATED)
|
||||
->andReturn(true);
|
||||
$authorization = Mockery::mock(Authorization::class);
|
||||
$authorization
|
||||
->shouldReceive('id')
|
||||
->andReturn($authorizationId);
|
||||
$authorization
|
||||
->shouldReceive('status')
|
||||
->andReturn($authorizationStatus);
|
||||
$payments = Mockery::mock(Payments::class);
|
||||
$payments
|
||||
->expects('authorizations')
|
||||
->andReturn([$authorization]);
|
||||
$purchaseUnit = Mockery::mock(PurchaseUnit::class);
|
||||
$purchaseUnit
|
||||
->expects('payments')
|
||||
->andReturn($payments);
|
||||
$order = Mockery::mock(Order::class);
|
||||
$order
|
||||
->expects('purchaseUnits')
|
||||
->andReturn([$purchaseUnit]);
|
||||
$orderEndpoint = Mockery::mock(OrderEndpoint::class);
|
||||
$orderEndpoint
|
||||
->expects('order')
|
||||
->with($orderId)
|
||||
->andReturn($order);
|
||||
$paymentsEndpoint = Mockery::mock(PaymentsEndpoint::class);
|
||||
$paymentsEndpoint
|
||||
->expects('capture')
|
||||
->with($authorizationId)
|
||||
->andReturn($authorization);
|
||||
$testee = new AuthorizedPaymentsProcessor($orderEndpoint, $paymentsEndpoint);
|
||||
|
||||
$wcOrder = Mockery::mock(\WC_Order::class);
|
||||
$wcOrder
|
||||
->expects('get_meta')
|
||||
->with(WcGateway::ORDER_ID_META_KEY)
|
||||
->andReturn($orderId);
|
||||
$result = $testee->process($wcOrder);
|
||||
$this->assertEquals(AuthorizedPaymentsProcessor::SUCCESSFUL, $result);
|
||||
}
|
||||
|
||||
public function testInaccessible() {
|
||||
$orderId = 'abc';
|
||||
$orderEndpoint = Mockery::mock(OrderEndpoint::class);
|
||||
$orderEndpoint
|
||||
->expects('order')
|
||||
->with($orderId)
|
||||
->andThrow(RuntimeException::class);
|
||||
$paymentsEndpoint = Mockery::mock(PaymentsEndpoint::class);
|
||||
$testee = new AuthorizedPaymentsProcessor($orderEndpoint, $paymentsEndpoint);
|
||||
|
||||
$wcOrder = Mockery::mock(\WC_Order::class);
|
||||
$wcOrder
|
||||
->expects('get_meta')
|
||||
->with(WcGateway::ORDER_ID_META_KEY)
|
||||
->andReturn($orderId);
|
||||
$result = $testee->process($wcOrder);
|
||||
$this->assertEquals(AuthorizedPaymentsProcessor::INACCESSIBLE, $result);
|
||||
}
|
||||
|
||||
public function testNotFound() {
|
||||
$orderId = 'abc';
|
||||
$orderEndpoint = Mockery::mock(OrderEndpoint::class);
|
||||
$orderEndpoint
|
||||
->expects('order')
|
||||
->with($orderId)
|
||||
->andThrow(new RuntimeException("text", 404));
|
||||
$paymentsEndpoint = Mockery::mock(PaymentsEndpoint::class);
|
||||
$testee = new AuthorizedPaymentsProcessor($orderEndpoint, $paymentsEndpoint);
|
||||
|
||||
$wcOrder = Mockery::mock(\WC_Order::class);
|
||||
$wcOrder
|
||||
->expects('get_meta')
|
||||
->with(WcGateway::ORDER_ID_META_KEY)
|
||||
->andReturn($orderId);
|
||||
$result = $testee->process($wcOrder);
|
||||
$this->assertEquals(AuthorizedPaymentsProcessor::NOT_FOUND, $result);
|
||||
}
|
||||
|
||||
public function testCaptureFails() {
|
||||
$orderId = 'abc';
|
||||
$authorizationId = 'def';
|
||||
$authorizationStatus = Mockery::mock(AuthorizationStatus::class);
|
||||
$authorizationStatus
|
||||
->shouldReceive('is')
|
||||
->with(AuthorizationStatus::CAPTURED)
|
||||
->andReturn(false);
|
||||
$authorizationStatus
|
||||
->shouldReceive('is')
|
||||
->with(AuthorizationStatus::CREATED)
|
||||
->andReturn(true);
|
||||
$authorization = Mockery::mock(Authorization::class);
|
||||
$authorization
|
||||
->shouldReceive('id')
|
||||
->andReturn($authorizationId);
|
||||
$authorization
|
||||
->shouldReceive('status')
|
||||
->andReturn($authorizationStatus);
|
||||
$payments = Mockery::mock(Payments::class);
|
||||
$payments
|
||||
->expects('authorizations')
|
||||
->andReturn([$authorization]);
|
||||
$purchaseUnit = Mockery::mock(PurchaseUnit::class);
|
||||
$purchaseUnit
|
||||
->expects('payments')
|
||||
->andReturn($payments);
|
||||
$order = Mockery::mock(Order::class);
|
||||
$order
|
||||
->expects('purchaseUnits')
|
||||
->andReturn([$purchaseUnit]);
|
||||
$orderEndpoint = Mockery::mock(OrderEndpoint::class);
|
||||
$orderEndpoint
|
||||
->expects('order')
|
||||
->with($orderId)
|
||||
->andReturn($order);
|
||||
$paymentsEndpoint = Mockery::mock(PaymentsEndpoint::class);
|
||||
$paymentsEndpoint
|
||||
->expects('capture')
|
||||
->with($authorizationId)
|
||||
->andThrow(RuntimeException::class);
|
||||
$testee = new AuthorizedPaymentsProcessor($orderEndpoint, $paymentsEndpoint);
|
||||
|
||||
$wcOrder = Mockery::mock(\WC_Order::class);
|
||||
$wcOrder
|
||||
->expects('get_meta')
|
||||
->with(WcGateway::ORDER_ID_META_KEY)
|
||||
->andReturn($orderId);
|
||||
$result = $testee->process($wcOrder);
|
||||
$this->assertEquals(AuthorizedPaymentsProcessor::FAILED, $result);
|
||||
}
|
||||
|
||||
public function testAllAreCaptured() {
|
||||
$orderId = 'abc';
|
||||
$authorizationId = 'def';
|
||||
$authorizationStatus = Mockery::mock(AuthorizationStatus::class);
|
||||
$authorizationStatus
|
||||
->shouldReceive('is')
|
||||
->with(AuthorizationStatus::CAPTURED)
|
||||
->andReturn(true);
|
||||
$authorizationStatus
|
||||
->shouldReceive('is')
|
||||
->with(AuthorizationStatus::CREATED)
|
||||
->andReturn(true);
|
||||
$authorization = Mockery::mock(Authorization::class);
|
||||
$authorization
|
||||
->shouldReceive('id')
|
||||
->andReturn($authorizationId);
|
||||
$authorization
|
||||
->shouldReceive('status')
|
||||
->andReturn($authorizationStatus);
|
||||
$payments = Mockery::mock(Payments::class);
|
||||
$payments
|
||||
->expects('authorizations')
|
||||
->andReturn([$authorization]);
|
||||
$purchaseUnit = Mockery::mock(PurchaseUnit::class);
|
||||
$purchaseUnit
|
||||
->expects('payments')
|
||||
->andReturn($payments);
|
||||
$order = Mockery::mock(Order::class);
|
||||
$order
|
||||
->expects('purchaseUnits')
|
||||
->andReturn([$purchaseUnit]);
|
||||
$orderEndpoint = Mockery::mock(OrderEndpoint::class);
|
||||
$orderEndpoint
|
||||
->expects('order')
|
||||
->with($orderId)
|
||||
->andReturn($order);
|
||||
$paymentsEndpoint = Mockery::mock(PaymentsEndpoint::class);
|
||||
$testee = new AuthorizedPaymentsProcessor($orderEndpoint, $paymentsEndpoint);
|
||||
|
||||
$wcOrder = Mockery::mock(\WC_Order::class);
|
||||
$wcOrder
|
||||
->expects('get_meta')
|
||||
->with(WcGateway::ORDER_ID_META_KEY)
|
||||
->andReturn($orderId);
|
||||
$result = $testee->process($wcOrder);
|
||||
$this->assertEquals(AuthorizedPaymentsProcessor::ALREADY_CAPTURED, $result);
|
||||
}
|
||||
}
|
240
tests/PHPUnit/WcGateway/Processor/OrderProcessorTest.php
Normal file
240
tests/PHPUnit/WcGateway/Processor/OrderProcessorTest.php
Normal file
|
@ -0,0 +1,240 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Inpsyde\PayPalCommerce\WcGateway\Processor;
|
||||
|
||||
|
||||
use Inpsyde\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint;
|
||||
use Inpsyde\PayPalCommerce\ApiClient\Endpoint\PaymentsEndpoint;
|
||||
use Inpsyde\PayPalCommerce\ApiClient\Entity\Order;
|
||||
use Inpsyde\PayPalCommerce\ApiClient\Entity\OrderStatus;
|
||||
use Inpsyde\PayPalCommerce\ApiClient\Factory\OrderFactory;
|
||||
use Inpsyde\PayPalCommerce\ApiClient\Repository\CartRepository;
|
||||
use Inpsyde\PayPalCommerce\Session\SessionHandler;
|
||||
use Inpsyde\PayPalCommerce\TestCase;
|
||||
use Inpsyde\PayPalCommerce\WcGateway\Gateway\WcGateway;
|
||||
use Mockery;
|
||||
|
||||
class OrderProcessorTest extends TestCase
|
||||
{
|
||||
|
||||
public function testAuthorize() {
|
||||
$wcOrder = Mockery::mock(\WC_Order::class);
|
||||
$orderStatus = Mockery::mock(OrderStatus::class);
|
||||
$orderStatus
|
||||
->expects('is')
|
||||
->with(OrderStatus::APPROVED)
|
||||
->andReturn(true);
|
||||
$orderStatus
|
||||
->expects('is')
|
||||
->with(OrderStatus::COMPLETED)
|
||||
->andReturn(true);
|
||||
$orderId = 'abc';
|
||||
$orderIntent = 'AUTHORIZE';
|
||||
$currentOrder = Mockery::mock(Order::class);
|
||||
$currentOrder
|
||||
->expects('id')
|
||||
->andReturn($orderId);
|
||||
$currentOrder
|
||||
->shouldReceive('intent')
|
||||
->andReturn($orderIntent);
|
||||
$currentOrder
|
||||
->shouldReceive('status')
|
||||
->andReturn($orderStatus);
|
||||
$sessionHandler = Mockery::mock(SessionHandler::class);
|
||||
$sessionHandler
|
||||
->expects('order')
|
||||
->andReturn($currentOrder);
|
||||
$sessionHandler
|
||||
->expects('destroySessionData');
|
||||
$cartRepository = Mockery::mock(CartRepository::class);
|
||||
$orderEndpoint = Mockery::mock(OrderEndpoint::class);
|
||||
$orderEndpoint
|
||||
->expects('patchOrderWith')
|
||||
->with($currentOrder, $currentOrder)
|
||||
->andReturn($currentOrder);
|
||||
$orderEndpoint
|
||||
->expects('authorize')
|
||||
->with($currentOrder)
|
||||
->andReturn($currentOrder);
|
||||
$paymentsEndpoint = Mockery::mock(PaymentsEndpoint::class);
|
||||
$orderFactory = Mockery::mock(OrderFactory::class);
|
||||
$orderFactory
|
||||
->expects('fromWcOrder')
|
||||
->with($wcOrder, $currentOrder)
|
||||
->andReturn($currentOrder);
|
||||
|
||||
$testee = new OrderProcessor(
|
||||
$sessionHandler,
|
||||
$cartRepository,
|
||||
$orderEndpoint,
|
||||
$paymentsEndpoint,
|
||||
$orderFactory
|
||||
);
|
||||
|
||||
$cart = Mockery::mock(\WC_Cart::class);
|
||||
$cart
|
||||
->expects('empty_cart');
|
||||
$woocommerce = (object) ['cart' => $cart];
|
||||
|
||||
$wcOrder
|
||||
->expects('update_meta_data')
|
||||
->with(
|
||||
WcGateway::ORDER_ID_META_KEY,
|
||||
$orderId
|
||||
);
|
||||
$wcOrder
|
||||
->expects('update_meta_data')
|
||||
->with(
|
||||
WcGateway::CAPTURED_META_KEY,
|
||||
'false'
|
||||
);
|
||||
$wcOrder
|
||||
->expects('update_meta_data')
|
||||
->with(
|
||||
WcGateway::INTENT_META_KEY,
|
||||
$orderIntent
|
||||
);
|
||||
$wcOrder
|
||||
->expects('update_status')
|
||||
->with('on-hold', 'Awaiting payment.');
|
||||
$this->assertTrue($testee->process($wcOrder, $woocommerce));
|
||||
}
|
||||
|
||||
public function testCapture() {
|
||||
$wcOrder = Mockery::mock(\WC_Order::class);
|
||||
$orderStatus = Mockery::mock(OrderStatus::class);
|
||||
$orderStatus
|
||||
->expects('is')
|
||||
->with(OrderStatus::APPROVED)
|
||||
->andReturn(true);
|
||||
$orderStatus
|
||||
->expects('is')
|
||||
->with(OrderStatus::COMPLETED)
|
||||
->andReturn(true);
|
||||
$orderId = 'abc';
|
||||
$orderIntent = 'CAPTURE';
|
||||
$currentOrder = Mockery::mock(Order::class);
|
||||
$currentOrder
|
||||
->expects('id')
|
||||
->andReturn($orderId);
|
||||
$currentOrder
|
||||
->shouldReceive('intent')
|
||||
->andReturn($orderIntent);
|
||||
$currentOrder
|
||||
->shouldReceive('status')
|
||||
->andReturn($orderStatus);
|
||||
$sessionHandler = Mockery::mock(SessionHandler::class);
|
||||
$sessionHandler
|
||||
->expects('order')
|
||||
->andReturn($currentOrder);
|
||||
$sessionHandler
|
||||
->expects('destroySessionData');
|
||||
$cartRepository = Mockery::mock(CartRepository::class);
|
||||
$orderEndpoint = Mockery::mock(OrderEndpoint::class);
|
||||
$orderEndpoint
|
||||
->expects('patchOrderWith')
|
||||
->with($currentOrder, $currentOrder)
|
||||
->andReturn($currentOrder);
|
||||
$orderEndpoint
|
||||
->expects('capture')
|
||||
->with($currentOrder)
|
||||
->andReturn($currentOrder);
|
||||
$paymentsEndpoint = Mockery::mock(PaymentsEndpoint::class);
|
||||
$orderFactory = Mockery::mock(OrderFactory::class);
|
||||
$orderFactory
|
||||
->expects('fromWcOrder')
|
||||
->with($wcOrder, $currentOrder)
|
||||
->andReturn($currentOrder);
|
||||
|
||||
$testee = new OrderProcessor(
|
||||
$sessionHandler,
|
||||
$cartRepository,
|
||||
$orderEndpoint,
|
||||
$paymentsEndpoint,
|
||||
$orderFactory
|
||||
);
|
||||
|
||||
$cart = Mockery::mock(\WC_Cart::class);
|
||||
$cart
|
||||
->expects('empty_cart');
|
||||
$woocommerce = (object) ['cart' => $cart];
|
||||
|
||||
$wcOrder
|
||||
->expects('update_meta_data')
|
||||
->with(
|
||||
WcGateway::ORDER_ID_META_KEY,
|
||||
$orderId
|
||||
);
|
||||
$wcOrder
|
||||
->expects('update_meta_data')
|
||||
->with(
|
||||
WcGateway::INTENT_META_KEY,
|
||||
$orderIntent
|
||||
);
|
||||
$wcOrder
|
||||
->expects('update_status')
|
||||
->with('on-hold', 'Awaiting payment.');
|
||||
$wcOrder
|
||||
->expects('update_status')
|
||||
->with('processing', 'Payment received.');
|
||||
$this->assertTrue($testee->process($wcOrder, $woocommerce));
|
||||
}
|
||||
|
||||
public function testError() {
|
||||
$wcOrder = Mockery::mock(\WC_Order::class);
|
||||
$orderStatus = Mockery::mock(OrderStatus::class);
|
||||
$orderStatus
|
||||
->expects('is')
|
||||
->with(OrderStatus::APPROVED)
|
||||
->andReturn(false);
|
||||
$orderId = 'abc';
|
||||
$orderIntent = 'CAPTURE';
|
||||
$currentOrder = Mockery::mock(Order::class);
|
||||
$currentOrder
|
||||
->expects('id')
|
||||
->andReturn($orderId);
|
||||
$currentOrder
|
||||
->shouldReceive('intent')
|
||||
->andReturn($orderIntent);
|
||||
$currentOrder
|
||||
->shouldReceive('status')
|
||||
->andReturn($orderStatus);
|
||||
$sessionHandler = Mockery::mock(SessionHandler::class);
|
||||
$sessionHandler
|
||||
->expects('order')
|
||||
->andReturn($currentOrder);
|
||||
$cartRepository = Mockery::mock(CartRepository::class);
|
||||
$orderEndpoint = Mockery::mock(OrderEndpoint::class);
|
||||
$paymentsEndpoint = Mockery::mock(PaymentsEndpoint::class);
|
||||
$orderFactory = Mockery::mock(OrderFactory::class);
|
||||
|
||||
$testee = new OrderProcessor(
|
||||
$sessionHandler,
|
||||
$cartRepository,
|
||||
$orderEndpoint,
|
||||
$paymentsEndpoint,
|
||||
$orderFactory
|
||||
);
|
||||
|
||||
$cart = Mockery::mock(\WC_Cart::class);
|
||||
$woocommerce = (object) ['cart' => $cart];
|
||||
|
||||
$wcOrder
|
||||
->expects('update_meta_data')
|
||||
->with(
|
||||
WcGateway::ORDER_ID_META_KEY,
|
||||
$orderId
|
||||
);
|
||||
$wcOrder
|
||||
->expects('update_meta_data')
|
||||
->with(
|
||||
WcGateway::INTENT_META_KEY,
|
||||
$orderIntent
|
||||
);
|
||||
$this->assertFalse($testee->process($wcOrder, $woocommerce));
|
||||
$this->assertNotEmpty($testee->lastError());
|
||||
}
|
||||
|
||||
|
||||
}
|
5
tests/PHPUnit/bootstrap.php
Normal file
5
tests/PHPUnit/bootstrap.php
Normal file
|
@ -0,0 +1,5 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
require_once __DIR__ . '/../../vendor/autoload.php';
|
||||
require_once __DIR__ . '/../stubs/WC_Payment_Gateway.php';
|
7
tests/stubs/WC_Payment_Gateway.php
Normal file
7
tests/stubs/WC_Payment_Gateway.php
Normal file
|
@ -0,0 +1,7 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
class WC_Payment_Gateway
|
||||
{
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue