-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
BaseConfigTest.php
297 lines (240 loc) · 9.17 KB
/
BaseConfigTest.php
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
<?php
/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace CodeIgniter\Config;
use CodeIgniter\Test\CIUnitTestCase;
use Encryption;
use RegistrarConfig;
use RuntimeException;
use SimpleConfig;
use Tests\Support\Config\BadRegistrar;
use Tests\Support\Config\TestRegistrar;
/**
* @internal
*
* @group SeparateProcess
*/
final class BaseConfigTest extends CIUnitTestCase
{
private string $fixturesFolder;
protected function setUp(): void
{
parent::setUp();
$this->fixturesFolder = __DIR__ . '/fixtures';
if (! class_exists('SimpleConfig', false)) {
require $this->fixturesFolder . '/SimpleConfig.php';
}
if (! class_exists('RegistrarConfig', false)) {
require $this->fixturesFolder . '/RegistrarConfig.php';
}
if (! class_exists('Encryption', false)) {
require $this->fixturesFolder . '/Encryption.php';
}
}
public function testBasicValues(): void
{
$dotenv = new DotEnv($this->fixturesFolder, '.env');
$dotenv->load();
$config = new SimpleConfig();
$this->assertNull($config->FOO);
$this->assertSame('', $config->echo);
$this->assertTrue($config->foxtrot);
$this->assertSame(18, $config->golf);
}
public function testUseDefaultValueTypeIntAndFloatValues(): void
{
$dotenv = new DotEnv($this->fixturesFolder, '.env');
$dotenv->load();
$config = new SimpleConfig();
$this->assertSame(0.0, $config->float);
$this->assertSame(999, $config->int);
}
public function testUseDefaultValueTypeStringValue(): void
{
$dotenv = new DotEnv($this->fixturesFolder, '.env');
$dotenv->load();
$config = new SimpleConfig();
$this->assertSame('123456', $config->password);
}
/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testServerValues(): void
{
$_SERVER = [
'simpleconfig.shortie' => 123,
'SimpleConfig.longie' => 456,
];
$dotenv = new DotEnv($this->fixturesFolder, '.env');
$dotenv->load();
$config = new SimpleConfig();
$this->assertSame('123', $config->shortie);
$this->assertSame('456', $config->longie);
}
public function testEnvironmentOverrides(): void
{
$dotenv = new DotEnv($this->fixturesFolder, '.env');
$dotenv->load();
$config = new SimpleConfig();
// override config with ENV var
$this->assertSame('pow', $config->alpha);
// config should not be over-written by wrongly named ENV var
$this->assertSame('three', $config->charlie);
// override config with shortPrefix ENV var
$this->assertSame('hubbahubba', $config->delta);
// incorrect env name should not inject property
$this->assertFalse(property_exists($config, 'notthere'));
// empty ENV var should not affect config setting
$this->assertSame('pineapple', $config->fruit);
// non-empty ENV var should overrideconfig setting
$this->assertSame('banana', $config->dessert);
// null property should not be affected
$this->assertNull($config->QEMPTYSTR);
// property name with underscore
$this->assertSame('bar', $config->onedeep_value);
// array property name with underscore and key with underscore
$this->assertSame('foo', $config->one_deep['under_deep']);
// The default property value is null but has type
$this->assertSame(20, $config->size);
}
public function testPrefixedValues(): void
{
$dotenv = new DotEnv($this->fixturesFolder, '.env');
$dotenv->load();
$config = new SimpleConfig();
$this->assertSame('baz', $config->onedeep);
}
public function testPrefixedArrayValues(): void
{
$dotenv = new DotEnv($this->fixturesFolder, '.env');
$dotenv->load();
$config = new SimpleConfig();
$this->assertSame('ci4', $config->default['name']);
$this->assertSame('Malcolm', $config->crew['captain']);
$this->assertSame('Spock', $config->crew['science']);
$this->assertArrayNotHasKey('pilot', $config->crew);
$this->assertTrue($config->crew['comms']);
$this->assertFalse($config->crew['doctor']);
}
public function testArrayValues(): void
{
$dotenv = new DotEnv($this->fixturesFolder, '.env');
$dotenv->load();
$config = new SimpleConfig();
$this->assertSame('complex', $config->simple['name']);
$this->assertSame('foo', $config->first);
$this->assertSame('bar', $config->second);
}
public function testSetsDefaultValues(): void
{
$dotenv = new DotEnv($this->fixturesFolder, 'commented.env');
$dotenv->load();
$config = new SimpleConfig();
$this->assertSame('foo', $config->first);
$this->assertSame('bar', $config->second);
}
/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testSetsDefaultValuesEncryptionUsingHex2Bin(): void
{
$dotenv = new DotEnv($this->fixturesFolder, 'encryption.env');
$dotenv->load();
$config = new Encryption();
// override config with ENV var
$this->assertSame('f699c7fd18a8e082d0228932f3acd40e1ef5ef92efcedda32842a211d62f0aa6', bin2hex($config->key));
$this->assertSame('OpenSSL', $config->driver);
}
/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testSetDefaultValuesEncryptionUsingBase64(): void
{
$dotenv = new DotEnv($this->fixturesFolder, 'base64encryption.env');
$dotenv->load();
$config = new Encryption('base64');
$this->assertSame('L40bKo6b8Nu541LeVeZ1i5RXfGgnkar42CPTfukhGhw=', base64_encode($config->key));
$this->assertSame('OpenSSL', $config->driver);
}
public function testSetsDefaultValuesHex2Bin(): void
{
$dotenv = new DotEnv($this->fixturesFolder, 'commented.env');
$dotenv->load();
$config = new Encryption();
// override config with ENV var
$this->assertSame('84cf2c0811d5daf9e1c897825a3debce91f9a33391e639f72f7a4740b30675a2', bin2hex($config->key));
$this->assertSame('MCrypt', $config->driver);
}
public function testSetDefaultValuesBase64(): void
{
$dotenv = new DotEnv($this->fixturesFolder, 'commented.env');
$dotenv->load();
$config = new Encryption('base64');
$this->assertSame('Psf8bUHRh1UJYG2M7e+5ec3MdjpKpzAr0twamcAvOcI=', base64_encode($config->key));
$this->assertSame('MCrypt', $config->driver);
}
public function testRecognizesLooseValues(): void
{
$dotenv = new DotEnv($this->fixturesFolder, 'loose.env');
$dotenv->load();
$config = new SimpleConfig();
$this->assertSame(0, (int) $config->QZERO);
$this->assertSame('0', $config->QZEROSTR);
$this->assertSame(' ', $config->QEMPTYSTR);
$this->assertFalse($config->QFALSE);
}
public function testRegistrars(): void
{
$config = new RegistrarConfig();
$config::$registrars = [TestRegistrar::class];
$this->setPrivateProperty($config, 'didDiscovery', true);
$method = $this->getPrivateMethodInvoker($config, 'registerProperties');
$method();
// no change to unmodified property
$this->assertSame('bar', $config->foo);
// add to an existing array property
$this->assertSame(['baz', 'first', 'second'], $config->bar);
}
public function testBadRegistrar(): void
{
// Shouldn't change any values.
$config = new RegistrarConfig();
$config::$registrars = [BadRegistrar::class];
$this->setPrivateProperty($config, 'didDiscovery', true);
$this->expectException(RuntimeException::class);
$method = $this->getPrivateMethodInvoker($config, 'registerProperties');
$method();
$this->assertSame('bar', $config->foo);
}
public function testNotEnabled(): void
{
$modulesConfig = config('Modules');
$modulesConfig->enabled = false;
$config = new RegistrarConfig();
$config::$registrars = [];
$expected = $config::$registrars;
$method = $this->getPrivateMethodInvoker($config, 'registerProperties');
$method();
$this->assertSame($expected, $config::$registrars);
}
public function testDidDiscovery(): void
{
$modulesConfig = config('Modules');
$modulesConfig->enabled = true;
$config = new RegistrarConfig();
$config::$registrars = [];
$this->setPrivateProperty($config, 'didDiscovery', false);
$method = $this->getPrivateMethodInvoker($config, 'registerProperties');
$method();
$this->assertTrue($this->getPrivateProperty($config, 'didDiscovery'));
}
}