-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayFetchTest.php
More file actions
53 lines (43 loc) · 1.43 KB
/
ArrayFetchTest.php
File metadata and controls
53 lines (43 loc) · 1.43 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
<?php
declare(strict_types=1);
namespace Recruiter\Array\Tests;
use PHPUnit\Framework\TestCase;
use function Recruiter\Array\array_fetch;
class ArrayFetchTest extends TestCase
{
private array $array;
protected function setUp(): void
{
$this->array = [0, 1, 2, null, 'a' => 1, 'b' => null];
}
public function testArrayFetch(): void
{
$this->assertSame(0, array_fetch($this->array, 0));
$this->assertSame(1, array_fetch($this->array, 'a'));
$this->assertSame(null, array_fetch($this->array, 3));
$this->assertSame(null, array_fetch($this->array, 'b'));
}
public function testArrayFetchFallback(): void
{
$this->assertSame('fallback', array_fetch($this->array, 4, 'fallback'));
$this->assertSame('fallback', array_fetch($this->array, 'c', 'fallback'));
$this->assertSame(null, array_fetch($this->array, 'c', null));
}
public function testArrayFetchClosure(): void
{
$this->assertSame(
4,
array_fetch($this->array, 4, fn ($i) => $i),
);
$this->assertSame(
'c',
array_fetch($this->array, 'c', fn ($i) => $i),
);
}
public function testError(): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('key not found 4');
$this->assertSame('fallback', array_fetch($this->array, '4'));
}
}