-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayMapTest.php
More file actions
57 lines (46 loc) · 1.38 KB
/
ArrayMapTest.php
File metadata and controls
57 lines (46 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?php
declare(strict_types=1);
namespace Recruiter\Array\Tests;
use PHPUnit\Framework\TestCase;
use function Recruiter\Array\array_map;
use Recruiter\Array\Range;
class ArrayMapTest extends TestCase
{
public function testMap(): void
{
$this->assertSame(
[2, 4, 6],
array_map([1, 2, 3], fn ($value): int|float => $value * 2),
);
}
public function testIterator(): void
{
$this->assertSame(
[2, 4, 6],
array_map(new Range(1, 4), fn ($value): int|float => $value * 2),
);
}
public function testIdentity(): void
{
$this->assertSame([], array_map([]));
$this->assertSame([1, 2, 3], array_map([1, 2, 3]));
}
public function testIdentityPreservingKeys(): void
{
$array = ['1' => 1, '2' => 2, '3' => 3];
$this->assertSame($array, array_map($array, null, true));
$this->assertNotSame($array, array_map($array, null, false));
}
public function testIndexesAreLost(): void
{
$this->assertSame([1, 2, 3], array_map(['1' => 1, '2' => 2, '3' => 3]));
}
public function testIndexesArePassedAsParameters(): void
{
$returnKeys = (fn ($value, $key) => $key);
$this->assertSame(
['one', 'two', 'three'],
array_map(['one' => 1, 'two' => 2, 'three' => 3], $returnKeys),
);
}
}