Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
13532 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 setting display_errors with debug.
153
 *
154
 * @return void
155
 */
156
	public function testDebugSettingDisplayErrors() {
157
		Configure::write('debug', 0);
158
		$result = ini_get('display_errors');
159
		$this->assertEquals(0, $result);
160
 
161
		Configure::write('debug', 2);
162
		$result = ini_get('display_errors');
163
		$this->assertEquals(1, $result);
164
	}
165
 
166
/**
167
 * testDelete method
168
 *
169
 * @return void
170
 */
171
	public function testDelete() {
172
		Configure::write('SomeName.someKey', 'myvalue');
173
		$result = Configure::read('SomeName.someKey');
174
		$this->assertEquals('myvalue', $result);
175
 
176
		Configure::delete('SomeName.someKey');
177
		$result = Configure::read('SomeName.someKey');
178
		$this->assertTrue($result === null);
179
 
180
		Configure::write('SomeName', array('someKey' => 'myvalue', 'otherKey' => 'otherValue'));
181
 
182
		$result = Configure::read('SomeName.someKey');
183
		$this->assertEquals('myvalue', $result);
184
 
185
		$result = Configure::read('SomeName.otherKey');
186
		$this->assertEquals('otherValue', $result);
187
 
188
		Configure::delete('SomeName');
189
 
190
		$result = Configure::read('SomeName.someKey');
191
		$this->assertTrue($result === null);
192
 
193
		$result = Configure::read('SomeName.otherKey');
194
		$this->assertTrue($result === null);
195
	}
196
 
197
/**
198
 * testCheck method
199
 *
200
 * @return void
201
 */
202
	public function testCheck() {
203
		Configure::write('ConfigureTestCase', 'value');
204
		$this->assertTrue(Configure::check('ConfigureTestCase'));
205
 
206
		$this->assertFalse(Configure::check('NotExistingConfigureTestCase'));
207
	}
208
 
209
/**
210
 * testCheckingSavedEmpty method
211
 *
212
 * @return void
213
 */
214
	public function testCheckingSavedEmpty() {
215
		$this->assertTrue(Configure::write('ConfigureTestCase', 0));
216
		$this->assertTrue(Configure::check('ConfigureTestCase'));
217
 
218
		$this->assertTrue(Configure::write('ConfigureTestCase', '0'));
219
		$this->assertTrue(Configure::check('ConfigureTestCase'));
220
 
221
		$this->assertTrue(Configure::write('ConfigureTestCase', false));
222
		$this->assertTrue(Configure::check('ConfigureTestCase'));
223
 
224
		$this->assertTrue(Configure::write('ConfigureTestCase', null));
225
		$this->assertFalse(Configure::check('ConfigureTestCase'));
226
	}
227
 
228
/**
229
 * testCheckKeyWithSpaces method
230
 *
231
 * @return void
232
 */
233
	public function testCheckKeyWithSpaces() {
234
		$this->assertTrue(Configure::write('Configure Test', "test"));
235
		$this->assertTrue(Configure::check('Configure Test'));
236
		Configure::delete('Configure Test');
237
 
238
		$this->assertTrue(Configure::write('Configure Test.Test Case', "test"));
239
		$this->assertTrue(Configure::check('Configure Test.Test Case'));
240
	}
241
 
242
/**
243
 * testCheckEmpty
244
 *
245
 * @return void
246
 */
247
	public function testCheckEmpty() {
248
		$this->assertFalse(Configure::check());
249
	}
250
 
251
/**
252
 * testLoad method
253
 *
254
 * @expectedException RuntimeException
255
 * @return void
256
 */
257
	public function testLoadExceptionOnNonExistantFile() {
258
		Configure::config('test', new PhpReader());
259
		Configure::load('non_existing_configuration_file', 'test');
260
	}
261
 
262
/**
263
 * test load method for default config creation
264
 *
265
 * @return void
266
 */
267
	public function testLoadDefaultConfig() {
268
		try {
269
			Configure::load('non_existing_configuration_file');
270
		} catch (Exception $e) {
271
			$result = Configure::configured('default');
272
			$this->assertTrue($result);
273
		}
274
	}
275
 
276
/**
277
 * test load with merging
278
 *
279
 * @return void
280
 */
281
	public function testLoadWithMerge() {
282
		Configure::config('test', new PhpReader(CAKE . 'Test' . DS . 'test_app' . DS . 'Config' . DS));
283
 
284
		$result = Configure::load('var_test', 'test');
285
		$this->assertTrue($result);
286
 
287
		$this->assertEquals('value', Configure::read('Read'));
288
 
289
		$result = Configure::load('var_test2', 'test', true);
290
		$this->assertTrue($result);
291
 
292
		$this->assertEquals('value2', Configure::read('Read'));
293
		$this->assertEquals('buried2', Configure::read('Deep.Second.SecondDeepest'));
294
		$this->assertEquals('buried', Configure::read('Deep.Deeper.Deepest'));
295
		$this->assertEquals('Overwrite', Configure::read('TestAcl.classname'));
296
		$this->assertEquals('one', Configure::read('TestAcl.custom'));
297
	}
298
 
299
/**
300
 * test loading with overwrite
301
 *
302
 * @return void
303
 */
304
	public function testLoadNoMerge() {
305
		Configure::config('test', new PhpReader(CAKE . 'Test' . DS . 'test_app' . DS . 'Config' . DS));
306
 
307
		$result = Configure::load('var_test', 'test');
308
		$this->assertTrue($result);
309
 
310
		$this->assertEquals('value', Configure::read('Read'));
311
 
312
		$result = Configure::load('var_test2', 'test', false);
313
		$this->assertTrue($result);
314
 
315
		$this->assertEquals('value2', Configure::read('Read'));
316
		$this->assertEquals('buried2', Configure::read('Deep.Second.SecondDeepest'));
317
		$this->assertNull(Configure::read('Deep.Deeper.Deepest'));
318
	}
319
 
320
/**
321
 * testLoad method
322
 *
323
 * @return void
324
 */
325
	public function testLoadPlugin() {
326
		App::build(array(
327
			'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
328
		), App::RESET);
329
		Configure::config('test', new PhpReader());
330
		CakePlugin::load('TestPlugin');
331
		$result = Configure::load('TestPlugin.load', 'test');
332
		$this->assertTrue($result);
333
		$expected = '/test_app/plugins/test_plugin/config/load.php';
334
		$config = Configure::read('plugin_load');
335
		$this->assertEquals($expected, $config);
336
 
337
		$result = Configure::load('TestPlugin.more.load', 'test');
338
		$this->assertTrue($result);
339
		$expected = '/test_app/plugins/test_plugin/config/more.load.php';
340
		$config = Configure::read('plugin_more_load');
341
		$this->assertEquals($expected, $config);
342
		CakePlugin::unload();
343
	}
344
 
345
/**
346
 * testStore method
347
 *
348
 * @return void
349
 */
350
	public function testStoreAndRestore() {
351
		Configure::write('Cache.disable', false);
352
 
353
		Configure::write('Testing', 'yummy');
354
		$this->assertTrue(Configure::store('store_test', 'default'));
355
 
356
		Configure::delete('Testing');
357
		$this->assertNull(Configure::read('Testing'));
358
 
359
		Configure::restore('store_test', 'default');
360
		$this->assertEquals('yummy', Configure::read('Testing'));
361
 
362
		Cache::delete('store_test', 'default');
363
	}
364
 
365
/**
366
 * test that store and restore only store/restore the provided data.
367
 *
368
 * @return void
369
 */
370
	public function testStoreAndRestoreWithData() {
371
		Configure::write('Cache.disable', false);
372
 
373
		Configure::write('testing', 'value');
374
		Configure::store('store_test', 'default', array('store_test' => 'one'));
375
		Configure::delete('testing');
376
		$this->assertNull(Configure::read('store_test'), 'Calling store with data shouldn\'t modify runtime.');
377
 
378
		Configure::restore('store_test', 'default');
379
		$this->assertEquals('one', Configure::read('store_test'));
380
		$this->assertNull(Configure::read('testing'), 'Values that were not stored are not restored.');
381
 
382
		Cache::delete('store_test', 'default');
383
	}
384
 
385
/**
386
 * testVersion method
387
 *
388
 * @return void
389
 */
390
	public function testVersion() {
391
		$result = Configure::version();
392
		$this->assertTrue(version_compare($result, '1.2', '>='));
393
	}
394
 
395
/**
396
 * test adding new readers.
397
 *
398
 * @return void
399
 */
400
	public function testReaderSetup() {
401
		$reader = new PhpReader();
402
		Configure::config('test', $reader);
403
		$configured = Configure::configured();
404
 
405
		$this->assertTrue(in_array('test', $configured));
406
 
407
		$this->assertTrue(Configure::configured('test'));
408
		$this->assertFalse(Configure::configured('fake_garbage'));
409
 
410
		$this->assertTrue(Configure::drop('test'));
411
		$this->assertFalse(Configure::drop('test'), 'dropping things that do not exist should return false.');
412
	}
413
 
414
/**
415
 * test reader() throwing exceptions on missing interface.
416
 *
417
 * @expectedException PHPUnit_Framework_Error
418
 * @return void
419
 */
420
	public function testReaderExceptionOnIncorrectClass() {
421
		$reader = new StdClass();
422
		Configure::config('test', $reader);
423
	}
424
 
425
/**
426
 * Test that clear wipes all values.
427
 *
428
 * @return void
429
 */
430
	public function testClear() {
431
		Configure::write('test', 'value');
432
		$this->assertTrue(Configure::clear());
433
		$this->assertNull(Configure::read('debug'));
434
		$this->assertNull(Configure::read('test'));
435
	}
436
 
437
/**
438
 * @expectedException ConfigureException
439
 */
440
	public function testDumpNoAdapter() {
441
		Configure::dump(TMP . 'test.php', 'does_not_exist');
442
	}
443
 
444
/**
445
 * test dump integrated with the PhpReader.
446
 *
447
 * @return void
448
 */
449
	public function testDump() {
450
		Configure::config('test_reader', new PhpReader(TMP));
451
 
452
		$result = Configure::dump('config_test.php', 'test_reader');
453
		$this->assertTrue($result > 0);
454
		$result = file_get_contents(TMP . 'config_test.php');
455
		$this->assertContains('<?php', $result);
456
		$this->assertContains('$config = ', $result);
457
		if (file_exists(TMP . 'config_test.php')) {
458
			unlink(TMP . 'config_test.php');
459
		}
460
	}
461
 
462
/**
463
 * Test dumping only some of the data.
464
 *
465
 * @return void
466
 */
467
	public function testDumpPartial() {
468
		Configure::config('test_reader', new PhpReader(TMP));
469
 
470
		$result = Configure::dump('config_test.php', 'test_reader', array('Error'));
471
		$this->assertTrue($result > 0);
472
		$result = file_get_contents(TMP . 'config_test.php');
473
		$this->assertContains('<?php', $result);
474
		$this->assertContains('$config = ', $result);
475
		$this->assertContains('Error', $result);
476
		$this->assertNotContains('debug', $result);
477
 
478
		if (file_exists(TMP . 'config_test.php')) {
479
			unlink(TMP . 'config_test.php');
480
		}
481
	}
482
 
483
}