Skip to content

Commit

Permalink
Added more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Aashish committed Oct 11, 2023
1 parent 0ed6a9c commit 5953793
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 11 deletions.
8 changes: 7 additions & 1 deletion includes/gateway/class-omise-payment-alipayplus.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,20 @@ public function init_form_fields() {
* @inheritdoc
*/
public function charge($order_id, $order)
{
$requestData = $this->get_charge_request($order_id, $order);
return OmiseCharge::create($requestData);
}

public function get_charge_request($order_id, $order)
{
$requestData = $this->build_charge_request(
$order_id, $order, $this->source_type, $this->id . "_callback"
);
$requestData['source'] = array_merge($requestData['source'], [
'platform_type' => Omise_Util::get_platform_type(wc_get_user_agent())
]);
return OmiseCharge::create($requestData);
return $requestData;
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

define('OMISE_PHP_LIB_VERSION', '2.16.0');
define('OMISE_API_URL', 'https://api.staging-omise.co/');
define('OMISE_VAULT_URL', 'https://vault.staging-omise.co/');
define('OMISE_API_URL', 'https://api.omise.co/');
define('OMISE_VAULT_URL', 'https://vault.omise.co/');

class OmiseApiResource extends OmiseObject
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

require_once __DIR__ . '/class-omise-offsite-test.php';

class Omise_Payment_Alipay_Test extends Omise_Offsite_Test
{
public function setUp(): void
{
$this->sourceType = 'alipay_hk';
parent::setUp();
require_once __DIR__ . '/../../../../includes/gateway/class-omise-payment-alipayplus.php';
}
}
46 changes: 39 additions & 7 deletions tests/unit/includes/gateway/class-omise-payment-atome-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ function plugins_url() {}
}

// dummy version
define('WC_VERSION', '1.0.0');
if (!defined('WC_VERSION')) {
define('WC_VERSION', '1.0.0');
}
}

public function testGetChargeRequest()
Expand All @@ -29,10 +31,6 @@ public function testGetChargeRequest()
$orderId = 'order_123';
$orderMock = $this->getOrderMock($expectedAmount, $expectedCurrency);

// $mock = $this->getMockBuilder('Omise_Payment_Base_Card')
// ->onlyMethods(['prepareChargeData'])
// ->getMock();

$wcProduct = Mockery::mock('overload:WC_Product');
$wcProduct->shouldReceive('get_sku')
->once()
Expand All @@ -43,11 +41,45 @@ public function testGetChargeRequest()
$obj = new Omise_Payment_Atome();
$result = $obj->get_charge_request($orderId, $orderMock);

echo '<pre>' . print_r($result, true) . '</pre>';

$this->assertEquals($this->sourceType, $result['source']['type']);

unset($_POST['source']);
unset($obj);
}

public function testCharge()
{
$orderId = 'order_123';
$expectedAmount = 999999;
$expectedCurrency = 'thb';
$expectedRequest = [
"object" => "charge",
"id" => "chrg_test_no1t4tnemucod0e51mo",
"location" => "/charges/chrg_test_no1t4tnemucod0e51mo",
"amount" => $expectedAmount,
"currency" => $expectedCurrency
];

// Create a mock for OmiseCharge
$chargeMock = Mockery::mock('overload:OmiseCharge');
$chargeMock->shouldReceive('create')->once()->andReturn($expectedRequest);

$orderMock = $this->getOrderMock($expectedAmount, $expectedCurrency);

$wcProduct = Mockery::mock('overload:WC_Product');
$wcProduct->shouldReceive('get_sku')
->once()
->andReturn('sku_1234');

$_POST['omise_atome_phone_default'] = true;

$obj = new Omise_Payment_Atome();
$result = $obj->charge($orderId, $orderMock);

$this->assertEquals($expectedAmount, $result['amount']);
$this->assertEquals($expectedCurrency, $result['currency']);

unset($_POST['source']);
unset($obj);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ function get_rest_url() {
}
}

/**
* close mockery after tests are done
*/
public function tearDown(): void
{
Mockery::close();
}

public function getOrderMock($expectedAmount, $expectedCurrency)
{
// Create a mock of the $order object
Expand Down Expand Up @@ -76,8 +84,17 @@ public function testBuildChargeRequestForOfflinePayment()

$orderMock = $this->getOrderMock($expectedAmount, $expectedCurrency);

// Create a mock of the $order object
$setting = Mockery::mock('alias:Omise_Setting')->makePartial();

$setting->shouldReceive('is_dynamic_webhook_enabled')
->shouldReceive(1);

// Define expectations for the mock
$setting->shouldReceive('instance')
->andReturn($setting);

$source_type = 'promptpay';
$callback_url = 'omise_promptpay_callback';
$mock = $this->getMockForTrait('Charge_Request_Builder');
$result = $mock->build_charge_request(
$order_id,
Expand Down

0 comments on commit 5953793

Please sign in to comment.