From e01820870753e8cabab8c3f48bb08769b4534c86 Mon Sep 17 00:00:00 2001 From: Clement Boirie Date: Wed, 14 May 2025 23:02:26 +0200 Subject: [PATCH 1/3] Update PHPDoc for `patch` commands - Remove extra spaces in `cache patch` PHPDoc annotation - Specify default value for `expiration` argument in `transient patch` PHPDoc annotation --- src/Cache_Command.php | 8 ++++---- src/Transient_Command.php | 5 ++++- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/Cache_Command.php b/src/Cache_Command.php index b56eda0f..8b2f556f 100644 --- a/src/Cache_Command.php +++ b/src/Cache_Command.php @@ -499,10 +499,10 @@ function ( $key ) { * --- * * [--expiration=] - * : Define how long to keep the value, in seconds. `0` means as long as possible. - * --- - * default: 0 - * --- + * : Define how long to keep the value, in seconds. `0` means as long as possible. + * --- + * default: 0 + * --- * * [--format=] * : The serialization format for the value. diff --git a/src/Transient_Command.php b/src/Transient_Command.php index 9166935a..8fa0623e 100644 --- a/src/Transient_Command.php +++ b/src/Transient_Command.php @@ -497,6 +497,9 @@ function ( $key ) { * * [--expiration=] * : Time until expiration, in seconds. + * --- + * default: 0 + * --- * * [--network] * : Get the value of a network|site transient. On single site, this is @@ -505,7 +508,7 @@ function ( $key ) { */ public function patch( $args, $assoc_args ) { list( $action, $key ) = $args; - $expiration = (int) Utils\get_flag_value( $assoc_args, 'expiration', 0 ); + $expiration = (int) Utils\get_flag_value( $assoc_args, 'expiration' ); $read_func = Utils\get_flag_value( $assoc_args, 'network' ) ? 'get_site_transient' : 'get_transient'; $write_func = Utils\get_flag_value( $assoc_args, 'network' ) ? 'set_site_transient' : 'set_transient'; From a3c3782cb6ec054fe01ab5555a89615a4934fd6a Mon Sep 17 00:00:00 2001 From: Clement Boirie Date: Thu, 15 May 2025 06:33:29 +0200 Subject: [PATCH 2/3] Add tests covering additional `pluch`/`patch` arguments --- features/cache-patch.feature | 37 ++++++++++++++++++++++++++++++++ features/cache-pluck.feature | 12 +++++++++++ features/transient-patch.feature | 12 +++++++++++ features/transient-pluck.feature | 12 +++++++++++ 4 files changed, 73 insertions(+) diff --git a/features/cache-patch.feature b/features/cache-patch.feature index 770cb14a..fe027adb 100644 --- a/features/cache-patch.feature +++ b/features/cache-patch.feature @@ -8,6 +8,7 @@ Feature: Patch command available for the object cache $set_foo = function(){ wp_cache_set( 'my_key', ['foo' => 'bar'] ); wp_cache_set( 'other_key', ['fuz' => 'biz'] ); + wp_cache_set( 'my_key_in_group', ['fuz' => 'biz'], 'my_group' ); $complex_key = (object) [ 'foo' => (object) [ @@ -34,12 +35,24 @@ Feature: Patch command available for the object cache Success: Updated cache key 'complex_key'. """ + When I try `wp cache patch insert my_key_in_group foo bar --group=my_group` + Then STDOUT should be: + """ + Success: Updated cache key 'my_key_in_group'. + """ + When I try `wp cache patch insert unknown_key foo bar` Then STDERR should be: """ Error: Cannot create key "foo" on data type boolean """ + When I try `wp cache patch insert other_key foo bar --group=unknown_group` + Then STDERR should be: + """ + Error: Cannot create key "foo" on data type boolean + """ + When I run `wp cache patch update my_key foo test` Then STDOUT should be: """ @@ -58,6 +71,12 @@ Feature: Patch command available for the object cache Success: Updated cache key 'complex_key'. """ + When I try `wp cache patch update my_key_in_group fuz baz --group=my_group` + Then STDOUT should be: + """ + Success: Updated cache key 'my_key_in_group'. + """ + When I try `wp cache patch update unknown_key foo test` Then STDERR should be: """ @@ -70,12 +89,24 @@ Feature: Patch command available for the object cache Error: No data exists for key "bar" """ + When I try `wp cache patch update my_key foo baz --expiration=60` + Then STDOUT should be: + """ + Success: Updated cache key 'my_key'. + """ + When I run `wp cache patch delete my_key foo` Then STDOUT should be: """ Success: Updated cache key 'my_key'. """ + When I try `wp cache patch delete my_key_in_group fuz --group=my_group` + Then STDOUT should be: + """ + Success: Updated cache key 'my_key_in_group'. + """ + When I try `wp cache patch delete unknown_key foo` Then STDERR should be: """ @@ -87,3 +118,9 @@ Feature: Patch command available for the object cache """ Error: No data exists for key "bar" """ + + When I try `wp cache patch delete my_key foo --group=my_group` + Then STDERR should be: + """ + Error: No data exists for key "foo" + """ diff --git a/features/cache-pluck.feature b/features/cache-pluck.feature index bec68e34..c930b084 100644 --- a/features/cache-pluck.feature +++ b/features/cache-pluck.feature @@ -26,6 +26,18 @@ Feature: Pluck command available for the object cache baz """ + When I run `wp cache pluck my_key_2 foo bar --format=json` + Then STDOUT should be: + """ + "baz" + """ + + When I run `wp cache pluck my_key_2 foo --format=json` + Then STDOUT should be: + """ + {"bar":"baz"} + """ + When I run `wp cache pluck my_key_3 foo --group=my_custom_group` Then STDOUT should be: """ diff --git a/features/transient-patch.feature b/features/transient-patch.feature index f44628db..f588cd9d 100644 --- a/features/transient-patch.feature +++ b/features/transient-patch.feature @@ -53,6 +53,18 @@ Feature: Patch command available for the transient cache {"foo":{"bar":"baz","fuz":"biz"}} """ + When I run `wp transient patch insert my_key fiz bar --expiration=300` + Then STDOUT should be: + """ + Success: Updated transient 'my_key'. + """ + + When I run `wp transient get my_key --format=json` + Then STDOUT should be: + """ + {"foo":"bar","fuz":"baz","fiz":"bar"} + """ + When I try `wp transient patch insert unknown_key foo bar` Then STDERR should be: """ diff --git a/features/transient-pluck.feature b/features/transient-pluck.feature index 61be9ade..baaef81d 100644 --- a/features/transient-pluck.feature +++ b/features/transient-pluck.feature @@ -17,6 +17,18 @@ Feature: Pluck command available for the transient cache baz """ + When I run `wp transient pluck my_key_2 foo bar --format=json` + Then STDOUT should be: + """ + "baz" + """ + + When I run `wp transient pluck my_key_2 foo --format=json` + Then STDOUT should be: + """ + {"bar":"baz"} + """ + When I try `wp transient pluck unknown_key foo` Then STDERR should be: """ From 4bf6850d21b6bda7b4367b02111fd6a3dcaa0ae1 Mon Sep 17 00:00:00 2001 From: Clement Boirie Date: Thu, 15 May 2025 21:58:21 +0200 Subject: [PATCH 3/3] Cast expiration value as integer in `cache patch` command --- src/Cache_Command.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Cache_Command.php b/src/Cache_Command.php index 8b2f556f..bb99e403 100644 --- a/src/Cache_Command.php +++ b/src/Cache_Command.php @@ -516,7 +516,7 @@ function ( $key ) { public function patch( $args, $assoc_args ) { list( $action, $key ) = $args; $group = Utils\get_flag_value( $assoc_args, 'group' ); - $expiration = Utils\get_flag_value( $assoc_args, 'expiration' ); + $expiration = (int) Utils\get_flag_value( $assoc_args, 'expiration' ); $key_path = array_map( function ( $key ) {