|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Koded\Stdlib; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use PHPUnit\Framework\TestCase; |
|
7
|
|
|
|
|
8
|
|
|
class ConfigTest extends TestCase |
|
9
|
|
|
{ |
|
10
|
|
|
|
|
11
|
|
|
public function test_should_load_defaults_from_other_instance() |
|
12
|
|
|
{ |
|
13
|
|
|
$config = new Config('', new MockOtherConfigInstance); |
|
14
|
|
|
$this->assertSame([1, 2, 3], $config->list); |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
public function test_that_build_method_should_throw_exception() |
|
18
|
|
|
{ |
|
19
|
|
|
$this->expectException(Exception::class); |
|
20
|
|
|
$this->expectExceptionMessage('Configuration factory should implement the method '); |
|
21
|
|
|
(new Config)->build(''); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
public function test_should_load_parameters() |
|
25
|
|
|
{ |
|
26
|
|
|
$config = new Config; |
|
27
|
|
|
$this->assertNull($config->key1); |
|
28
|
|
|
|
|
29
|
|
|
$config->withParameters(['key1' => 'value1']); |
|
30
|
|
|
$this->assertSame('value1', $config->key1); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function test_should_load_json_file_from_relative_path() |
|
34
|
|
|
{ |
|
35
|
|
|
$config = new Config; |
|
36
|
|
|
$config->fromJsonFile('composer.json'); |
|
37
|
|
|
|
|
38
|
|
|
$this->assertSame('koded/stdlib', $config->name); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function test_should_load_json_file_from_absolute_path() |
|
42
|
|
|
{ |
|
43
|
|
|
$config = new Config; |
|
44
|
|
|
$config->fromJsonFile(__DIR__ . '/../composer.json'); |
|
45
|
|
|
|
|
46
|
|
|
$this->assertSame('koded/stdlib', $config->name); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function test_should_load_options_from_object_instance() |
|
50
|
|
|
{ |
|
51
|
|
|
$config = new Config; |
|
52
|
|
|
$this->assertNull($config->foo); |
|
53
|
|
|
|
|
54
|
|
|
$config->fromObject(new MockOtherConfigInstance); |
|
55
|
|
|
$this->assertSame('bar', $config->foo); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function test_should_load_options_from_object_fqn() |
|
59
|
|
|
{ |
|
60
|
|
|
$config = new Config; |
|
61
|
|
|
$this->assertNull($config->foo); |
|
62
|
|
|
|
|
63
|
|
|
$config->fromObject(MockOtherConfigInstance::class); |
|
64
|
|
|
$this->assertSame('bar', $config->foo); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function test_should_load_ini_file() |
|
68
|
|
|
{ |
|
69
|
|
|
$config = new Config; |
|
70
|
|
|
$config->fromIniFile('Tests/fixtures/config-test.ini'); |
|
71
|
|
|
$this->assertSame(include __DIR__ . '/fixtures/expected-ini-data.php', $config->section1); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/* |
|
75
|
|
|
* .env can alter keys with namespace |
|
76
|
|
|
* |
|
77
|
|
|
*/ |
|
78
|
|
|
public function test_should_load_env_file_as_is() |
|
79
|
|
|
{ |
|
80
|
|
|
$config = new Config; |
|
81
|
|
|
$config->fromEnvFile(__DIR__ . '/fixtures/.env'); |
|
82
|
|
|
$this->assertSame(include __DIR__ . '/fixtures/expected-env-data.php', $config->toArray()); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public function test_should_return_empty_if_env_file_was_not_found() |
|
86
|
|
|
{ |
|
87
|
|
|
$config = new Config; |
|
88
|
|
|
$config->fromEnvFile(__DIR__ . '/fixtures/non-existing.env'); |
|
89
|
|
|
$this->assertSame([], $config->toArray()); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public function test_should_load_env_file_and_trim_the_namespace() |
|
93
|
|
|
{ |
|
94
|
|
|
$config = new Config(getcwd()); |
|
95
|
|
|
$config->fromEnvFile('Tests/fixtures/.env', 'KEY_'); |
|
96
|
|
|
$this->assertSame(include __DIR__ . '/fixtures/expected-env-trim-ns.php', $config->toArray()); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
public function test_should_load_from_env_variable() |
|
100
|
|
|
{ |
|
101
|
|
|
putenv('CONFIG_FILE=Tests/fixtures/nested-array.php'); |
|
102
|
|
|
$config = new Config; |
|
103
|
|
|
$config->fromEnvVariable('CONFIG_FILE'); |
|
104
|
|
|
|
|
105
|
|
|
$this->assertSame('found me', $config->find('array.key3.key3-1.key3-1-1')); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
public function test_should_load_ini_file_from_env_variable() |
|
109
|
|
|
{ |
|
110
|
|
|
putenv('CONFIG_FILE=Tests/fixtures/config-test.ini'); |
|
111
|
|
|
$config = new Config; |
|
112
|
|
|
$config->fromEnvVariable('CONFIG_FILE'); |
|
113
|
|
|
|
|
114
|
|
|
$this->assertSame(42, $config->find('section1.key1')); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
public function test_should_throw_exception_on_bad_method_call() |
|
118
|
|
|
{ |
|
119
|
|
|
$this->expectException(Exception::class); |
|
120
|
|
|
$this->expectExceptionMessage('Unable to load the configuration file'); |
|
121
|
|
|
|
|
122
|
|
|
(new Config)->nonExistentMethod(); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
public function test_should_be_silent_on_bad_method_call() |
|
126
|
|
|
{ |
|
127
|
|
|
$config = (new Config) |
|
128
|
|
|
->silent(true) |
|
129
|
|
|
->nonExistentMethod(); |
|
130
|
|
|
|
|
131
|
|
|
$this->assertInstanceOf(Config::class, $config); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
public function test_should_fail_loading_from_env_non_existing_file() |
|
135
|
|
|
{ |
|
136
|
|
|
$this->expectException(Exception::class); |
|
137
|
|
|
$this->expectExceptionMessage('Unable to load the configuration file'); |
|
138
|
|
|
|
|
139
|
|
|
putenv('CONFIG_FILE=non-existent.conf'); |
|
140
|
|
|
|
|
141
|
|
|
(new Config)->fromEnvVariable('CONFIG_FILE'); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
public function test_should_throw_exception_from_empty_env_variable_value() |
|
145
|
|
|
{ |
|
146
|
|
|
$this->expectException(Exception::class); |
|
147
|
|
|
$this->expectExceptionMessage('The environment variable "CONFIG_FILE" is not set'); |
|
148
|
|
|
|
|
149
|
|
|
putenv('CONFIG_FILE='); |
|
150
|
|
|
|
|
151
|
|
|
(new Config)->fromEnvVariable('CONFIG_FILE'); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
public function test_should_silently_fail_loading_from_invalid_env_variable_value() |
|
155
|
|
|
{ |
|
156
|
|
|
$config = (new Config)->silent(true)->fromEnvVariable('INVALID_CONFIG_FILE'); |
|
157
|
|
|
$this->assertInstanceOf(Config::class, $config); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
public function test_should_lowercase_names_from_environment_variables() |
|
161
|
|
|
{ |
|
162
|
|
|
// ye.. |
|
163
|
|
|
putenv('KEY_4=true'); |
|
164
|
|
|
$config = (new Config)->fromEnvironment(['KEY_1', 'KEY_3', 'KEY_4', 'KEY_5', 'UNKNOWN_VAR']); |
|
165
|
|
|
|
|
166
|
|
|
$expected = include __DIR__ . '/fixtures/expected-data-lowercase.php'; |
|
167
|
|
|
$expected['unknown_var'] = null; |
|
168
|
|
|
$this->assertSame($expected, $config->toArray()); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
public function test_should_trim_names_from_environment_variables() |
|
172
|
|
|
{ |
|
173
|
|
|
putenv('KEY_4=true'); |
|
174
|
|
|
$config = (new Config)->fromEnvironment(['KEY_1', 'KEY_3', 'KEY_4', 'KEY_5'], 'KEY_'); |
|
175
|
|
|
|
|
176
|
|
|
$this->assertSame(include __DIR__ . '/fixtures/expected-env-trim-ns.php', $config->toArray()); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
public function test_should_not_alter_the_keys_from_environment_variables() |
|
180
|
|
|
{ |
|
181
|
|
|
putenv('KEY_4=true'); |
|
182
|
|
|
$config = new Config; |
|
183
|
|
|
$config->fromEnvironment(['KEY_1', 'KEY_3', 'KEY_4', 'KEY_5'], '', false); |
|
184
|
|
|
|
|
185
|
|
|
$this->assertSame(include __DIR__ . '/fixtures/expected-env-data.php', $config->toArray()); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
public function test_should_create_arguments_object_using_a_namespace() |
|
189
|
|
|
{ |
|
190
|
|
|
putenv('KEY_4=true'); |
|
191
|
|
|
$config = new Config; |
|
192
|
|
|
$config->fromEnvironment(['KEY_1', 'KEY_3', 'KEY_4', 'KEY_5']); |
|
193
|
|
|
|
|
194
|
|
|
$arguments = $config->namespace('KEY_'); |
|
195
|
|
|
|
|
196
|
|
|
$this->assertInstanceOf(Config::class, $arguments); |
|
197
|
|
|
$this->assertSame(include __DIR__ . '/fixtures/expected-data-lowercase.php', $arguments->toArray()); |
|
198
|
|
|
} |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
class MockOtherConfigInstance extends Config |
|
202
|
|
|
{ |
|
203
|
|
|
|
|
204
|
|
|
public function __construct() |
|
205
|
|
|
{ |
|
206
|
|
|
parent::__construct(); |
|
207
|
|
|
$this->fromPhpFile(__DIR__ . '/fixtures/nested-array.php'); |
|
208
|
|
|
} |
|
209
|
|
|
} |
|
210
|
|
|
|