diff --git a/tests/unit/includes/gateway/abstract-omise-payment-offline-test.php b/tests/unit/includes/gateway/abstract-omise-payment-offline-test.php index be048760..0be53dc6 100644 --- a/tests/unit/includes/gateway/abstract-omise-payment-offline-test.php +++ b/tests/unit/includes/gateway/abstract-omise-payment-offline-test.php @@ -1,28 +1,15 @@ 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'); @@ -31,6 +18,5 @@ public function charge() $result = $mock->charge('order_123', $orderMock); var_dump(print_r($result, true)); - } } diff --git a/tests/unit/includes/gateway/bootstrap-test-setup.php b/tests/unit/includes/gateway/bootstrap-test-setup.php new file mode 100644 index 00000000..421dc1db --- /dev/null +++ b/tests/unit/includes/gateway/bootstrap-test-setup.php @@ -0,0 +1,98 @@ +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']); + } +} + diff --git a/tests/unit/includes/gateway/class-omise-offsite-test.php b/tests/unit/includes/gateway/class-omise-offsite-test.php index 8954cc93..79f06b59 100644 --- a/tests/unit/includes/gateway/class-omise-offsite-test.php +++ b/tests/unit/includes/gateway/class-omise-offsite-test.php @@ -1,13 +1,15 @@ shouldReceive('init_settings'); @@ -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']); + } } diff --git a/tests/unit/includes/gateway/class-omise-payment-alipayplus-hk-test.php b/tests/unit/includes/gateway/class-omise-payment-alipayplus-hk-test.php index 8c68dd0a..1cccb6e6 100644 --- a/tests/unit/includes/gateway/class-omise-payment-alipayplus-hk-test.php +++ b/tests/unit/includes/gateway/class-omise-payment-alipayplus-hk-test.php @@ -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); } } diff --git a/tests/unit/includes/gateway/class-omise-payment-atome-test.php b/tests/unit/includes/gateway/class-omise-payment-atome-test.php index 530cb762..47c1ac9e 100644 --- a/tests/unit/includes/gateway/class-omise-payment-atome-test.php +++ b/tests/unit/includes/gateway/class-omise-payment-atome-test.php @@ -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); } } 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 33a6fd3e..8a16749e 100644 --- a/tests/unit/includes/gateway/class-omise-payment-installment-test.php +++ b/tests/unit/includes/gateway/class-omise-payment-installment-test.php @@ -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']); + } } diff --git a/tests/unit/includes/gateway/class-omise-payment-konbini-test.php b/tests/unit/includes/gateway/class-omise-payment-konbini-test.php index 23a60044..8ff4b8b0 100644 --- a/tests/unit/includes/gateway/class-omise-payment-konbini-test.php +++ b/tests/unit/includes/gateway/class-omise-payment-konbini-test.php @@ -1,8 +1,6 @@ shouldReceive('get_currency') - ->andReturn($expectedCurrency); - $orderMock->shouldReceive('get_total') - ->andReturn($expectedAmount); // in units - return $orderMock; - } - public function testGetChargeRequest() { $obj = new Omise_Payment_Konbini(); @@ -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); + } } diff --git a/tests/unit/includes/gateway/class-omise-payment-ocbc-digital-test.php b/tests/unit/includes/gateway/class-omise-payment-ocbc-digital-test.php index 62059b47..d4eeec07 100644 --- a/tests/unit/includes/gateway/class-omise-payment-ocbc-digital-test.php +++ b/tests/unit/includes/gateway/class-omise-payment-ocbc-digital-test.php @@ -88,33 +88,6 @@ public function getIconReturnsCorrectImageLink() */ public function testCharge() { - $expectedCurrency = 'SGD'; - $expectedAmount = 1000000; // in subunits - $expectedAmount = 1000000; // in subunits - $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); - - // 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/100); // in units - - $order_id = "123"; - $result = $this->obj->charge($order_id, $orderMock); - $this->assertEquals($expectedAmount, $result['amount']); - $this->assertEquals($expectedCurrency, $result['currency']); + $this->getChargeTest($this->obj); } } diff --git a/tests/unit/includes/gateway/class-omise-payment-touch-n-go-test.php b/tests/unit/includes/gateway/class-omise-payment-touch-n-go-test.php index 1a18c01e..b565b2e7 100644 --- a/tests/unit/includes/gateway/class-omise-payment-touch-n-go-test.php +++ b/tests/unit/includes/gateway/class-omise-payment-touch-n-go-test.php @@ -23,4 +23,11 @@ public function restrictedCountriesHasRequiredCountries() unset($obj); } + + // public function testCharge() + // { + // $obj = new Omise_Payment_TouchNGo(); + // echo '
' . print_r(get_class_methods($obj), true) . ''; + // $this->getChargeTest($obj); + // } } diff --git a/tests/unit/includes/gateway/class-omise-payment-truemoney-test.php b/tests/unit/includes/gateway/class-omise-payment-truemoney-test.php index b9c94652..f71bc0d7 100644 --- a/tests/unit/includes/gateway/class-omise-payment-truemoney-test.php +++ b/tests/unit/includes/gateway/class-omise-payment-truemoney-test.php @@ -28,4 +28,12 @@ public function testGetChargeRequest() unset($_POST['omise_phone_number_default']); unset($obj); } + + public function testCharge() + { + $_POST['omise_phone_number_default'] = true; + $obj = new Omise_Payment_Truemoney(); + // echo '
' . print_r(get_class_methods($obj), true) . ''; + $this->getChargeTest($obj); + } }