Skip to content

Commit

Permalink
test: Add additional coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
jordanleven committed Apr 21, 2024
1 parent 47c0efc commit b309fac
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 4 deletions.
42 changes: 42 additions & 0 deletions includes/services/classes/class-versions-storage-service-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,20 @@ final class Versions_Storage_Service_Test extends TestCase {
*/
private static $mock_delete_post_meta;

/**
* Our store for the mock of `current_time`.
*
* @var Mock
*/
private static $mock_current_time;

/**
* Our store for the mock of `wp_hash`.
*
* @var Mock
*/
private static $mock_wp_hash;

/**
* Initial test setup.
*
Expand All @@ -66,6 +80,23 @@ public static function setUpBeforeClass(): void {
self::$mock_delete_option = new Mocks\Mock_Delete_Option( __NAMESPACE__ );
self::$mock_update_post_meta = new Mocks\Mock_Update_Post_Meta( __NAMESPACE__ );
self::$mock_delete_post_meta = new Mocks\Mock_Delete_Post_Meta( __NAMESPACE__ );
self::$mock_current_time = new Mocks\Mock_Current_Time( __NAMESPACE__ );
self::$mock_wp_hash = new Mocks\Mock_WP_Hash( __NAMESPACE__ );
}

/**
* Test teardown.
*
* @return void
*/
public static function tearDownAfterClass(): void {
self::$mock_get_option->disable();
self::$mock_add_option->disable();
self::$mock_delete_option->disable();
self::$mock_update_post_meta->disable();
self::$mock_delete_post_meta->disable();
self::$mock_current_time->disable();
self::$mock_wp_hash->disable();
}

/**
Expand Down Expand Up @@ -129,4 +160,15 @@ public function testSetPageVersion() {
)
);
}

/**
* Returns a truncated and hashed version of the time.
*/
public function testGetNewVersion() {
self::$mock_current_time->set_return_value( '1984' );

$new_version = Versions_Storage_Service::get_new_version();

$this->assertEquals( 'hash-198', $new_version );
}
}
3 changes: 3 additions & 0 deletions test/php-mocks/classes/class-mock-current-time.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@ final class Mock_Current_Time extends Mock_Function implements Mock_Function_Int
*/
const MOCK_FUNCTION_NAME = 'current_time';

const MOCK_TIME = '06-29-2007 18:00:00';

/**
* Class constructor.
*
* @param string $mock_namespace The namespace for the function to be mocked.
*/
public function __construct( string $mock_namespace ) {
parent::__construct( $mock_namespace, self::MOCK_FUNCTION_NAME );
$this->set_return_value( self::MOCK_TIME );
}
}
10 changes: 6 additions & 4 deletions test/php-mocks/classes/class-mock-function.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,13 @@ class Mock_Function implements Mock_Function_Interface {
/**
* Class constructor.
*
* @param string $mock_namespace The namespace for the mock function.
* @param string $mock_function_name The name of the function to mock.
* @param string $mock_namespace The namespace for the mock function.
* @param string $mock_function_name The name of the function to mock.
* @param callable $mock_implementation The mock implementation.
*/
public function __construct( string $mock_namespace, string $mock_function_name ) {
$spy = new Spy( $mock_namespace, $mock_function_name, fn () => $this->mock_return_value );
public function __construct( string $mock_namespace, string $mock_function_name, callable $mock_implementation = null ) {
$implementation = (bool) $mock_implementation ? $mock_implementation : fn () => $this->mock_return_value;
$spy = new Spy( $mock_namespace, $mock_function_name, $implementation );
$this->mock_instance = $spy;
$this->mock_instance->enable();
}
Expand Down
31 changes: 31 additions & 0 deletions test/php-mocks/classes/class-mock-wp-hash.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
/**
* Our mock for the global `wp_hash` function.
*
* @package ForceRefresh
*/

namespace JordanLeven\Plugins\ForceRefresh\Mocks;

/**
* Class for the `wp_hash` Mock
*/
final class Mock_WP_Hash extends Mock_Function implements Mock_Function_Interface {

/**
* The name of the function to mock.
*
* @var string
*/
const MOCK_FUNCTION_NAME = 'wp_hash';

/**
* Class constructor.
*
* @param string $mock_namespace The namespace for the function to be mocked.
*/
public function __construct( string $mock_namespace ) {
$mock_implementation = fn( $a ) => sprintf( 'hash-%s', $a );
parent::__construct( $mock_namespace, self::MOCK_FUNCTION_NAME, $mock_implementation );
}
}

0 comments on commit b309fac

Please sign in to comment.