|
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
|
|
|
/** |
|
89
|
|
|
* @return array |
|
90
|
|
|
*/ |
|
91
|
|
|
public function mustGetIntDataProvider(): array |
|
92
|
|
|
{ |
|
93
|
|
|
return [ |
|
94
|
|
|
['354', 354], |
|
95
|
|
|
['-17', -17], |
|
96
|
|
|
]; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @dataProvider mustGetIntDataProvider |
|
101
|
|
|
* |
|
102
|
|
|
* @param bool|int|float|string $value |
|
103
|
|
|
* @param int $expectedResult |
|
104
|
|
|
*/ |
|
105
|
|
|
public function testMustGetIntReturnsBoolIfConfigIsBooleanLikeString($value, int $expectedResult): void |
|
106
|
|
|
{ |
|
107
|
|
|
Config::set(static::CATEGORY, static::SETTING, $value); |
|
108
|
|
|
|
|
109
|
|
|
$actualResult = Config::mustGetInt(static::CATEGORY, static::SETTING); |
|
110
|
|
|
|
|
111
|
|
|
$this->assertEquals($expectedResult, $actualResult); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @return array |
|
116
|
|
|
*/ |
|
117
|
|
|
public function mustGetFloatDataProvider(): array |
|
118
|
|
|
{ |
|
119
|
|
|
return [ |
|
120
|
|
|
['354.45', 354.45], |
|
121
|
|
|
['-17.94', -17.94], |
|
122
|
|
|
]; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @dataProvider mustGetFloatDataProvider |
|
127
|
|
|
* |
|
128
|
|
|
* @param bool|int|float|string $value |
|
129
|
|
|
* @param float $expectedResult |
|
130
|
|
|
*/ |
|
131
|
|
|
public function testMustGetFloatReturnsBoolIfConfigIsBooleanLikeString($value, float $expectedResult): void |
|
132
|
|
|
{ |
|
133
|
|
|
Config::set(static::CATEGORY, static::SETTING, $value); |
|
134
|
|
|
|
|
135
|
|
|
$actualResult = Config::mustGetFloat(static::CATEGORY, static::SETTING); |
|
136
|
|
|
|
|
137
|
|
|
$this->assertEquals($expectedResult, $actualResult); |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|