Skip to content

Commit

Permalink
Covering abstract-omise-block-apm.php with tests. Changing setUp and …
Browse files Browse the repository at this point in the history
…tearDown visibility to protected from public. Fixing failed test for class-omise-payment-duitnow-obw.php.
  • Loading branch information
Aashish committed May 30, 2024
1 parent bb5264b commit df9b16a
Show file tree
Hide file tree
Showing 30 changed files with 192 additions and 60 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ vendor
.phpunit.result.cache
composer.lock
.nvmrc
wp-content
9 changes: 8 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,16 @@
"require-dev": {
"phpunit/phpunit": "^5.7 || ^9.5",
"mockery/mockery": "^1.6",
"brain/monkey": "^2.6"
"brain/monkey": "^2.6",
"woocommerce/woocommerce-blocks": "^11.7"
},
"scripts": {
"test": "vendor/bin/phpunit --testdox --colors"
},
"config": {
"allow-plugins": {
"composer/installers": true,
"automattic/jetpack-autoloader": true
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
*/
class Omise_Page_Card_From_Customization_Test extends TestCase
{
public function setUp(): void
protected function setUp(): void
{
Brain\Monkey\setUp();
Mockery::mock('alias:Omise_Admin_Page');
require_once __DIR__ . '/../../../../includes/admin/class-omise-page-card-form-customization.php';
}

public function tearDown(): void
protected function tearDown(): void
{
Brain\Monkey\tearDown();
Mockery::close();
Expand Down
121 changes: 121 additions & 0 deletions tests/unit/includes/blocks/gateways/abstract-omise-block-apm-test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<?php

use PHPUnit\Framework\TestCase;
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
use Brain\Monkey;

class Omise_Block_Apm_Test extends TestCase
{
// Adds Mockery expectations to the PHPUnit assertions count.
use MockeryPHPUnitIntegration;

public $obj;

// @runInSeparateProcess
protected function setUp() : void
{
parent::setUp();
require_once __DIR__ . '/../../../../../includes/blocks/gateways/abstract-omise-block-apm.php';
$this->obj = new class extends Omise_Block_Apm {};
}

/**
* @test
*/
public function initialize()
{
$clazz = (object) [
'payment_gateways' => new class {
function payment_gateways() {
// dummy gateway
$gateway = new class {
public $supports = ['products'];

public function is_available() {
return true;
}

public function supports() {
return $this->supports;
}
};

return [
'abc' => $gateway,
'xyz' => $gateway,
];
}
}
];

Monkey\Functions\expect('get_option')->andReturn(null);
Monkey\Functions\expect('WC')->andReturn($clazz);

$reflection = new \ReflectionClass($this->obj);
$name_property = $reflection->getProperty('name');
$name_property->setAccessible(true);
$name_property->setValue($this->obj, 'abc');

$this->obj->initialize();

$gateway_property = $reflection->getProperty('gateway');
$gateway_property->setAccessible(true);
$gateway_val = $gateway_property->getValue($this->obj);

$this->assertEquals('object', gettype($gateway_val));
}

/**
* @test
*/
public function is_active()
{
// Calling initialize() to set $gateway value
$reflection = new \ReflectionClass($this->obj);
$name_property = $reflection->getProperty('name');
$name_property->setAccessible(true);
$name_property->setValue($this->obj, 'abc');

$this->obj->initialize();

$is_active = $this->obj->is_active();
$this->assertTrue($is_active);
}

/**
* @test
*/
public function get_payment_method_data()
{
// Calling initialize() to set $gateway value
$reflection = new \ReflectionClass($this->obj);
$name_property = $reflection->getProperty('name');
$name_property->setAccessible(true);
$name_property->setValue($this->obj, 'abc');

$this->obj->initialize();

$data = $this->obj->get_payment_method_data();

$this->assertArrayHasKey('title', $data);
$this->assertArrayHasKey('description', $data);
$this->assertArrayHasKey('supports', $data);
$this->assertEquals('array', gettype($data['supports']));
$this->assertEquals('abc', $data['name']);
}

/**
* @test
*/
public function get_payment_method_script_handles()
{
Monkey\Functions\expect('wp_script_is')->andReturn(false);
Monkey\Functions\expect('wp_register_script');
Monkey\Functions\expect('plugin_dir_url');
Monkey\Functions\expect('wp_enqueue_script');

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

$this->assertEquals([ 'wc-omise-one-click-apms-payments-blocks' ], $result);
}
}
2 changes: 1 addition & 1 deletion tests/unit/includes/class-omise-capabilities-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Omise_Capabilities_Test extends Bootstrap_Test_Setup
/**
* setup add_action and do_action before the test run
*/
public function setUp(): void
protected function setUp(): void
{
parent::setUp();

Expand Down
2 changes: 1 addition & 1 deletion tests/unit/includes/class-omise-setting-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class Omise_Setting_Test extends TestCase
{
public function setUp(): void
protected function setUp(): void
{
require_once __DIR__ . '/../../../includes/class-omise-setting.php';
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Omise_Payment_Base_Card_Test extends TestCase
{
public $obj;

public function setUp(): void
protected function setUp(): void
{
$omisePaymentMock = Mockery::mock('overload:Omise_Payment');
$omisePaymentMock->shouldReceive('is_test')
Expand Down Expand Up @@ -45,7 +45,7 @@ public function returnThis()
/**
* close mockery after tests are done
*/
public function tearDown(): void
protected function tearDown(): void
{
Mockery::close();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

abstract class Omise_Payment_Offline_Test extends Bootstrap_Test_Setup
{
public function setUp(): void
protected function setUp(): void
{
parent::setUp();
Mockery::mock('alias:Omise_Payment')->makePartial();
Expand Down
19 changes: 13 additions & 6 deletions tests/unit/includes/gateway/bootstrap-test-setup.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
<?php

use PHPUnit\Framework\TestCase;
use Brain\Monkey;
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;

// @runInSeparateProcess
abstract class Bootstrap_Test_Setup extends TestCase
{
// Adds Mockery expectations to the PHPUnit assertions count.
use MockeryPHPUnitIntegration;

public $sourceType;

public function setUp(): void
protected function setUp(): void
{
Brain\Monkey\setUp();
Brain\Monkey\Functions\stubs( [
parent::setUp();
Monkey\setUp();
Monkey\Functions\stubs( [
'wp_kses' => null,
'add_action' => null,
] );
Expand All @@ -18,10 +25,10 @@ public function setUp(): void
/**
* close mockery after tests are done
*/
public function tearDown(): void
protected function tearDown(): void
{
Brain\Monkey\tearDown();
Mockery::close();
Monkey\tearDown();
parent::tearDown();
}

public function getOrderMock($expectedAmount, $expectedCurrency)
Expand Down
5 changes: 1 addition & 4 deletions tests/unit/includes/gateway/class-omise-offsite-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ abstract class Omise_Offsite_Test extends Bootstrap_Test_Setup
{
public $sourceType;

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

Expand All @@ -19,9 +19,6 @@ public function setUp(): void
->andReturn([
'source' => [ 'type' => $this->sourceType ]
]);

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

public function getChargeTest($classObj)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/
class Omise_Payment_Alipay_Hk_Test extends Omise_Offsite_Test
{
public function setUp(): void
protected function setUp(): void
{
$this->sourceType = 'alipay_hk';
parent::setUp();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/
class Omise_Payment_Kakaopay_Test extends Omise_Offsite_Test
{
public function setUp(): void
protected function setUp(): void
{
$this->sourceType = 'kakaopay';
parent::setUp();
Expand Down
10 changes: 6 additions & 4 deletions tests/unit/includes/gateway/class-omise-payment-atome-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class Omise_Payment_Atome_Test extends Omise_Offsite_Test
{
public function setUp(): void
protected function setUp(): void
{
$this->sourceType = 'atome';
parent::setUp();
Expand All @@ -22,6 +22,11 @@ function plugins_url() {}
if (!defined('WC_VERSION')) {
define('WC_VERSION', '1.0.0');
}

Brain\Monkey\setUp();
Brain\Monkey\Functions\stubs( [
'wp_kses' => null,
] );
}

public function testGetChargeRequest()
Expand All @@ -42,9 +47,6 @@ public function testGetChargeRequest()
$result = $obj->get_charge_request($orderId, $orderMock);

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

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

public function testCharge()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class Omise_Payment_CreditCard_Test extends TestCase
{
public function setUp(): void
protected function setUp(): void
{
Brain\Monkey\setUp();

Expand Down Expand Up @@ -34,7 +34,7 @@ public function setUp(): void
require_once __DIR__ . '/../../../../includes/gateway/class-omise-payment-creditcard.php';
}

public function tearDown(): void
protected function tearDown(): void
{
Brain\Monkey\tearDown();
Mockery::close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@

class Omise_Payment_DuitNow_OBW_Test extends Omise_Offsite_Test
{
public function setUp(): void
private $omise_capability_mock;

protected function setUp(): void
{
$this->sourceType = 'duitnow_obw';
parent::setUp();
$this->omise_capability_mock = Mockery::mock('alias:Omise_Capabilities');
require_once __DIR__ . '/../../../../includes/gateway/class-omise-payment-duitnow-obw.php';
}

Expand All @@ -19,6 +22,7 @@ function sanitize_text_field() {
}
}

$this->omise_capability_mock->shouldReceive('retrieve')->once();
$_POST['source'] = ['bank' => 'SCB'];
$obj = new Omise_Payment_DuitNow_OBW();
$this->getChargeTest($obj);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class Omise_Payment_FPX_Test extends Omise_Offsite_Test
{
public function setUp(): void
protected function setUp(): void
{
$this->sourceType = 'fpx';
parent::setUp();
Expand Down
Loading

0 comments on commit df9b16a

Please sign in to comment.