From c204a6aa273dc899a677f16aced5941526878c60 Mon Sep 17 00:00:00 2001 From: Jonny Harris Date: Wed, 10 May 2023 09:09:06 +0000 Subject: [PATCH] Build/Test Tools: Call `wp_cache_flush_runtime` in WP_UnitTestCase. In `WP_UnitTestCase::flush_cache` method, the properties of the global `$wp_object_cache` object were manaully being reset to flush the cache. The function `wp_cache_flush_runtime` was added in [52772] and is designed to reset any class properties to default values. Using `wp_cache_flush_runtime` improve compatibility with third party object caches, as it allows developers to define their own `wp_cache_flush_runtime` function. Props rmccue, johnbillion, wonderboymusic, boonebgorges, voldemortensen, dd32, DrewAPicture, tillkruess, spacedmonkey. Fixes #31463. git-svn-id: https://develop.svn.wordpress.org/trunk@55741 602fd350-edb4-49c9-b593-d223f7449a82 --- tests/phpunit/includes/abstract-testcase.php | 7 ++----- tests/phpunit/includes/object-cache.php | 22 ++++++++++++++++++++ 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/tests/phpunit/includes/abstract-testcase.php b/tests/phpunit/includes/abstract-testcase.php index 95a0e0df57..b03d12a097 100644 --- a/tests/phpunit/includes/abstract-testcase.php +++ b/tests/phpunit/includes/abstract-testcase.php @@ -387,12 +387,9 @@ abstract class WP_UnitTestCase_Base extends PHPUnit_Adapter_TestCase { public static function flush_cache() { global $wp_object_cache; - $wp_object_cache->group_ops = array(); - $wp_object_cache->stats = array(); - $wp_object_cache->memcache_debug = array(); - $wp_object_cache->cache = array(); + wp_cache_flush_runtime(); - if ( method_exists( $wp_object_cache, '__remoteset' ) ) { + if ( is_object( $wp_object_cache ) && method_exists( $wp_object_cache, '__remoteset' ) ) { $wp_object_cache->__remoteset(); } diff --git a/tests/phpunit/includes/object-cache.php b/tests/phpunit/includes/object-cache.php index f9e2c4693e..2bea171f19 100644 --- a/tests/phpunit/includes/object-cache.php +++ b/tests/phpunit/includes/object-cache.php @@ -325,12 +325,23 @@ function wp_cache_flush( $delay = 0 ) { function wp_cache_supports( $feature ) { switch ( $feature ) { case 'get_multiple': + case 'flush_runtime': return true; default: return false; } } +/** + * Removes all cache items from the in-memory runtime cache. + * + * @return bool True on success, false on failure. + */ +function wp_cache_flush_runtime() { + global $wp_object_cache; + return $wp_object_cache->flush_runtime(); +} + /** * Retrieves object from cache. * @@ -1412,6 +1423,17 @@ class WP_Object_Cache { return $result; } + /** + * Clears the in-memory cache of all data leaving the external cache untouched. + * + * @return bool Always returns true. + */ + public function flush_runtime() { + $this->cache = array(); + + return true; + } + /** * Retrieves object from cache. *