woocommerce-paypal-payments/tests/PHPUnit/Button/Helper/DisabledFundingSourcesTest.php

114 lines
3.1 KiB
PHP
Raw Normal View History

2024-06-28 16:28:57 +02:00
<?php
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\Button\Helper;
use Mockery;
use WC_Payment_Gateways;
use WooCommerce;
use WooCommerce\PayPalCommerce\TestCase;
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
2025-03-14 11:59:52 +01:00
use WooCommerce\PayPalCommerce\WcGateway\Helper\DCCGatewayConfiguration;
2024-06-28 16:28:57 +02:00
use function Brain\Monkey\Functions\when;
class DisabledFundingSourcesTest extends TestCase
{
private $settings;
2025-03-14 11:59:52 +01:00
private $dcc_configuration;
2024-06-28 16:28:57 +02:00
public function setUp(): void
{
parent::setUp();
$this->settings = Mockery::mock(Settings::class);
2025-03-14 11:59:52 +01:00
$this->dcc_configuration = Mockery::mock(DCCGatewayConfiguration::class);
2024-06-28 16:28:57 +02:00
}
/**
* Block checkout page configured in WC "Checkout page" setting,
* `is_checkout` returns true when visiting the block checkout page.
*/
public function test_is_checkout_true_add_card_when_checkout_block_context()
2024-06-28 16:28:57 +02:00
{
2025-03-14 11:59:52 +01:00
$this->dcc_configuration->shouldReceive('is_enabled')->andReturn(true);
$sut = new DisabledFundingSources($this->settings, [], $this->dcc_configuration);
2024-06-28 16:28:57 +02:00
$this->setExpectations();
$this->setWcPaymentGateways();
when('is_checkout')->justReturn(true);
$this->assertEquals(['card'], $sut->sources('checkout-block'));
2024-06-28 16:28:57 +02:00
}
/**
* Classic checkout page configured in WC "Checkout page" setting,
* `is_checkout` returns false when visiting the block checkout page.
*/
public function test_is_checkout_false_add_card_when_checkout_context()
2024-06-28 16:28:57 +02:00
{
2025-03-14 11:59:52 +01:00
$this->dcc_configuration->shouldReceive('is_enabled')->andReturn(true);
$sut = new DisabledFundingSources($this->settings, [], $this->dcc_configuration);
2024-06-28 16:28:57 +02:00
$this->setExpectations();
$this->setWcPaymentGateways();
when('is_checkout')->justReturn(false);
$this->assertEquals(['card'], $sut->sources('checkout'));
2024-06-28 16:28:57 +02:00
}
public function test_is_checkout_true_add_allowed_sources_when_checkout_block_context()
2024-06-28 16:28:57 +02:00
{
2025-03-14 11:59:52 +01:00
$this->dcc_configuration->shouldReceive('is_enabled')->andReturn(true);
$sut = new DisabledFundingSources(
$this->settings,
[
'card' => 'Credit or debit cards',
'paypal' => 'PayPal',
'foo' => 'Bar',
],
$this->dcc_configuration
);
2024-06-28 16:28:57 +02:00
$this->setExpectations();
$this->setWcPaymentGateways();
when('is_checkout')->justReturn(true);
$this->assertEquals(['card', 'foo'], $sut->sources('checkout-block'));
2024-06-28 16:28:57 +02:00
}
private function setExpectations(
array $disabledFundings = [],
bool $dccEnambled = true
): void
{
$this->settings->shouldReceive('has')
->with('disable_funding')
->andReturn(true);
$this->settings->shouldReceive('get')
->with('disable_funding')
->andReturn($disabledFundings);
$this->settings->shouldReceive('has')
->with('dcc_enabled')
->andReturn(true);
$this->settings->shouldReceive('get')
->with('dcc_enabled')
->andReturn($dccEnambled);
}
private function setWcPaymentGateways(array $paymentGateways = []): void
{
$woocommerce = Mockery::mock(WooCommerce::class);
$payment_gateways = Mockery::mock(WC_Payment_Gateways::class);
when('WC')->justReturn($woocommerce);
$woocommerce->payment_gateways = $payment_gateways;
$payment_gateways->shouldReceive('get_available_payment_gateways')
->andReturn($paymentGateways);
}
}