Completed
Push — master ( 41ae82...437c90 )
by Dave
13s queued 11s
created

ArrayUtilsTest::testGetInasAsStringNoneString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace DaveLiddament\StaticAnalysisResultsBaseliner\Tests\Unit\Core\Utils;
6
7
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\Utils\ArrayParseException;
8
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\Utils\ArrayUtils;
9
use PHPUnit\Framework\TestCase;
10
11
class ArrayUtilsTest extends TestCase
12
{
13
    private const TEST_ARRAY = [
14
        self::NAME_KEY => self::NAME_VALUE,
15
        self::AGE_KEY => self::AGE_VALUE,
16
        self::ADDRESS_KEY => self::ADDRESS_VALUE,
17
        self::INT_AS_STRING_KEY => self::INT_AS_STRING_VALUE,
18
        self::NULL_KEY => null,
19
    ];
20
21
    private const NULL_KEY = 'null';
22
    private const NAME_KEY = 'name';
23
    private const AGE_KEY = 'age';
24
    private const ADDRESS_KEY = 'address';
25
    private const NAME_VALUE = 'dave';
26
    private const AGE_VALUE = 21;
27
    private const INT_AS_STRING_KEY = 'number';
28
    private const INT_AS_STRING_VALUE = '31';
29
    private const INT_AS_INT_VALUE = 31;
30
    private const ADDRESS_VALUE = [
31
        'some street',
32
        'some town',
33
    ];
34
35
    public function testGetStringValue(): void
36
    {
37
        $actual = ArrayUtils::getStringValue(self::TEST_ARRAY, self::NAME_KEY);
38
        $this->assertSame(self::NAME_VALUE, $actual);
39
    }
40
41
    public function testGetIntValue(): void
42
    {
43
        $actual = ArrayUtils::getIntValue(self::TEST_ARRAY, self::AGE_KEY);
44
        $this->assertSame(self::AGE_VALUE, $actual);
45
    }
46
47
    public function testGetArrayValue(): void
48
    {
49
        $actual = ArrayUtils::getArrayValue(self::TEST_ARRAY, self::ADDRESS_KEY);
50
        $this->assertEquals(self::ADDRESS_VALUE, $actual);
51
    }
52
53
    public function testGetIntAsString(): void
54
    {
55
        $actual = ArrayUtils::getIntAsStringValue(self::TEST_ARRAY, self::INT_AS_STRING_KEY);
56
        $this->assertEquals(self::INT_AS_INT_VALUE, $actual);
57
    }
58
59
    public function testGetStringOnNoneString(): void
60
    {
61
        $this->expectException(ArrayParseException::class);
62
        ArrayUtils::getStringValue(self::TEST_ARRAY, self::AGE_KEY);
63
    }
64
65
    public function testGetIntOnNoneString(): void
66
    {
67
        $this->expectException(ArrayParseException::class);
68
        ArrayUtils::getIntValue(self::TEST_ARRAY, self::NAME_KEY);
69
    }
70
71
    public function testGetArrayOnNoneString(): void
72
    {
73
        $this->expectException(ArrayParseException::class);
74
        ArrayUtils::getArrayValue(self::TEST_ARRAY, self::AGE_KEY);
75
    }
76
77
    public function testGetIntOrNullWithInt(): void
78
    {
79
        $actual = ArrayUtils::getIntOrNullValue(self::TEST_ARRAY, self::AGE_KEY);
80
        $this->assertEquals(self::AGE_VALUE, $actual);
81
    }
82
83
    public function testGetIntOrNullWithNull(): void
84
    {
85
        $actual = ArrayUtils::getIntOrNullValue(self::TEST_ARRAY, self::NULL_KEY);
86
        $this->assertNull($actual);
87
    }
88
89
    public function testGetIntOrNullWithString(): void
90
    {
91
        $this->expectException(ArrayParseException::class);
92
        ArrayUtils::getIntOrNullValue(self::TEST_ARRAY, self::NAME_KEY);
93
    }
94
95
    public function testGetInasAsStringNoneString(): void
96
    {
97
        $this->expectException(ArrayParseException::class);
98
        ArrayUtils::getIntAsStringValue(self::TEST_ARRAY, self::AGE_KEY);
99
    }
100
101
    /**
102
     * @doesNotPerformAssertions
103
     */
104
    public function testAssertArrayWithArray(): void
105
    {
106
        ArrayUtils::assertArray(self::TEST_ARRAY);
107
    }
108
109
    public function testAssertArratOnNoneArray(): void
110
    {
111
        $this->expectException(ArrayParseException::class);
112
        ArrayUtils::assertArray(self::AGE_VALUE);
113
    }
114
115
    public function testInvalidArrayKey(): void
116
    {
117
        $this->expectException(ArrayParseException::class);
118
        ArrayUtils::getStringValue(self::TEST_ARRAY, 'foo');
119
    }
120
}
121