Skip to content

Commit e4789d0

Browse files
Add tests
1 parent 952d028 commit e4789d0

File tree

2 files changed

+123
-0
lines changed

2 files changed

+123
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
--TEST--
2+
Test array_get() function
3+
--FILE--
4+
<?php
5+
/*
6+
* Test functionality of array_get()
7+
*/
8+
9+
echo "*** Testing array_get() ***\n";
10+
11+
// Basic array access
12+
$array = ['products' => ['desk' => ['price' => 100]]];
13+
14+
// Test nested access with dot notation
15+
var_dump(array_get($array, 'products.desk.price'));
16+
17+
// Test with default value when key doesn't exist
18+
var_dump(array_get($array, 'products.desk.discount', 0));
19+
20+
// Test simple key access
21+
$simple = ['name' => 'John', 'age' => 30];
22+
var_dump(array_get($simple, 'name'));
23+
var_dump(array_get($simple, 'missing', 'default'));
24+
25+
// Test with integer key
26+
$indexed = ['a', 'b', 'c'];
27+
var_dump(array_get($indexed, 0));
28+
var_dump(array_get($indexed, 5, 'not found'));
29+
30+
// Test with null key (returns whole array)
31+
$test = ['foo' => 'bar'];
32+
var_dump(array_get($test, null));
33+
34+
// Test nested with missing intermediate key
35+
var_dump(array_get($array, 'products.chair.price', 50));
36+
37+
// Test single level key that doesn't exist
38+
var_dump(array_get($array, 'missing'));
39+
40+
// Test with numeric string in path (like users.0.name)
41+
$users = ['users' => [['name' => 'Alice'], ['name' => 'Bob']]];
42+
var_dump(array_get($users, 'users.0.name'));
43+
var_dump(array_get($users, 'users.1.age', 70));
44+
45+
echo "Done";
46+
?>
47+
--EXPECT--
48+
*** Testing array_get() ***
49+
int(100)
50+
int(0)
51+
string(4) "John"
52+
string(7) "default"
53+
string(1) "a"
54+
string(9) "not found"
55+
array(1) {
56+
["foo"]=>
57+
string(3) "bar"
58+
}
59+
int(50)
60+
NULL
61+
string(5) "Alice"
62+
int(70)
63+
Done
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
--TEST--
2+
Test array_has() function
3+
--FILE--
4+
<?php
5+
/*
6+
* Test functionality of array_has()
7+
*/
8+
9+
echo "*** Testing array_has() ***\n";
10+
11+
// Basic array
12+
$array = ['product' => ['name' => 'Desk', 'price' => 100]];
13+
14+
// Test nested key exists with dot notation
15+
var_dump(array_has($array, 'product.name'));
16+
17+
// Test nested key doesn't exist
18+
var_dump(array_has($array, 'product.color'));
19+
20+
// Test intermediate key doesn't exist
21+
var_dump(array_has($array, 'category.name'));
22+
23+
// Test simple key access
24+
$simple = ['name' => 'John', 'age' => 30];
25+
var_dump(array_has($simple, 'name'));
26+
var_dump(array_has($simple, 'missing'));
27+
28+
// Test with integer key
29+
$indexed = ['a', 'b', 'c'];
30+
var_dump(array_has($indexed, 0));
31+
var_dump(array_has($indexed, 1));
32+
var_dump(array_has($indexed, 5));
33+
34+
// Test with value that is null (key exists, but value is null)
35+
$withNull = ['key' => null];
36+
var_dump(array_has($withNull, 'key'));
37+
38+
// Test with numeric string in path (like users.0.name)
39+
$users = ['users' => [['name' => 'Alice'], ['name' => 'Bob']]];
40+
var_dump(array_has($users, 'users.0.name'));
41+
var_dump(array_has($users, 'users.1.age'));
42+
var_dump(array_has($users, 'users.2.name'));
43+
44+
echo "Done";
45+
?>
46+
--EXPECT--
47+
*** Testing array_has() ***
48+
bool(true)
49+
bool(false)
50+
bool(false)
51+
bool(true)
52+
bool(false)
53+
bool(true)
54+
bool(true)
55+
bool(false)
56+
bool(true)
57+
bool(true)
58+
bool(false)
59+
bool(false)
60+
Done

0 commit comments

Comments
 (0)