mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-06-28 14:20:15 +00:00
General: Add _wp_array_set function.
This adds the _wp_array_set function, which is the counterpart of the existing _wp_array_get. This utility is to be used by the Global Settings work. Props nosolosw, jorgefilipecosta. See #53175. git-svn-id: https://develop.svn.wordpress.org/trunk@50958 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
130
tests/phpunit/tests/class-wp-array-set-test.php
Normal file
130
tests/phpunit/tests/class-wp-array-set-test.php
Normal file
@@ -0,0 +1,130 @@
|
||||
<?php
|
||||
/**
|
||||
* _wp_array_set.
|
||||
*
|
||||
* @package WordPress
|
||||
*/
|
||||
|
||||
/**
|
||||
* Test _wp_array_set function.
|
||||
*
|
||||
* @package WordPress
|
||||
*/
|
||||
class WP_Array_Set_Test extends WP_UnitTestCase {
|
||||
/**
|
||||
* Test _wp_array_set() with simple non subtree path.
|
||||
*/
|
||||
public function test_simple_not_subtree_set() {
|
||||
$test_array = array();
|
||||
_wp_array_set( $test_array, array( 'a' ), 1 );
|
||||
$this->assertSame(
|
||||
$test_array,
|
||||
array( 'a' => 1 )
|
||||
);
|
||||
|
||||
$test_array = array( 'a' => 2 );
|
||||
_wp_array_set( $test_array, array( 'a' ), 3 );
|
||||
$this->assertSame(
|
||||
$test_array,
|
||||
array( 'a' => 3 )
|
||||
);
|
||||
|
||||
$test_array = array( 'b' => 1 );
|
||||
_wp_array_set( $test_array, array( 'a' ), 3 );
|
||||
$this->assertSame(
|
||||
$test_array,
|
||||
array(
|
||||
'b' => 1,
|
||||
'a' => 3,
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test _wp_array_set() with subtree paths.
|
||||
*/
|
||||
public function test_subtree_set() {
|
||||
$test_array = array();
|
||||
_wp_array_set( $test_array, array( 'a', 'b', 'c' ), 1 );
|
||||
$this->assertSame(
|
||||
$test_array,
|
||||
array( 'a' => array( 'b' => array( 'c' => 1 ) ) )
|
||||
);
|
||||
|
||||
$test_array = array( 'b' => 3 );
|
||||
_wp_array_set( $test_array, array( 'a', 'b', 'c' ), 1 );
|
||||
$this->assertSame(
|
||||
$test_array,
|
||||
array(
|
||||
'b' => 3,
|
||||
'a' => array( 'b' => array( 'c' => 1 ) ),
|
||||
)
|
||||
);
|
||||
|
||||
$test_array = array(
|
||||
'b' => 3,
|
||||
'a' => 1,
|
||||
);
|
||||
_wp_array_set( $test_array, array( 'a', 'b', 'c' ), 1 );
|
||||
$this->assertSame(
|
||||
$test_array,
|
||||
array(
|
||||
'b' => 3,
|
||||
'a' => array( 'b' => array( 'c' => 1 ) ),
|
||||
)
|
||||
);
|
||||
|
||||
$test_array = array(
|
||||
'b' => 3,
|
||||
'a' => array(),
|
||||
);
|
||||
_wp_array_set( $test_array, array( 'a', 'b', 'c' ), 1 );
|
||||
$this->assertSame(
|
||||
$test_array,
|
||||
array(
|
||||
'b' => 3,
|
||||
'a' => array( 'b' => array( 'c' => 1 ) ),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test _wp_array_set() with invalid parameters.
|
||||
*/
|
||||
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 )
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user