Skip to content

Commit acf70e5

Browse files
committed
Add tests
1 parent 960cdde commit acf70e5

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed

tests/ArrayFindTest.php

+80
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,88 @@
77
class ArrayFindTest extends TestCase {
88

99
public function testArrayFind() {
10+
$array1 = [
11+
"a" => 1,
12+
"b" => 2,
13+
"c" => 3,
14+
"d" => 4,
15+
"e" => 5,
16+
];
17+
18+
$array2 = [
19+
1, 2, 3, 4, 5,
20+
];
21+
22+
$evenFunction = static function($input) {
23+
return $input % 2 === 0;
24+
};
25+
1026
self::assertSame(array_find(['apple', 'banana'], static function($value, $key) {
1127
return $value === 'banana';
1228
}), 'banana');
29+
30+
self::assertSame(2, array_find($array1, $evenFunction));
31+
32+
self::assertNull(array_find($array2, static function($value) {
33+
return $value > 5;
34+
}));
35+
36+
self::assertNull(array_find($array2, static function($value) {
37+
return $value > 5;
38+
}));
39+
40+
self::assertNull(array_find([], static function($value) {
41+
return true;
42+
}));
43+
44+
self::assertSame(3, array_find($array1, static function($value, $key) {
45+
return $key === "c";
46+
}));
47+
48+
self::assertNull(array_find($array1, static function($value) {
49+
return false;
50+
}));
51+
}
52+
53+
public function testArrayFindKey() {
54+
$array1 = [
55+
"a" => 1,
56+
"b" => 2,
57+
"c" => 3,
58+
"d" => 4,
59+
"e" => 5,
60+
];
61+
62+
$array2 = [
63+
1, 2, 3, 4, 5,
64+
];
65+
66+
$evenFunction = static function($input) {
67+
return $input % 2 === 0;
68+
};
69+
70+
self::assertSame("d", array_find_key($array1, static function($value) {
71+
return $value > 3;
72+
}));
73+
74+
self::assertSame(3, array_find_key($array2, static function($value) {
75+
return $value > 3;
76+
}));
77+
78+
self::assertNull(array_find_key($array2, static function($value) {
79+
return $value > 5;
80+
}));
81+
82+
self::assertNull(array_find_key([], static function($value) {
83+
return true;
84+
}));
85+
86+
self::assertSame("c", array_find_key($array1, static function($value, $key) {
87+
return $key === "c";
88+
}));
89+
90+
self::assertNull(array_find_key($array2, static function($value, $key) {
91+
return false;
92+
}));
1393
}
1494
}

0 commit comments

Comments
 (0)