|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace SkyVerge\WooCommerce\PluginFramework\v5_15_1\Tests\Unit\Payment_Gateway\External_Checkout\Google_Pay; |
| 4 | + |
| 5 | +use Mockery; |
| 6 | +use ReflectionException; |
| 7 | +use SkyVerge\WooCommerce\PluginFramework\v5_15_3\Payment_Gateway\External_Checkout\External_Checkout; |
| 8 | +use SkyVerge\WooCommerce\PluginFramework\v5_15_3\Payment_Gateway\External_Checkout\Google_Pay\Frontend; |
| 9 | +use SkyVerge\WooCommerce\PluginFramework\v5_15_3\SV_WC_Payment_Gateway; |
| 10 | +use SkyVerge\WooCommerce\PluginFramework\v5_15_3\SV_WC_Payment_Gateway_Plugin; |
| 11 | +use SkyVerge\WooCommerce\PluginFramework\v5_15_3\Tests\TestCase; |
| 12 | +use WP_Mock; |
| 13 | + |
| 14 | +/** |
| 15 | + * @coversDefaultClass \SkyVerge\WooCommerce\PluginFramework\v5_15_3\Payment_Gateway\External_Checkout\Google_Pay\Frontend |
| 16 | + */ |
| 17 | +class FrontendTest extends TestCase |
| 18 | +{ |
| 19 | + /** @var Mockery\MockInterface&Frontend */ |
| 20 | + private $testObject; |
| 21 | + |
| 22 | + /** @var Mockery\MockInterface&SV_WC_Payment_Gateway */ |
| 23 | + private $gateway; |
| 24 | + |
| 25 | + /** @var Mockery\MockInterface&External_Checkout */ |
| 26 | + private $handler; |
| 27 | + |
| 28 | + public function setUp() : void |
| 29 | + { |
| 30 | + parent::setUp(); |
| 31 | + |
| 32 | + Mockery::mock('\WC_Payment_Gateway'); |
| 33 | + |
| 34 | + $this->gateway = Mockery::mock(SV_WC_Payment_Gateway::class); |
| 35 | + $this->handler = Mockery::mock(External_Checkout::class); |
| 36 | + |
| 37 | + $this->testObject = Mockery::mock(Frontend::class) |
| 38 | + ->shouldAllowMockingProtectedMethods() |
| 39 | + ->makePartial(); |
| 40 | + |
| 41 | + $this->testObject->allows('get_gateway')->andReturn($this->gateway); |
| 42 | + $this->testObject->allows('get_handler')->andReturn($this->handler); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * @covers ::get_js_handler_args() |
| 47 | + * @throws ReflectionException |
| 48 | + */ |
| 49 | + public function testCanGetJsHandlerArgs() : void |
| 50 | + { |
| 51 | + $this->gateway->allows('get_id') |
| 52 | + ->andReturn($gatewayId = 'TEST_GATEWAY_ID'); |
| 53 | + |
| 54 | + $this->gateway->allows('get_id_dasherized') |
| 55 | + ->andReturn($gatewayIdDasherized = 'TEST_GATEWAY_ID_DASHERIZED'); |
| 56 | + |
| 57 | + $this->handler->allows('get_gateway_merchant_id') |
| 58 | + ->andReturn($gatewayMerchantId = 'TEST_GATEWAY_MERCHANT_ID'); |
| 59 | + |
| 60 | + $this->gateway->allows('get_environment') |
| 61 | + ->andReturn('production'); |
| 62 | + |
| 63 | + $this->gateway->allows('get_plugin') |
| 64 | + ->andReturn($gatewayPlugin = Mockery::mock(SV_WC_Payment_Gateway_Plugin::class)); |
| 65 | + |
| 66 | + $this->testObject->allows('get_plugin') |
| 67 | + ->andReturn($gatewayPlugin); |
| 68 | + |
| 69 | + $gatewayPlugin->allows('get_id') |
| 70 | + ->andReturn($gatewayPluginId = 'TEST_GATEWAY_PLUGIN_ID'); |
| 71 | + |
| 72 | + $gatewayPlugin->allows('get_gateway') |
| 73 | + ->andReturn($this->gateway); |
| 74 | + |
| 75 | + $this->handler->expects('get_merchant_id') |
| 76 | + ->andReturn($merchantId = 'TEST_MERCHANT_ID'); |
| 77 | + |
| 78 | + $this->handler->expects('get_merchant_name') |
| 79 | + ->andReturn($merchantName = 'TEST_MERCHANT_NAME'); |
| 80 | + |
| 81 | + WP_Mock::userFunction('admin_url') |
| 82 | + ->once() |
| 83 | + ->with('admin-ajax.php') |
| 84 | + ->andReturn($ajaxUrl = 'https://domain.test/admin-ajax.php'); |
| 85 | + |
| 86 | + WP_Mock::userFunction('wp_create_nonce') |
| 87 | + ->once() |
| 88 | + ->with('wc_'.$gatewayId.'_google_pay_recalculate_totals') |
| 89 | + ->andReturn($recalculateTotalsNonce = 'TEST_RECALCULATE_TOTALS_NONCE'); |
| 90 | + |
| 91 | + WP_Mock::userFunction('wp_create_nonce') |
| 92 | + ->once() |
| 93 | + ->with('wc_'.$gatewayId.'_google_pay_process_payment') |
| 94 | + ->andReturn($processNonce = 'TEST_PROCESS_NONCE'); |
| 95 | + |
| 96 | + $this->handler->expects('get_button_style') |
| 97 | + ->andReturn($buttonStyle = 'TEST_BUTTON_STYLE'); |
| 98 | + |
| 99 | + $this->handler->expects('get_supported_networks') |
| 100 | + ->andReturn($cardTypes = 'TEST_CARD_TYPES'); |
| 101 | + |
| 102 | + $this->handler->expects('get_available_countries') |
| 103 | + ->andReturn($availableCountries = 'TEST_AVAILABLE_COUNTRIES'); |
| 104 | + |
| 105 | + WP_Mock::userFunction('get_woocommerce_currency') |
| 106 | + ->once() |
| 107 | + ->andReturn($currency = 'USD'); |
| 108 | + |
| 109 | + $expectedArgs = [ |
| 110 | + 'plugin_id' => $gatewayPluginId, |
| 111 | + 'merchant_id' => $merchantId, |
| 112 | + 'merchant_name' => $merchantName, |
| 113 | + 'gateway_id' => $gatewayId, |
| 114 | + 'gateway_id_dasherized' => $gatewayIdDasherized, |
| 115 | + 'gateway_merchant_id' => $gatewayMerchantId, |
| 116 | + 'environment' => 'PRODUCTION', |
| 117 | + 'ajax_url' => $ajaxUrl, |
| 118 | + 'recalculate_totals_nonce' => $recalculateTotalsNonce, |
| 119 | + 'process_nonce' => $processNonce, |
| 120 | + 'button_style' => $buttonStyle, |
| 121 | + 'card_types' => $cardTypes, |
| 122 | + 'available_countries' => $availableCountries, |
| 123 | + 'currency_code' => $currency, |
| 124 | + 'generic_error' => 'An error occurred, please try again or try an alternate form of payment', |
| 125 | + ]; |
| 126 | + |
| 127 | + WP_Mock::onFilter('wc_'.$gatewayId.'_google_pay_js_handler_params') |
| 128 | + ->with($expectedArgs)->reply($expectedArgs); |
| 129 | + |
| 130 | + $this->assertSame($expectedArgs, $this->invokeInaccessibleMethod($this->testObject, 'get_js_handler_args')); |
| 131 | + } |
| 132 | +} |
0 commit comments