Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
16591 anikendra 1
<?php
2
/**
3
 * ConfigureTest file
4
 *
5
 * Holds several tests
6
 *
7
 * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
8
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
9
 *
10
 * Licensed under The MIT License
11
 * For full copyright and license information, please see the LICENSE.txt
12
 * Redistributions of files must retain the above copyright notice
13
 *
14
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
15
 * @link          http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
16
 * @package       Cake.Test.Case.Core
17
 * @since         CakePHP(tm) v 1.2.0.5432
18
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
19
 */
20
 
21
App::uses('PhpReader', 'Configure');
22
 
23
/**
24
 * ConfigureTest
25
 *
26
 * @package       Cake.Test.Case.Core
27
 */
28
class ConfigureTest extends CakeTestCase {
29
 
30
/**
31
 * setUp method
32
 *
33
 * @return void
34
 */
35
	public function setUp() {
36
		parent::setUp();
37
		Configure::write('Cache.disable', true);
38
		App::build();
39
		App::objects('plugin', null, true);
40
	}
41
 
42
/**
43
 * tearDown method
44
 *
45
 * @return void
46
 */
47
	public function tearDown() {
48
		parent::tearDown();
49
		if (file_exists(TMP . 'cache' . DS . 'persistent' . DS . 'cake_core_core_paths')) {
50
			unlink(TMP . 'cache' . DS . 'persistent' . DS . 'cake_core_core_paths');
51
		}
52
		if (file_exists(TMP . 'cache' . DS . 'persistent' . DS . 'cake_core_dir_map')) {
53
			unlink(TMP . 'cache' . DS . 'persistent' . DS . 'cake_core_dir_map');
54
		}
55
		if (file_exists(TMP . 'cache' . DS . 'persistent' . DS . 'cake_core_file_map')) {
56
			unlink(TMP . 'cache' . DS . 'persistent' . DS . 'cake_core_file_map');
57
		}
58
		if (file_exists(TMP . 'cache' . DS . 'persistent' . DS . 'cake_core_object_map')) {
59
			unlink(TMP . 'cache' . DS . 'persistent' . DS . 'cake_core_object_map');
60
		}
61
		if (file_exists(TMP . 'cache' . DS . 'persistent' . DS . 'test.config.php')) {
62
			unlink(TMP . 'cache' . DS . 'persistent' . DS . 'test.config.php');
63
		}
64
		if (file_exists(TMP . 'cache' . DS . 'persistent' . DS . 'test.php')) {
65
			unlink(TMP . 'cache' . DS . 'persistent' . DS . 'test.php');
66
		}
67
		Configure::drop('test');
68
	}
69
 
70
/**
71
 * Test to ensure bootrapping doesn't overwrite prior configs set under 'App' key
72
 * @return void
73
 */
74
	public function testBootstrap() {
75
		$expected = array(
76
			'foo' => 'bar'
77
		);
78
		Configure::write('App', $expected);
79
 
80
		Configure::bootstrap(true);
81
		$result = Configure::read('App');
82
 
83
		$this->assertEquals($expected['foo'], $result['foo']);
84
		$this->assertFalse($result['base']);
85
	}
86
 
87
/**
88
 * testRead method
89
 *
90
 * @return void
91
 */
92
	public function testRead() {
93
		$expected = 'ok';
94
		Configure::write('level1.level2.level3_1', $expected);
95
		Configure::write('level1.level2.level3_2', 'something_else');
96
		$result = Configure::read('level1.level2.level3_1');
97
		$this->assertEquals($expected, $result);
98
 
99
		$result = Configure::read('level1.level2.level3_2');
100
		$this->assertEquals('something_else', $result);
101
 
102
		$result = Configure::read('debug');
103
		$this->assertTrue($result >= 0);
104
 
105
		$result = Configure::read();
106
		$this->assertTrue(is_array($result));
107
		$this->assertTrue(isset($result['debug']));
108
		$this->assertTrue(isset($result['level1']));
109
 
110
		$result = Configure::read('something_I_just_made_up_now');
111
		$this->assertEquals(null, $result, 'Missing key should return null.');
112
	}
113
 
114
/**
115
 * testWrite method
116
 *
117
 * @return void
118
 */
119
	public function testWrite() {
120
		$writeResult = Configure::write('SomeName.someKey', 'myvalue');
121
		$this->assertTrue($writeResult);
122
		$result = Configure::read('SomeName.someKey');
123
		$this->assertEquals('myvalue', $result);
124
 
125
		$writeResult = Configure::write('SomeName.someKey', null);
126
		$this->assertTrue($writeResult);
127
		$result = Configure::read('SomeName.someKey');
128
		$this->assertEquals(null, $result);
129
 
130
		$expected = array('One' => array('Two' => array('Three' => array('Four' => array('Five' => 'cool')))));
131
		$writeResult = Configure::write('Key', $expected);
132
		$this->assertTrue($writeResult);
133
 
134
		$result = Configure::read('Key');
135
		$this->assertEquals($expected, $result);
136
 
137
		$result = Configure::read('Key.One');
138
		$this->assertEquals($expected['One'], $result);
139
 
140
		$result = Configure::read('Key.One.Two');
141
		$this->assertEquals($expected['One']['Two'], $result);
142
 
143
		$result = Configure::read('Key.One.Two.Three.Four.Five');
144
		$this->assertEquals('cool', $result);
145
 
146
		Configure::write('one.two.three.four', '4');
147
		$result = Configure::read('one.two.three.four');
148
		$this->assertEquals('4', $result);
149
	}
150
 
151
/**
152
 * Test the consume method.
153
 *
154
 * @return void
155
 */
156
	public function testConsume() {
157
		$this->assertNull(Configure::consume('DoesNotExist'), 'Should be null on empty value');
158
		Configure::write('Test', array('key' => 'value', 'key2' => 'value2'));
159
 
160
		$result = Configure::consume('Test.key');
161
		$this->assertEquals('value', $result);
162
 
163
		$result = Configure::read('Test.key2');
164
		$this->assertEquals('value2', $result, 'Other values should remain.');
165
 
166
		$result = Configure::consume('Test');
167
		$expected = array('key2' => 'value2');
168
		$this->assertEquals($expected, $result);
169
	}
170
 
171
/**
172
 * testConsumeEmpty
173
 *
174
 * @return void
175
 */
176
	public function testConsumeEmpty() {
177
		Configure::write('Test', array('key' => 'value', 'key2' => 'value2'));
178
		$result = Configure::consume('');
179
		$this->assertNull($result);
180
		$result = Configure::consume(null);
181
		$this->assertNull($result);
182
	}
183
 
184
/**
185
 * test setting display_errors with debug.
186
 *
187
 * @return void
188
 */
189
	public function testDebugSettingDisplayErrors() {
190
		Configure::write('debug', 0);
191
		$result = ini_get('display_errors');
192
		$this->assertEquals(0, $result);
193
 
194
		Configure::write('debug', 2);
195
		$result = ini_get('display_errors');
196
		$this->assertEquals(1, $result);
197
	}
198
 
199
/**
200
 * testDelete method
201
 *
202
 * @return void
203
 */
204
	public function testDelete() {
205
		Configure::write('SomeName.someKey', 'myvalue');
206
		$result = Configure::read('SomeName.someKey');
207
		$this->assertEquals('myvalue', $result);
208
 
209
		Configure::delete('SomeName.someKey');
210
		$result = Configure::read('SomeName.someKey');
211
		$this->assertNull($result);
212
 
213
		Configure::write('SomeName', array('someKey' => 'myvalue', 'otherKey' => 'otherValue'));
214
 
215
		$result = Configure::read('SomeName.someKey');
216
		$this->assertEquals('myvalue', $result);
217
 
218
		$result = Configure::read('SomeName.otherKey');
219
		$this->assertEquals('otherValue', $result);
220
 
221
		Configure::delete('SomeName');
222
 
223
		$result = Configure::read('SomeName.someKey');
224
		$this->assertNull($result);
225
 
226
		$result = Configure::read('SomeName.otherKey');
227
		$this->assertNull($result);
228
	}
229
 
230
/**
231
 * testCheck method
232
 *
233
 * @return void
234
 */
235
	public function testCheck() {
236
		Configure::write('ConfigureTestCase', 'value');
237
		$this->assertTrue(Configure::check('ConfigureTestCase'));
238
 
239
		$this->assertFalse(Configure::check('NotExistingConfigureTestCase'));
240
	}
241
 
242
/**
243
 * testCheckingSavedEmpty method
244
 *
245
 * @return void
246
 */
247
	public function testCheckingSavedEmpty() {
248
		$this->assertTrue(Configure::write('ConfigureTestCase', 0));
249
		$this->assertTrue(Configure::check('ConfigureTestCase'));
250
 
251
		$this->assertTrue(Configure::write('ConfigureTestCase', '0'));
252
		$this->assertTrue(Configure::check('ConfigureTestCase'));
253
 
254
		$this->assertTrue(Configure::write('ConfigureTestCase', false));
255
		$this->assertTrue(Configure::check('ConfigureTestCase'));
256
 
257
		$this->assertTrue(Configure::write('ConfigureTestCase', null));
258
		$this->assertFalse(Configure::check('ConfigureTestCase'));
259
	}
260
 
261
/**
262
 * testCheckKeyWithSpaces method
263
 *
264
 * @return void
265
 */
266
	public function testCheckKeyWithSpaces() {
267
		$this->assertTrue(Configure::write('Configure Test', "test"));
268
		$this->assertTrue(Configure::check('Configure Test'));
269
		Configure::delete('Configure Test');
270
 
271
		$this->assertTrue(Configure::write('Configure Test.Test Case', "test"));
272
		$this->assertTrue(Configure::check('Configure Test.Test Case'));
273
	}
274
 
275
/**
276
 * testCheckEmpty
277
 *
278
 * @return void
279
 */
280
	public function testCheckEmpty() {
281
		$this->assertFalse(Configure::check(''));
282
		$this->assertFalse(Configure::check(null));
283
	}
284
 
285
/**
286
 * testLoad method
287
 *
288
 * @expectedException RuntimeException
289
 * @return void
290
 */
291
	public function testLoadExceptionOnNonExistantFile() {
292
		Configure::config('test', new PhpReader());
293
		Configure::load('non_existing_configuration_file', 'test');
294
	}
295
 
296
/**
297
 * test load method for default config creation
298
 *
299
 * @return void
300
 */
301
	public function testLoadDefaultConfig() {
302
		try {
303
			Configure::load('non_existing_configuration_file');
304
		} catch (Exception $e) {
305
			$result = Configure::configured('default');
306
			$this->assertTrue($result);
307
		}
308
	}
309
 
310
/**
311
 * test load with merging
312
 *
313
 * @return void
314
 */
315
	public function testLoadWithMerge() {
316
		Configure::config('test', new PhpReader(CAKE . 'Test' . DS . 'test_app' . DS . 'Config' . DS));
317
 
318
		$result = Configure::load('var_test', 'test');
319
		$this->assertTrue($result);
320
 
321
		$this->assertEquals('value', Configure::read('Read'));
322
 
323
		$result = Configure::load('var_test2', 'test', true);
324
		$this->assertTrue($result);
325
 
326
		$this->assertEquals('value2', Configure::read('Read'));
327
		$this->assertEquals('buried2', Configure::read('Deep.Second.SecondDeepest'));
328
		$this->assertEquals('buried', Configure::read('Deep.Deeper.Deepest'));
329
		$this->assertEquals('Overwrite', Configure::read('TestAcl.classname'));
330
		$this->assertEquals('one', Configure::read('TestAcl.custom'));
331
	}
332
 
333
/**
334
 * test loading with overwrite
335
 *
336
 * @return void
337
 */
338
	public function testLoadNoMerge() {
339
		Configure::config('test', new PhpReader(CAKE . 'Test' . DS . 'test_app' . DS . 'Config' . DS));
340
 
341
		$result = Configure::load('var_test', 'test');
342
		$this->assertTrue($result);
343
 
344
		$this->assertEquals('value', Configure::read('Read'));
345
 
346
		$result = Configure::load('var_test2', 'test', false);
347
		$this->assertTrue($result);
348
 
349
		$this->assertEquals('value2', Configure::read('Read'));
350
		$this->assertEquals('buried2', Configure::read('Deep.Second.SecondDeepest'));
351
		$this->assertNull(Configure::read('Deep.Deeper.Deepest'));
352
	}
353
 
354
/**
355
 * testLoad method
356
 *
357
 * @return void
358
 */
359
	public function testLoadPlugin() {
360
		App::build(array(
361
			'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
362
		), App::RESET);
363
		Configure::config('test', new PhpReader());
364
		CakePlugin::load('TestPlugin');
365
		$result = Configure::load('TestPlugin.load', 'test');
366
		$this->assertTrue($result);
367
		$expected = '/test_app/plugins/test_plugin/config/load.php';
368
		$config = Configure::read('plugin_load');
369
		$this->assertEquals($expected, $config);
370
 
371
		$result = Configure::load('TestPlugin.more.load', 'test');
372
		$this->assertTrue($result);
373
		$expected = '/test_app/plugins/test_plugin/config/more.load.php';
374
		$config = Configure::read('plugin_more_load');
375
		$this->assertEquals($expected, $config);
376
		CakePlugin::unload();
377
	}
378
 
379
/**
380
 * testStore method
381
 *
382
 * @return void
383
 */
384
	public function testStoreAndRestore() {
385
		Configure::write('Cache.disable', false);
386
 
387
		Configure::write('Testing', 'yummy');
388
		$this->assertTrue(Configure::store('store_test', 'default'));
389
 
390
		Configure::delete('Testing');
391
		$this->assertNull(Configure::read('Testing'));
392
 
393
		Configure::restore('store_test', 'default');
394
		$this->assertEquals('yummy', Configure::read('Testing'));
395
 
396
		Cache::delete('store_test', 'default');
397
	}
398
 
399
/**
400
 * test that store and restore only store/restore the provided data.
401
 *
402
 * @return void
403
 */
404
	public function testStoreAndRestoreWithData() {
405
		Configure::write('Cache.disable', false);
406
 
407
		Configure::write('testing', 'value');
408
		Configure::store('store_test', 'default', array('store_test' => 'one'));
409
		Configure::delete('testing');
410
		$this->assertNull(Configure::read('store_test'), 'Calling store with data shouldn\'t modify runtime.');
411
 
412
		Configure::restore('store_test', 'default');
413
		$this->assertEquals('one', Configure::read('store_test'));
414
		$this->assertNull(Configure::read('testing'), 'Values that were not stored are not restored.');
415
 
416
		Cache::delete('store_test', 'default');
417
	}
418
 
419
/**
420
 * testVersion method
421
 *
422
 * @return void
423
 */
424
	public function testVersion() {
425
		$result = Configure::version();
426
		$this->assertTrue(version_compare($result, '1.2', '>='));
427
	}
428
 
429
/**
430
 * test adding new readers.
431
 *
432
 * @return void
433
 */
434
	public function testReaderSetup() {
435
		$reader = new PhpReader();
436
		Configure::config('test', $reader);
437
		$configured = Configure::configured();
438
 
439
		$this->assertTrue(in_array('test', $configured));
440
 
441
		$this->assertTrue(Configure::configured('test'));
442
		$this->assertFalse(Configure::configured('fake_garbage'));
443
 
444
		$this->assertTrue(Configure::drop('test'));
445
		$this->assertFalse(Configure::drop('test'), 'dropping things that do not exist should return false.');
446
	}
447
 
448
/**
449
 * test reader() throwing exceptions on missing interface.
450
 *
451
 * @expectedException PHPUnit_Framework_Error
452
 * @return void
453
 */
454
	public function testReaderExceptionOnIncorrectClass() {
455
		$reader = new StdClass();
456
		Configure::config('test', $reader);
457
	}
458
 
459
/**
460
 * Test that clear wipes all values.
461
 *
462
 * @return void
463
 */
464
	public function testClear() {
465
		Configure::write('test', 'value');
466
		$this->assertTrue(Configure::clear());
467
		$this->assertNull(Configure::read('debug'));
468
		$this->assertNull(Configure::read('test'));
469
	}
470
 
471
/**
472
 * @expectedException ConfigureException
473
 * @return void
474
 */
475
	public function testDumpNoAdapter() {
476
		Configure::dump(TMP . 'test.php', 'does_not_exist');
477
	}
478
 
479
/**
480
 * test dump integrated with the PhpReader.
481
 *
482
 * @return void
483
 */
484
	public function testDump() {
485
		Configure::config('test_reader', new PhpReader(TMP));
486
 
487
		$result = Configure::dump('config_test.php', 'test_reader');
488
		$this->assertTrue($result > 0);
489
		$result = file_get_contents(TMP . 'config_test.php');
490
		$this->assertContains('<?php', $result);
491
		$this->assertContains('$config = ', $result);
492
		if (file_exists(TMP . 'config_test.php')) {
493
			unlink(TMP . 'config_test.php');
494
		}
495
	}
496
 
497
/**
498
 * Test dumping only some of the data.
499
 *
500
 * @return void
501
 */
502
	public function testDumpPartial() {
503
		Configure::config('test_reader', new PhpReader(TMP));
504
 
505
		$result = Configure::dump('config_test.php', 'test_reader', array('Error'));
506
		$this->assertTrue($result > 0);
507
		$result = file_get_contents(TMP . 'config_test.php');
508
		$this->assertContains('<?php', $result);
509
		$this->assertContains('$config = ', $result);
510
		$this->assertContains('Error', $result);
511
		$this->assertNotContains('debug', $result);
512
 
513
		if (file_exists(TMP . 'config_test.php')) {
514
			unlink(TMP . 'config_test.php');
515
		}
516
	}
517
 
518
}