public function testExceptionIsThrownWhenCallbacksReturnsInvalidKey()
{
$array = ['v1', 'v2', 'v3', 'v4', 'v5', 'v6'];
$keyMap = [true, 1, -1, 2.1, 'str', null];
$fn = function ($v, $k, $collection) use(&$keyMap) {
return $keyMap[$k];
};
$result = [1 => [0 => 'v1', 1 => 'v2'], -1 => [2 => 'v3'], 2 => [3 => 'v4'], 'str' => [4 => 'v5'], null => [5 => 'v6']];
$this->assertSame($result, group($array, $fn));
$this->assertSame($result, group(new ArrayIterator($array), $fn));
$invalidTypes = ['resource' => stream_context_create(), 'object' => new \stdClass(), 'array' => []];
foreach ($invalidTypes as $type => $value) {
$keyMap = [$value];
try {
group(['v1'], $fn);
$this->fail(sprintf('Error expected for array key type "%s"', $type));
} catch (\Exception $e) {
$this->assertSame(sprintf('Functional\\group(): callback returned invalid array key of type "%s". Expected NULL, string, integer, double or boolean', $type), $e->getMessage());
}
}
}