ConfigTest.php
12.2 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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
<?php
class ConfigTest_DefinesFoo extends Object implements TestOnly {
protected static $foo = 1;
}
class ConfigTest_DefinesBar extends ConfigTest_DefinesFoo {
public static $bar = 2;
}
class ConfigTest_DefinesFooAndBar extends ConfigTest_DefinesFoo {
protected static $foo = 3;
public static $bar = 3;
}
class ConfigTest_DefinesFooDoesntExtendObject implements TestOnly {
protected static $foo = 4;
}
class ConfigStaticTest_First extends Config implements TestOnly {
/** @config */
private static $first = array('test_1');
/** @config */
private static $second = array('test_1');
/** @config */
private static $third = 'test_1';
/** @config */
private static $bool = true;
/** @config */
private static $int = 42;
/** @config */
private static $string = 'value';
/** @config */
private static $nullable = 'value';
/** @config */
private static $default_false = false;
/** @config */
private static $default_null = null;
/** @config */
private static $default_zero = 0;
public static $default_emtpy_string = '';
}
class ConfigStaticTest_Second extends ConfigStaticTest_First {
private static $first = array('test_2');
}
class ConfigStaticTest_Third extends ConfigStaticTest_Second {
private static $first = array('test_3');
private static $second = array('test_3');
public static $fourth = array('test_3');
}
class ConfigStaticTest_Fourth extends ConfigStaticTest_Third {
public static $fourth = array('test_4');
}
class ConfigStaticTest_Combined1 extends Config implements TestOnly {
/** @config */
private static $first = array('test_1');
/** @config */
private static $second = array('test_1');
}
class ConfigStaticTest_Combined2 extends ConfigStaticTest_Combined1 {
private static $first = array('test_2');
private static $second = null;
}
class ConfigStaticTest_Combined3 extends ConfigStaticTest_Combined2 {
private static $first = array('test_3');
private static $second = array('test_3');
}
class ConfigTest_TestNest extends Object implements TestOnly {
/** @config */
private static $foo = 3;
/** @config */
private static $bar = 5;
}
class ConfigTest extends SapphireTest {
public function testNest() {
// Check basic config
$this->assertEquals(3, Config::inst()->get('ConfigTest_TestNest', 'foo'));
$this->assertEquals(5, Config::inst()->get('ConfigTest_TestNest', 'bar'));
// Test nest copies data
Config::nest();
$this->assertEquals(3, Config::inst()->get('ConfigTest_TestNest', 'foo'));
$this->assertEquals(5, Config::inst()->get('ConfigTest_TestNest', 'bar'));
// Test nested data can be updated
Config::inst()->update('ConfigTest_TestNest', 'foo', 4);
$this->assertEquals(4, Config::inst()->get('ConfigTest_TestNest', 'foo'));
$this->assertEquals(5, Config::inst()->get('ConfigTest_TestNest', 'bar'));
// Test unnest restores data
Config::unnest();
$this->assertEquals(3, Config::inst()->get('ConfigTest_TestNest', 'foo'));
$this->assertEquals(5, Config::inst()->get('ConfigTest_TestNest', 'bar'));
}
public function testUpdateStatic() {
$this->assertEquals(Config::inst()->get('ConfigStaticTest_First', 'first', Config::FIRST_SET),
array('test_1'));
$this->assertEquals(Config::inst()->get('ConfigStaticTest_Second', 'first', Config::FIRST_SET),
array('test_2'));
$this->assertEquals(Config::inst()->get('ConfigStaticTest_Third', 'first', Config::FIRST_SET),
array('test_3'));
Config::inst()->update('ConfigStaticTest_First', 'first', array('test_1_2'));
Config::inst()->update('ConfigStaticTest_Third', 'first', array('test_3_2'));
Config::inst()->update('ConfigStaticTest_Fourth', 'first', array('test_4'));
$this->assertEquals(Config::inst()->get('ConfigStaticTest_First', 'first', Config::FIRST_SET),
array('test_1_2', 'test_1'));
Config::inst()->update('ConfigStaticTest_Fourth', 'second', array('test_4'));
Config::inst()->update('ConfigStaticTest_Third', 'second', array('test_3_2'));
$this->assertEquals(Config::inst()->get('ConfigStaticTest_Fourth', 'second', Config::FIRST_SET),
array('test_4'));
$this->assertEquals(Config::inst()->get('ConfigStaticTest_Third', 'second', Config::FIRST_SET),
array('test_3_2', 'test_3'));
Config::inst()->remove('ConfigStaticTest_Third', 'second');
$this->assertEquals(array(), Config::inst()->get('ConfigStaticTest_Third', 'second'));
Config::inst()->update('ConfigStaticTest_Third', 'second', array('test_3_2'));
$this->assertEquals(Config::inst()->get('ConfigStaticTest_Third', 'second', Config::FIRST_SET),
array('test_3_2'));
}
public function testUpdateWithFalsyValues() {
// Booleans
$this->assertTrue(Config::inst()->get('ConfigStaticTest_First', 'bool'));
Config::inst()->update('ConfigStaticTest_First', 'bool', false);
$this->assertFalse(Config::inst()->get('ConfigStaticTest_First', 'bool'));
Config::inst()->update('ConfigStaticTest_First', 'bool', true);
$this->assertTrue(Config::inst()->get('ConfigStaticTest_First', 'bool'));
// Integers
$this->assertEquals(42, Config::inst()->get('ConfigStaticTest_First', 'int'));
Config::inst()->update('ConfigStaticTest_First', 'int', 0);
$this->assertEquals(0, Config::inst()->get('ConfigStaticTest_First', 'int'));
Config::inst()->update('ConfigStaticTest_First', 'int', 42);
$this->assertEquals(42, Config::inst()->get('ConfigStaticTest_First', 'int'));
// Strings
$this->assertEquals('value', Config::inst()->get('ConfigStaticTest_First', 'string'));
Config::inst()->update('ConfigStaticTest_First', 'string', '');
$this->assertEquals('', Config::inst()->get('ConfigStaticTest_First', 'string'));
Config::inst()->update('ConfigStaticTest_First', 'string', 'value');
$this->assertEquals('value', Config::inst()->get('ConfigStaticTest_First', 'string'));
// Nulls
$this->assertEquals('value', Config::inst()->get('ConfigStaticTest_First', 'nullable'));
Config::inst()->update('ConfigStaticTest_First', 'nullable', null);
$this->assertNull(Config::inst()->get('ConfigStaticTest_First', 'nullable'));
Config::inst()->update('ConfigStaticTest_First', 'nullable', 'value');
$this->assertEquals('value', Config::inst()->get('ConfigStaticTest_First', 'nullable'));
}
public function testSetsFalsyDefaults() {
$this->assertFalse(Config::inst()->get('ConfigStaticTest_First', 'default_false'));
// Technically the same as an undefined config key
$this->assertNull(Config::inst()->get('ConfigStaticTest_First', 'default_null'));
$this->assertEquals(0, Config::inst()->get('ConfigStaticTest_First', 'default_zero'));
$this->assertEquals('', Config::inst()->get('ConfigStaticTest_First', 'default_empty_string'));
}
public function testUninheritedStatic() {
$this->assertEquals(Config::inst()->get('ConfigStaticTest_First', 'third', Config::UNINHERITED), 'test_1');
$this->assertEquals(Config::inst()->get('ConfigStaticTest_Fourth', 'third', Config::UNINHERITED), null);
Config::inst()->update('ConfigStaticTest_First', 'first', array('test_1b'));
Config::inst()->update('ConfigStaticTest_Second', 'first', array('test_2b'));
// Check that it can be applied to parent and subclasses, and queried directly
$this->assertContains('test_1b',
Config::inst()->get('ConfigStaticTest_First', 'first', Config::UNINHERITED));
$this->assertContains('test_2b',
Config::inst()->get('ConfigStaticTest_Second', 'first', Config::UNINHERITED));
// But it won't affect subclasses - this is *uninherited* static
$this->assertNotContains('test_2b',
Config::inst()->get('ConfigStaticTest_Third', 'first', Config::UNINHERITED));
$this->assertNotContains('test_2b',
Config::inst()->get('ConfigStaticTest_Fourth', 'first', Config::UNINHERITED));
// Subclasses that don't have the static explicitly defined should allow definition, also
// This also checks that set can be called after the first uninherited get()
// call (which can be buggy due to caching)
Config::inst()->update('ConfigStaticTest_Fourth', 'first', array('test_4b'));
$this->assertContains('test_4b', Config::inst()->get('ConfigStaticTest_Fourth', 'first', Config::UNINHERITED));
}
public function testCombinedStatic() {
$this->assertEquals(Config::inst()->get('ConfigStaticTest_Combined3', 'first'),
array('test_3', 'test_2', 'test_1'));
// test that null values are ignored, but values on either side are still merged
$this->assertEquals(Config::inst()->get('ConfigStaticTest_Combined3', 'second'),
array('test_3', 'test_1'));
}
public function testMerges() {
$result = array('A' => 1, 'B' => 2, 'C' => 3);
Config::merge_array_low_into_high($result, array('C' => 4, 'D' => 5));
$this->assertEquals($result, array('A' => 1, 'B' => 2, 'C' => 3, 'D' => 5));
$result = array('A' => 1, 'B' => 2, 'C' => 3);
Config::merge_array_high_into_low($result, array('C' => 4, 'D' => 5));
$this->assertEquals($result, array('A' => 1, 'B' => 2, 'C' => 4, 'D' => 5));
$result = array('A' => 1, 'B' => 2, 'C' => array(1, 2, 3));
Config::merge_array_low_into_high($result, array('C' => array(4, 5, 6), 'D' => 5));
$this->assertEquals($result, array('A' => 1, 'B' => 2, 'C' => array(1, 2, 3, 4, 5, 6), 'D' => 5));
$result = array('A' => 1, 'B' => 2, 'C' => array(1, 2, 3));
Config::merge_array_high_into_low($result, array('C' => array(4, 5, 6), 'D' => 5));
$this->assertEquals($result, array('A' => 1, 'B' => 2, 'C' => array(4, 5, 6, 1, 2, 3), 'D' => 5));
$result = array('A' => 1, 'B' => 2, 'C' => array('Foo' => 1, 'Bar' => 2), 'D' => 3);
Config::merge_array_low_into_high($result, array('C' => array('Bar' => 3, 'Baz' => 4)));
$this->assertEquals($result,
array('A' => 1, 'B' => 2, 'C' => array('Foo' => 1, 'Bar' => 2, 'Baz' => 4), 'D' => 3));
$result = array('A' => 1, 'B' => 2, 'C' => array('Foo' => 1, 'Bar' => 2), 'D' => 3);
Config::merge_array_high_into_low($result, array('C' => array('Bar' => 3, 'Baz' => 4)));
$this->assertEquals($result,
array('A' => 1, 'B' => 2, 'C' => array('Foo' => 1, 'Bar' => 3, 'Baz' => 4), 'D' => 3));
}
public function testStaticLookup() {
$this->assertEquals(Object::static_lookup('ConfigTest_DefinesFoo', 'foo'), 1);
$this->assertEquals(Object::static_lookup('ConfigTest_DefinesFoo', 'bar'), null);
$this->assertEquals(Object::static_lookup('ConfigTest_DefinesBar', 'foo'), null);
$this->assertEquals(Object::static_lookup('ConfigTest_DefinesBar', 'bar'), 2);
$this->assertEquals(Object::static_lookup('ConfigTest_DefinesFooAndBar', 'foo'), 3);
$this->assertEquals(Object::static_lookup('ConfigTest_DefinesFooAndBar', 'bar'), 3);
$this->assertEquals(Object::static_lookup('ConfigTest_DefinesFooDoesntExtendObject', 'foo'), 4);
$this->assertEquals(Object::static_lookup('ConfigTest_DefinesFooDoesntExtendObject', 'bar'), null);
}
public function testFragmentOrder() {
$this->markTestIncomplete();
}
public function testLRUDiscarding() {
$cache = new ConfigTest_Config_LRU();
for ($i = 0; $i < Config_LRU::SIZE*2; $i++) $cache->set($i, $i);
$this->assertEquals(
Config_LRU::SIZE, count($cache->indexing),
'Homogenous usage gives exact discarding'
);
$cache = new ConfigTest_Config_LRU();
for ($i = 0; $i < Config_LRU::SIZE; $i++) $cache->set($i, $i);
for ($i = 0; $i < Config_LRU::SIZE; $i++) $cache->set(-1, -1);
$this->assertLessThan(
Config_LRU::SIZE, count($cache->indexing),
'Heterogenous usage gives sufficient discarding'
);
}
public function testLRUCleaning() {
$cache = new ConfigTest_Config_LRU();
for ($i = 0; $i < Config_LRU::SIZE; $i++) $cache->set($i, $i);
$this->assertEquals(Config_LRU::SIZE, count($cache->indexing));
$cache->clean();
$this->assertEquals(0, count($cache->indexing), 'Clean clears all items');
$this->assertFalse($cache->get(1), 'Clean clears all items');
$cache->set(1, 1, array('Foo'));
$this->assertEquals(1, count($cache->indexing));
$cache->clean('Foo');
$this->assertEquals(0, count($cache->indexing), 'Clean items with matching tag');
$this->assertFalse($cache->get(1), 'Clean items with matching tag');
$cache->set(1, 1, array('Foo', 'Bar'));
$this->assertEquals(1, count($cache->indexing));
$cache->clean('Bar');
$this->assertEquals(0, count($cache->indexing), 'Clean items with any single matching tag');
$this->assertFalse($cache->get(1), 'Clean items with any single matching tag');
}
}
class ConfigTest_Config_LRU extends Config_LRU implements TestOnly {
public $cache;
public $indexing;
}