From b309fac9082b62e66d2eabc818b2af7a20787320 Mon Sep 17 00:00:00 2001 From: Jordan Leven Date: Sun, 21 Apr 2024 14:12:29 -0400 Subject: [PATCH] test: Add additional coverage --- .../class-versions-storage-service-test.php | 42 +++++++++++++++++++ .../classes/class-mock-current-time.php | 3 ++ .../php-mocks/classes/class-mock-function.php | 10 +++-- test/php-mocks/classes/class-mock-wp-hash.php | 31 ++++++++++++++ 4 files changed, 82 insertions(+), 4 deletions(-) create mode 100644 test/php-mocks/classes/class-mock-wp-hash.php diff --git a/includes/services/classes/class-versions-storage-service-test.php b/includes/services/classes/class-versions-storage-service-test.php index 7f4bfe34..dc2ca623 100644 --- a/includes/services/classes/class-versions-storage-service-test.php +++ b/includes/services/classes/class-versions-storage-service-test.php @@ -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. * @@ -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(); } /** @@ -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 ); + } } diff --git a/test/php-mocks/classes/class-mock-current-time.php b/test/php-mocks/classes/class-mock-current-time.php index 869d6a14..f13ed0e8 100644 --- a/test/php-mocks/classes/class-mock-current-time.php +++ b/test/php-mocks/classes/class-mock-current-time.php @@ -19,6 +19,8 @@ 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. * @@ -26,5 +28,6 @@ final class Mock_Current_Time extends Mock_Function implements Mock_Function_Int */ public function __construct( string $mock_namespace ) { parent::__construct( $mock_namespace, self::MOCK_FUNCTION_NAME ); + $this->set_return_value( self::MOCK_TIME ); } } diff --git a/test/php-mocks/classes/class-mock-function.php b/test/php-mocks/classes/class-mock-function.php index cd1df554..56b9b7be 100644 --- a/test/php-mocks/classes/class-mock-function.php +++ b/test/php-mocks/classes/class-mock-function.php @@ -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(); } diff --git a/test/php-mocks/classes/class-mock-wp-hash.php b/test/php-mocks/classes/class-mock-wp-hash.php new file mode 100644 index 00000000..27ade628 --- /dev/null +++ b/test/php-mocks/classes/class-mock-wp-hash.php @@ -0,0 +1,31 @@ + sprintf( 'hash-%s', $a ); + parent::__construct( $mock_namespace, self::MOCK_FUNCTION_NAME, $mock_implementation ); + } +}