Skip to content

Commit

Permalink
add mapToDictionary tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Connor S. Parks authored and Connor S. Parks committed Oct 2, 2017
1 parent d348e70 commit 7d8d816
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions tests/Support/SupportCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1257,6 +1257,35 @@ public function testFlatMap()
$this->assertEquals(['programming', 'basketball', 'music', 'powerlifting'], $data->all());
}

public function testMapToDictionary()
{
$data = new Collection([
['id' => 1, 'name' => 'A'],
['id' => 2, 'name' => 'B'],
['id' => 3, 'name' => 'C'],
['id' => 4, 'name' => 'B'],
]);

$groups = $data->mapToDictionary(function ($item, $key) {
return [$item['name'] => $item['id']];
});

$this->assertInstanceOf(Collection::class, $groups);
$this->assertEquals(['A' => [1], 'B' => [2, 4], 'C' => [3]], $groups->toArray());
$this->assertInternalType('array', $groups['A']);
}

public function testMapToDictionaryWithNumericKeys()
{
$data = new Collection([1, 2, 3, 2, 1]);

$groups = $data->mapToDictionary(function ($item, $key) {
return [$item => $key];
});

$this->assertEquals([1 => [0, 4], 2 => [1, 3], 3 => [2]], $groups->toArray());
}

public function testMapToGroups()
{
$data = new Collection([
Expand Down

0 comments on commit 7d8d816

Please sign in to comment.