General: Some documentation and test improvements for the _wp_array_set():

* Update the function DocBlock per the documentation standards.
* Move the unit tests to a more appropriate place.
* Rename and reorder the tests for consistency with `_wp_array_get()` tests.

Follow-up to [50958], [50962], [50964].

See #53175, #52625.

git-svn-id: https://develop.svn.wordpress.org/trunk@50965 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Sergey Biryukov
2021-05-24 12:24:14 +00:00
parent cea9485383
commit fa0fca4670
3 changed files with 119 additions and 111 deletions

View File

@@ -4636,6 +4636,7 @@ function _wp_array_get( $array, $path, $default = null ) {
*
* $array = array();
* _wp_array_set( $array, array( 'a', 'b', 'c', 1 );
*
* $array becomes:
* array(
* 'a' => array(
@@ -4645,9 +4646,14 @@ function _wp_array_get( $array, $path, $default = null ) {
* ),
* );
*
* @param array $array An array that we want to mutate to include a specific value in a path.
* @param array $path An array of keys describing the path that we want to mutate.
* @param mixed $value The value that will be set.
* @internal
*
* @since 5.8.0
* @access private
*
* @param array $array An array that we want to mutate to include a specific value in a path.
* @param array $path An array of keys describing the path that we want to mutate.
* @param mixed $value The value that will be set.
*/
function _wp_array_set( &$array, $path, $value = null ) {
// Confirm $array is valid.
@@ -4659,10 +4665,13 @@ function _wp_array_set( &$array, $path, $value = null ) {
if ( ! is_array( $path ) ) {
return;
}
$path_length = count( $path );
if ( 0 === $path_length ) {
return;
}
foreach ( $path as $path_element ) {
if (
! is_string( $path_element ) && ! is_integer( $path_element ) &&
@@ -4682,6 +4691,7 @@ function _wp_array_set( &$array, $path, $value = null ) {
}
$array = &$array[ $path_element ]; // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.VariableRedeclaration
}
$array[ $path[ $i ] ] = $value;
}

View File

@@ -57,12 +57,12 @@ class Tests_Functions_wpArrayGet extends WP_UnitTestCase {
}
/**
* Test _wp_array_get() with non subtree paths.
* Test _wp_array_get() with non-subtree paths.
*
* @ticket 51720
*/
public function test_wp_array_get_simple_non_subtree() {
// Simple non sub tree test.
// Simple non-subtree test.
$this->assertSame(
_wp_array_get(
array(
@@ -73,7 +73,7 @@ class Tests_Functions_wpArrayGet extends WP_UnitTestCase {
4
);
// Simple non sub tree not found.
// Simple non-subtree not found.
$this->assertSame(
_wp_array_get(
array(
@@ -84,7 +84,7 @@ class Tests_Functions_wpArrayGet extends WP_UnitTestCase {
null
);
// Simple non sub tree not found with a default.
// Simple non-subtree not found with a default.
$this->assertSame(
_wp_array_get(
array(
@@ -96,7 +96,7 @@ class Tests_Functions_wpArrayGet extends WP_UnitTestCase {
1
);
// Simple non sub tree integer path.
// Simple non-subtree integer path.
$this->assertSame(
_wp_array_get(
array(
@@ -110,6 +110,55 @@ class Tests_Functions_wpArrayGet extends WP_UnitTestCase {
);
}
/**
* Test _wp_array_get() with subtrees.
*
* @ticket 51720
*/
public function test_wp_array_get_subtree() {
$this->assertSame(
_wp_array_get(
array(
'a' => array(
'b' => array(
'c' => 1,
),
),
),
array( 'a', 'b' )
),
array( 'c' => 1 )
);
$this->assertSame(
_wp_array_get(
array(
'a' => array(
'b' => array(
'c' => 1,
),
),
),
array( 'a', 'b', 'c' )
),
1
);
$this->assertSame(
_wp_array_get(
array(
'a' => array(
'b' => array(
'c' => 1,
),
),
),
array( 'a', 'b', 'c', 'd' )
),
null
);
}
/**
* Test _wp_array_get() with zero strings.
*
@@ -161,56 +210,6 @@ class Tests_Functions_wpArrayGet extends WP_UnitTestCase {
);
}
/**
* Test _wp_array_get() with subtrees.
*
* @ticket 51720
*/
public function test_wp_array_get_subtree() {
$this->assertSame(
_wp_array_get(
array(
'a' => array(
'b' => array(
'c' => 1,
),
),
),
array( 'a', 'b' )
),
array( 'c' => 1 )
);
$this->assertSame(
_wp_array_get(
array(
'a' => array(
'b' => array(
'c' => 1,
),
),
),
array( 'a', 'b', 'c' )
),
1
);
$this->assertSame(
_wp_array_get(
array(
'a' => array(
'b' => array(
'c' => 1,
),
),
),
array( 'a', 'b', 'c', 'd' )
),
null
);
}
/**
* Test _wp_array_get() with null values.
*

View File

@@ -1,22 +1,63 @@
<?php
/**
* _wp_array_set.
*
* @package WordPress
*/
/**
* Test _wp_array_set function.
* Tests for the _wp_array_get() function
*
* @package WordPress
* @since 5.8.0
*
* @group functions.php
* @covers ::_wp_array_get
*/
class WP_Array_Set_Test extends WP_UnitTestCase {
class Tests_Functions_wpArraySet extends WP_UnitTestCase {
/**
* Test _wp_array_set() with simple non subtree path.
* Test _wp_array_set() with invalid parameters.
*
* @ticket 53175
*/
public function test_simple_not_subtree_set() {
public function test_wp_array_set_invalid_parameters() {
$test = 3;
_wp_array_set( $test, array( 'a' ), 1 );
$this->assertSame(
$test,
3
);
$test_array = array( 'a' => 2 );
_wp_array_set( $test_array, 'a', 3 );
$this->assertSame(
$test_array,
array( 'a' => 2 )
);
$test_array = array( 'a' => 2 );
_wp_array_set( $test_array, null, 3 );
$this->assertSame(
$test_array,
array( 'a' => 2 )
);
$test_array = array( 'a' => 2 );
_wp_array_set( $test_array, array(), 3 );
$this->assertSame(
$test_array,
array( 'a' => 2 )
);
$test_array = array( 'a' => 2 );
_wp_array_set( $test_array, array( 'a', array() ), 3 );
$this->assertSame(
$test_array,
array( 'a' => 2 )
);
}
/**
* Test _wp_array_set() with simple non-subtree path.
*
* @ticket 53175
*/
public function test_wp_array_set_simple_non_subtree() {
$test_array = array();
_wp_array_set( $test_array, array( 'a' ), 1 );
$this->assertSame(
@@ -47,7 +88,7 @@ class WP_Array_Set_Test extends WP_UnitTestCase {
*
* @ticket 53175
*/
public function test_subtree_set() {
public function test_wp_array_set_subtree() {
$test_array = array();
_wp_array_set( $test_array, array( 'a', 'b', 'c' ), 1 );
$this->assertSame(
@@ -91,46 +132,4 @@ class WP_Array_Set_Test extends WP_UnitTestCase {
)
);
}
/**
* Test _wp_array_set() with invalid parameters.
*
* @ticket 53175
*/
public function test_invalid_parameters_set() {
$test = 3;
_wp_array_set( $test, array( 'a' ), 1 );
$this->assertSame(
$test,
3
);
$test_array = array( 'a' => 2 );
_wp_array_set( $test_array, 'a', 3 );
$this->assertSame(
$test_array,
array( 'a' => 2 )
);
$test_array = array( 'a' => 2 );
_wp_array_set( $test_array, null, 3 );
$this->assertSame(
$test_array,
array( 'a' => 2 )
);
$test_array = array( 'a' => 2 );
_wp_array_set( $test_array, array(), 3 );
$this->assertSame(
$test_array,
array( 'a' => 2 )
);
$test_array = array( 'a' => 2 );
_wp_array_set( $test_array, array( 'a', array() ), 3 );
$this->assertSame(
$test_array,
array( 'a' => 2 )
);
}
}