Skip to content

Commit

Permalink
Refactoring and fixing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Aashish committed Oct 10, 2023
1 parent 447b226 commit b71df3a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 40 deletions.
13 changes: 2 additions & 11 deletions includes/gateway/traits/charge-request-builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,7 @@ public function build_charge_request(
{
$currency = $order->get_currency();
$return_uri = $this->getRedirectUrl($callback_endpoint, $order_id, $order);
$description = apply_filters(
'omise_charge_params_description',
'WooCommerce Order id ' . $order_id,
$order
);
$description = 'WooCommerce Order id ' . $order_id;

return [
'amount' => Omise_Money::to_subunit($order->get_total(), $currency),
Expand All @@ -38,12 +34,7 @@ public function getMetadata($order_id, $order, $additionalData = [])
// override order_id as a reference for webhook handlers.
$orderId = [ 'order_id' => $order_id ];

echo var_dump(apply_filters('omise_charge_params_metadata', [], $order));

return array_merge(
apply_filters('omise_charge_params_metadata', [], $order),
array_merge($orderId, $additionalData)
);
return array_merge($orderId, $additionalData);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,25 @@ public function setUp(): void
require_once __DIR__ . '/../../../../includes/gateway/class-omise-payment-ocbc-digital.php';
require_once __DIR__ . '/../../../../includes/classes/class-omise-image.php';
$this->obj = new Omise_Payment_OCBC_Digital();

// mocking WP built-in functions
if (!function_exists('plugins_url')) {
function plugins_url() {
return "http://localhost";
}
}

if (!function_exists('apply_filters')) {
function apply_filters() {
return "http://localhost/image.png";
}
}

if (!function_exists('wc_get_user_agent')) {
function wc_get_user_agent() {
return "Chrome Web";
}
}
}

public function tearDown(): void
Expand Down Expand Up @@ -59,21 +78,7 @@ public function supportsIsCorrect()
*/
public function getIconReturnsCorrectImageLink()
{
// mocking WP built-in functions
if (!function_exists('plugins_url')) {
function plugins_url() {
return "http://localhost";
}
}

if (!function_exists('apply_filters')) {
function apply_filters() {
return "http://localhost/image.png";
}
}

$result = $this->obj->get_icon();

$this->assertEquals("http://localhost/image.png", $result);
}

Expand Down Expand Up @@ -106,13 +111,6 @@ public function testCharge()
$orderMock->shouldReceive('get_total')
->andReturn($expectedAmount/100); // in units

if (!function_exists('wc_get_user_agent')) {
function wc_get_user_agent() {
return "Chrome Web";
}
}

$expectedSourceType = 'mobile_banking_ocbc';
$order_id = "123";
$result = $this->obj->charge($order_id, $orderMock);
$this->assertEquals($expectedAmount, $result['amount']);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ public static function setUpBeforeClass(): void
*/
public function buildChargeRequestReturnsValidResponse()
{
if (!function_exists('get_rest_url')) {
function get_rest_url() {
return "http://localhost/";
}
}

$mock = $this->getMockForTrait('Charge_Request_Builder');
$redirectUrlMock = Mockery::mock('alias:RedirectUrl');
$redirectUrlMock->shouldReceive('create')
Expand All @@ -38,14 +44,6 @@ public function add_meta_data() {}
$source_type = 'alipay';
$callback_url = 'omise_alipay_callback';

// removed function_exists('apply_filters') check because "apply_filters"
// returns different data type such as string or array depending how it's
// implemented. So, even if "apply_filters" is already declared somewhere,
// it might not return the desired result. So, we are overriding it.
function apply_filters() {
return [];
}

$result = $mock->build_charge_request(
$order_id,
$order,
Expand Down

0 comments on commit b71df3a

Please sign in to comment.