-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add: PHP util function equivalent to lodash set.
- Loading branch information
1 parent
9d80a39
commit a879fc9
Showing
2 changed files
with
190 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
<?php | ||
/** | ||
* Test Gutenberg Utils. | ||
* | ||
* @package Gutenberg | ||
*/ | ||
|
||
/** | ||
* Test WP_Theme_JSON class. | ||
* | ||
* @package Gutenberg | ||
*/ | ||
class Gutenberg_Utils_Test extends WP_UnitTestCase { | ||
/** | ||
* Test gutenberg_experimental_set() with simple non subtree path. | ||
*/ | ||
public function test_simple_not_subtree_set() { | ||
$test_array = array(); | ||
gutenberg_experimental_set( $test_array, array( 'a' ), 1 ); | ||
$this->assertSame( | ||
$test_array, | ||
array( 'a' => 1 ) | ||
); | ||
|
||
$test_array = array( 'a' => 2 ); | ||
gutenberg_experimental_set( $test_array, array( 'a' ), 3 ); | ||
$this->assertSame( | ||
$test_array, | ||
array( 'a' => 3 ) | ||
); | ||
|
||
$test_array = array( 'b' => 1 ); | ||
gutenberg_experimental_set( $test_array, array( 'a' ), 3 ); | ||
$this->assertSame( | ||
$test_array, | ||
array( | ||
'b' => 1, | ||
'a' => 3, | ||
) | ||
); | ||
} | ||
|
||
/** | ||
* Test gutenberg_experimental_set() with subtree paths. | ||
*/ | ||
public function test_subtree_set() { | ||
$test_array = array(); | ||
gutenberg_experimental_set( $test_array, array( 'a', 'b', 'c' ), 1 ); | ||
$this->assertSame( | ||
$test_array, | ||
array( 'a' => array( 'b' => array( 'c' => 1 ) ) ) | ||
); | ||
|
||
$test_array = array( 'b' => 3 ); | ||
gutenberg_experimental_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, | ||
); | ||
gutenberg_experimental_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(), | ||
); | ||
gutenberg_experimental_set( $test_array, array( 'a', 'b', 'c' ), 1 ); | ||
$this->assertSame( | ||
$test_array, | ||
array( | ||
'b' => 3, | ||
'a' => array( 'b' => array( 'c' => 1 ) ), | ||
) | ||
); | ||
} | ||
|
||
/** | ||
* Test gutenberg_experimental_set() with invalid parameters. | ||
*/ | ||
public function test_invalid_parameters_set() { | ||
$test = 3; | ||
gutenberg_experimental_set( $test, array( 'a' ), 1 ); | ||
$this->assertSame( | ||
$test, | ||
3 | ||
); | ||
|
||
$test_array = array( 'a' => 2 ); | ||
gutenberg_experimental_set( $test_array, 'a', 3 ); | ||
$this->assertSame( | ||
$test_array, | ||
array( 'a' => 2 ) | ||
); | ||
|
||
$test_array = array( 'a' => 2 ); | ||
gutenberg_experimental_set( $test_array, null, 3 ); | ||
$this->assertSame( | ||
$test_array, | ||
array( 'a' => 2 ) | ||
); | ||
|
||
$test_array = array( 'a' => 2 ); | ||
gutenberg_experimental_set( $test_array, array(), 3 ); | ||
$this->assertSame( | ||
$test_array, | ||
array( 'a' => 2 ) | ||
); | ||
|
||
$test_array = array( 'a' => 2 ); | ||
gutenberg_experimental_set( $test_array, array( 'a', array() ), 3 ); | ||
$this->assertSame( | ||
$test_array, | ||
array( 'a' => 2 ) | ||
); | ||
} | ||
} |