woocommerce-paypal-payments/tests/PHPUnit/Button/Helper/DisabledFundingSourcesTest.php
2025-03-18 17:03:59 +01:00

116 lines
3.3 KiB
PHP

<?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;
use WooCommerce\PayPalCommerce\WcGateway\Helper\CardPaymentsConfiguration;
use function Brain\Monkey\Functions\when;
class DisabledFundingSourcesTest extends TestCase
{
private $settings;
private $dcc_configuration;
public function setUp(): void
{
parent::setUp();
$this->settings = Mockery::mock(Settings::class);
$this->dcc_configuration = Mockery::mock(CardPaymentsConfiguration::class);
}
/**
* 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()
{
$this->dcc_configuration->shouldReceive('is_enabled')->andReturn(true);
$this->dcc_configuration->shouldReceive('use_acdc')->andReturn(true);
$sut = new DisabledFundingSources($this->settings, [], $this->dcc_configuration);
$this->setExpectations();
$this->setWcPaymentGateways();
when('is_checkout')->justReturn(true);
$this->assertEquals(['card'], $sut->sources('checkout-block'));
}
/**
* 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()
{
$this->dcc_configuration->shouldReceive('is_enabled')->andReturn(true);
$this->dcc_configuration->shouldReceive('use_acdc')->andReturn(true);
$sut = new DisabledFundingSources($this->settings, [], $this->dcc_configuration);
$this->setExpectations();
$this->setWcPaymentGateways();
when('is_checkout')->justReturn(false);
$this->assertEquals(['card'], $sut->sources('checkout'));
}
public function test_is_checkout_true_add_allowed_sources_when_checkout_block_context()
{
$this->dcc_configuration->shouldReceive('is_enabled')->andReturn(true);
$this->dcc_configuration->shouldReceive('use_acdc')->andReturn(true);
$sut = new DisabledFundingSources(
$this->settings,
[
'card' => 'Credit or debit cards',
'paypal' => 'PayPal',
'foo' => 'Bar',
],
$this->dcc_configuration
);
$this->setExpectations();
$this->setWcPaymentGateways();
when('is_checkout')->justReturn(true);
$this->assertEquals(['card', 'foo'], $sut->sources('checkout-block'));
}
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);
}
}