mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-06 18:16:38 +08:00
add WcGateway tests
This commit is contained in:
parent
4b14a7f193
commit
80b129c92d
3 changed files with 290 additions and 9 deletions
|
@ -74,7 +74,7 @@ class WcGateway extends WcGatewayBase
|
||||||
public function process_payment($orderId): ?array
|
public function process_payment($orderId): ?array
|
||||||
{
|
{
|
||||||
global $woocommerce;
|
global $woocommerce;
|
||||||
$wcOrder = new \WC_Order($orderId);
|
$wcOrder = wc_get_order($orderId);
|
||||||
if (! is_a($wcOrder, \WC_Order::class)) {
|
if (! is_a($wcOrder, \WC_Order::class)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@ -97,10 +97,7 @@ class WcGateway extends WcGatewayBase
|
||||||
|
|
||||||
if ($isProcessed) {
|
if ($isProcessed) {
|
||||||
$wcOrder->add_order_note(
|
$wcOrder->add_order_note(
|
||||||
__(
|
__('Payment successfully captured.', 'woocommerce-paypal-gateway')
|
||||||
'Payment successfully captured.',
|
|
||||||
'woocommerce-paypal-gateway'
|
|
||||||
)
|
|
||||||
);
|
);
|
||||||
|
|
||||||
$wcOrder->set_status('processing');
|
$wcOrder->set_status('processing');
|
||||||
|
@ -112,10 +109,7 @@ class WcGateway extends WcGatewayBase
|
||||||
if ($this->authorizedPayments->lastStatus() === AuthorizedPaymentsProcessor::ALREADY_CAPTURED) {
|
if ($this->authorizedPayments->lastStatus() === AuthorizedPaymentsProcessor::ALREADY_CAPTURED) {
|
||||||
if ($wcOrder->get_status() === 'on-hold') {
|
if ($wcOrder->get_status() === 'on-hold') {
|
||||||
$wcOrder->add_order_note(
|
$wcOrder->add_order_note(
|
||||||
__(
|
__('Payment successfully captured.','woocommerce-paypal-gateway')
|
||||||
'Payment successfully captured.',
|
|
||||||
'woocommerce-paypal-gateway'
|
|
||||||
)
|
|
||||||
);
|
);
|
||||||
$wcOrder->set_status('processing');
|
$wcOrder->set_status('processing');
|
||||||
}
|
}
|
||||||
|
@ -124,6 +118,7 @@ class WcGateway extends WcGatewayBase
|
||||||
$wcOrder->save();
|
$wcOrder->save();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function renderAuthorizationMessageForStatus(string $status) {
|
private function renderAuthorizationMessageForStatus(string $status) {
|
||||||
|
|
271
tests/PHPUnit/WcGateway/Gateway/WcGatewayTest.php
Normal file
271
tests/PHPUnit/WcGateway/Gateway/WcGatewayTest.php
Normal file
|
@ -0,0 +1,271 @@
|
||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Inpsyde\PayPalCommerce\WcGateway\Gateway;
|
||||||
|
|
||||||
|
|
||||||
|
use Inpsyde\PayPalCommerce\TestCase;
|
||||||
|
use Inpsyde\PayPalCommerce\WcGateway\Notice\AuthorizeOrderActionNotice;
|
||||||
|
use Inpsyde\PayPalCommerce\WcGateway\Processor\AuthorizedPaymentsProcessor;
|
||||||
|
use Inpsyde\PayPalCommerce\WcGateway\Processor\OrderProcessor;
|
||||||
|
use Inpsyde\PayPalCommerce\WcGateway\Settings\SettingsFields;
|
||||||
|
use Mockery;
|
||||||
|
use function Brain\Monkey\Functions\expect;
|
||||||
|
|
||||||
|
class WcGatewayTest extends TestCase
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
public function testFormFieldsAreSet()
|
||||||
|
{
|
||||||
|
|
||||||
|
$expectedFields = ['key' => 'value'];
|
||||||
|
$settingsFields = Mockery::mock(SettingsFields::class);
|
||||||
|
$settingsFields
|
||||||
|
->expects('fields')
|
||||||
|
->andReturn($expectedFields);
|
||||||
|
$orderProcessor = Mockery::mock(OrderProcessor::class);
|
||||||
|
$authorizedPaymentsProcessor = Mockery::mock(AuthorizedPaymentsProcessor::class);
|
||||||
|
$authorizedOrderActionNotice = Mockery::mock(AuthorizeOrderActionNotice::class);
|
||||||
|
$testee = new WcGateway(
|
||||||
|
$settingsFields,
|
||||||
|
$orderProcessor,
|
||||||
|
$authorizedPaymentsProcessor,
|
||||||
|
$authorizedOrderActionNotice
|
||||||
|
);
|
||||||
|
$this->assertEquals($testee->form_fields, $expectedFields);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public function testProcessPaymentSuccess() {
|
||||||
|
|
||||||
|
$orderId = 1;
|
||||||
|
$wcOrder = Mockery::mock(\WC_Order::class);
|
||||||
|
$settingsFields = Mockery::mock(SettingsFields::class);
|
||||||
|
$settingsFields
|
||||||
|
->expects('fields')
|
||||||
|
->andReturn([]);
|
||||||
|
$orderProcessor = Mockery::mock(OrderProcessor::class);
|
||||||
|
$orderProcessor
|
||||||
|
->expects('process')
|
||||||
|
->andReturnUsing(
|
||||||
|
function(\WC_Order $order, $woocommerce) use ($wcOrder) : bool {
|
||||||
|
return $order === $wcOrder;
|
||||||
|
}
|
||||||
|
);
|
||||||
|
$authorizedPaymentsProcessor = Mockery::mock(AuthorizedPaymentsProcessor::class);
|
||||||
|
$authorizedOrderActionNotice = Mockery::mock(AuthorizeOrderActionNotice::class);
|
||||||
|
$testee = new WcGateway(
|
||||||
|
$settingsFields,
|
||||||
|
$orderProcessor,
|
||||||
|
$authorizedPaymentsProcessor,
|
||||||
|
$authorizedOrderActionNotice
|
||||||
|
);
|
||||||
|
|
||||||
|
expect('wc_get_order')
|
||||||
|
->with($orderId)
|
||||||
|
->andReturn($wcOrder);
|
||||||
|
|
||||||
|
$result = $testee->process_payment($orderId);
|
||||||
|
$this->assertIsArray($result);
|
||||||
|
$this->assertEquals('success', $result['result']);
|
||||||
|
$this->assertEquals($result['redirect'], $wcOrder);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testProcessPaymentOrderNotFound() {
|
||||||
|
|
||||||
|
$orderId = 1;
|
||||||
|
$settingsFields = Mockery::mock(SettingsFields::class);
|
||||||
|
$settingsFields
|
||||||
|
->expects('fields')
|
||||||
|
->andReturn([]);
|
||||||
|
$orderProcessor = Mockery::mock(OrderProcessor::class);
|
||||||
|
$authorizedPaymentsProcessor = Mockery::mock(AuthorizedPaymentsProcessor::class);
|
||||||
|
$authorizedOrderActionNotice = Mockery::mock(AuthorizeOrderActionNotice::class);
|
||||||
|
$testee = new WcGateway(
|
||||||
|
$settingsFields,
|
||||||
|
$orderProcessor,
|
||||||
|
$authorizedPaymentsProcessor,
|
||||||
|
$authorizedOrderActionNotice
|
||||||
|
);
|
||||||
|
|
||||||
|
expect('wc_get_order')
|
||||||
|
->with($orderId)
|
||||||
|
->andReturn(false);
|
||||||
|
|
||||||
|
$this->assertNull($testee->process_payment($orderId));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public function testProcessPaymentFails() {
|
||||||
|
|
||||||
|
$orderId = 1;
|
||||||
|
$wcOrder = Mockery::mock(\WC_Order::class);
|
||||||
|
$lastError = 'some-error';
|
||||||
|
$settingsFields = Mockery::mock(SettingsFields::class);
|
||||||
|
$settingsFields
|
||||||
|
->expects('fields')
|
||||||
|
->andReturn([]);
|
||||||
|
$orderProcessor = Mockery::mock(OrderProcessor::class);
|
||||||
|
$orderProcessor
|
||||||
|
->expects('process')
|
||||||
|
->andReturnFalse();
|
||||||
|
$orderProcessor
|
||||||
|
->expects('lastError')
|
||||||
|
->andReturn($lastError);
|
||||||
|
$authorizedPaymentsProcessor = Mockery::mock(AuthorizedPaymentsProcessor::class);
|
||||||
|
$authorizedOrderActionNotice = Mockery::mock(AuthorizeOrderActionNotice::class);
|
||||||
|
$testee = new WcGateway(
|
||||||
|
$settingsFields,
|
||||||
|
$orderProcessor,
|
||||||
|
$authorizedPaymentsProcessor,
|
||||||
|
$authorizedOrderActionNotice
|
||||||
|
);
|
||||||
|
|
||||||
|
expect('wc_get_order')
|
||||||
|
->with($orderId)
|
||||||
|
->andReturn($wcOrder);
|
||||||
|
expect('wc_add_notice')
|
||||||
|
->with($lastError);
|
||||||
|
|
||||||
|
$result = $testee->process_payment($orderId);
|
||||||
|
$this->assertNull($result);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testCaptureAuthorizedPayment() {
|
||||||
|
|
||||||
|
$wcOrder = Mockery::mock(\WC_Order::class);
|
||||||
|
$wcOrder
|
||||||
|
->expects('add_order_note');
|
||||||
|
$wcOrder
|
||||||
|
->expects('set_status')
|
||||||
|
->with('processing');
|
||||||
|
$wcOrder
|
||||||
|
->expects('update_meta_data')
|
||||||
|
->with(WcGateway::CAPTURED_META_KEY, 'true');
|
||||||
|
$wcOrder
|
||||||
|
->expects('save');
|
||||||
|
$settingsFields = Mockery::mock(SettingsFields::class);
|
||||||
|
$settingsFields
|
||||||
|
->expects('fields')
|
||||||
|
->andReturn([]);
|
||||||
|
$orderProcessor = Mockery::mock(OrderProcessor::class);
|
||||||
|
$authorizedPaymentsProcessor = Mockery::mock(AuthorizedPaymentsProcessor::class);
|
||||||
|
$authorizedPaymentsProcessor
|
||||||
|
->expects('process')
|
||||||
|
->with($wcOrder)
|
||||||
|
->andReturnTrue();
|
||||||
|
$authorizedPaymentsProcessor
|
||||||
|
->expects('lastStatus')
|
||||||
|
->andReturn(AuthorizedPaymentsProcessor::SUCCESSFUL);
|
||||||
|
$authorizedOrderActionNotice = Mockery::mock(AuthorizeOrderActionNotice::class);
|
||||||
|
$authorizedOrderActionNotice
|
||||||
|
->expects('displayMessage')
|
||||||
|
->with(AuthorizeOrderActionNotice::SUCCESS);
|
||||||
|
$testee = new WcGateway(
|
||||||
|
$settingsFields,
|
||||||
|
$orderProcessor,
|
||||||
|
$authorizedPaymentsProcessor,
|
||||||
|
$authorizedOrderActionNotice
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertTrue($testee->captureAuthorizedPayment($wcOrder));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testCaptureAuthorizedPaymentHasAlreadyBeenCaptured() {
|
||||||
|
|
||||||
|
$wcOrder = Mockery::mock(\WC_Order::class);
|
||||||
|
$wcOrder
|
||||||
|
->expects('get_status')
|
||||||
|
->andReturn('on-hold');
|
||||||
|
$wcOrder
|
||||||
|
->expects('add_order_note');
|
||||||
|
$wcOrder
|
||||||
|
->expects('set_status')
|
||||||
|
->with('processing');
|
||||||
|
$wcOrder
|
||||||
|
->expects('update_meta_data')
|
||||||
|
->with(WcGateway::CAPTURED_META_KEY, 'true');
|
||||||
|
$wcOrder
|
||||||
|
->expects('save');
|
||||||
|
$settingsFields = Mockery::mock(SettingsFields::class);
|
||||||
|
$settingsFields
|
||||||
|
->expects('fields')
|
||||||
|
->andReturn([]);
|
||||||
|
$orderProcessor = Mockery::mock(OrderProcessor::class);
|
||||||
|
$authorizedPaymentsProcessor = Mockery::mock(AuthorizedPaymentsProcessor::class);
|
||||||
|
$authorizedPaymentsProcessor
|
||||||
|
->expects('process')
|
||||||
|
->with($wcOrder)
|
||||||
|
->andReturnFalse();
|
||||||
|
$authorizedPaymentsProcessor
|
||||||
|
->shouldReceive('lastStatus')
|
||||||
|
->andReturn(AuthorizedPaymentsProcessor::ALREADY_CAPTURED);
|
||||||
|
$authorizedOrderActionNotice = Mockery::mock(AuthorizeOrderActionNotice::class);
|
||||||
|
$authorizedOrderActionNotice
|
||||||
|
->expects('displayMessage')
|
||||||
|
->with(AuthorizeOrderActionNotice::ALREADY_CAPTURED);
|
||||||
|
$testee = new WcGateway(
|
||||||
|
$settingsFields,
|
||||||
|
$orderProcessor,
|
||||||
|
$authorizedPaymentsProcessor,
|
||||||
|
$authorizedOrderActionNotice
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertTrue($testee->captureAuthorizedPayment($wcOrder));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider dataForTestCaptureAuthorizedPaymentNoActionableFailures
|
||||||
|
*
|
||||||
|
* @param string $lastStatus
|
||||||
|
* @param int $expectedMessage
|
||||||
|
*/
|
||||||
|
public function testCaptureAuthorizedPaymentNoActionableFailures($lastStatus, $expectedMessage) {
|
||||||
|
|
||||||
|
$wcOrder = Mockery::mock(\WC_Order::class);
|
||||||
|
$settingsFields = Mockery::mock(SettingsFields::class);
|
||||||
|
$settingsFields
|
||||||
|
->expects('fields')
|
||||||
|
->andReturn([]);
|
||||||
|
$orderProcessor = Mockery::mock(OrderProcessor::class);
|
||||||
|
$authorizedPaymentsProcessor = Mockery::mock(AuthorizedPaymentsProcessor::class);
|
||||||
|
$authorizedPaymentsProcessor
|
||||||
|
->expects('process')
|
||||||
|
->with($wcOrder)
|
||||||
|
->andReturnFalse();
|
||||||
|
$authorizedPaymentsProcessor
|
||||||
|
->shouldReceive('lastStatus')
|
||||||
|
->andReturn($lastStatus);
|
||||||
|
$authorizedOrderActionNotice = Mockery::mock(AuthorizeOrderActionNotice::class);
|
||||||
|
$authorizedOrderActionNotice
|
||||||
|
->expects('displayMessage')
|
||||||
|
->with($expectedMessage);
|
||||||
|
$testee = new WcGateway(
|
||||||
|
$settingsFields,
|
||||||
|
$orderProcessor,
|
||||||
|
$authorizedPaymentsProcessor,
|
||||||
|
$authorizedOrderActionNotice
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertFalse($testee->captureAuthorizedPayment($wcOrder));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function dataForTestCaptureAuthorizedPaymentNoActionableFailures() : array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'inaccessible' => [
|
||||||
|
AuthorizedPaymentsProcessor::INACCESSIBLE,
|
||||||
|
AuthorizeOrderActionNotice::NO_INFO,
|
||||||
|
],
|
||||||
|
'not_found' => [
|
||||||
|
AuthorizedPaymentsProcessor::NOT_FOUND,
|
||||||
|
AuthorizeOrderActionNotice::NOT_FOUND,
|
||||||
|
],
|
||||||
|
'not_mapped' => [
|
||||||
|
'some-other-failure',
|
||||||
|
AuthorizeOrderActionNotice::FAILED,
|
||||||
|
],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
|
@ -4,4 +4,19 @@ declare(strict_types=1);
|
||||||
class WC_Payment_Gateway
|
class WC_Payment_Gateway
|
||||||
{
|
{
|
||||||
|
|
||||||
|
protected function get_option(string $key) : string {
|
||||||
|
return $key;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function init_settings() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function get_return_url($wcOrder) {
|
||||||
|
return $wcOrder;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function process_admin_options() {
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Add table
Add a link
Reference in a new issue