Passed
Pull Request — master (#5)
by Jonas
01:17
created

ArrayGetTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
c 1
b 0
f 0
dl 0
loc 44
ccs 24
cts 24
cp 1
rs 10
1
<?php
2
3
namespace ArrayHelpers;
4
5
use PHPUnit\Framework\TestCase;
6
7
class ArrayGetTest extends TestCase
8
{
9
    public function testWillReturnDefaultIfNotAvailable()
10
    {
11
        $data = [];
12
        $key = 'foo';
13
        $default = 'bar';
14
        $result = Arr::get($data, $key, $default);
15
        $this->assertEquals($default, $result);
16
        $result = array_get($data, $key, $default);
17
        $this->assertEquals($default, $result);
18
    }
19
20
    public function testWillReturnNullIfNoDefaultSpecified()
21
    {
22
        $data = [];
23
        $key = 'foo';
24
        $this->assertNull(Arr::get($data, $key));
25
        $this->assertNull(array_get($data, $key));
26
    }
27
28
    public function testWillReturnValueForKey()
29
    {
30
        $data = [
31
            'foo' => 'fighters',
32
            'bar' => 'tenders',
33
        ];
34
        $key = 'foo';
35
        $result = Arr::get($data, $key);
36
        $this->assertEquals('fighters', $result);
37
        $result = array_get($data, $key);
38
        $this->assertEquals('fighters', $result);
39
    }
40
41
    public function testWillFindUsingDotNotation()
42
    {
43
        $data = [
44
            'i' => [
45
                "can't" => [
46
                    'get' => [
47
                        'no' => 'satisfaction',
48
                    ],
49
                ],
50
            ],
51
        ];
52
        $key = "i.can't.get.no";
53
        $result = Arr::get($data, $key);
54
        $this->assertEquals('satisfaction', $result);
55
        $result = array_get($data, $key);
56
        $this->assertEquals('satisfaction', $result);
57
    }
58
59
    public function testWillReturnDefaultWhenDotNotationKeyNotFound()
60
    {
61
        $data = [
62
            'i' => [
63
                "can't" => [
64
                    'get' => [
65
                        'no' => 'satisfaction',
66
                    ],
67
                ],
68
            ],
69
        ];
70
        $key = "yes.i.tried";
71
        $this->assertNull(Arr::get($data, $key));
72
        $this->assertNull(array_get($data, $key));
73
    }
74
}
75