From f857a7172e27ffd5a4d3712477c426d62cf9cdf1 Mon Sep 17 00:00:00 2001 From: Aashish Date: Mon, 18 Sep 2023 12:15:14 +0700 Subject: [PATCH] Updated ocbc digital class to write test. --- .../class-omise-payment-ocbc-digital.php | 13 +++++-- .../gateway/class-omise-offsite-test.php | 2 ++ .../class-omise-payment-ocbc-digital-test.php | 35 ++++++++++++++++++- 3 files changed, 47 insertions(+), 3 deletions(-) diff --git a/includes/gateway/class-omise-payment-ocbc-digital.php b/includes/gateway/class-omise-payment-ocbc-digital.php index 39a58e00..2f9851f5 100644 --- a/includes/gateway/class-omise-payment-ocbc-digital.php +++ b/includes/gateway/class-omise-payment-ocbc-digital.php @@ -56,10 +56,19 @@ public function init_form_fields() { * @inheritdoc */ public function charge($order_id, $order) + { + return OmiseCharge::create($this->get_charge_request($order_id, $order)); + } + + /** + * @order_id integer + * @order object + */ + public function get_charge_request($order_id, $order) { $currency = $order->get_currency(); - return OmiseCharge::create([ + return [ 'amount' => Omise_Money::to_subunit($order->get_total(), $currency), 'currency' => $currency, 'description' => apply_filters('omise_charge_params_description', 'WooCommerce Order id ' . $order_id, $order), @@ -69,7 +78,7 @@ public function charge($order_id, $order) ], 'return_uri' => $this->getRedirectUrl("{$this->id}_callback", $order_id, $order), 'metadata' => $this->getMetadata($order_id, $order) - ]); + ]; } /** diff --git a/tests/unit/includes/gateway/class-omise-offsite-test.php b/tests/unit/includes/gateway/class-omise-offsite-test.php index 99b68419..56233409 100644 --- a/tests/unit/includes/gateway/class-omise-offsite-test.php +++ b/tests/unit/includes/gateway/class-omise-offsite-test.php @@ -11,6 +11,8 @@ public function setUp(): void $offsite->shouldReceive('init_settings'); $offsite->shouldReceive('get_option'); $offsite->shouldReceive('get_provider'); + $offsite->shouldReceive('getRedirectUrl'); + $offsite->shouldReceive('getMetadata'); // mocking WP built-in functions if (!function_exists('wp_kses')) { diff --git a/tests/unit/includes/gateway/class-omise-payment-ocbc-digital-test.php b/tests/unit/includes/gateway/class-omise-payment-ocbc-digital-test.php index a72dac51..4bdab42b 100644 --- a/tests/unit/includes/gateway/class-omise-payment-ocbc-digital-test.php +++ b/tests/unit/includes/gateway/class-omise-payment-ocbc-digital-test.php @@ -56,7 +56,7 @@ public function supportsIsCorrect() /** * @test */ - public function getIcon() + public function getIconReturnsCorrectImageLink() { // mocking WP built-in functions if (!function_exists('plugins_url')) { @@ -75,4 +75,37 @@ function apply_filters() { $this->assertEquals("http://localhost/image.png", $result); } + + /** + * @test + */ + public function getChargeRequestReturnsCorrectData() + { + $order = new class { + public function get_currency() + { + return 'thb'; + } + + public function get_total() + { + return 10000; + } + }; + + if (!function_exists('wc_get_user_agent')) { + function wc_get_user_agent() { + return "Chrome Web"; + } + } + + $expectedAmount = 1000000; + $expectedCurrency = 'thb'; + $expectedSourceType = 'mobile_banking_ocbc'; + $order_id = "123"; + $result = $this->obj->get_charge_request($order_id, $order); + $this->assertEquals($expectedAmount, $result['amount']); + $this->assertEquals($expectedCurrency, $result['currency']); + $this->assertEquals($expectedSourceType, $result['source']['type']); + } }