|
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
|
|
|
private const INVALID_KEY = 'foo'; |
|
35
|
|
|
|
|
36
|
|
|
public function testGetStringValue(): void |
|
37
|
|
|
{ |
|
38
|
|
|
$actual = ArrayUtils::getStringValue(self::TEST_ARRAY, self::NAME_KEY); |
|
39
|
|
|
$this->assertSame(self::NAME_VALUE, $actual); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function testGetIntValue(): void |
|
43
|
|
|
{ |
|
44
|
|
|
$actual = ArrayUtils::getIntValue(self::TEST_ARRAY, self::AGE_KEY); |
|
45
|
|
|
$this->assertSame(self::AGE_VALUE, $actual); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function testGetArrayValue(): void |
|
49
|
|
|
{ |
|
50
|
|
|
$actual = ArrayUtils::getArrayValue(self::TEST_ARRAY, self::ADDRESS_KEY); |
|
51
|
|
|
$this->assertEquals(self::ADDRESS_VALUE, $actual); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function testGetIntAsString(): void |
|
55
|
|
|
{ |
|
56
|
|
|
$actual = ArrayUtils::getIntAsStringValue(self::TEST_ARRAY, self::INT_AS_STRING_KEY); |
|
57
|
|
|
$this->assertEquals(self::INT_AS_INT_VALUE, $actual); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function testGetStringOnNoneString(): void |
|
61
|
|
|
{ |
|
62
|
|
|
$this->expectException(ArrayParseException::class); |
|
63
|
|
|
ArrayUtils::getStringValue(self::TEST_ARRAY, self::AGE_KEY); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function testGetIntOnNoneString(): void |
|
67
|
|
|
{ |
|
68
|
|
|
$this->expectException(ArrayParseException::class); |
|
69
|
|
|
ArrayUtils::getIntValue(self::TEST_ARRAY, self::NAME_KEY); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function testGetArrayOnNoneString(): void |
|
73
|
|
|
{ |
|
74
|
|
|
$this->expectException(ArrayParseException::class); |
|
75
|
|
|
ArrayUtils::getArrayValue(self::TEST_ARRAY, self::AGE_KEY); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function testGetIntOrNullWithInt(): void |
|
79
|
|
|
{ |
|
80
|
|
|
$actual = ArrayUtils::getIntOrNullValue(self::TEST_ARRAY, self::AGE_KEY); |
|
81
|
|
|
$this->assertEquals(self::AGE_VALUE, $actual); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
public function testGetIntOrNullWithNull(): void |
|
85
|
|
|
{ |
|
86
|
|
|
$actual = ArrayUtils::getIntOrNullValue(self::TEST_ARRAY, self::NULL_KEY); |
|
87
|
|
|
$this->assertNull($actual); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function testGetIntOrNullWithString(): void |
|
91
|
|
|
{ |
|
92
|
|
|
$this->expectException(ArrayParseException::class); |
|
93
|
|
|
ArrayUtils::getIntOrNullValue(self::TEST_ARRAY, self::NAME_KEY); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
public function testGetInAsAsStringNoneString(): void |
|
97
|
|
|
{ |
|
98
|
|
|
$this->expectException(ArrayParseException::class); |
|
99
|
|
|
ArrayUtils::getIntAsStringValue(self::TEST_ARRAY, self::AGE_KEY); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
public function testGetInAsAsStringKeyMissing(): void |
|
103
|
|
|
{ |
|
104
|
|
|
$this->expectException(ArrayParseException::class); |
|
105
|
|
|
ArrayUtils::getIntAsStringValue(self::TEST_ARRAY, self::INVALID_KEY); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @doesNotPerformAssertions |
|
110
|
|
|
*/ |
|
111
|
|
|
public function testAssertArrayWithArray(): void |
|
112
|
|
|
{ |
|
113
|
|
|
ArrayUtils::assertArray(self::TEST_ARRAY); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
public function testAssertArrayOnNoneArray(): void |
|
117
|
|
|
{ |
|
118
|
|
|
$this->expectException(ArrayParseException::class); |
|
119
|
|
|
ArrayUtils::assertArray(self::AGE_VALUE); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
public function testInvalidArrayKey(): void |
|
123
|
|
|
{ |
|
124
|
|
|
$this->expectException(ArrayParseException::class); |
|
125
|
|
|
ArrayUtils::getStringValue(self::TEST_ARRAY, self::INVALID_KEY); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
public function testGetOptionalStringWithStringValue(): void |
|
129
|
|
|
{ |
|
130
|
|
|
$actual = ArrayUtils::getOptionalStringValue(self::TEST_ARRAY, self::NAME_KEY); |
|
131
|
|
|
$this->assertEquals(self::NAME_VALUE, $actual); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
public function testGetOptionalStringWithNoKey(): void |
|
135
|
|
|
{ |
|
136
|
|
|
$actual = ArrayUtils::getOptionalStringValue(self::TEST_ARRAY, self::INVALID_KEY); |
|
137
|
|
|
$this->assertNull($actual); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
public function testGetOptionalStringWithIncorrectType(): void |
|
141
|
|
|
{ |
|
142
|
|
|
$this->expectException(ArrayParseException::class); |
|
143
|
|
|
ArrayUtils::getOptionalStringValue(self::TEST_ARRAY, self::AGE_KEY); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
public function testAssertArrayOfStringsOnArrayContainingNonStringValues(): void |
|
147
|
|
|
{ |
|
148
|
|
|
$this->expectException(ArrayParseException::class); |
|
149
|
|
|
ArrayUtils::assertArrayOfStrings(self::TEST_ARRAY); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** @doesNotPerformAssertions */ |
|
153
|
|
|
public function testAssertArrayOfStringsOnArrayContainingOnlyStrings(): void |
|
154
|
|
|
{ |
|
155
|
|
|
$array = [ |
|
156
|
|
|
'foo', |
|
157
|
|
|
'bar', |
|
158
|
|
|
]; |
|
159
|
|
|
ArrayUtils::assertArrayOfStrings($array); |
|
160
|
|
|
} |
|
161
|
|
|
} |
|
162
|
|
|
|