This repository has been archived on 2020-03-30. You can view files and clone it, but cannot push or open issues or pull requests.
ext-collections/tests/006-associate-by.phpt

23 lines
708 B
Plaintext
Raw Normal View History

--TEST--
Test Collection::associateBy() and Collection::associateByTo().
--FILE--
<?php
$array = ['a' => 'b', 'cd' => 'd', 'fgh' => 'i'];
$collection = Collection::init($array)
->associateBy(function ($value, $key) {
return strlen($key) - 1;
});
2018-08-28 14:11:09 +00:00
if ($collection->toArray() != array_values($array)) {
echo 'Collection::associateBy() failed.', PHP_EOL;
2018-08-28 14:11:09 +00:00
}
$array = ['foo' => 'bar'];
$collection = Collection::init($array);
$array1 = $collection->associateByTo($collection, function ($value, $key) {
return $key;
})->toArray();
2018-08-28 14:11:09 +00:00
if ($array1 != $array + $collection->toArray() || $collection->toArray() != $array1) {
2018-09-05 16:00:15 +00:00
echo 'Collection::associateByTo() failed.', PHP_EOL;
2018-08-28 14:11:09 +00:00
}
?>
--EXPECT--