From 375709013e4f4b50f531d7297e516418f248abc7 Mon Sep 17 00:00:00 2001 From: Aashish Date: Thu, 3 Aug 2023 20:41:50 +0700 Subject: [PATCH] Updated test --- .../class-omise-payment-installment.php | 14 ++-- .../class-omise-payment-installment-test.php | 65 ++++++++++++------- 2 files changed, 47 insertions(+), 32 deletions(-) diff --git a/includes/gateway/class-omise-payment-installment.php b/includes/gateway/class-omise-payment-installment.php index 6ac0e096..c12790f1 100644 --- a/includes/gateway/class-omise-payment-installment.php +++ b/includes/gateway/class-omise-payment-installment.php @@ -70,7 +70,10 @@ public function payment_fields() parent::payment_fields(); $currency = get_woocommerce_currency(); - $cart_total = $this->getTotalAmount(); + + global $wp; + $cart_total = $this->getTotalAmount($wp); + $capabilities = $this->backend->capabilities(); $installmentMinLimit = $capabilities->getInstallmentMinLimit(); @@ -87,13 +90,10 @@ public function payment_fields() /** * Get the total amount of an order */ - public function getTotalAmount() + public function getTotalAmount($wp) { - // if it's order pay page for admin then get the order total - global $wp; - - if ( isset($wp->query_vars['order-pay']) && absint($wp->query_vars['order-pay']) > 0 ) { - $order_id = absint($wp->query_vars['order-pay']); // The order ID + if ( isset($wp->query_vars['order-pay']) && (int)$wp->query_vars['order-pay'] > 0 ) { + $order_id = (int)$wp->query_vars['order-pay']; // The order ID $order = wc_get_order( $order_id ); // Get the WC_Order Object instance return $order->total; diff --git a/tests/unit/includes/gateway/class-omise-payment-installment-test.php b/tests/unit/includes/gateway/class-omise-payment-installment-test.php index 45e4d3ba..e6155832 100644 --- a/tests/unit/includes/gateway/class-omise-payment-installment-test.php +++ b/tests/unit/includes/gateway/class-omise-payment-installment-test.php @@ -5,34 +5,14 @@ class Omise_Payment_Installment_Test extends TestCase { + private $wp; + public function setUp(): void { $offsite = Mockery::mock('overload:Omise_Payment_Offsite'); $offsite->shouldReceive('init_settings'); $offsite->shouldReceive('get_option'); - if (!function_exists('wc_get_order')) { - function wc_get_order($orderId) { - $class = new stdClass(); - $class->total = 999999; - return $class; - } - } - - if (!function_exists('WC')) { - function WC() { - $class = new stdClass(); - $class->cart = new stdClass(); - $class->cart->total = 999999; - return $class; - } - } - - if (!isset($_GLOBALS['wp'])) { - $wp = new stdClass(); - $wp->query_vars = ['order-pay' => 11]; - } - if (!function_exists('wp_kses')) { function wp_kses() {} } @@ -42,6 +22,8 @@ function add_action() {} } require_once __DIR__ . '/../../../../includes/gateway/class-omise-payment-installment.php'; + + $this->wp = new stdClass(); } /** @@ -55,11 +37,44 @@ public function tearDown(): void /** * @test */ - public function getTotalAmount() + public function getTotalAmountFromAdminOrderpage() + { + $totalAmount = 999999; + + if (!function_exists('wc_get_order')) { + function wc_get_order($orderId) { + $class = new stdClass(); + $class->total = 999999; + return $class; + } + } + + $this->wp->query_vars = ['order-pay' => 11]; + $installment = new Omise_Payment_Installment(); + $total = $installment->getTotalAmount($this->wp); + + $this->assertEquals($total, $totalAmount); + } + + /** + * @test + */ + public function getTotalAmountCart() { + $totalAmount = 999999; + + if (!function_exists('WC')) { + function WC() { + $class = new stdClass(); + $class->cart = new stdClass(); + $class->cart->total = 999999; + return $class; + } + } + $installment = new Omise_Payment_Installment(); - $total = $installment->getTotalAmount(); + $total = $installment->getTotalAmount($this->wp); - $this->assertEquals($total, 999999); + $this->assertEquals($total, $totalAmount); } }