Skip to content

Commit 334ec3d

Browse files
authored
Merge pull request #152 from wp-cli/151-patch-clone-object
Properly clone objects for comparison
2 parents 4c5c736 + fa6389a commit 334ec3d

File tree

5 files changed

+52
-0
lines changed

5 files changed

+52
-0
lines changed

features/option-pluck-patch.feature

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,3 +269,33 @@ Feature: Option commands have pluck and patch.
269269
"""
270270
[ "new", "bar" ]
271271
"""
272+
273+
@patch @pluck
274+
Scenario: An object value can be updated
275+
Given a WP install
276+
And a setup.php file:
277+
"""
278+
<?php
279+
$option = new stdClass;
280+
$option->test_mode = 0;
281+
$ret = update_option( 'wp_cli_test', $option );
282+
"""
283+
And I run `wp eval-file setup.php`
284+
285+
When I run `wp option pluck wp_cli_test test_mode`
286+
Then STDOUT should be:
287+
"""
288+
0
289+
"""
290+
291+
When I run `wp option patch update wp_cli_test test_mode 1`
292+
Then STDOUT should be:
293+
"""
294+
Success: Updated 'wp_cli_test' option.
295+
"""
296+
297+
When I run `wp option pluck wp_cli_test test_mode`
298+
Then STDOUT should be:
299+
"""
300+
1
301+
"""

src/Option_Command.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,9 @@ public function patch( $args, $assoc_args ) {
543543

544544
/* Need to make a copy of $current_value here as it is modified by reference */
545545
$old_value = $current_value = sanitize_option( $key, get_option( $key ) );
546+
if ( is_object( $current_value ) ) {
547+
$old_value = clone $current_value;
548+
}
546549

547550
$traverser = new RecursiveDataStructureTraverser( $current_value );
548551

src/Site_Option_Command.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,9 @@ public function patch( $args, $assoc_args ) {
372372

373373
/* Need to make a copy of $current_value here as it is modified by reference */
374374
$old_value = $current_value = sanitize_option( $key, get_site_option( $key ) );
375+
if ( is_object( $current_value ) ) {
376+
$old_value = clone $current_value;
377+
}
375378

376379
$traverser = new RecursiveDataStructureTraverser( $current_value );
377380

src/WP_CLI/CommandWithMeta.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,9 @@ public function patch( $args, $assoc_args ) {
390390

391391
/* Need to make a copy of $current_meta_value here as it is modified by reference */
392392
$current_meta_value = $old_meta_value = sanitize_meta( $meta_key, get_metadata( $this->meta_type, $object_id, $meta_key, true ), $this->meta_type );
393+
if ( is_object( $current_meta_value ) ) {
394+
$old_meta_value = clone $current_meta_value;
395+
}
393396

394397
$traverser = new RecursiveDataStructureTraverser( $current_meta_value );
395398

tests/RecursiveDataStructureTraverserTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,19 @@ function it_can_set_a_nested_object_value() {
8686
$this->assertEquals( 'new', $object->foo->bar );
8787
}
8888

89+
/** @test */
90+
function it_can_update_an_integer_object_value() {
91+
$object = (object) array(
92+
'test_mode' => 0,
93+
);
94+
$this->assertEquals( 0, $object->test_mode );
95+
96+
$traverser = new RecursiveDataStructureTraverser( $object );
97+
$traverser->update( array( 'test_mode' ), 1 );
98+
99+
$this->assertEquals( 1, $object->test_mode );
100+
}
101+
89102
/** @test */
90103
function it_can_delete_a_nested_array_value() {
91104
$array = array(

0 commit comments

Comments
 (0)