-
Notifications
You must be signed in to change notification settings - Fork 8k
Expand file tree
/
Copy patharray_path_get.phpt
More file actions
94 lines (80 loc) · 2.81 KB
/
array_path_get.phpt
File metadata and controls
94 lines (80 loc) · 2.81 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
--TEST--
Test array_path_get() function
--FILE--
<?php
echo "*** Testing array_path_get() ***\n";
// Basic nested array access
$array = ['products' => ['desk' => ['price' => 100]]];
// Test nested access with array path
var_dump(array_path_get($array, ['products', 'desk', 'price']));
// Test with default value when path doesn't exist
var_dump(array_path_get($array, ['products', 'desk', 'discount'], 5));
// Test simple path with single level
$simple = ['name' => 'John', 'age' => 30];
var_dump(array_path_get($simple, ['name']));
var_dump(array_path_get($simple, ['missing'], 'default'));
// Test single level key that doesn't exist
var_dump(array_path_get($array, ['missing']));
// Test with integer key in path
$users = ['users' => [['name' => 'Alice'], ['name' => 'Bob']]];
var_dump(array_path_get($users, ['users', 0, 'name']));
var_dump(array_path_get($users, ['users', 1, 'name']));
// Test nested with missing intermediate key
var_dump(array_path_get($array, ['products', 'chair', 'price'], 75));
// Test with invalid segment type in array path
try {
var_dump(array_path_get($array, ['products', new stdClass(), 'price'], 'invalid'));
} catch (TypeError $e) {
echo $e->getMessage() . "\n";
}
// Test with invalid segment type even when path doesn't exist
$empty_array = [];
try {
var_dump(array_path_get($empty_array, ['foo', 'bar', new stdClass()], 'default'));
} catch (TypeError $e) {
echo $e->getMessage() . "\n";
}
// Test with references - ensure returned value is a copy, not a reference
$ref_array = ['data' => ['value' => 'original']];
$ref =& $ref_array['data']['value'];
$result = array_path_get($ref_array, ['data', 'value']);
var_dump($result);
$ref = 'modified';
var_dump($result); // Should still be 'original' (not affected by reference change)
// Test with default value being a reference
$default_value = 'default';
$default_ref =& $default_value;
$result_with_ref_default = array_path_get($ref_array, ['missing', 'key'], $default_ref);
var_dump($result_with_ref_default);
$default_value = 'changed';
var_dump($result_with_ref_default); // Should still be 'default' (not affected by reference change)
// Test with reference to an array in the path
$array2 = ['world'];
$array_with_ref = ['hello' => &$array2];
var_dump(array_path_get($array_with_ref, ['hello', 0]));
// Test with path segment that is a reference
$key1 = 'products';
$key2 = 'desk';
$path_with_refs = [&$key1, &$key2, 'price'];
var_dump(array_path_get($array, $path_with_refs));
echo "Done";
?>
--EXPECT--
*** Testing array_path_get() ***
int(100)
int(5)
string(4) "John"
string(7) "default"
NULL
string(5) "Alice"
string(3) "Bob"
int(75)
Path segment must be of type string|int, stdClass given
Path segment must be of type string|int, stdClass given
string(8) "original"
string(8) "original"
string(7) "default"
string(7) "default"
string(5) "world"
int(100)
Done