forked from WordPress/wordpress-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharrayFirst.php
More file actions
95 lines (88 loc) · 1.95 KB
/
arrayFirst.php
File metadata and controls
95 lines (88 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php
/**
* @group compat
*
* @covers ::array_first
*/
class Tests_Compat_arrayFirst extends WP_UnitTestCase {
/**
* @ticket 63853
*
* Test that array_first() is always available (either from PHP or WP).
*/
public function test_array_first_availability(): void {
$this->assertTrue( function_exists( 'array_first' ) );
}
/**
* @ticket 63853
*
* @dataProvider data_array_first
*
* @param mixed $expected The value extracted from the given array.
* @param array $arr The array to get the first value from.
*/
public function test_array_first( $expected, $arr ): void {
$this->assertSame( $expected, array_first( $arr ) );
}
/**
* Data provider.
*
* @return array[]
*/
public function data_array_first(): array {
$obj = new \stdClass();
return array(
'string values' => array(
'expected' => 'a',
'arr' => array( 'a', 'b', 'c' ),
),
'associative array' => array(
'expected' => 10,
'arr' => array(
'foo' => 10,
'bar' => 20,
),
),
'empty array' => array(
'expected' => null,
'arr' => array(),
),
'single element array' => array(
'expected' => 42,
'arr' => array( 42 ),
),
'null values' => array(
'expected' => null,
'arr' => array( null, 'b', 'c' ),
),
'objects' => array(
'expected' => $obj,
'arr' => array(
$obj,
1,
2,
),
),
'boolean values' => array(
'expected' => false,
'arr' => array( false, true, 1, 2, 3 ),
),
);
}
/**
* Test that array_first() returns the pointer is not the first element.
*
* @ticket 63853
*/
public function test_array_first_with_end_pointer() {
$arr = array(
'key1' => 'val1',
'key2' => 'val2',
);
// change the pointer to the last element
end( $arr );
$val = array_first( $arr );
$this->assertSame( 'val2', current( $arr ) );
$this->assertSame( 'val1', $val );
}
}