diff options
Diffstat (limited to 'Zend/tests/array_unpack/string_keys.phpt')
-rw-r--r-- | Zend/tests/array_unpack/string_keys.phpt | 64 |
1 files changed, 50 insertions, 14 deletions
diff --git a/Zend/tests/array_unpack/string_keys.phpt b/Zend/tests/array_unpack/string_keys.phpt index e4cfd77f58..d446e69cab 100644 --- a/Zend/tests/array_unpack/string_keys.phpt +++ b/Zend/tests/array_unpack/string_keys.phpt @@ -1,22 +1,58 @@ --TEST-- -array unpacking with string keys (not supported) +Array unpacking with string keys --FILE-- <?php -try { - $array = [1, 2, "foo" => 3, 4]; - var_dump([...$array]); -} catch (Error $ex) { - var_dump($ex->getMessage()); -} -try { - $iterator = new ArrayIterator([1, 2, "foo" => 3, 4]); - var_dump([...$iterator]); -} catch (Error $ex) { - var_dump($ex->getMessage()); +// Works with both arrays and Traversables. +$array = [1, 2, "foo" => 3, 4]; +var_dump([...$array]); + +$iterator = new ArrayIterator([1, 2, "foo" => 3, 4]); +var_dump([...$iterator]); + +// Test overwriting behavior. +$array1 = ["foo" => 1]; +$array2 = ["foo" => 2]; +var_dump(["foo" => 0, ...$array1, ...$array2]); +var_dump(["foo" => 0, ...$array1, ...$array2, "foo" => 3]); + +// Test numeric string key from iterator. +function gen() { + yield "42" => 42; } +var_dump([...gen()]); ?> --EXPECT-- -string(36) "Cannot unpack array with string keys" -string(42) "Cannot unpack Traversable with string keys" +array(4) { + [0]=> + int(1) + [1]=> + int(2) + ["foo"]=> + int(3) + [2]=> + int(4) +} +array(4) { + [0]=> + int(1) + [1]=> + int(2) + ["foo"]=> + int(3) + [2]=> + int(4) +} +array(1) { + ["foo"]=> + int(2) +} +array(1) { + ["foo"]=> + int(3) +} +array(1) { + [0]=> + int(42) +} |