Skip to content

Commit

Permalink
improve ArrayHelper::map tests
Browse files Browse the repository at this point in the history
+ also test with callbacks
  • Loading branch information
chriscpty committed Oct 17, 2024
1 parent ca4bae9 commit facef5a
Showing 1 changed file with 39 additions and 1 deletion.
40 changes: 39 additions & 1 deletion tests/framework/helpers/ArrayHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,44 @@ public function testMap()
'345' => 'ccc',
],
], $result);

$result = ArrayHelper::map($array,
static function (array $group) {
return $group['id'] . $group['name'];
},
static function (array $group) {
return $group['name'] . $group['class'];
}
);

$this->assertEquals([
'123aaa' => 'aaax',
'124bbb' => 'bbbx',
'345ccc' => 'cccy',
], $result);

$result = ArrayHelper::map($array,
static function (array $group) {
return $group['id'] . $group['name'];
},
static function (array $group) {
return $group['name'] . $group['class'];
},
static function (array $group) {
return $group['class'] . '-' . $group['class'];
}
);

$this->assertEquals([
'x-x' => [
'123aaa' => 'aaax',
'124bbb' => 'bbbx',
],
'y-y' => [
'345ccc' => 'cccy',
],
], $result);

}

public function testKeyExists()
Expand All @@ -759,7 +797,7 @@ public function testKeyExistsWithFloat()
if (version_compare(PHP_VERSION, '8.1.0', '>=')) {
$this->markTestSkipped('Using floats as array key is deprecated.');
}

$array = [
1 => 3,
2.2 => 4, // Note: Floats are cast to ints, which means that the fractional part will be truncated.
Expand Down

0 comments on commit facef5a

Please sign in to comment.