Completed
Push — master ( 43d0b8...1d1227 )
by Daniel
16:45
created

ConfigTest::testFragmentOrder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
class ConfigTest_DefinesFoo extends Object implements TestOnly {
4
	protected static $foo = 1;
5
6
	private static $not_foo = 1;
7
}
8
9
class ConfigTest_DefinesBar extends ConfigTest_DefinesFoo {
10
	public static $bar = 2;
11
}
12
13
class ConfigTest_DefinesFooAndBar extends ConfigTest_DefinesFoo {
14
	protected static $foo = 3;
15
	public static $bar = 3;
16
}
17
18
class ConfigTest_DefinesFooDoesntExtendObject implements TestOnly {
19
	protected static $foo = 4;
20
}
21
22
class ConfigStaticTest_First extends Config implements TestOnly {
23
	/** @config */
24
	private static $first  = array('test_1');
25
	/** @config */
26
	private static $second = array('test_1');
27
	/** @config */
28
	private static $third  = 'test_1';
29
30
	/** @config */
31
	private static $bool = true;
32
	/** @config */
33
	private static $int = 42;
34
	/** @config */
35
	private static $string = 'value';
36
	/** @config */
37
	private static $nullable = 'value';
38
39
	/** @config */
40
	private static $default_false = false;
41
	/** @config */
42
	private static $default_null = null;
43
	/** @config */
44
	private static $default_zero = 0;
45
	public static $default_emtpy_string = '';
46
}
47
48
class ConfigStaticTest_Second extends ConfigStaticTest_First {
49
	private static $first = array('test_2');
50
}
51
52
class ConfigStaticTest_Third extends ConfigStaticTest_Second {
53
	private static $first  = array('test_3');
54
	private static $second = array('test_3');
55
	public static $fourth = array('test_3');
56
}
57
58
class ConfigStaticTest_Fourth extends ConfigStaticTest_Third {
59
	public static $fourth = array('test_4');
60
}
61
62
class ConfigStaticTest_Combined1 extends Config implements TestOnly {
63
	/** @config */
64
	private static $first  = array('test_1');
65
	/** @config */
66
	private static $second = array('test_1');
67
}
68
69
class ConfigStaticTest_Combined2 extends ConfigStaticTest_Combined1 {
70
	private static $first  = array('test_2');
71
	private static $second = null;
72
}
73
74
class ConfigStaticTest_Combined3 extends ConfigStaticTest_Combined2 {
75
	private static $first  = array('test_3');
76
	private static $second = array('test_3');
77
}
78
79
class ConfigTest_TestNest extends Object implements TestOnly {
80
	/** @config */
81
	private static $foo = 3;
82
	/** @config */
83
	private static $bar = 5;
84
}
85
86
class ConfigTest extends SapphireTest {
87
88
	protected $depSettings = null;
89
90
	public function setUp() {
91
		parent::setUp();
92
		$this->depSettings = Deprecation::dump_settings();
93
		Deprecation::set_enabled(false);
94
	}
95
96
	public function tearDown() {
97
		Deprecation::restore_settings($this->depSettings);
98
		parent::tearDown();
99
	}
100
101
	public function testNest() {
102
103
		// Check basic config
104
		$this->assertEquals(3, Config::inst()->get('ConfigTest_TestNest', 'foo'));
105
		$this->assertEquals(5, Config::inst()->get('ConfigTest_TestNest', 'bar'));
106
107
		// Test nest copies data
108
		Config::nest();
109
		$this->assertEquals(3, Config::inst()->get('ConfigTest_TestNest', 'foo'));
110
		$this->assertEquals(5, Config::inst()->get('ConfigTest_TestNest', 'bar'));
111
112
		// Test nested data can be updated
113
		Config::inst()->update('ConfigTest_TestNest', 'foo', 4);
114
		$this->assertEquals(4, Config::inst()->get('ConfigTest_TestNest', 'foo'));
115
		$this->assertEquals(5, Config::inst()->get('ConfigTest_TestNest', 'bar'));
116
117
		// Test unnest restores data
118
		Config::unnest();
119
		$this->assertEquals(3, Config::inst()->get('ConfigTest_TestNest', 'foo'));
120
		$this->assertEquals(5, Config::inst()->get('ConfigTest_TestNest', 'bar'));
121
	}
122
123
	public function testUpdateStatic() {
124
		$this->assertEquals(Config::inst()->get('ConfigStaticTest_First', 'first', Config::FIRST_SET),
125
			array('test_1'));
126
		$this->assertEquals(Config::inst()->get('ConfigStaticTest_Second', 'first', Config::FIRST_SET),
127
			array('test_2'));
128
		$this->assertEquals(Config::inst()->get('ConfigStaticTest_Third', 'first', Config::FIRST_SET),
129
			array('test_3'));
130
131
		Config::inst()->update('ConfigStaticTest_First', 'first', array('test_1_2'));
132
		Config::inst()->update('ConfigStaticTest_Third', 'first', array('test_3_2'));
133
		Config::inst()->update('ConfigStaticTest_Fourth', 'first', array('test_4'));
134
135
		$this->assertEquals(Config::inst()->get('ConfigStaticTest_First', 'first', Config::FIRST_SET),
136
			array('test_1_2', 'test_1'));
137
138
		Config::inst()->update('ConfigStaticTest_Fourth', 'second', array('test_4'));
139
		Config::inst()->update('ConfigStaticTest_Third', 'second', array('test_3_2'));
140
141
		$this->assertEquals(Config::inst()->get('ConfigStaticTest_Fourth', 'second', Config::FIRST_SET),
142
			array('test_4'));
143
		$this->assertEquals(Config::inst()->get('ConfigStaticTest_Third', 'second', Config::FIRST_SET),
144
			array('test_3_2', 'test_3'));
145
146
		Config::inst()->remove('ConfigStaticTest_Third', 'second');
147
		$this->assertEquals(array(), Config::inst()->get('ConfigStaticTest_Third', 'second'));
148
		Config::inst()->update('ConfigStaticTest_Third', 'second', array('test_3_2'));
149
		$this->assertEquals(Config::inst()->get('ConfigStaticTest_Third', 'second', Config::FIRST_SET),
150
			array('test_3_2'));
151
	}
152
153
	public function testUpdateWithFalsyValues() {
154
		// Booleans
155
		$this->assertTrue(Config::inst()->get('ConfigStaticTest_First', 'bool'));
156
		Config::inst()->update('ConfigStaticTest_First', 'bool', false);
157
		$this->assertFalse(Config::inst()->get('ConfigStaticTest_First', 'bool'));
158
		Config::inst()->update('ConfigStaticTest_First', 'bool', true);
159
		$this->assertTrue(Config::inst()->get('ConfigStaticTest_First', 'bool'));
160
161
		// Integers
162
		$this->assertEquals(42, Config::inst()->get('ConfigStaticTest_First', 'int'));
163
		Config::inst()->update('ConfigStaticTest_First', 'int', 0);
164
		$this->assertEquals(0, Config::inst()->get('ConfigStaticTest_First', 'int'));
165
		Config::inst()->update('ConfigStaticTest_First', 'int', 42);
166
		$this->assertEquals(42, Config::inst()->get('ConfigStaticTest_First', 'int'));
167
168
		// Strings
169
		$this->assertEquals('value', Config::inst()->get('ConfigStaticTest_First', 'string'));
170
		Config::inst()->update('ConfigStaticTest_First', 'string', '');
171
		$this->assertEquals('', Config::inst()->get('ConfigStaticTest_First', 'string'));
172
		Config::inst()->update('ConfigStaticTest_First', 'string', 'value');
173
		$this->assertEquals('value', Config::inst()->get('ConfigStaticTest_First', 'string'));
174
175
		// Nulls
176
		$this->assertEquals('value', Config::inst()->get('ConfigStaticTest_First', 'nullable'));
177
		Config::inst()->update('ConfigStaticTest_First', 'nullable', null);
178
		$this->assertNull(Config::inst()->get('ConfigStaticTest_First', 'nullable'));
179
		Config::inst()->update('ConfigStaticTest_First', 'nullable', 'value');
180
		$this->assertEquals('value', Config::inst()->get('ConfigStaticTest_First', 'nullable'));
181
	}
182
183
	public function testSetsFalsyDefaults() {
184
		$this->assertFalse(Config::inst()->get('ConfigStaticTest_First', 'default_false'));
185
		// Technically the same as an undefined config key
186
		$this->assertNull(Config::inst()->get('ConfigStaticTest_First', 'default_null'));
187
		$this->assertEquals(0, Config::inst()->get('ConfigStaticTest_First', 'default_zero'));
188
		$this->assertEquals('', Config::inst()->get('ConfigStaticTest_First', 'default_empty_string'));
189
	}
190
191
	public function testUninheritedStatic() {
192
		$this->assertEquals(Config::inst()->get('ConfigStaticTest_First',  'third', Config::UNINHERITED), 'test_1');
193
		$this->assertEquals(Config::inst()->get('ConfigStaticTest_Fourth', 'third', Config::UNINHERITED), null);
194
195
		Config::inst()->update('ConfigStaticTest_First', 'first', array('test_1b'));
196
		Config::inst()->update('ConfigStaticTest_Second', 'first', array('test_2b'));
197
198
		// Check that it can be applied to parent and subclasses, and queried directly
199
		$this->assertContains('test_1b',
200
			Config::inst()->get('ConfigStaticTest_First', 'first', Config::UNINHERITED));
201
		$this->assertContains('test_2b',
202
			Config::inst()->get('ConfigStaticTest_Second', 'first', Config::UNINHERITED));
203
204
		// But it won't affect subclasses - this is *uninherited* static
205
		$this->assertNotContains('test_2b',
206
			Config::inst()->get('ConfigStaticTest_Third', 'first', Config::UNINHERITED));
207
		$this->assertNull(Config::inst()->get('ConfigStaticTest_Fourth', 'first', Config::UNINHERITED));
208
209
		// Subclasses that don't have the static explicitly defined should allow definition, also
210
		// This also checks that set can be called after the first uninherited get()
211
		// call (which can be buggy due to caching)
212
		Config::inst()->update('ConfigStaticTest_Fourth', 'first', array('test_4b'));
213
		$this->assertContains('test_4b', Config::inst()->get('ConfigStaticTest_Fourth', 'first', Config::UNINHERITED));
214
	}
215
216
	public function testCombinedStatic() {
217
		$this->assertEquals(Config::inst()->get('ConfigStaticTest_Combined3', 'first'),
218
			array('test_3', 'test_2', 'test_1'));
219
220
		// test that null values are ignored, but values on either side are still merged
221
		$this->assertEquals(Config::inst()->get('ConfigStaticTest_Combined3', 'second'),
222
			array('test_3', 'test_1'));
223
	}
224
225
	public function testMerges() {
226
		$result = array('A' => 1, 'B' => 2, 'C' => 3);
227
		Config::merge_array_low_into_high($result, array('C' => 4, 'D' => 5));
228
		$this->assertEquals($result, array('A' => 1, 'B' => 2, 'C' => 3, 'D' => 5));
229
230
		$result = array('A' => 1, 'B' => 2, 'C' => 3);
231
		Config::merge_array_high_into_low($result, array('C' => 4, 'D' => 5));
232
		$this->assertEquals($result, array('A' => 1, 'B' => 2, 'C' => 4, 'D' => 5));
233
234
		$result = array('A' => 1, 'B' => 2, 'C' => array(1, 2, 3));
235
		Config::merge_array_low_into_high($result, array('C' => array(4, 5, 6), 'D' => 5));
236
		$this->assertEquals($result, array('A' => 1, 'B' => 2, 'C' => array(1, 2, 3, 4, 5, 6), 'D' => 5));
237
238
		$result = array('A' => 1, 'B' => 2, 'C' => array(1, 2, 3));
239
		Config::merge_array_high_into_low($result, array('C' => array(4, 5, 6), 'D' => 5));
240
		$this->assertEquals($result, array('A' => 1, 'B' => 2, 'C' => array(4, 5, 6, 1, 2, 3), 'D' => 5));
241
242
		$result = array('A' => 1, 'B' => 2, 'C' => array('Foo' => 1, 'Bar' => 2), 'D' => 3);
243
		Config::merge_array_low_into_high($result, array('C' => array('Bar' => 3, 'Baz' => 4)));
244
		$this->assertEquals($result,
245
			array('A' => 1, 'B' => 2, 'C' => array('Foo' => 1, 'Bar' => 2, 'Baz' => 4), 'D' => 3));
246
247
		$result = array('A' => 1, 'B' => 2, 'C' => array('Foo' => 1, 'Bar' => 2), 'D' => 3);
248
		Config::merge_array_high_into_low($result, array('C' => array('Bar' => 3, 'Baz' => 4)));
249
		$this->assertEquals($result,
250
			array('A' => 1, 'B' => 2, 'C' => array('Foo' => 1, 'Bar' => 3, 'Baz' => 4), 'D' => 3));
251
	}
252
253
	public function testStaticLookup() {
254
		$this->assertEquals(Object::static_lookup('ConfigTest_DefinesFoo', 'foo'), 1);
255
		$this->assertEquals(Object::static_lookup('ConfigTest_DefinesFoo', 'bar'), null);
256
257
		$this->assertEquals(Object::static_lookup('ConfigTest_DefinesBar', 'foo'), null);
258
		$this->assertEquals(Object::static_lookup('ConfigTest_DefinesBar', 'bar'), 2);
259
260
		$this->assertEquals(Object::static_lookup('ConfigTest_DefinesFooAndBar', 'foo'), 3);
261
		$this->assertEquals(Object::static_lookup('ConfigTest_DefinesFooAndBar', 'bar'), 3);
262
263
		$this->assertEquals(Object::static_lookup('ConfigTest_DefinesFooDoesntExtendObject', 'foo'), 4);
264
		$this->assertEquals(Object::static_lookup('ConfigTest_DefinesFooDoesntExtendObject', 'bar'), null);
265
	}
266
267
	public function testForClass() {
268
		$config = ConfigTest_DefinesFoo::config();
269
		// Set values
270
		$this->assertTrue(isset($config->not_foo));
271
		$this->assertFalse(empty($config->not_foo));
272
		$this->assertEquals(1, $config->not_foo);
273
274
		// Unset values
275
		$this->assertFalse(isset($config->bar));
276
		$this->assertTrue(empty($config->bar));
277
		$this->assertNull($config->bar);
278
	}
279
280
	public function testFragmentOrder() {
281
		$this->markTestIncomplete();
282
	}
283
284
	public function testCacheCleaning() {
285
		$cache = new ConfigTest_Config_MemCache();
286
287
		for ($i = 0; $i < 1000; $i++) $cache->set($i, $i);
288
		$this->assertEquals(1000, count($cache->cache));
289
290
		$cache->clean();
291
		$this->assertEquals(0, count($cache->cache), 'Clean clears all items');
292
		$this->assertFalse($cache->get(1), 'Clean clears all items');
293
294
		$cache->set(1, 1, array('Foo'));
295
		$this->assertEquals(1, count($cache->cache));
296
		$this->assertEquals(1, count($cache->tags));
297
298
		$cache->clean('Foo');
299
		$this->assertEquals(0, count($cache->tags), 'Clean items with matching tag');
300
		$this->assertFalse($cache->get(1), 'Clean items with matching tag');
301
302
		$cache->set(1, 1, array('Foo', 'Bar'));
303
		$this->assertEquals(2, count($cache->tags));
304
		$this->assertEquals(1, count($cache->cache));
305
306
		$cache->clean('Bar');
307
		$this->assertEquals(1, count($cache->tags));
308
		$this->assertEquals(0, count($cache->cache), 'Clean items with any single matching tag');
309
		$this->assertFalse($cache->get(1), 'Clean items with any single matching tag');
310
	}
311
312
	public function testLRUDiscarding() {
313
		$cache = new Config_LRU();
0 ignored issues
show
Deprecated Code introduced by
The class Config_LRU has been deprecated with message: 4.0

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
314
		for ($i = 0; $i < Config_LRU::SIZE*2; $i++) $cache->set($i, $i);
315
		$this->assertEquals(
316
			Config_LRU::SIZE, $cache->getIndexCount(),
317
			'Homogenous usage gives exact discarding'
318
		);
319
		$cache = new Config_LRU();
0 ignored issues
show
Deprecated Code introduced by
The class Config_LRU has been deprecated with message: 4.0

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
320
		for ($i = 0; $i < Config_LRU::SIZE; $i++) $cache->set($i, $i);
321
		for ($i = 0; $i < Config_LRU::SIZE; $i++) $cache->set(-1, -1);
322
		$this->assertLessThan(
323
			Config_LRU::SIZE, $cache->getIndexCount(),
324
			'Heterogenous usage gives sufficient discarding'
325
		);
326
	}
327
328
329
	public function testLRUCleaning() {
330
		$cache = new Config_LRU();
0 ignored issues
show
Deprecated Code introduced by
The class Config_LRU has been deprecated with message: 4.0

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
331
		for ($i = 0; $i < Config_LRU::SIZE; $i++) $cache->set($i, $i);
332
		$this->assertEquals(Config_LRU::SIZE, $cache->getIndexCount());
333
		$cache->clean();
334
		$this->assertEquals(0, $cache->getIndexCount(), 'Clean clears all items');
335
		$this->assertFalse($cache->get(1), 'Clean clears all items');
336
		$cache->set(1, 1, array('Foo'));
337
		$this->assertEquals(1, $cache->getIndexCount());
338
		$cache->clean('Foo');
339
		$this->assertEquals(0, $cache->getIndexCount(), 'Clean items with matching tag');
340
		$this->assertFalse($cache->get(1), 'Clean items with matching tag');
341
		$cache->set(1, 1, array('Foo', 'Bar'));
342
		$this->assertEquals(1, $cache->getIndexCount());
343
		$cache->clean('Bar');
344
		$this->assertEquals(0, $cache->getIndexCount(), 'Clean items with any single matching tag');
345
		$this->assertFalse($cache->get(1), 'Clean items with any single matching tag');
346
	}
347
}
348
349
class ConfigTest_Config_MemCache extends Config_MemCache implements TestOnly {
350
351
	public $cache;
352
	public $tags;
353
354
}
355