Skip to content

Commit

Permalink
Update SiteCapabilities to use Transient class
Browse files Browse the repository at this point in the history
  • Loading branch information
BrianHenryIE committed Sep 10, 2024
1 parent 0731d31 commit 6d44bee
Show file tree
Hide file tree
Showing 2 changed files with 204 additions and 5 deletions.
27 changes: 22 additions & 5 deletions includes/SiteCapabilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace NewfoldLabs\WP\Module\Data;

use NewfoldLabs\WP\Module\Data\Helpers\Transient;

/**
* Class SiteCapabilities
*
Expand All @@ -11,6 +13,22 @@
*/
class SiteCapabilities {

/**
* Implementation of transient functionality which uses the WordPress options table when an object cache is present.
*
* @var Transient
*/
protected $transient;

/**
* Constructor.
*
* @param ?Transient $transient Inject instance of Transient class.
*/
public function __construct( ?Transient $transient = null ) {
$this->transient = $transient ?? new Transient();
}

/**
* Get the value of a capability.
*
Expand All @@ -31,10 +49,10 @@ public function get( string $capability ): bool {
* Get all capabilities.
*/
protected function all(): array {
$capabilities = get_transient( 'nfd_site_capabilities' );
$capabilities = $this->transient->get( 'nfd_site_capabilities' );
if ( false === $capabilities ) {
$capabilities = $this->fetch();
set_transient( 'nfd_site_capabilities', $capabilities, 4 * HOUR_IN_SECONDS );
$this->transient->set( 'nfd_site_capabilities', $capabilities, 4 * constant( 'HOUR_IN_SECONDS' ) );
}

return $capabilities;
Expand All @@ -52,13 +70,13 @@ protected function exists( string $capability ): bool {
/**
* Fetch all capabilities from Hiive.
*
* @return array
* @return array<string, bool>
*/
protected function fetch(): array {
$capabilities = array();

$response = wp_remote_get(
NFD_HIIVE_URL . '/sites/v1/capabilities',
constant( 'NFD_HIIVE_URL' ) . '/sites/v1/capabilities',
array(
'headers' => array(
'Content-Type' => 'application/json',
Expand All @@ -78,5 +96,4 @@ protected function fetch(): array {

return $capabilities;
}

}
182 changes: 182 additions & 0 deletions tests/wpunit/includes/SiteCapabilitiesWPUnitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
<?php
/**
* SiteCapabilities has only one public method: {@see \NewfoldLabs\WP\Module\Data\SiteCapabilities::get()}
*/

namespace NewfoldLabs\WP\Module\Data;

use Mockery;
use NewfoldLabs\WP\Module\Data\Helpers\Transient;
use WP_Error;

/**
* @coversDefaultClass \NewfoldLabs\WP\Module\Data\SiteCapabilities
*/
class SiteCapabilitiesWPUnitTest extends \lucatume\WPBrowser\TestCase\WPTestCase {

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

require_once codecept_root_dir( 'vendor/antecedent/patchwork/Patchwork.php' );

\Patchwork\redefine(
'constant',
function ( string $constant_name ) {
switch ( $constant_name ) {
case 'NFD_HIIVE_URL':
return 'https://hiive.localhost';
default:
return \Patchwork\relay( func_get_args() );
}
}
);
}

protected function tearDown() {
Mockery::resetContainer();
}

/**
* @covers ::get
* @covers ::exists
* @covers ::all
*/
public function test_get_capability(): void {

$transient = Mockery::mock( Transient::class );

$transient->shouldReceive( 'get' )
->once()
->with( 'nfd_site_capabilities' )
->andReturn( array( 'test_capability' => true ) );

$sut = new SiteCapabilities( $transient );

$result = $sut->get( 'test_capability' );

$this->assertTrue( $result );
}

/**
* @covers ::all
* @covers ::fetch
*/
public function test_fetch_capabilities(): void {

$transient = Mockery::mock( Transient::class );

$transient->shouldReceive( 'get' )
->once()
->with( 'nfd_site_capabilities' )
->andReturnFalse();

/**
* @see \WP_Http::request()
*/
add_filter(
'pre_http_request',
function () {
return array(
'body' => json_encode( array( 'test_capability' => true ) ),
'response' => array( 'code' => 200 ),
);
}
);

$transient->shouldReceive( 'set' )
->once()
->with(
'nfd_site_capabilities',
array( 'test_capability' => true ),
14400,
)
->andReturnTrue();

$sut = new SiteCapabilities( $transient );

$result = $sut->get( 'test_capability' );

$this->assertTrue( $result );
}

/**
* @covers ::all
* @covers ::fetch
*/
public function test_fetch_capabilities_wp_error(): void {

$transient = Mockery::mock( Transient::class );

$transient->shouldReceive( 'get' )
->once()
->with( 'nfd_site_capabilities' )
->andReturnFalse();

/**
* @see \WP_Http::request()
*/
add_filter(
'pre_http_request',
function () {
return new WP_Error( 'could_not_connect', 'Could not connect to Hiive' );
}
);

$transient->shouldReceive( 'set' )
->once()
->with(
'nfd_site_capabilities',
array(),
14400,
)
->andReturnTrue();

$sut = new SiteCapabilities( $transient );

$result = $sut->get( 'test_capability' );

$this->assertFalse( $result );
}

/**
* @covers ::all
* @covers ::fetch
*/
public function test_fetch_capabilities_401(): void {

$transient = Mockery::mock( Transient::class );

$transient->shouldReceive( 'get' )
->once()
->with( 'nfd_site_capabilities' )
->andReturnFalse();

/**
* @see \WP_Http::request()
*/
add_filter(
'pre_http_request',
function () {
return array(
'body' => '',
'response' => array( 'code' => 401 ),
);
}
);

$transient->shouldReceive( 'set' )
->once()
->with(
'nfd_site_capabilities',
array(),
14400,
)
->andReturnTrue();

$sut = new SiteCapabilities( $transient );

$result = $sut->get( 'test_capability' );

$this->assertFalse( $result );
}
}

0 comments on commit 6d44bee

Please sign in to comment.