diff --git a/modules/ppcp-button/src/Helper/DisabledFundingSources.php b/modules/ppcp-button/src/Helper/DisabledFundingSources.php index acab7d5f7..fc60ba96b 100644 --- a/modules/ppcp-button/src/Helper/DisabledFundingSources.php +++ b/modules/ppcp-button/src/Helper/DisabledFundingSources.php @@ -53,7 +53,7 @@ class DisabledFundingSources { * @return array|int[]|mixed|string[] * @throws NotFoundException When the setting is not found. */ - public function sources( $context ) { + public function sources( string $context ) { $disable_funding = $this->settings->has( 'disable_funding' ) ? $this->settings->get( 'disable_funding' ) : array(); diff --git a/tests/PHPUnit/Button/Helper/DisabledFundingSourcesTest.php b/tests/PHPUnit/Button/Helper/DisabledFundingSourcesTest.php new file mode 100644 index 000000000..7185a2255 --- /dev/null +++ b/tests/PHPUnit/Button/Helper/DisabledFundingSourcesTest.php @@ -0,0 +1,95 @@ +settings = Mockery::mock(Settings::class); + } + + public function test_is_checkout_true_does_not_add_card() + { + $sut = new DisabledFundingSources($this->settings, []); + + $this->setExpectations(); + $this->setWcPaymentGateways(); + + when('is_checkout')->justReturn(true); + + $this->assertEquals([], $sut->sources('')); + } + + public function test_is_checkout_false_adds_card() + { + $sut = new DisabledFundingSources($this->settings, []); + + $this->setExpectations(); + $this->setWcPaymentGateways(); + + when('is_checkout')->justReturn(false); + + $this->assertEquals(['card'], $sut->sources('checkout-block')); + } + + public function test_checkout_block_context_adds_source() + { + $sut = new DisabledFundingSources($this->settings, [ + 'card' => 'Credit or debit cards', + 'paypal' => 'PayPal', + 'foo' => 'Bar', + ]); + + $this->setExpectations(); + $this->setWcPaymentGateways(); + + when('is_checkout')->justReturn(true); + + $this->assertEquals(['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); + } +}