woocommerce-paypal-payments/tests/PHPUnit/WcGateway/Gateway/WcGatewayTest.php

292 lines
8.8 KiB
PHP
Raw Normal View History

2020-04-29 08:58:16 +03:00
<?php
declare(strict_types=1);
2020-09-14 07:51:45 +03:00
namespace WooCommerce\PayPalCommerce\WcGateway\Gateway;
2020-04-29 08:58:16 +03:00
2021-07-28 10:03:43 +03:00
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\PaymentsEndpoint;
use Psr\Log\NullLogger;
2021-10-08 10:23:19 +03:00
use WooCommerce\PayPalCommerce\ApiClient\Entity\Capture;
use WooCommerce\PayPalCommerce\ApiClient\Entity\CaptureStatus;
use WooCommerce\PayPalCommerce\ApiClient\Factory\ShippingPreferenceFactory;
2021-10-06 17:44:41 +03:00
use WooCommerce\PayPalCommerce\Onboarding\Environment;
2020-09-28 11:47:24 +03:00
use WooCommerce\PayPalCommerce\Onboarding\State;
2020-09-14 07:51:45 +03:00
use WooCommerce\PayPalCommerce\Session\SessionHandler;
2021-06-04 11:57:25 +02:00
use WooCommerce\PayPalCommerce\Subscription\Helper\SubscriptionHelper;
2020-09-14 07:51:45 +03:00
use WooCommerce\PayPalCommerce\TestCase;
use WooCommerce\PayPalCommerce\Vaulting\PaymentTokenRepository;
use WooCommerce\PayPalCommerce\WcGateway\FundingSource\FundingSourceRenderer;
2020-09-14 07:51:45 +03:00
use WooCommerce\PayPalCommerce\WcGateway\Notice\AuthorizeOrderActionNotice;
use WooCommerce\PayPalCommerce\WcGateway\Processor\AuthorizedPaymentsProcessor;
use WooCommerce\PayPalCommerce\WcGateway\Processor\OrderProcessor;
2020-09-28 11:47:24 +03:00
use WooCommerce\PayPalCommerce\WcGateway\Processor\RefundProcessor;
2020-09-14 07:51:45 +03:00
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
use WooCommerce\PayPalCommerce\WcGateway\Settings\SettingsRenderer;
2020-04-29 08:58:16 +03:00
use Mockery;
use function Brain\Monkey\Functions\expect;
2021-03-17 10:30:02 +02:00
use function Brain\Monkey\Functions\when;
2020-04-29 08:58:16 +03:00
class WcGatewayTest extends TestCase
{
private $isAdmin = false;
private $sessionHandler;
private $fundingSource = null;
private $settingsRenderer;
private $funding_source_renderer;
private $orderProcessor;
private $authorizedOrdersProcessor;
private $settings;
private $refundProcessor;
private $onboardingState;
private $transactionUrlProvider;
private $subscriptionHelper;
2021-10-06 17:44:41 +03:00
private $environment;
private $paymentTokenRepository;
private $shipping_preference_factory;
private $logger;
private $paymentsEndpoint;
private $orderEndpoint;
2022-04-22 15:26:44 +02:00
private $apiShopCountry;
2020-04-29 08:58:16 +03:00
2021-10-06 17:44:41 +03:00
public function setUp(): void {
parent::setUp();
expect('is_admin')->andReturnUsing(function () {
return $this->isAdmin;
});
$this->settingsRenderer = Mockery::mock(SettingsRenderer::class);
$this->orderProcessor = Mockery::mock(OrderProcessor::class);
$this->authorizedOrdersProcessor = Mockery::mock(AuthorizedPaymentsProcessor::class);
$this->settings = Mockery::mock(Settings::class);
$this->sessionHandler = Mockery::mock(SessionHandler::class);
$this->refundProcessor = Mockery::mock(RefundProcessor::class);
$this->onboardingState = Mockery::mock(State::class);
$this->transactionUrlProvider = Mockery::mock(TransactionUrlProvider::class);
$this->subscriptionHelper = Mockery::mock(SubscriptionHelper::class);
2021-10-06 17:44:41 +03:00
$this->environment = Mockery::mock(Environment::class);
$this->paymentTokenRepository = Mockery::mock(PaymentTokenRepository::class);
$this->shipping_preference_factory = Mockery::mock(ShippingPreferenceFactory::class);
$this->logger = Mockery::mock(LoggerInterface::class);
$this->paymentsEndpoint = Mockery::mock(PaymentsEndpoint::class);
$this->orderEndpoint = Mockery::mock(OrderEndpoint::class);
$this->funding_source_renderer = new FundingSourceRenderer($this->settings);
2022-04-22 15:26:44 +02:00
$this->apiShopCountry = 'DE';
$this->onboardingState->shouldReceive('current_state')->andReturn(State::STATE_ONBOARDED);
$this->sessionHandler
->shouldReceive('funding_source')
->andReturnUsing(function () {
return $this->fundingSource;
});
$this->settings->shouldReceive('has')->andReturnFalse();
$this->logger->shouldReceive('info');
2021-10-06 17:44:41 +03:00
}
private function createGateway()
{
return new PayPalGateway(
$this->settingsRenderer,
$this->funding_source_renderer,
$this->orderProcessor,
$this->authorizedOrdersProcessor,
$this->settings,
$this->sessionHandler,
$this->refundProcessor,
$this->onboardingState,
$this->transactionUrlProvider,
$this->subscriptionHelper,
PayPalGateway::ID,
$this->environment,
$this->paymentTokenRepository,
$this->shipping_preference_factory,
$this->logger,
$this->paymentsEndpoint,
2022-04-22 15:26:44 +02:00
$this->orderEndpoint,
$this->apiShopCountry
);
}
2020-04-29 08:58:16 +03:00
public function testProcessPaymentSuccess() {
2020-04-29 08:58:16 +03:00
$orderId = 1;
$wcOrder = Mockery::mock(\WC_Order::class);
$wcOrder->shouldReceive('get_customer_id')->andReturn(1);
$wcOrder->shouldReceive('get_meta')->andReturn('');
$this->orderProcessor
2020-04-29 08:58:16 +03:00
->expects('process')
->andReturnUsing(
2021-03-17 10:30:02 +02:00
function(\WC_Order $order) use ($wcOrder) : bool {
2020-04-29 08:58:16 +03:00
return $order === $wcOrder;
}
);
$this->sessionHandler
->shouldReceive('destroy_session_data');
$this->subscriptionHelper
2021-06-04 11:57:25 +02:00
->shouldReceive('has_subscription')
->with($orderId)
->andReturn(true)
->andReturn(false);
$this->subscriptionHelper
2021-06-04 11:57:25 +02:00
->shouldReceive('is_subscription_change_payment')
->andReturn(true);
$testee = $this->createGateway();
2020-04-29 08:58:16 +03:00
expect('wc_get_order')
->with($orderId)
->andReturn($wcOrder);
2021-03-17 10:30:02 +02:00
when('wc_get_checkout_url')
->justReturn('test');
$woocommerce = Mockery::mock(\WooCommerce::class);
$cart = Mockery::mock(\WC_Cart::class);
when('WC')->justReturn($woocommerce);
$woocommerce->cart = $cart;
$cart->shouldReceive('empty_cart');
2020-04-29 08:58:16 +03:00
$result = $testee->process_payment($orderId);
2021-03-17 10:30:02 +02:00
2020-04-29 08:58:16 +03:00
$this->assertIsArray($result);
2021-03-17 10:30:02 +02:00
2020-04-29 08:58:16 +03:00
$this->assertEquals('success', $result['result']);
$this->assertEquals($result['redirect'], $wcOrder);
}
public function testProcessPaymentOrderNotFound() {
$orderId = 1;
$testee = $this->createGateway();
2020-04-29 08:58:16 +03:00
expect('wc_get_order')
->with($orderId)
->andReturn(false);
2021-03-17 10:30:02 +02:00
$redirectUrl = 'http://example.com/checkout';
when('wc_get_checkout_url')
->justReturn($redirectUrl);
expect('wc_add_notice')
->with('Couldn\'t find order to process','error');
$this->assertEquals(
[
'result' => 'failure',
'redirect' => $redirectUrl
],
$testee->process_payment($orderId)
);
2020-04-29 08:58:16 +03:00
}
public function testProcessPaymentFails() {
$orderId = 1;
$wcOrder = Mockery::mock(\WC_Order::class);
$lastError = 'some-error';
$this->orderProcessor
2020-04-29 08:58:16 +03:00
->expects('process')
->andReturnFalse();
$this->orderProcessor
2020-08-31 13:03:16 +03:00
->expects('last_error')
2022-01-31 12:31:13 +01:00
->twice()
2020-04-29 08:58:16 +03:00
->andReturn($lastError);
$this->subscriptionHelper->shouldReceive('has_subscription')->with($orderId)->andReturn(true);
$this->subscriptionHelper->shouldReceive('is_subscription_change_payment')->andReturn(true);
2021-08-13 14:39:27 +02:00
$wcOrder->shouldReceive('update_status')->andReturn(true);
2021-06-04 11:57:25 +02:00
$testee = $this->createGateway();
2020-04-29 08:58:16 +03:00
expect('wc_get_order')
->with($orderId)
->andReturn($wcOrder);
expect('wc_add_notice')
2020-08-31 13:03:16 +03:00
->with($lastError, 'error');
2020-04-29 08:58:16 +03:00
2021-03-17 10:30:02 +02:00
$redirectUrl = 'http://example.com/checkout';
when('wc_get_checkout_url')
->justReturn($redirectUrl);
2021-06-04 11:57:25 +02:00
2020-04-29 08:58:16 +03:00
$result = $testee->process_payment($orderId);
2021-03-17 10:30:02 +02:00
$this->assertEquals(
[
'result' => 'failure',
'redirect' => $redirectUrl
],
$result
);
2020-04-29 08:58:16 +03:00
}
2021-07-28 10:03:43 +03:00
/**
* @dataProvider dataForTestNeedsSetup
*/
public function testNeedsSetup($currentState, $needSetup)
{
$this->isAdmin = true;
$this->onboardingState = Mockery::mock(State::class);
$this->onboardingState
2021-07-28 10:03:43 +03:00
->expects('current_state')
->andReturn($currentState);
$testee = $this->createGateway();
2021-08-27 17:28:15 +03:00
2021-07-28 10:03:43 +03:00
$this->assertSame($needSetup, $testee->needs_setup());
}
2020-04-29 08:58:16 +03:00
/**
* @dataProvider dataForFundingSource
*/
public function testFundingSource($fundingSource, $title, $description)
{
$this->fundingSource = $fundingSource;
$testee = $this->createGateway();
self::assertEquals($title, $testee->title);
self::assertEquals($description, $testee->description);
}
2020-04-29 08:58:16 +03:00
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,
],
];
}
2021-08-27 17:28:15 +03:00
2021-07-28 10:03:43 +03:00
public function dataForTestNeedsSetup(): array
{
return [
[State::STATE_START, true],
[State::STATE_ONBOARDED, false]
];
}
public function dataForFundingSource(): array
{
return [
[null, 'PayPal', 'Pay via PayPal.'],
['venmo', 'Venmo', 'Pay via Venmo.'],
['qwerty', 'PayPal', 'Pay via PayPal.'],
];
}
2021-02-19 19:57:25 +02:00
}