Skip to content

Commit 375a5fc

Browse files
Unit test Payment_Gateway\External_Checkout\Google_Pay\Google_Pay::get_merchant_id method
1 parent 5fb687d commit 375a5fc

File tree

2 files changed

+83
-1
lines changed

2 files changed

+83
-1
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
namespace SkyVerge\WooCommerce\PluginFramework\v5_15_1\Tests\Unit\Payment_Gateway\External_Checkout\Google_Pay;
4+
5+
use Generator;
6+
use Mockery;
7+
use ReflectionException;
8+
use SkyVerge\WooCommerce\PluginFramework\v5_15_3\Payment_Gateway\External_Checkout\Google_Pay\Google_Pay;
9+
use SkyVerge\WooCommerce\PluginFramework\v5_15_3\Tests\TestCase;
10+
use WP_Mock;
11+
12+
/**
13+
* @coversDefaultClass \SkyVerge\WooCommerce\PluginFramework\v5_15_3\Payment_Gateway\External_Checkout\Google_Pay\Google_Pay
14+
*/
15+
class GooglePayTest extends TestCase
16+
{
17+
/** @var Mockery\MockInterface&Google_Pay */
18+
private $testObject;
19+
20+
public function setUp() : void
21+
{
22+
parent::setUp();
23+
24+
$this->testObject = Mockery::mock(Google_Pay::class)
25+
->shouldAllowMockingProtectedMethods()
26+
->makePartial();
27+
}
28+
29+
/**
30+
* @covers ::get_merchant_id()
31+
* @dataProvider providerCanGetMerchantId
32+
*
33+
* @throws ReflectionException
34+
*/
35+
public function testCanGetMerchantId($merchantId, string $expected) : void
36+
{
37+
$this->setInaccessiblePropertyValue($this->testObject, 'id', $id = 'TEST_ID');
38+
39+
WP_Mock::userFunction('get_option')
40+
->once()
41+
->with("sv_wc_{$id}_merchant_id")
42+
->andReturn($merchantId);
43+
44+
$this->assertSame($expected, $this->testObject->get_merchant_id());
45+
}
46+
47+
/** @see testCanGetMerchantId */
48+
public function providerCanGetMerchantId() : Generator
49+
{
50+
yield 'valid value' => [
51+
'merchantId' => 'TEST_MERCHANT_ID',
52+
'expected' => 'TEST_MERCHANT_ID',
53+
];
54+
55+
yield 'empty string value' => [
56+
'merchantId' => '',
57+
'expected' => '',
58+
];
59+
60+
yield 'scalar value: bool' => [
61+
'merchantId' => false,
62+
'expected' => '',
63+
];
64+
65+
yield 'scalar value: int' => [
66+
'merchantId' => 123,
67+
'expected' => '123',
68+
];
69+
70+
yield 'scalar value: float' => [
71+
'merchantId' => 45.6,
72+
'expected' => '45.6',
73+
];
74+
75+
yield 'non-string value: array' => [
76+
'merchantId' => ['test'],
77+
'expected' => '',
78+
];
79+
}
80+
}

woocommerce/payment-gateway/External_Checkout/Google_Pay/Google_Pay.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -713,7 +713,9 @@ public function get_supported_networks() {
713713
*/
714714
public function get_merchant_id() : string {
715715

716-
return get_option( "sv_wc_{$this->id}_merchant_id" ) ?? '';
716+
$optionValue = get_option("sv_wc_{$this->id}_merchant_id");
717+
718+
return $optionValue && is_scalar($optionValue) ? (string) $optionValue : '';
717719
}
718720

719721
/**

0 commit comments

Comments
 (0)