Skip to content

Commit

Permalink
update function structure
Browse files Browse the repository at this point in the history
  • Loading branch information
ajzkk committed Sep 4, 2023
1 parent 66b9a86 commit d22d6b3
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 55 deletions.
18 changes: 6 additions & 12 deletions includes/class-omise-capabilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,7 @@ class Omise_Capabilities {
*/
public static function retrieve($pKey = null, $sKey = null)
{
$shouldCallApi = self::shouldCallApi(
is_checkout(),
// If endpoint url is `order-received`, it mean thank you page.
is_wc_endpoint_url( 'order-received' ),
is_admin(),
);

if ( !$shouldCallApi ) {
if ( !self::shouldCallApi() ) {
return null;
}

Expand Down Expand Up @@ -85,18 +78,19 @@ public static function retrieve($pKey = null, $sKey = null)
* @param boolean $isThankYouPage
* @return boolean
*/
public static function shouldCallApi($isCheckout, $isThankYouPage, $isAdmin,) {
$omisePages = [ 'omise' ];
public static function shouldCallApi() {
$omiseSettingPages = [ 'omise' ];

$currentAdminPage = isset( $_GET[ 'page' ] ) ? $_GET[ 'page' ] : '';

// checkout page but not thank you page
// By default thank you page is also part of checkout pages
// and we do not need to call capabilities on thank you page.
$isPaymentPage = $isCheckout && !$isThankYouPage;
// If endpoint url is `order-received`, it mean thank you page.
$isPaymentPage = is_checkout() && !is_wc_endpoint_url( 'order-received' );

// if from admin panel and is omise page.
$isOmiseSettingPage = $isAdmin && in_array( $currentAdminPage, $omisePages );
$isOmiseSettingPage = is_admin() && in_array( $currentAdminPage, $omiseSettingPages );

return $isPaymentPage || $isOmiseSettingPage;
}
Expand Down
72 changes: 29 additions & 43 deletions tests/unit/includes/class-omise-capabilities-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,68 +27,54 @@ public function tearDown(): void
}

/**
* @dataProvider retrieve_data_provider
* @runInSeparateProcess
* @covers Omise_Capabilities
*/
public function test_retrieve_should_return_null_when_it_should_not_call_api()
{
function is_admin() { return false; }
function is_checkout() { return false; }
function is_wc_endpoint_url($page) { return true; }

$result = Omise_Capabilities::retrieve();
$this->assertEquals(null, $result);
}

/**
* @runInSeparateProcess
* @covers Omise_Capabilities
*/
public function test_retrieve_should_return_value_when_it_should_call_api()
{
function is_admin() { return false; }
function is_checkout() { return true; }
function is_wc_endpoint_url($page) { return false; }
public function test_retrieve_should_return_value_when_it_should_call_api($isCheckout, $isThankYouPage, $isAdmin, $adminPageName, $expected)
{
// assigning to global variable, so that we can use in child functions
$GLOBALS['isCheckout'] = $isCheckout;
$GLOBALS['isThankYouPage'] = $isThankYouPage;
$GLOBALS['isAdmin'] = $isAdmin;

$this->omiseSettingMock->shouldReceive('instance')->andReturn($this->omiseSettingMock);
$this->omiseSettingMock->shouldReceive('public_key')->andReturn('pkey_xxx');
$this->omiseSettingMock->shouldReceive('secret_key')->andReturn('skey_xxx');
$this->omiseCapabilitiesMock->shouldReceive('retrieve')->once();

$result = Omise_Capabilities::retrieve();
// mocking page name
$_GET['page'] = $adminPageName;

$this->assertEquals('Omise_Capabilities', get_class($result));
$this->assertTrue(true);
}
function is_checkout() { return $GLOBALS['isCheckout']; }
function is_wc_endpoint_url($page) { return $GLOBALS['isThankYouPage']; }
function is_admin() { return $GLOBALS['isAdmin']; }

/**
* @dataProvider should_call_api_data_provider
* @covers Omise_Capabilities
*/
public function test_should_call_capability_api($isCheckout, $isThankYouPage, $isAdmin, $adminPageName, $expected)
{
$_GET['page'] = $adminPageName;
$result = Omise_Capabilities::shouldCallApi($isCheckout, $isThankYouPage, $isAdmin);
$this->assertEquals($expected, $result);
if ($expected) {
$this->omiseSettingMock->shouldReceive('instance')->andReturn($this->omiseSettingMock);
$this->omiseSettingMock->shouldReceive('public_key')->andReturn('pkey_xxx');
$this->omiseSettingMock->shouldReceive('secret_key')->andReturn('skey_xxx');
$this->omiseCapabilitiesMock->shouldReceive('retrieve')->once();
$result = Omise_Capabilities::retrieve();
$this->assertEquals('Omise_Capabilities', get_class($result));
} else {
$result = Omise_Capabilities::retrieve();
$this->assertEquals(null, $result);
}
}

/**
* Data provider for toSubunitReturnCorrectFormat
*/
public function should_call_api_data_provider()
public function retrieve_data_provider()
{
return [
// checkout page and not thank you page
[true, false, false, '', true],
// checkout page and also thank you page
// // checkout page and also thank you page
[true, true, false, '', false],
// omise setting page
// // omise setting page
[true, true, true, 'omise', true],
// other admin page
// // other admin page
[true, true, true, 'other-page', false],
// non checkout page and also no-admin page
// // non checkout page and also no-admin page
[false, false, false, 'other-page', false],
// non checkout page, non admin page
// // non checkout page, non admin page
[false, false, false, '', false],
];
}
Expand Down

0 comments on commit d22d6b3

Please sign in to comment.