This repository was archived by the owner on May 16, 2020. It is now read-only.
forked from codeigniter4/CodeIgniter4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseConfigTest.php
More file actions
226 lines (175 loc) · 6.31 KB
/
Copy pathBaseConfigTest.php
File metadata and controls
226 lines (175 loc) · 6.31 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
<?php
namespace CodeIgniter\Config;
use CodeIgniter\Test\CIUnitTestCase;
class BaseConfigTest extends CIUnitTestCase
{
protected $fixturesFolder;
//--------------------------------------------------------------------
protected function setUp()
{
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';
}
}
//--------------------------------------------------------------------
public function testBasicValues()
{
$dotenv = new DotEnv($this->fixturesFolder, '.env');
$dotenv->load();
$config = new \SimpleConfig();
$this->assertEquals('bar', $config->FOO);
// empty treated as boolean false
$this->assertEquals(false, $config->echo);
// 'true' should be treated as boolean true
$this->assertTrue($config->foxtrot);
// numbers should be treated properly
$this->assertEquals(18, $config->golf);
}
/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testServerValues()
{
$_SERVER = [
'simpleconfig.shortie' => 123,
'SimpleConfig.longie' => 456,
];
$dotenv = new DotEnv($this->fixturesFolder, '.env');
$dotenv->load();
$config = new \SimpleConfig();
$this->assertEquals(123, $config->shortie);
$this->assertEquals(456, $config->longie);
}
//--------------------------------------------------------------------
public function testEnvironmentOverrides()
{
$dotenv = new DotEnv($this->fixturesFolder, '.env');
$dotenv->load();
$config = new \SimpleConfig();
// override config with ENV var
$this->assertEquals('pow', $config->alpha);
// config should not be over-written by wrongly named ENV var
$this->assertEquals('three', $config->charlie);
// override config with shortPrefix ENV var
$this->assertEquals('hubbahubba', $config->delta);
// incorrect env name should not inject property
$this->assertObjectNotHasAttribute('notthere', $config);
// same ENV var as property, but not namespaced, still over-rides
$this->assertEquals('kazaam', $config->bravo);
// empty ENV var should not affect config setting
$this->assertEquals('pineapple', $config->fruit);
// non-empty ENV var should overrideconfig setting
$this->assertEquals('banana', $config->dessert);
// null property should not be affected
$this->assertNull($config->QEMPTYSTR);
}
//--------------------------------------------------------------------
public function testPrefixedValues()
{
$dotenv = new DotEnv($this->fixturesFolder, '.env');
$dotenv->load();
$config = new \SimpleConfig();
$this->assertEquals('baz', $config->onedeep);
}
//--------------------------------------------------------------------
public function testPrefixedArrayValues()
{
$dotenv = new DotEnv($this->fixturesFolder, '.env');
$dotenv->load();
$config = new \SimpleConfig();
$this->assertEquals('ci4', $config->default['name']);
$this->assertEquals('Malcolm', $config->crew['captain']);
$this->assertEquals('Spock', $config->crew['science']);
$this->assertFalse(array_key_exists('pilot', $config->crew));
$this->assertTrue($config->crew['comms']);
$this->assertFalse($config->crew['doctor']);
}
//--------------------------------------------------------------------
public function testArrayValues()
{
$dotenv = new DotEnv($this->fixturesFolder, '.env');
$dotenv->load();
$config = new \SimpleConfig();
$this->assertEquals('complex', $config->simple['name']);
$this->assertEquals('foo', $config->first);
$this->assertEquals('bar', $config->second);
}
//--------------------------------------------------------------------
public function testSetsDefaultValues()
{
$dotenv = new DotEnv($this->fixturesFolder, 'commented.env');
$dotenv->load();
$config = new \SimpleConfig();
$this->assertEquals('foo', $config->first);
$this->assertEquals('bar', $config->second);
}
//--------------------------------------------------------------------
public function testRecognizesLooseValues()
{
$dotenv = new DotEnv($this->fixturesFolder, 'loose.env');
$dotenv->load();
$config = new \SimpleConfig();
$this->assertEquals(0, $config->QZERO);
$this->assertSame('0', $config->QZEROSTR);
$this->assertEquals(' ', $config->QEMPTYSTR);
$this->assertFalse($config->QFALSE);
}
//--------------------------------------------------------------------
public function testRegistrars()
{
$config = new \RegistrarConfig();
$config::$registrars = ['\Tests\Support\Config\Registrar'];
$this->setPrivateProperty($config, 'didDiscovery', true);
$method = $this->getPrivateMethodInvoker($config, 'registerProperties');
$method();
// no change to unmodified property
$this->assertEquals('bar', $config->foo);
// add to an existing array property
$this->assertEquals(['baz', 'first', 'second'], $config->bar);
// add a new property
$this->assertEquals('nice', $config->format);
// add a new array property
$this->assertEquals(['apple', 'banana'], $config->fruit);
}
public function testBadRegistrar()
{
// Shouldn't change any values.
$config = new \RegistrarConfig();
$config::$registrars = ['\Tests\Support\Config\BadRegistrar'];
$this->setPrivateProperty($config, 'didDiscovery', true);
$this->expectException(\RuntimeException::class);
$method = $this->getPrivateMethodInvoker($config, 'registerProperties');
$method();
$this->assertEquals('bar', $config->foo);
}
public function testNotEnabled()
{
$modulesConfig = config('Modules');
$modulesConfig->enabled = false;
$config = new \RegistrarConfig();
$config::$registrars = [];
$expected = $config::$registrars;
$method = $this->getPrivateMethodInvoker($config, 'registerProperties');
$method();
$this->assertEquals($expected, $config::$registrars);
}
public function testDidDiscovery()
{
$modulesConfig = config('Modules');
$modulesConfig->enabled = true;
$config = new \RegistrarConfig();
$config::$registrars = [];
$this->setPrivateProperty($config, 'didDiscovery', false);
$method = $this->getPrivateMethodInvoker($config, 'registerProperties');
$method();
$this->assertEquals(true, $this->getPrivateProperty($config, 'didDiscovery'));
}
}