Skip to content

Commit

Permalink
Added tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Aashish committed Jan 8, 2024
1 parent 271b47a commit fe2a1a2
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 16 deletions.
5 changes: 2 additions & 3 deletions includes/gateway/class-omise-payment-truemoney.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,8 @@ public function init_form_fields()
public function payment_fields()
{
parent::payment_fields();

if (self::WALLET === $this->source_type) {
Omise_Util::render_view( 'templates/payment/form-truemoney.php', array() );
Omise_Util::render_view( 'templates/payment/form-truemoney.php', [] );
}
}

Expand Down Expand Up @@ -118,7 +117,7 @@ public function get_charge_request($order_id, $order)
* Return the right ShopeePay backend depending on the platform and availability of
* the backend in the capability
*/
private function get_source()
public function get_source()
{
$capabilities = Omise_Capabilities::retrieve();

Expand Down
106 changes: 93 additions & 13 deletions tests/unit/includes/gateway/class-omise-payment-truemoney-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class Omise_Payment_Truemoney_Test extends Omise_Offsite_Test
{
private $obj;
private $mockOmiseCapability;

public function setUp(): void
{
Expand All @@ -20,44 +20,124 @@ public function setUp(): void
->with('123')
->andReturn(true);
require_once __DIR__ . '/../../../../includes/gateway/class-omise-payment-truemoney.php';
require_once __DIR__ . '/../../../../includes/class-omise-capabilities.php';

$this->obj = new Omise_Payment_Truemoney();
$this->mockOmiseCapability = Mockery::mock('alias:Omise_Capabilities');
}

public function testGetChargeRequest()
public function test_get_charge_request()
{
$this->mockOmiseCapability->shouldReceive('retrieve')->once();
// set source type to truemoney wallet
$this->obj->source_type = 'truemoney';
$obj = new Omise_Payment_Truemoney();
$obj->source_type = 'truemoney';
$orderId = 'order_123';
$expectedAmount = 999999;
$expectedCurrency = 'thb';
$orderMock = $this->getOrderMock($expectedAmount, $expectedCurrency);

$_POST['omise_phone_number_default'] = true;
$result = $this->obj->get_charge_request($orderId, $orderMock);
$result = $obj->get_charge_request($orderId, $orderMock);

$this->assertEquals($orderMock->get_billing_phone(), $result['source']['phone_number']);
}

public function testGetChargeRequestWhenCustomerOverridesDefaultPhone()
public function test_get_charge_request_when_customer_overrides_default_phone()
{
$this->mockOmiseCapability->shouldReceive('retrieve')->once();
$orderId = 'order_123';
$expectedAmount = 999999;
$expectedCurrency = 'thb';
$orderMock = $this->getOrderMock($expectedAmount, $expectedCurrency);

$_POST['omise_phone_number_default'] = false;
$_POST['omise_phone_number'] = '1234567890';

$result = $this->obj->get_charge_request($orderId, $orderMock);

$obj = new Omise_Payment_Truemoney();
$result = $obj->get_charge_request($orderId, $orderMock);

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

public function testCharge()
public function test_charge()
{
$this->mockOmiseCapability->shouldReceive('retrieve')->once();
$_POST['omise_phone_number_default'] = true;
$this->getChargeTest($this->obj);
$obj = new Omise_Payment_Truemoney();
$this->getChargeTest($obj);
}

public function test_get_source_returns_jumpapp()
{
$this->mockOmiseCapability->shouldReceive('retrieve');
$obj = new Omise_Payment_Truemoney();
$source_type = $obj->get_source();
$this->assertEquals('truemoney_jumpapp', $source_type);
}

public function test_get_source_returns_wallet()
{
$this->mockOmiseCapability->shouldReceive('retrieve')
->andReturn(new class() {
public function get_truemoney_backend($source_type) {
if ('truemoney' === $source_type) {
return (object)[
'truemoney' => [
'type' => 'truemoney',
'currencies' => [
'thb'
],
'amount' => [
'min' => 2000,
'max' => 500000000000
]
]
];
}

return null;
}
});

$obj = new Omise_Payment_Truemoney();
$source_type = $obj->get_source();
$this->assertEquals('truemoney', $source_type);
}

public function test_get_source_returns_jumpapp_when_both_are_enabled()
{
$this->mockOmiseCapability->shouldReceive('retrieve')
->andReturn(new class() {
public function get_truemoney_backend($source_type) {
if ('truemoney' === $source_type) {
return (object)[
'truemoney' => [
'type' => 'truemoney',
'currencies' => [
'thb'
],
'amount' => [
'min' => 2000,
'max' => 500000000000
]
]
];
}

return (object)[
'truemoney_jumpapp' => [
'type' => 'truemoney_jumpapp',
'currencies' => [
'thb'
],
'amount' => [
'min' => 2000,
'max' => 500000000000
]
]
];
}
});

$obj = new Omise_Payment_Truemoney();
$source_type = $obj->get_source();
$this->assertEquals('truemoney_jumpapp', $source_type);
}
}

0 comments on commit fe2a1a2

Please sign in to comment.