|
8 | 8 | use Illuminate\Support\Collection; |
9 | 9 | use Illuminate\Support\ItemNotFoundException; |
10 | 10 | use Illuminate\Support\MultipleItemsFoundException; |
| 11 | +use Illuminate\Support\Str; |
11 | 12 | use InvalidArgumentException; |
12 | 13 | use PHPUnit\Framework\Attributes\IgnoreDeprecations; |
13 | 14 | use PHPUnit\Framework\TestCase; |
@@ -993,6 +994,37 @@ public function testMap() |
993 | 994 | $this->assertEquals(['first' => 'taylor', 'last' => 'otwell'], $data); |
994 | 995 | } |
995 | 996 |
|
| 997 | + public function testMapKeys() |
| 998 | + { |
| 999 | + // Basic transformation |
| 1000 | + $array = ['first_name' => 'Taylor', 'last_name' => 'Otwell']; |
| 1001 | + $result = Arr::mapKeys($array, fn($key) => Str::camel($key)); |
| 1002 | + $this->assertEquals(['firstName' => 'Taylor', 'lastName' => 'Otwell'], $result); |
| 1003 | + |
| 1004 | + // With value parameter |
| 1005 | + $array = ['name' => 'Taylor', 'age' => 31]; |
| 1006 | + $result = Arr::mapKeys($array, fn($key, $value) => is_numeric($value) ? "num_{$key}" : $key); |
| 1007 | + $this->assertEquals(['name' => 'Taylor', 'num_age' => 31], $result); |
| 1008 | + |
| 1009 | + // Key collision - last wins |
| 1010 | + $array = ['name' => 'First', 'NAME' => 'Second']; |
| 1011 | + $result = Arr::mapKeys($array, fn($key) => strtolower($key)); |
| 1012 | + $this->assertEquals(['name' => 'Second'], $result); |
| 1013 | + |
| 1014 | + // Empty array |
| 1015 | + $this->assertEquals([], Arr::mapKeys([], fn($key) => $key)); |
| 1016 | + |
| 1017 | + // Numeric keys |
| 1018 | + $array = [1, 2, 3]; |
| 1019 | + $result = Arr::mapKeys($array, fn($key) => "item_{$key}"); |
| 1020 | + $this->assertEquals(['item_0' => 1, 'item_1' => 2, 'item_2' => 3], $result); |
| 1021 | + |
| 1022 | + // Prefix example |
| 1023 | + $config = ['host' => 'localhost', 'port' => 3306, 'database' => 'app']; |
| 1024 | + $result = Arr::mapKeys($config, fn($key) => "db_{$key}"); |
| 1025 | + $this->assertEquals(['db_host' => 'localhost', 'db_port' => 3306, 'db_database' => 'app'], $result); |
| 1026 | + } |
| 1027 | + |
996 | 1028 | public function testMapWithEmptyArray() |
997 | 1029 | { |
998 | 1030 | $mapped = Arr::map([], static function ($value, $key) { |
|
0 commit comments