1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace AbterPhp\Framework\Config; |
6
|
|
|
|
7
|
|
|
use PHPUnit\Framework\TestCase; |
8
|
|
|
|
9
|
|
|
class ConfigTest extends TestCase |
10
|
|
|
{ |
11
|
|
|
private const CATEGORY = "FOO"; |
12
|
|
|
private const SETTING = "BAR01"; |
13
|
|
|
|
14
|
|
|
public function testMustGetGetsDefaultIfEnvironmentVariableIsMissing(): void |
15
|
|
|
{ |
16
|
|
|
$value = 17; |
17
|
|
|
|
18
|
|
|
$this->assertEquals($value, Config::mustGet(static::CATEGORY, static::SETTING, $value)); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function testMustGetGetsEnvironmentVariableValueByDefault(): void |
22
|
|
|
{ |
23
|
|
|
$value = 17; |
24
|
|
|
$wrong = "BARBAZ002"; |
25
|
|
|
|
26
|
|
|
Config::set(static::CATEGORY, static::SETTING, $value); |
27
|
|
|
|
28
|
|
|
$this->assertEquals($value, Config::mustGet(static::CATEGORY, static::SETTING, $wrong)); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function testMustGetThrowsExceptionIfEnvironmentVariableAndDefaultAreMissing(): void |
32
|
|
|
{ |
33
|
|
|
Config::set(static::CATEGORY, static::SETTING, null); |
34
|
|
|
|
35
|
|
|
$this->expectException(\RuntimeException::class); |
36
|
|
|
|
37
|
|
|
Config::mustGet(static::CATEGORY, static::SETTING); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function testMustGetStringThrowsExceptionIfConfigIsInvalid(): void |
41
|
|
|
{ |
42
|
|
|
Config::set(static::CATEGORY, static::SETTING, new \stdClass()); |
43
|
|
|
|
44
|
|
|
$this->expectException(\RuntimeException::class); |
45
|
|
|
|
46
|
|
|
Config::mustGetString(static::CATEGORY, static::SETTING); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function testMustGetStringReturnsStringIfConfigIsScalar(): void |
50
|
|
|
{ |
51
|
|
|
Config::set(static::CATEGORY, static::SETTING, true); |
52
|
|
|
|
53
|
|
|
$actualResult = Config::mustGetString(static::CATEGORY, static::SETTING); |
54
|
|
|
|
55
|
|
|
$this->assertEquals('1', $actualResult); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @return array |
60
|
|
|
*/ |
61
|
|
|
public function mustGetBoolDataProvider(): array |
62
|
|
|
{ |
63
|
|
|
return [ |
64
|
|
|
['0', false], |
65
|
|
|
['1', true], |
66
|
|
|
['false', false], |
67
|
|
|
['true', true], |
68
|
|
|
[0, false], |
69
|
|
|
[1, true], |
70
|
|
|
]; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @dataProvider mustGetBoolDataProvider |
75
|
|
|
* |
76
|
|
|
* @param bool|int|float|string $value |
77
|
|
|
* @param bool $expectedResult |
78
|
|
|
*/ |
79
|
|
|
public function testMustGetBoolReturnsBoolIfConfigIsBooleanLikeString($value, bool $expectedResult): void |
80
|
|
|
{ |
81
|
|
|
Config::set(static::CATEGORY, static::SETTING, $value); |
82
|
|
|
|
83
|
|
|
$actualResult = Config::mustGetBool(static::CATEGORY, static::SETTING); |
84
|
|
|
|
85
|
|
|
$this->assertEquals($expectedResult, $actualResult); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function testMustGetBoolThrowsExceptionIfStringCanNotBeConvertedToBool(): void |
89
|
|
|
{ |
90
|
|
|
$this->expectException(\RuntimeException::class); |
91
|
|
|
|
92
|
|
|
Config::set(static::CATEGORY, static::SETTING, 'yes'); |
93
|
|
|
|
94
|
|
|
Config::mustGetBool(static::CATEGORY, static::SETTING); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return array |
99
|
|
|
*/ |
100
|
|
|
public function mustGetIntDataProvider(): array |
101
|
|
|
{ |
102
|
|
|
return [ |
103
|
|
|
['354', 354], |
104
|
|
|
['-17', -17], |
105
|
|
|
]; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @dataProvider mustGetIntDataProvider |
110
|
|
|
* |
111
|
|
|
* @param bool|int|float|string $value |
112
|
|
|
* @param int $expectedResult |
113
|
|
|
*/ |
114
|
|
|
public function testMustGetIntReturnsBoolIfConfigIsBooleanLikeString($value, int $expectedResult): void |
115
|
|
|
{ |
116
|
|
|
Config::set(static::CATEGORY, static::SETTING, $value); |
117
|
|
|
|
118
|
|
|
$actualResult = Config::mustGetInt(static::CATEGORY, static::SETTING); |
119
|
|
|
|
120
|
|
|
$this->assertEquals($expectedResult, $actualResult); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function testMustGetIntThrowsExceptionIfStringCanNotBeConvertedToInt(): void |
124
|
|
|
{ |
125
|
|
|
$this->expectException(\RuntimeException::class); |
126
|
|
|
|
127
|
|
|
Config::set(static::CATEGORY, static::SETTING, 'int23'); |
128
|
|
|
|
129
|
|
|
Config::mustGetInt(static::CATEGORY, static::SETTING); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @return array |
134
|
|
|
*/ |
135
|
|
|
public function mustGetFloatDataProvider(): array |
136
|
|
|
{ |
137
|
|
|
return [ |
138
|
|
|
['354.45', 354.45], |
139
|
|
|
['-17.94', -17.94], |
140
|
|
|
]; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @dataProvider mustGetFloatDataProvider |
145
|
|
|
* |
146
|
|
|
* @param bool|int|float|string $value |
147
|
|
|
* @param float $expectedResult |
148
|
|
|
*/ |
149
|
|
|
public function testMustGetFloatReturnsBoolIfConfigIsBooleanLikeString($value, float $expectedResult): void |
150
|
|
|
{ |
151
|
|
|
Config::set(static::CATEGORY, static::SETTING, $value); |
152
|
|
|
|
153
|
|
|
$actualResult = Config::mustGetFloat(static::CATEGORY, static::SETTING); |
154
|
|
|
|
155
|
|
|
$this->assertEquals($expectedResult, $actualResult); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
public function testMustGetFloatThrowsExceptionIfStringCanNotBeConvertedToFloat(): void |
159
|
|
|
{ |
160
|
|
|
$this->expectException(\RuntimeException::class); |
161
|
|
|
|
162
|
|
|
Config::set(static::CATEGORY, static::SETTING, 'int23.3'); |
163
|
|
|
|
164
|
|
|
Config::mustGetFloat(static::CATEGORY, static::SETTING); |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|