Skip to content

Commit

Permalink
Added more test to cover charge method.
Browse files Browse the repository at this point in the history
  • Loading branch information
Aashish committed Oct 11, 2023
1 parent 2995684 commit 7eb6401
Show file tree
Hide file tree
Showing 10 changed files with 184 additions and 160 deletions.
Original file line number Diff line number Diff line change
@@ -1,28 +1,15 @@
<?php

use PHPUnit\Framework\TestCase;
require_once __DIR__ . '/bootstrap-test-setup.php';

class Omise_Payment_Offline_Test extends TestCase
class Omise_Payment_Offline_Test extends Bootstrap_Test_Setup
{
public function setUp(): void
{
parent::setUp();
require_once __DIR__ . '/../../../../includes/gateway/abstract-omise-payment-offline.php';
}

public function getOrderMock($expectedAmount, $expectedCurrency)
{
// Create a mock of the $order object
$orderMock = Mockery::mock('WC_Order');

// Define expectations for the mock
$orderMock->shouldReceive('get_currency')
->andReturn($expectedCurrency);
$orderMock->shouldReceive('get_total')
->andReturn($expectedAmount); // in units
$orderMock->shouldReceive('add_meta_data');
return $orderMock;
}

public function charge()
{
$orderMock = $this->getOrderMock(99999, 'THB');
Expand All @@ -31,6 +18,5 @@ public function charge()
$result = $mock->charge('order_123', $orderMock);

var_dump(print_r($result, true));

}
}
98 changes: 98 additions & 0 deletions tests/unit/includes/gateway/bootstrap-test-setup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

use PHPUnit\Framework\TestCase;

abstract class Bootstrap_Test_Setup extends TestCase
{
public $sourceType;

public function setUp(): void
{
// mocking WP built-in functions
if (!function_exists('wp_kses')) {
function wp_kses() {}
}

if (!function_exists('add_action')) {
function add_action() {}
}

if (!function_exists('sanitize_text_field')) {
function sanitize_text_field() {
return 'Sanitized text';
}
}
}

public function getOrderMock($expectedAmount, $expectedCurrency)
{
// Create a mock of the $order object
$orderMock = Mockery::mock('WC_Order');

// Define expectations for the mock
$orderMock->shouldReceive('get_currency')
->andReturn($expectedCurrency);
$orderMock->shouldReceive('get_total')
->andReturn($expectedAmount); // in units
$orderMock->shouldReceive('add_meta_data');
$orderMock->shouldReceive('get_billing_phone')
->andReturn('1234567890');
$orderMock->shouldReceive('get_address')
->andReturn([
'country' => 'Thailand',
'city' => 'Bangkok',
'postcode' => '10110',
'state' => 'Bangkok',
'address_1' => 'Sukumvit Road'
]);
$orderMock->shouldReceive('get_items')
->andReturn([
[
'name' => 'T Shirt',
'subtotal' => 600,
'qty' => 1,
'product_id' => 'product_123',
'variation_id' => null
]
]);
return $orderMock;
}

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

public function getChargeTest($classObj)
{
$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');

$orderId = 'order_123';
$result = $classObj->charge($orderId, $orderMock);
$this->assertEquals($expectedAmount, $result['amount']);
$this->assertEquals($expectedCurrency, $result['currency']);
}
}

79 changes: 34 additions & 45 deletions tests/unit/includes/gateway/class-omise-offsite-test.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
<?php

use PHPUnit\Framework\TestCase;
require_once __DIR__ . '/bootstrap-test-setup.php';

abstract class Omise_Offsite_Test extends TestCase
abstract class Omise_Offsite_Test extends Bootstrap_Test_Setup
{
public $sourceType;

public function setUp(): void
{
parent::setUp();

// Mocking the parent class
$offsite = Mockery::mock('overload:Omise_Payment_Offsite');
$offsite->shouldReceive('init_settings');
Expand All @@ -18,58 +20,45 @@ public function setUp(): void
'source' => [ 'type' => $this->sourceType ]
]);

// mocking WP built-in functions
if (!function_exists('wp_kses')) {
function wp_kses() {}
}

if (!function_exists('add_action')) {
function add_action() {}
}

// destroy object and clear memory
unset($offsite);
}

public function getOrderMock($expectedAmount, $expectedCurrency)
{
// Create a mock of the $order object
$orderMock = Mockery::mock('WC_Order');

// Define expectations for the mock
$orderMock->shouldReceive('get_currency')
->andReturn($expectedCurrency);
$orderMock->shouldReceive('get_total')
->andReturn($expectedAmount); // in units
$orderMock->shouldReceive('add_meta_data');
$orderMock->shouldReceive('get_billing_phone')
->andReturn('1234567890');
$orderMock->shouldReceive('get_address')
->andReturn([
'country' => 'Thailand',
'city' => 'Bangkok',
'postcode' => '10110',
'state' => 'Bangkok',
'address_1' => 'Sukumvit Road'
]);
$orderMock->shouldReceive('get_items')
->andReturn([
[
'name' => 'T Shirt',
'subtotal' => 600,
'qty' => 1,
'product_id' => 'product_123',
'variation_id' => null
]
]);
return $orderMock;
}

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

public function getChargeTest($classObj)
{
$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');

$orderId = 'order_123';
$result = $classObj->charge($orderId, $orderMock);
$this->assertEquals($expectedAmount, $result['amount']);
$this->assertEquals($expectedCurrency, $result['currency']);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,34 +44,8 @@ public function testGetChargeRequest()

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');

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

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

$this->getChargeTest($obj);
unset($obj);
}
}
29 changes: 2 additions & 27 deletions tests/unit/includes/gateway/class-omise-payment-atome-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,35 +49,10 @@ public function testGetChargeRequest()

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']);
$this->getChargeTest($obj);
unset($_POST['omise_atome_phone_default']);
unset($obj);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,17 @@ public function testGetChargeRequest()
unset($_POST['source']);
unset($_POST[$this->sourceType . '_installment_terms']);
}

public function testCharge()
{
$_POST['source'] = ['type' => $this->sourceType];
$_POST[$this->sourceType . '_installment_terms'] = 3;

$obj = new Omise_Payment_Installment();
$this->getChargeTest($obj);
unset($obj);

unset($_POST['source']);
unset($_POST[$this->sourceType . '_installment_terms']);
}
}
33 changes: 17 additions & 16 deletions tests/unit/includes/gateway/class-omise-payment-konbini-test.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
<?php

use PHPUnit\Framework\TestCase;

class Omise_Payment_Konbini_Test extends TestCase
class Omise_Payment_Konbini_Test extends Omise_Payment_Offline_Test
{
public $expectedAmount = 999999;
public $expectedCurrency = 'thb';
Expand Down Expand Up @@ -30,19 +28,6 @@ function sanitize_text_field() {
}
}

public function getOrderMock($expectedAmount, $expectedCurrency)
{
// Create a mock of the $order object
$orderMock = Mockery::mock('WC_Order');

// Define expectations for the mock
$orderMock->shouldReceive('get_currency')
->andReturn($expectedCurrency);
$orderMock->shouldReceive('get_total')
->andReturn($expectedAmount); // in units
return $orderMock;
}

public function testGetChargeRequest()
{
$obj = new Omise_Payment_Konbini();
Expand Down Expand Up @@ -71,4 +56,20 @@ public function testGetChargeRequest()
unset($_POST['omise_konbini_phone']);
unset($obj);
}

public function testCharge()
{
$_POST['omise_konbini_name'] = 'Sanitized text';
$_POST['omise_konbini_email'] = 'omsie@opn.ooo';
$_POST['omise_konbini_phone'] = '1234567890';

$obj = new Omise_Payment_Konbini();
$this->getChargeTest($obj);
unset($obj);

unset($_POST['omise_konbini_name']);
unset($_POST['omise_konbini_email']);
unset($_POST['omise_konbini_phone']);
unset($obj);
}
}
Loading

0 comments on commit 7eb6401

Please sign in to comment.