Tests: Add missing tests for the _wp_array_get() function.

Follow-up to [49143], [49144], [49580].

Props jorgefilipecosta, johnbillion.
See #51461, #51720, #52625.

git-svn-id: https://develop.svn.wordpress.org/trunk@50964 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Sergey Biryukov 2021-05-24 11:06:15 +00:00
parent c35bfc0378
commit cea9485383

View File

@ -0,0 +1,286 @@
<?php
/**
* Tests for the _wp_array_get() function
*
* @since 5.6.0
*
* @group functions.php
* @covers ::_wp_array_get
*/
class Tests_Functions_wpArrayGet extends WP_UnitTestCase {
/**
* Test _wp_array_get() with invalid parameters.
*
* @ticket 51720
*/
public function test_wp_array_get_invalid_parameters() {
$this->assertSame(
_wp_array_get(
null,
array( 'a' )
),
null
);
$this->assertSame(
_wp_array_get(
array(
'key' => 4,
),
null
),
null
);
$this->assertSame(
_wp_array_get(
array(
'key' => 4,
),
array()
),
null
);
$this->assertSame(
_wp_array_get(
array(
'key' => 4,
),
array(),
true
),
true
);
}
/**
* Test _wp_array_get() with non subtree paths.
*
* @ticket 51720
*/
public function test_wp_array_get_simple_non_subtree() {
// Simple non sub tree test.
$this->assertSame(
_wp_array_get(
array(
'key' => 4,
),
array( 'key' ),
),
4
);
// Simple non sub tree not found.
$this->assertSame(
_wp_array_get(
array(
'key' => 4,
),
array( 'invalid' ),
),
null
);
// Simple non sub tree not found with a default.
$this->assertSame(
_wp_array_get(
array(
'key' => 4,
),
array( 'invalid' ),
1
),
1
);
// Simple non sub tree integer path.
$this->assertSame(
_wp_array_get(
array(
'a',
'b',
'c',
),
array( 1 )
),
'b'
);
}
/**
* Test _wp_array_get() with zero strings.
*
* @ticket 51720
*/
public function test_wp_array_get_handle_zeros() {
$this->assertSame(
_wp_array_get(
array(
'-0' => 'a',
'0' => 'b',
),
array( 0 )
),
'b'
);
$this->assertSame(
_wp_array_get(
array(
'-0' => 'a',
'0' => 'b',
),
array( -0 )
),
'b'
);
$this->assertSame(
_wp_array_get(
array(
'-0' => 'a',
'0' => 'b',
),
array( '-0' )
),
'a'
);
$this->assertSame(
_wp_array_get(
array(
'-0' => 'a',
'0' => 'b',
),
array( '0' )
),
'b'
);
}
/**
* 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.
*
* @ticket 51720
*/
public function test_wp_array_get_null() {
$this->assertSame(
_wp_array_get(
array(
'key' => null,
),
array( 'key' ),
true
),
null
);
$this->assertSame(
_wp_array_get(
array(
'key' => null,
),
array( 'key', 'subkey' ),
true
),
true
);
$this->assertSame(
_wp_array_get(
array(
'key' => array(
null => 4,
),
),
array( 'key', null ),
true
),
4
);
}
/**
* Test _wp_array_get() with empty paths.
*
* @ticket 51720
*/
public function test_wp_array_get_empty_paths() {
$this->assertSame(
_wp_array_get(
array(
'a' => 4,
),
array()
),
null
);
$this->assertSame(
_wp_array_get(
array(
'a' => array(
'b' => array(
'c' => 1,
),
),
),
array( 'a', 'b', array() )
),
null
);
}
}