From 031530305dabe2262e0130ba4a44c52fa6d33a67 Mon Sep 17 00:00:00 2001 From: Vedanshu Jain Date: Mon, 21 Aug 2023 14:20:29 +0530 Subject: [PATCH 1/2] Always return bool values from cache function. --- .../woocommerce/src/Caching/WPCacheEngine.php | 6 +- .../php/src/Caching/WPCacheEngineTest.php | 56 +++++++++++++++++++ 2 files changed, 59 insertions(+), 3 deletions(-) create mode 100644 plugins/woocommerce/tests/php/src/Caching/WPCacheEngineTest.php diff --git a/plugins/woocommerce/src/Caching/WPCacheEngine.php b/plugins/woocommerce/src/Caching/WPCacheEngine.php index 3bc65c23aa7..55373195f50 100644 --- a/plugins/woocommerce/src/Caching/WPCacheEngine.php +++ b/plugins/woocommerce/src/Caching/WPCacheEngine.php @@ -34,7 +34,7 @@ class WPCacheEngine implements CacheEngine { */ public function cache_object( string $key, $object, int $expiration, string $group = '' ): bool { $prefixed_key = self::get_prefixed_key( $key, $group ); - return wp_cache_set( $prefixed_key, $object, $group, $expiration ); + return false !== wp_cache_set( $prefixed_key, $object, $group, $expiration ); } /** @@ -47,7 +47,7 @@ class WPCacheEngine implements CacheEngine { */ public function delete_cached_object( string $key, string $group = '' ): bool { $prefixed_key = self::get_prefixed_key( $key, $group ); - return wp_cache_delete( $prefixed_key, $group ); + return false !== wp_cache_delete( $prefixed_key, $group ); } /** @@ -71,6 +71,6 @@ class WPCacheEngine implements CacheEngine { * @return bool True if the group is deleted successfully, false otherwise. */ public function delete_cache_group( string $group = '' ): bool { - return self::invalidate_cache_group( $group ); + return false !== self::invalidate_cache_group( $group ); } } diff --git a/plugins/woocommerce/tests/php/src/Caching/WPCacheEngineTest.php b/plugins/woocommerce/tests/php/src/Caching/WPCacheEngineTest.php new file mode 100644 index 00000000000..e2e4f24390e --- /dev/null +++ b/plugins/woocommerce/tests/php/src/Caching/WPCacheEngineTest.php @@ -0,0 +1,56 @@ +sut = wc_get_container()->get( WPCacheEngine::class ); + } + + /** + * @testDox Test that the cache engine can be used to cache and retrieve objects. + */ + public function test_caching_crud() { + $object_to_cache = new \stdClass(); + $object_to_cache->prop1 = 'dummy_value_1'; + $object_to_cache->prop2 = 'dummy_value_2'; + $key = 'dummy_object_key'; + $group = 'dummy_group'; + + $this->assertFalse( $this->sut->is_cached( $key, $group ) ); + $this->sut->cache_object( $key, $object_to_cache, HOUR_IN_SECONDS, $group ); + $this->assertTrue( $this->sut->is_cached( $key, $group ) ); + + $cached_obj = $this->sut->get_cached_object( $key, $group ); + $this->assertEquals( $object_to_cache->prop1, $cached_obj->prop1 ); + $this->assertEquals( $object_to_cache->prop2, $cached_obj->prop2 ); + + $this->sut->delete_cached_object( $key, $group ); + $this->assertFalse( $this->sut->is_cached( $key, $group ) ); + $this->assertNull( ( $this->sut->get_cached_object( $key, $group ) ) ); + + $this->sut->cache_object( $key, $object_to_cache, HOUR_IN_SECONDS, $group ); + $this->assertTrue( $this->sut->is_cached( $key, $group ) ); + $this->sut->delete_cache_group( $group ); + $this->assertFalse( $this->sut->is_cached( $key, $group ) ); + $this->assertNull( ( $this->sut->get_cached_object( $group ) ) ); + } + +} From fa7d9ec9202b05d47eb9a024e5eaed9703e4b5b0 Mon Sep 17 00:00:00 2001 From: github-actions Date: Mon, 21 Aug 2023 08:59:37 +0000 Subject: [PATCH 2/2] Add changefile(s) from automation for the following project(s): woocommerce --- plugins/woocommerce/changelog/fix-return_type_gurantee | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 plugins/woocommerce/changelog/fix-return_type_gurantee diff --git a/plugins/woocommerce/changelog/fix-return_type_gurantee b/plugins/woocommerce/changelog/fix-return_type_gurantee new file mode 100644 index 00000000000..c432ce6d304 --- /dev/null +++ b/plugins/woocommerce/changelog/fix-return_type_gurantee @@ -0,0 +1,4 @@ +Significance: patch +Type: fix + +Always return bool values from WPCacheEngine functions when expected.