Add API for easy programmatic capture

This commit is contained in:
Alex P 2023-03-14 16:40:20 +02:00
parent b62072c537
commit d40d01a4f4
No known key found for this signature in database
GPG key ID: 54487A734A204D71
9 changed files with 577 additions and 58 deletions

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);
}
}