-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathConfigTest.php
More file actions
205 lines (162 loc) · 6.28 KB
/
ConfigTest.php
File metadata and controls
205 lines (162 loc) · 6.28 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
<?php
namespace think\tests;
use org\bovigo\vfs\vfsStream;
use PHPUnit\Framework\TestCase;
use think\Config;
class ConfigTest extends TestCase
{
public function testLoad()
{
$root = vfsStream::setup();
$file = vfsStream::newFile('test.php')->setContent("<?php return ['key1'=> 'value1','key2'=>'value2'];");
$root->addChild($file);
$config = new Config();
$config->load($file->url(), 'test');
$this->assertEquals('value1', $config->get('test.key1'));
$this->assertEquals('value2', $config->get('test.key2'));
$this->assertSame(['key1' => 'value1', 'key2' => 'value2'], $config->get('test'));
}
public function testSetAndGet()
{
$config = new Config();
$config->set([
'key1' => 'value1',
'key2' => [
'key3' => 'value3',
],
], 'test');
$this->assertTrue($config->has('test.key1'));
$this->assertEquals('value1', $config->get('test.key1'));
$this->assertEquals('value3', $config->get('test.key2.key3'));
$this->assertEquals(['key3' => 'value3'], $config->get('test.key2'));
$this->assertFalse($config->has('test.key3'));
$this->assertEquals('none', $config->get('test.key3', 'none'));
}
public function testGlobalHook()
{
$config = new Config();
// Set initial config
$config->set(['key1' => 'original_value'], 'test');
// Register global hook
$config->hook(function ($name, $value) {
if ($name === 'test.key1') {
return 'hooked_value';
}
if ($name === 'test.key2' && is_null($value)) {
return 'default_from_hook';
}
return $value;
});
// Test hook modifies existing value
$this->assertEquals('hooked_value', $config->get('test.key1'));
// Test hook provides default for non-existent key
$this->assertEquals('default_from_hook', $config->get('test.key2'));
// Test hook returns original value for other keys
$config->set(['key3' => 'unchanged'], 'test');
$this->assertEquals('unchanged', $config->get('test.key3'));
}
public function testSpecificKeyHook()
{
$config = new Config();
// Set initial config
$config->set([
'key1' => 'value1',
'key2' => 'value2'
], 'test');
$config->set([
'key1' => 'value1'
], 'other');
// Register hook for specific key 'test'
$config->hook(function ($name, $value) {
if (str_contains($name, 'key1')) {
return 'test_hooked_' . $value;
}
return $value;
}, 'test');
// Register hook for specific key 'other'
$config->hook(function ($name, $value) {
if (str_contains($name, 'key1')) {
return 'other_hooked_' . $value;
}
return $value;
}, 'other');
// Test specific hook for 'test' configuration
$this->assertEquals('test_hooked_value1', $config->get('test.key1'));
$this->assertEquals('value2', $config->get('test.key2')); // No hook for key2
// Test specific hook for 'other' configuration
$this->assertEquals('other_hooked_value1', $config->get('other.key1'));
}
public function testHookPriority()
{
$config = new Config();
// Set initial config
$config->set(['key1' => 'value1'], 'test');
// Register global hook first
$config->hook(function ($name, $value) {
return 'global_' . $value;
});
// Register specific key hook (should override global)
$config->hook(function ($name, $value) {
return 'specific_' . $value;
}, 'test');
// Specific hook should take priority over global hook
$this->assertEquals('specific_value1', $config->get('test.key1'));
}
public function testHookWithNullReturn()
{
$config = new Config();
// Register hook that returns null
$config->hook(function ($name, $value) {
if ($name === 'test.nonexistent') {
return null; // This should trigger default value
}
return $value;
});
// Test with default value when hook returns null
$this->assertEquals('default_value', $config->get('test.nonexistent', 'default_value'));
}
public function testHookWithTopLevelConfig()
{
$config = new Config();
// Set top-level config
$config->set(['key1' => 'value1', 'key2' => 'value2'], 'database');
// Register hook for database config
$config->hook(function ($name, $value) {
if ($name === 'database') {
return array_merge($value, ['key3' => 'added_by_hook']);
}
return $value;
}, 'database');
// Test hook modifies entire config section
$result = $config->get('database');
$this->assertIsArray($result);
$this->assertEquals('value1', $result['key1']);
$this->assertEquals('value2', $result['key2']);
$this->assertEquals('added_by_hook', $result['key3']);
}
public function testLazyLoadingBehavior()
{
$config = new Config();
// Counter to verify hook is called
$hookCallCount = 0;
// Register hook with counter
$config->hook(function ($name, $value) use (&$hookCallCount) {
$hookCallCount++;
return $value ? $value . '_processed' : 'processed_default';
});
// Set config value
$config->set(['key1' => 'value1'], 'test');
// First call should trigger hook
$result1 = $config->get('test.key1');
$this->assertEquals('value1_processed', $result1);
$this->assertEquals(1, $hookCallCount);
// Second call should also trigger hook (no caching)
$result2 = $config->get('test.key1');
$this->assertEquals('value1_processed', $result2);
$this->assertEquals(2, $hookCallCount);
// Test with non-existent key
$result3 = $config->get('test.nonexistent', 'default');
$this->assertEquals('processed_default', $result3);
$this->assertEquals(3, $hookCallCount);
}
}