Skip to content

Commit

Permalink
Add code coverage for omise-block-payments.php. Refactored few tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Aashish committed Jun 4, 2024
1 parent 4d723f6 commit 476219c
Show file tree
Hide file tree
Showing 23 changed files with 117 additions and 57 deletions.
38 changes: 38 additions & 0 deletions tests/unit/includes/blocks/omise-block-payments-test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

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

class Omise_Block_Payments_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/omise-block-payments.php';
}

/**
* @test
*/
public function class_is_initialized()
{
$this->expectNotToPerformAssertions();
$container = \Automattic\WooCommerce\Blocks\Package::container();

Monkey\Functions\expect('add_action')->andReturn(null);

$mock = $this->getMockBuilder(Omise_Block_Payments::class)
->setConstructorArgs([$container])
->onlyMethods(['add_payment_methods', 'initialize'])
->getMock();

new Omise_Block_Payments($container);
}
}
1 change: 0 additions & 1 deletion tests/unit/includes/blocks/omise-block-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

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

class Omise_Block_Test extends TestCase
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public function returnThis()
protected function tearDown(): void
{
Mockery::close();
parent::tearDown();
}

public function getOrderMock($expectedAmount, $expectedCurrency)
Expand Down
6 changes: 1 addition & 5 deletions tests/unit/includes/gateway/bootstrap-test-setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@ protected function setUp(): void
{
parent::setUp();
Monkey\setUp();
Monkey\Functions\stubs( [
'wp_kses' => null,
'add_action' => null,
] );
Monkey\Functions\expect('plugins_url')->andReturn('');
}

/**
Expand All @@ -29,6 +24,7 @@ protected function setUp(): void
protected function tearDown(): void
{
Monkey\tearDown();
Mockery::close();
parent::tearDown();
}

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

use Brain\Monkey;

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

/**
Expand All @@ -11,6 +13,10 @@ protected function setUp(): void
{
$this->sourceType = 'alipay_hk';
parent::setUp();

Monkey\Functions\expect('wp_kses');
Monkey\Functions\expect('wc_get_user_agent')->andReturn('Chrome Web');

require_once __DIR__ . '/../../../../includes/gateway/class-omise-payment-alipayplus.php';

if (!function_exists('wc_get_user_agent')) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

use Brain\Monkey;

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

/**
Expand All @@ -11,6 +13,7 @@ protected function setUp(): void
{
$this->sourceType = 'kakaopay';
parent::setUp();
Monkey\Functions\expect('wp_kses');
require_once __DIR__ . '/../../../../includes/gateway/class-omise-payment-alipayplus.php';
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,13 @@ protected function setUp(): void
require_once __DIR__ . '/../../../../includes/gateway/class-omise-payment-atome.php';

Monkey\Functions\expect('wp_enqueue_script');
Monkey\Functions\expect('wp_kses');
Monkey\Functions\expect('plugins_url');

// dummy version
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 @@ -47,6 +44,7 @@ public function testGetChargeRequest()

public function testCharge()
{
Monkey\Functions\expect('add_action');
$_POST['omise_atome_phone_default'] = true;
$obj = new Omise_Payment_Atome();
$this->getChargeTest($obj);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ class Omise_Payment_CreditCard_Test extends TestCase
protected function setUp(): void
{
parent::setUp();
Monkey\setUp();

$this->omisePaymentMock = Mockery::mock('overload:Omise_Payment');
$this->omisePaymentMock->shouldReceive('init_settings');
Expand Down Expand Up @@ -39,6 +38,7 @@ protected function setUp(): void
define('WC_VERSION', '1.0.0');
}

Monkey\Functions\expect('add_action')->andReturn(null);
Monkey\Functions\expect('wp_enqueue_script');
Monkey\Functions\expect('wp_kses')
->times(3)
Expand All @@ -56,6 +56,7 @@ protected function tearDown(): void
{
Monkey\tearDown();
Mockery::close();
parent::tearDown();
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

use Brain\Monkey;

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

class Omise_Payment_DuitNow_OBW_Test extends Omise_Offsite_Test
Expand All @@ -10,6 +12,7 @@ protected function setUp(): void
{
$this->sourceType = 'duitnow_obw';
parent::setUp();
Monkey\Functions\expect('add_action');
$this->omise_capability_mock = Mockery::mock('alias:Omise_Capabilities');
require_once __DIR__ . '/../../../../includes/gateway/class-omise-payment-duitnow-obw.php';
}
Expand Down
3 changes: 3 additions & 0 deletions tests/unit/includes/gateway/class-omise-payment-fpx-test.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

use Brain\Monkey;

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

class Omise_Payment_FPX_Test extends Omise_Offsite_Test
Expand All @@ -8,6 +10,7 @@ protected function setUp(): void
{
$this->sourceType = 'fpx';
parent::setUp();
Monkey\Functions\expect('add_action');
require_once __DIR__ . '/../../../../includes/backends/class-omise-backend-fpx.php';
require_once __DIR__ . '/../../../../includes/gateway/class-omise-payment-fpx.php';
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ protected function setUp(): void
parent::setUp();
Monkey\setUp();

Monkey\Functions\expect('add_action')->andReturn(null);
Monkey\Functions\expect('wp_kses')->andReturn(null);
Monkey\Functions\expect('wp_enqueue_script');
Monkey\Functions\expect('plugins_url');
Monkey\Functions\expect('plugins_url')->andReturn('');
Monkey\Functions\expect('get_woocommerce_currency')->andReturn('thb');

// dummy version
Expand Down Expand Up @@ -48,6 +49,7 @@ protected function tearDown(): void
{
Monkey\tearDown();
Mockery::close();
parent::tearDown();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,9 @@ protected function setUp(): void
{
$this->sourceType = 'installment_ktc';
parent::setUp();
Monkey\Functions\expect('wp_kses');
Monkey\Functions\expect('add_action');
require_once __DIR__ . '/../../../../includes/gateway/class-omise-payment-installment.php';

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

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

use Brain\Monkey;

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

class Omise_Payment_Internetbanking_Test extends Omise_Offsite_Test
Expand All @@ -8,6 +10,8 @@ protected function setUp(): void
{
$this->sourceType = 'fpx';
parent::setUp();
Monkey\Functions\expect('wp_kses');
Monkey\Functions\expect('add_action');
require_once __DIR__ . '/../../../../includes/gateway/class-omise-payment-internetbanking.php';
}

Expand Down
11 changes: 5 additions & 6 deletions tests/unit/includes/gateway/class-omise-payment-konbini-test.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

use Brain\Monkey;

class Omise_Payment_Konbini_Test extends Omise_Payment_Offline_Test
{
public $expectedAmount = 999999;
Expand All @@ -8,6 +10,9 @@ class Omise_Payment_Konbini_Test extends Omise_Payment_Offline_Test
protected function setUp(): void
{
parent::setUp();
Monkey\Functions\expect('wp_kses');
Monkey\Functions\expect('add_action');

// Mocking the parent class
$offline = Mockery::mock('overload:Omise_Payment_Offline');
$offline->shouldReceive('init_settings');
Expand All @@ -21,12 +26,6 @@ protected function setUp(): void
]);

require_once __DIR__ . '/../../../../includes/gateway/class-omise-payment-konbini.php';

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

public function testGetChargeRequest()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

use Brain\Monkey;

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

class Omise_Payment_Mobilebanking_Test extends Omise_Offsite_Test
Expand All @@ -13,9 +15,11 @@ protected function setUp(): void

public function testCharge()
{
Brain\Monkey\Functions\expect('wc_get_user_agent')
Monkey\Functions\expect('wc_get_user_agent')
->with('123')
->andReturn('Chrome Web');
Monkey\Functions\expect('wp_kses');
Monkey\Functions\expect('add_action');
$_POST['omise-offsite'] = 'mobile_banking_bbl';
$obj = new Omise_Payment_Mobilebanking();
$this->getChargeTest($obj);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use Brain\Monkey;
require_once __DIR__ . '/class-omise-offsite-test.php';

class Omise_Payment_OCBC_Digital_Test extends Omise_Offsite_Test
Expand All @@ -10,6 +11,7 @@ protected function setUp(): void
{
$this->sourceType = 'mobile_banking_ocbc';
parent::setUp();
Monkey\Functions\expect('add_action');
require_once __DIR__ . '/../../../../includes/gateway/class-omise-payment-ocbc-digital.php';
require_once __DIR__ . '/../../../../includes/classes/class-omise-image.php';
$this->obj = new Omise_Payment_OCBC_Digital();
Expand Down Expand Up @@ -53,6 +55,7 @@ public function supportsIsCorrect()
*/
public function getIconReturnsCorrectImageLink()
{
Monkey\Functions\expect('plugins_url');
$result = $this->obj->get_icon();
$this->assertEquals(
"<img src='/ocbc-digital.svg' class='Omise-Image' style='width: 30px; max-height: 30px;' alt='OCBC Digital' />",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

use Brain\Monkey;

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

class Omise_Payment_OCBC_PAO_Test extends Omise_Offsite_Test
Expand All @@ -10,23 +12,17 @@ protected function setUp(): void
{
$this->sourceType = 'mobile_banking_ocbc';
parent::setUp();
Monkey\Functions\expect('add_action');
require_once __DIR__ . '/../../../../includes/gateway/class-omise-payment-ocbc-pao.php';
$this->obj = new Omise_Payment_OCBC_PAO();
}

protected function tearDown(): void
{
parent::tearDown();
// destroy object and clear memory
unset($this->obj);
}

/**
* @test
*/
public function testCharge()
{
Brain\Monkey\Functions\expect('wc_get_user_agent')
Monkey\Functions\expect('wc_get_user_agent')
->with('123')
->andReturn('Chrome Web');
$this->getChargeTest($this->obj);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ function admin_url() {}
public function textExpiresAtFieldIsPassedToJavascript()
{
Monkey\Functions\expect('wp_enqueue_script');
Monkey\Functions\expect('wp_kses');
Monkey\Functions\expect('add_action');
Monkey\Functions\expect('plugins_url');
$expiresAt = '2023-11-22T14:48:00.000Z';

$this->mockFileGetContent->shouldReceive('get_contents')->once()->andReturn('<svg></svg>');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

use Brain\Monkey;

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

class Omise_Payment_RabbitLinePay_Test extends Omise_Offsite_Test
Expand All @@ -10,17 +12,11 @@ protected function setUp(): void
{
$this->sourceType = 'mobile_banking_ocbc';
parent::setUp();
Monkey\Functions\expect('add_action');
require_once __DIR__ . '/../../../../includes/gateway/class-omise-payment-rabbit-linepay.php';
$this->obj = new Omise_Payment_RabbitLinePay();
}

protected function tearDown(): void
{
parent::tearDown();
// destroy object and clear memory
unset($this->obj);
}

/**
* @test
*/
Expand Down
Loading

0 comments on commit 476219c

Please sign in to comment.