Merge branch 'trunk' into pcp-157-blocks

This commit is contained in:
Alex P 2023-03-22 23:23:06 +02:00
commit 2a0fa49800
No known key found for this signature in database
GPG key ID: 54487A734A204D71
37 changed files with 1355 additions and 302 deletions

View file

@ -0,0 +1,83 @@
<?php
namespace WooCommerce\PayPalCommerce\Api;
use InvalidArgumentException;
use Mockery;
use RuntimeException;
use WC_Order;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Order;
use WooCommerce\PayPalCommerce\ModularTestCase;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
class GetOrderTest extends ModularTestCase
{
private $orderEndpoint;
public function setUp(): void {
parent::setUp();
$this->orderEndpoint = Mockery::mock(OrderEndpoint::class);
$this->bootstrapModule([
'api.endpoint.order' => function () {
return $this->orderEndpoint;
},
]);
}
public function testSuccess(): void {
$this->orderEndpoint
->expects('order')
->with('123abc')
->andReturn(Mockery::mock(Order::class))
->once();
ppcp_get_paypal_order('123abc');
}
public function testSuccessWithOrder(): void {
$wcOrder = Mockery::mock(WC_Order::class);
$wcOrder->expects('get_meta')
->with(PayPalGateway::ORDER_ID_META_KEY)
->andReturn('123abc');
$this->orderEndpoint
->expects('order')
->with('123abc')
->andReturn(Mockery::mock(Order::class))
->once();
ppcp_get_paypal_order($wcOrder);
}
public function testOrderWithoutId(): void {
$wcOrder = Mockery::mock(WC_Order::class);
$wcOrder->expects('get_meta')
->with(PayPalGateway::ORDER_ID_META_KEY)
->andReturn(false);
$this->expectException(InvalidArgumentException::class);
ppcp_get_paypal_order($wcOrder);
}
public function testFailure(): void {
$this->orderEndpoint
->expects('order')
->with('123abc')
->andThrow(new RuntimeException())
->once();
$this->expectException(RuntimeException::class);
ppcp_get_paypal_order('123abc');
}
public function testInvalidId(): void {
$this->expectException(InvalidArgumentException::class);
ppcp_get_paypal_order(123);
}
}

View file

@ -0,0 +1,92 @@
<?php
namespace WooCommerce\PayPalCommerce\Api;
use InvalidArgumentException;
use Mockery;
use RuntimeException;
use WC_Order;
use WooCommerce\PayPalCommerce\ModularTestCase;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
use WooCommerce\PayPalCommerce\WcGateway\Processor\AuthorizedPaymentsProcessor;
class OrderCaptureTest extends ModularTestCase
{
private $authorizedPaymentProcessor;
public function setUp(): void {
parent::setUp();
$this->authorizedPaymentProcessor = Mockery::mock(AuthorizedPaymentsProcessor::class);
$this->bootstrapModule([
'wcgateway.processor.authorized-payments' => function () {
return $this->authorizedPaymentProcessor;
},
]);
}
public function testSuccess(): void {
$wcOrder = Mockery::mock(WC_Order::class);
$wcOrder->expects('get_meta')
->with(PayPalGateway::INTENT_META_KEY)
->andReturn('AUTHORIZE');
$wcOrder->expects('get_meta')
->with(AuthorizedPaymentsProcessor::CAPTURED_META_KEY)
->andReturn(false);
$this->authorizedPaymentProcessor
->expects('capture_authorized_payment')
->andReturnTrue()
->once();
ppcp_capture_order($wcOrder);
}
public function testFailure(): void {
$wcOrder = Mockery::mock(WC_Order::class);
$wcOrder->expects('get_meta')
->with(PayPalGateway::INTENT_META_KEY)
->andReturn('AUTHORIZE');
$wcOrder->expects('get_meta')
->with(AuthorizedPaymentsProcessor::CAPTURED_META_KEY)
->andReturn(false);
$this->authorizedPaymentProcessor
->expects('capture_authorized_payment')
->andReturnFalse()
->once();
$this->expectException(RuntimeException::class);
ppcp_capture_order($wcOrder);
}
public function testNotAuthorize(): void {
$wcOrder = Mockery::mock(WC_Order::class);
$wcOrder->shouldReceive('get_meta')
->with(PayPalGateway::INTENT_META_KEY)
->andReturn('CAPTURE');
$wcOrder->shouldReceive('get_meta')
->with(AuthorizedPaymentsProcessor::CAPTURED_META_KEY)
->andReturn(false);
$this->expectException(InvalidArgumentException::class);
ppcp_capture_order($wcOrder);
}
public function testAlreadyCaptured(): void {
$wcOrder = Mockery::mock(WC_Order::class);
$wcOrder->shouldReceive('get_meta')
->with(PayPalGateway::INTENT_META_KEY)
->andReturn('CAPTURE');
$wcOrder->shouldReceive('get_meta')
->with(AuthorizedPaymentsProcessor::CAPTURED_META_KEY)
->andReturn(true);
$this->expectException(InvalidArgumentException::class);
ppcp_capture_order($wcOrder);
}
}

View file

@ -0,0 +1,90 @@
<?php
namespace WooCommerce\PayPalCommerce\Api;
use InvalidArgumentException;
use Mockery;
use RuntimeException;
use WC_Order;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Order;
use WooCommerce\PayPalCommerce\ModularTestCase;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
use WooCommerce\PayPalCommerce\WcGateway\Processor\RefundProcessor;
class OrderRefundTest extends ModularTestCase
{
private $refundProcessor;
private $orderEndpoint;
public function setUp(): void {
parent::setUp();
$this->refundProcessor = Mockery::mock(RefundProcessor::class);
$this->orderEndpoint = Mockery::mock(OrderEndpoint::class);
$this->bootstrapModule([
'wcgateway.processor.refunds' => function () {
return $this->refundProcessor;
},
'api.endpoint.order' => function () {
return $this->orderEndpoint;
},
]);
}
public function testSuccess(): void {
$wcOrder = Mockery::mock(WC_Order::class);
$wcOrder->expects('get_meta')
->with(PayPalGateway::ORDER_ID_META_KEY)
->andReturn('123abc');
$this->orderEndpoint
->expects('order')
->with('123abc')
->andReturn(Mockery::mock(Order::class))
->once();
$this->refundProcessor
->expects('refund')
->andReturn('456qwe')
->once();
$refund_id = ppcp_refund_order($wcOrder, 42.0, 'reason');
$this->assertEquals('456qwe', $refund_id);
}
public function testOrderWithoutId(): void {
$wcOrder = Mockery::mock(WC_Order::class);
$wcOrder->expects('get_meta')
->with(PayPalGateway::ORDER_ID_META_KEY)
->andReturn(false);
$this->expectException(InvalidArgumentException::class);
ppcp_refund_order($wcOrder, 42.0, 'reason');
}
public function testFailure(): void {
$wcOrder = Mockery::mock(WC_Order::class);
$wcOrder->expects('get_meta')
->with(PayPalGateway::ORDER_ID_META_KEY)
->andReturn('123abc');
$this->orderEndpoint
->expects('order')
->with('123abc')
->andReturn(Mockery::mock(Order::class))
->once();
$this->refundProcessor
->expects('refund')
->andThrow(new RuntimeException())
->once();
$this->expectException(RuntimeException::class);
ppcp_refund_order($wcOrder, 42.0, 'reason');
}
}

View file

@ -0,0 +1,88 @@
<?php
namespace WooCommerce\PayPalCommerce\Api;
use InvalidArgumentException;
use Mockery;
use RuntimeException;
use WC_Order;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Order;
use WooCommerce\PayPalCommerce\ModularTestCase;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
use WooCommerce\PayPalCommerce\WcGateway\Processor\RefundProcessor;
class OrderVoidTest extends ModularTestCase
{
private $refundProcessor;
private $orderEndpoint;
public function setUp(): void {
parent::setUp();
$this->refundProcessor = Mockery::mock(RefundProcessor::class);
$this->orderEndpoint = Mockery::mock(OrderEndpoint::class);
$this->bootstrapModule([
'wcgateway.processor.refunds' => function () {
return $this->refundProcessor;
},
'api.endpoint.order' => function () {
return $this->orderEndpoint;
},
]);
}
public function testSuccess(): void {
$wcOrder = Mockery::mock(WC_Order::class);
$wcOrder->expects('get_meta')
->with(PayPalGateway::ORDER_ID_META_KEY)
->andReturn('123abc');
$this->orderEndpoint
->expects('order')
->with('123abc')
->andReturn(Mockery::mock(Order::class))
->once();
$this->refundProcessor
->expects('void')
->once();
ppcp_void_order($wcOrder);
}
public function testOrderWithoutId(): void {
$wcOrder = Mockery::mock(WC_Order::class);
$wcOrder->expects('get_meta')
->with(PayPalGateway::ORDER_ID_META_KEY)
->andReturn(false);
$this->expectException(InvalidArgumentException::class);
ppcp_void_order($wcOrder);
}
public function testFailure(): void {
$wcOrder = Mockery::mock(WC_Order::class);
$wcOrder->expects('get_meta')
->with(PayPalGateway::ORDER_ID_META_KEY)
->andReturn('123abc');
$this->orderEndpoint
->expects('order')
->with('123abc')
->andReturn(Mockery::mock(Order::class))
->once();
$this->refundProcessor
->expects('void')
->andThrow(new RuntimeException())
->once();
$this->expectException(RuntimeException::class);
ppcp_void_order($wcOrder);
}
}

View file

@ -47,7 +47,7 @@ class IdentityTokenTest extends TestCase
public function testGenerateForCustomerReturnsToken()
{
$id = 1;
define( 'PPCP_FLAG_SUBSCRIPTION', true );
!defined('PPCP_FLAG_SUBSCRIPTION') && define('PPCP_FLAG_SUBSCRIPTION', true);
$token = Mockery::mock(Token::class);
$token
->expects('token')->andReturn('bearer');

View file

@ -82,6 +82,8 @@ class ModularTestCase extends TestCase
$bootstrap = require ("$rootDir/bootstrap.php");
$appContainer = $bootstrap($rootDir, [], [$module]);
PPCP::init($appContainer);
return $appContainer;
}
}

View file

@ -28,6 +28,9 @@ class TestCase extends \PHPUnit\Framework\TestCase
when('wc_print_r')->alias(function ($value, bool $return = false) {
return print_r($value, $return);
});
when('wc_string_to_bool')->alias(function ($string) {
return is_bool( $string ) ? $string : ( 'yes' === strtolower( $string ) || 1 === $string || 'true' === strtolower( $string ) || '1' === $string );
});
when('get_plugin_data')->justReturn(['Version' => '1.0']);
when('plugin_basename')->justReturn('woocommerce-paypal-payments/woocommerce-paypal-payments.php');