-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfigTest.php
More file actions
281 lines (227 loc) · 8.76 KB
/
Copy pathConfigTest.php
File metadata and controls
281 lines (227 loc) · 8.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
<?php
namespace Tests\Koded\Stdlib;
use Exception;
use Koded\Stdlib\Config;
use PHPUnit\Framework\TestCase;
use function Koded\Stdlib\env;
class ConfigTest extends TestCase
{
public function test_should_load_defaults_from_other_instance()
{
$config = new Config('', new TestOtherConfInstanceClass);
$this->assertSame([1, 2, 3], $config->list);
}
public function test_that_build_method_should_throw_exception()
{
$this->expectException(Exception::class);
$this->expectExceptionMessage('Configuration factory should implement the method ');
(new Config)->build('');
}
public function test_should_load_parameters()
{
$config = new Config;
$this->assertNull($config->key1);
$config->withParameters(['key1' => 'value1']);
$this->assertSame('value1', $config->key1);
}
public function test_should_load_json_file_from_relative_path()
{
$config = new Config;
$config->fromJsonFile('composer.json');
$this->assertSame('koded/stdlib', $config->name);
}
public function test_should_load_json_file_from_absolute_path()
{
$config = new Config;
$config->fromJsonFile(__DIR__ . '/../composer.json');
$this->assertSame('koded/stdlib', $config->name);
}
public function test_should_load_options_from_object_instance()
{
$config = new Config;
$this->assertNull($config->foo);
$config->fromObject(new TestOtherConfInstanceClass);
$this->assertSame('bar', $config->foo);
}
public function test_should_load_options_from_object_fqn()
{
$config = new Config;
$this->assertNull($config->foo);
$config->fromObject(TestOtherConfInstanceClass::class);
$this->assertSame('bar', $config->foo);
}
public function test_should_load_ini_file()
{
$config = new Config;
$config->fromIniFile('tests/fixtures/config-test.ini');
$this->assertSame(
include __DIR__ . '/fixtures/expected-ini-data.php',
$config->section1
);
}
/*
* .env can alter keys with namespace
*
*/
public function test_should_load_env_file_as_is()
{
$config = new Config;
$config->fromEnvFile(__DIR__ . '/fixtures/.env');
// Scalar type values are preserved
$this->assertSame(include __DIR__ . '/fixtures/expected-env-data.php', env());
$this->assertSame(42, env('KEY_1'));
$this->assertSame(null, env('KEY_2'), '.env key does not exist');
$this->assertSame('value3', env('KEY_3'));
$this->assertSame(true, env('KEY_4'));
$this->assertSame(null, env('KEY_5'));
// ENV variable values are mutated to strings
$this->assertSame('42', \getenv('KEY_1'));
$this->assertSame(false, \getenv('KEY_2'), '.env key does not exist');
$this->assertSame('value3', \getenv('KEY_3'));
$this->assertSame('1', \getenv('KEY_4'));
$this->assertSame('null', \getenv('KEY_5'));
}
public function test_should_return_empty_if_env_file_was_not_found()
{
$config = new Config;
$config->fromEnvFile(__DIR__ . '/fixtures/non-existing.env');
$this->assertSame([], env());
}
public function test_should_load_env_file_and_trim_the_namespace()
{
$config = new Config(getcwd());
$config->fromEnvFile('tests/fixtures/.env', 'KEY_');
$this->assertSame(include __DIR__ . '/fixtures/expected-env-trim-ns.php', env());
}
public function test_should_load_from_env_variable()
{
\putenv('CONFIG_FILE=tests/fixtures/nested-array.php');
$config = new Config;
$config->fromEnvVariable('CONFIG_FILE');
$this->assertSame('found me', $config->find('array.key3.key3-1.key3-1-1'));
}
public function test_should_not_populate_global_env_array()
{
$_ENV = []; // clear
$config = new Config;
$config->fromEnvFile(__DIR__ . '/fixtures/.env');
$this->assertArrayNotHasKey('KEY_1', $_ENV);
$this->assertArrayNotHasKey('KEY_2', $_ENV);
$this->assertArrayNotHasKey('KEY_3', $_ENV);
$this->assertArrayNotHasKey('KEY_4', $_ENV);
$this->assertArrayNotHasKey('KEY_5', $_ENV);
$this->assertSame('42', \getenv('KEY_1', true));
$this->assertSame(false, \getenv('KEY_2', true));
$this->assertSame('value3', \getenv('KEY_3', true));
$this->assertSame('1', \getenv('KEY_4', true));
$this->assertSame('null', \getenv('KEY_5', true));
}
public function test_should_load_ini_file_from_env_variable()
{
\putenv('CONFIG_FILE=tests/fixtures/config-test.ini');
$config = new Config;
$config->fromEnvVariable('CONFIG_FILE');
$this->assertSame(42, $config->find('section1.key1'));
}
public function test_should_throw_exception_on_bad_method_call()
{
$this->expectException(Exception::class);
$this->expectExceptionMessage('Unable to load the configuration file');
(new Config)->nonExistentMethod();
}
public function test_should_be_silent_on_bad_method_call()
{
$config = (new Config)
->silent(true)
->nonExistentMethod();
$this->assertInstanceOf(Config::class, $config);
}
public function test_should_fail_loading_from_env_non_existing_file()
{
$this->expectException(Exception::class);
$this->expectExceptionMessage('Unable to load the configuration file');
\putenv('CONFIG_FILE=non-existent.conf');
(new Config)->fromEnvVariable('CONFIG_FILE');
}
public function test_should_throw_exception_from_empty_env_variable_value()
{
$this->expectException(Exception::class);
$this->expectExceptionMessage('The environment variable "CONFIG_FILE" is not set');
\putenv('CONFIG_FILE=');
(new Config)->fromEnvVariable('CONFIG_FILE');
}
public function test_should_silently_fail_loading_from_invalid_env_variable_value()
{
$config = (new Config)->silent(true)->fromEnvVariable('INVALID_CONFIG_FILE');
$this->assertInstanceOf(Config::class, $config);
}
public function test_should_lowercase_names_from_environment_variables()
{
// ye..
putenv('KEY_4=true');
$config = (new Config)->fromEnvironment(['KEY_1', 'KEY_3', 'KEY_4', 'KEY_5', 'UNKNOWN_VAR']);
$expected = include __DIR__ . '/fixtures/expected-data-lowercase.php';
$expected['unknown_var'] = null;
$this->assertSame($expected, $config->toArray());
}
public function test_should_trim_names_from_environment_variables()
{
\putenv('KEY_4=true');
$config = (new Config)->fromEnvironment(['KEY_1', 'KEY_3', 'KEY_4', 'KEY_5'], 'KEY_');
$this->assertSame(include __DIR__ . '/fixtures/expected-env-trim-ns.php', $config->toArray());
}
public function test_should_not_alter_the_keys_from_environment_variables()
{
\putenv('KEY_4=true');
$config = new Config;
$config->fromEnvironment(['KEY_1', 'KEY_3', 'KEY_4', 'KEY_5'], '', false);
$this->assertSame(include __DIR__ . '/fixtures/expected-env-data.php', $config->toArray());
}
public function test_should_create_arguments_object_using_a_namespace()
{
\putenv('KEY_4=true');
$config = new Config;
$config->fromEnvironment(['KEY_1', 'KEY_3', 'KEY_4', 'KEY_5']);
$arguments = $config->namespace('KEY_');
$this->assertInstanceOf(Config::class, $arguments);
$this->assertSame(include __DIR__ . '/fixtures/expected-data-lowercase.php', $arguments->toArray());
}
public function test_ini_sections_parsing()
{
$config = new Config;
$config->fromIniFile(__DIR__ . '/fixtures/config-sections.ini');
$this->assertSame(include_once __DIR__ . '/fixtures/config-sections.php', $config->toArray());
}
public function test_should_set_root_on_empty_constructor_argument()
{
$config = new Config;
$this->assertNotEmpty($config->root);
}
public function test_shoud_set_root_from_empty_object_root()
{
$initial = new TestRootFromThisClass;
$config = new Config(__DIR__);
$config->fromObject($initial);
$this->assertSame($config->root, $initial->root);
$this->assertSame('/tmp', $config->root);
}
protected function tearDown(): void
{
env('', null, []);
}
}
class TestOtherConfInstanceClass extends Config
{
public function __construct()
{
parent::__construct();
$this->fromPhpFile(__DIR__ . '/fixtures/nested-array.php');
}
}
class TestRootFromThisClass extends Config
{
public function __construct()
{
parent::__construct('/tmp');
}
}