Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
13532 anikendra 1
<?php
2
/**
3
 * CacheTest file
4
 *
5
 * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
6
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
7
 *
8
 * Licensed under The MIT License
9
 * For full copyright and license information, please see the LICENSE.txt
10
 * Redistributions of files must retain the above copyright notice
11
 *
12
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
13
 * @link          http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
14
 * @package       Cake.Test.Case.Cache
15
 * @since         CakePHP(tm) v 1.2.0.5432
16
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
17
 */
18
 
19
App::uses('Cache', 'Cache');
20
 
21
/**
22
 * CacheTest class
23
 *
24
 * @package       Cake.Test.Case.Cache
25
 */
26
class CacheTest extends CakeTestCase {
27
 
28
/**
29
 * setUp method
30
 *
31
 * @return void
32
 */
33
	public function setUp() {
34
		parent::setUp();
35
		$this->_cacheDisable = Configure::read('Cache.disable');
36
		Configure::write('Cache.disable', false);
37
 
38
		$this->_defaultCacheConfig = Cache::config('default');
39
		Cache::config('default', array('engine' => 'File', 'path' => TMP . 'tests'));
40
	}
41
 
42
/**
43
 * tearDown method
44
 *
45
 * @return void
46
 */
47
	public function tearDown() {
48
		parent::tearDown();
49
		Cache::drop('latest');
50
		Cache::drop('page');
51
		Cache::drop('archive');
52
		Configure::write('Cache.disable', $this->_cacheDisable);
53
		Cache::config('default', $this->_defaultCacheConfig['settings']);
54
	}
55
 
56
/**
57
 * testConfig method
58
 *
59
 * @return void
60
 */
61
	public function testConfig() {
62
		$settings = array('engine' => 'File', 'path' => TMP . 'tests', 'prefix' => 'cake_test_');
63
		$results = Cache::config('new', $settings);
64
		$this->assertEquals(Cache::config('new'), $results);
65
		$this->assertTrue(isset($results['engine']));
66
		$this->assertTrue(isset($results['settings']));
67
	}
68
 
69
/**
70
 * testConfigInvalidEngine method
71
 *
72
 * @expectedException CacheException
73
 * @return void
74
 */
75
	public function testConfigInvalidEngine() {
76
		$settings = array('engine' => 'Imaginary');
77
		Cache::config('imaginary', $settings);
78
	}
79
 
80
/**
81
 * Check that no fatal errors are issued doing normal things when Cache.disable is true.
82
 *
83
 * @return void
84
 */
85
	public function testNonFatalErrorsWithCachedisable() {
86
		Configure::write('Cache.disable', true);
87
		Cache::config('test', array('engine' => 'File', 'path' => TMP, 'prefix' => 'error_test_'));
88
 
89
		Cache::write('no_save', 'Noooo!', 'test');
90
		Cache::read('no_save', 'test');
91
		Cache::delete('no_save', 'test');
92
		Cache::set('duration', '+10 minutes');
93
 
94
		Configure::write('Cache.disable', false);
95
	}
96
 
97
/**
98
 * test configuring CacheEngines in App/libs
99
 *
100
 * @return void
101
 */
102
	public function testConfigWithLibAndPluginEngines() {
103
		App::build(array(
104
			'Lib' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Lib' . DS),
105
			'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
106
		), App::RESET);
107
		CakePlugin::load('TestPlugin');
108
 
109
		$settings = array('engine' => 'TestAppCache', 'path' => TMP, 'prefix' => 'cake_test_');
110
		$result = Cache::config('libEngine', $settings);
111
		$this->assertEquals(Cache::config('libEngine'), $result);
112
 
113
		$settings = array('engine' => 'TestPlugin.TestPluginCache', 'path' => TMP, 'prefix' => 'cake_test_');
114
		$result = Cache::config('pluginLibEngine', $settings);
115
		$this->assertEquals(Cache::config('pluginLibEngine'), $result);
116
 
117
		Cache::drop('libEngine');
118
		Cache::drop('pluginLibEngine');
119
 
120
		App::build();
121
		CakePlugin::unload();
122
	}
123
 
124
/**
125
 * testInvalidConfig method
126
 *
127
 * Test that the cache class doesn't cause fatal errors with a partial path
128
 *
129
 * @expectedException PHPUnit_Framework_Error_Warning
130
 * @return void
131
 */
132
	public function testInvalidConfig() {
133
		// In debug mode it would auto create the folder.
134
		$debug = Configure::read('debug');
135
		Configure::write('debug', 0);
136
 
137
		Cache::config('invalid', array(
138
			'engine' => 'File',
139
			'duration' => '+1 year',
140
			'prefix' => 'testing_invalid_',
141
			'path' => 'data/',
142
			'serialize' => true,
143
			'random' => 'wii'
144
		));
145
		Cache::read('Test', 'invalid');
146
 
147
		Configure::write('debug', $debug);
148
	}
149
 
150
/**
151
 * Test reading from a config that is undefined.
152
 *
153
 * @return void
154
 */
155
	public function testReadNonExistingConfig() {
156
		$this->assertFalse(Cache::read('key', 'totally fake'));
157
		$this->assertFalse(Cache::write('key', 'value', 'totally fake'));
158
		$this->assertFalse(Cache::increment('key', 1, 'totally fake'));
159
		$this->assertFalse(Cache::decrement('key', 1, 'totally fake'));
160
	}
161
 
162
/**
163
 * test that trying to configure classes that don't extend CacheEngine fail.
164
 *
165
 * @expectedException CacheException
166
 * @return void
167
 */
168
	public function testAttemptingToConfigureANonCacheEngineClass() {
169
		$this->getMock('StdClass', array(), array(), 'RubbishEngine');
170
		Cache::config('Garbage', array(
171
			'engine' => 'Rubbish'
172
		));
173
	}
174
 
175
/**
176
 * testConfigChange method
177
 *
178
 * @return void
179
 */
180
	public function testConfigChange() {
181
		$_cacheConfigSessions = Cache::config('sessions');
182
		$_cacheConfigTests = Cache::config('tests');
183
 
184
		$result = Cache::config('sessions', array('engine' => 'File', 'path' => TMP . 'sessions'));
185
		$this->assertEquals(Cache::settings('sessions'), $result['settings']);
186
 
187
		$result = Cache::config('tests', array('engine' => 'File', 'path' => TMP . 'tests'));
188
		$this->assertEquals(Cache::settings('tests'), $result['settings']);
189
 
190
		Cache::config('sessions', $_cacheConfigSessions['settings']);
191
		Cache::config('tests', $_cacheConfigTests['settings']);
192
	}
193
 
194
/**
195
 * test that calling config() sets the 'default' configuration up.
196
 *
197
 * @return void
198
 */
199
	public function testConfigSettingDefaultConfigKey() {
200
		Cache::config('test_name', array('engine' => 'File', 'prefix' => 'test_name_'));
201
 
202
		Cache::write('value_one', 'I am cached', 'test_name');
203
		$result = Cache::read('value_one', 'test_name');
204
		$this->assertEquals('I am cached', $result);
205
 
206
		$result = Cache::read('value_one');
207
		$this->assertEquals(null, $result);
208
 
209
		Cache::write('value_one', 'I am in default config!');
210
		$result = Cache::read('value_one');
211
		$this->assertEquals('I am in default config!', $result);
212
 
213
		$result = Cache::read('value_one', 'test_name');
214
		$this->assertEquals('I am cached', $result);
215
 
216
		Cache::delete('value_one', 'test_name');
217
		Cache::delete('value_one', 'default');
218
	}
219
 
220
/**
221
 * testWritingWithConfig method
222
 *
223
 * @return void
224
 */
225
	public function testWritingWithConfig() {
226
		$_cacheConfigSessions = Cache::config('sessions');
227
 
228
		Cache::write('test_something', 'this is the test data', 'tests');
229
 
230
		$expected = array(
231
			'path' => TMP . 'sessions' . DS,
232
			'prefix' => 'cake_',
233
			'lock' => true,
234
			'serialize' => true,
235
			'duration' => 3600,
236
			'probability' => 100,
237
			'engine' => 'File',
238
			'isWindows' => DIRECTORY_SEPARATOR === '\\',
239
			'mask' => 0664,
240
			'groups' => array()
241
		);
242
		$this->assertEquals($expected, Cache::settings('sessions'));
243
 
244
		Cache::config('sessions', $_cacheConfigSessions['settings']);
245
	}
246
 
247
/**
248
 * testGroupConfigs method
249
 */
250
	public function testGroupConfigs() {
251
		Cache::config('latest', array(
252
			'duration' => 300,
253
			'engine' => 'File',
254
			'groups' => array(
255
				'posts', 'comments',
256
			),
257
		));
258
 
259
		$expected = array(
260
			'posts' => array('latest'),
261
			'comments' => array('latest'),
262
		);
263
		$result = Cache::groupConfigs();
264
		$this->assertEquals($expected, $result);
265
 
266
		$result = Cache::groupConfigs('posts');
267
		$this->assertEquals(array('posts' => array('latest')), $result);
268
 
269
		Cache::config('page', array(
270
			'duration' => 86400,
271
			'engine' => 'File',
272
			'groups' => array(
273
				'posts', 'archive'
274
			),
275
		));
276
 
277
		$result = Cache::groupConfigs();
278
		$expected = array(
279
			'posts' => array('latest', 'page'),
280
			'comments' => array('latest'),
281
			'archive' => array('page'),
282
		);
283
		$this->assertEquals($expected, $result);
284
 
285
		$result = Cache::groupConfigs('archive');
286
		$this->assertEquals(array('archive' => array('page')), $result);
287
 
288
		Cache::config('archive', array(
289
			'duration' => 86400 * 30,
290
			'engine' => 'File',
291
			'groups' => array(
292
				'posts', 'archive', 'comments',
293
			),
294
		));
295
 
296
		$result = Cache::groupConfigs('archive');
297
		$this->assertEquals(array('archive' => array('archive', 'page')), $result);
298
	}
299
 
300
/**
301
 * testGroupConfigsThrowsException method
302
 * @expectedException CacheException
303
 */
304
	public function testGroupConfigsThrowsException() {
305
		Cache::groupConfigs('bogus');
306
	}
307
 
308
/**
309
 * test that configured returns an array of the currently configured cache
310
 * settings
311
 *
312
 * @return void
313
 */
314
	public function testConfigured() {
315
		$result = Cache::configured();
316
		$this->assertTrue(in_array('_cake_core_', $result));
317
		$this->assertTrue(in_array('default', $result));
318
	}
319
 
320
/**
321
 * testInitSettings method
322
 *
323
 * @return void
324
 */
325
	public function testInitSettings() {
326
		$initial = Cache::settings();
327
		$override = array('engine' => 'File', 'path' => TMP . 'tests');
328
		Cache::config('for_test', $override);
329
 
330
		$settings = Cache::settings();
331
		$expecting = $override + $initial;
332
		$this->assertEquals($settings, $expecting);
333
	}
334
 
335
/**
336
 * test that drop removes cache configs, and that further attempts to use that config
337
 * do not work.
338
 *
339
 * @return void
340
 */
341
	public function testDrop() {
342
		App::build(array(
343
			'Lib' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Lib' . DS),
344
			'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
345
		), App::RESET);
346
 
347
		$result = Cache::drop('some_config_that_does_not_exist');
348
		$this->assertFalse($result);
349
 
350
		$_testsConfig = Cache::config('tests');
351
		$result = Cache::drop('tests');
352
		$this->assertTrue($result);
353
 
354
		Cache::config('unconfigTest', array(
355
			'engine' => 'TestAppCache'
356
		));
357
		$this->assertTrue(Cache::isInitialized('unconfigTest'));
358
 
359
		$this->assertTrue(Cache::drop('unconfigTest'));
360
		$this->assertFalse(Cache::isInitialized('TestAppCache'));
361
 
362
		Cache::config('tests', $_testsConfig);
363
		App::build();
364
	}
365
 
366
/**
367
 * testWriteEmptyValues method
368
 *
369
 * @return void
370
 */
371
	public function testWriteEmptyValues() {
372
		Cache::write('App.falseTest', false);
373
		$this->assertFalse(Cache::read('App.falseTest'));
374
 
375
		Cache::write('App.trueTest', true);
376
		$this->assertTrue(Cache::read('App.trueTest'));
377
 
378
		Cache::write('App.nullTest', null);
379
		$this->assertNull(Cache::read('App.nullTest'));
380
 
381
		Cache::write('App.zeroTest', 0);
382
		$this->assertSame(Cache::read('App.zeroTest'), 0);
383
 
384
		Cache::write('App.zeroTest2', '0');
385
		$this->assertSame(Cache::read('App.zeroTest2'), '0');
386
	}
387
 
388
/**
389
 * Test that failed writes cause errors to be triggered.
390
 *
391
 * @return void
392
 */
393
	public function testWriteTriggerError() {
394
		App::build(array(
395
			'Lib' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Lib' . DS),
396
			'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
397
		), App::RESET);
398
 
399
		Cache::config('test_trigger', array('engine' => 'TestAppCache', 'prefix' => ''));
400
		try {
401
			Cache::write('fail', 'value', 'test_trigger');
402
			$this->fail('No exception thrown');
403
		} catch (PHPUnit_Framework_Error $e) {
404
			$this->assertTrue(true);
405
		}
406
		Cache::drop('test_trigger');
407
		App::build();
408
	}
409
 
410
/**
411
 * testCacheDisable method
412
 *
413
 * Check that the "Cache.disable" configuration and a change to it
414
 * (even after a cache config has been setup) is taken into account.
415
 *
416
 * @return void
417
 */
418
	public function testCacheDisable() {
419
		Configure::write('Cache.disable', false);
420
		Cache::config('test_cache_disable_1', array('engine' => 'File', 'path' => TMP . 'tests'));
421
 
422
		$this->assertTrue(Cache::write('key_1', 'hello', 'test_cache_disable_1'));
423
		$this->assertSame(Cache::read('key_1', 'test_cache_disable_1'), 'hello');
424
 
425
		Configure::write('Cache.disable', true);
426
 
427
		$this->assertFalse(Cache::write('key_2', 'hello', 'test_cache_disable_1'));
428
		$this->assertFalse(Cache::read('key_2', 'test_cache_disable_1'));
429
 
430
		Configure::write('Cache.disable', false);
431
 
432
		$this->assertTrue(Cache::write('key_3', 'hello', 'test_cache_disable_1'));
433
		$this->assertSame(Cache::read('key_3', 'test_cache_disable_1'), 'hello');
434
 
435
		Configure::write('Cache.disable', true);
436
		Cache::config('test_cache_disable_2', array('engine' => 'File', 'path' => TMP . 'tests'));
437
 
438
		$this->assertFalse(Cache::write('key_4', 'hello', 'test_cache_disable_2'));
439
		$this->assertFalse(Cache::read('key_4', 'test_cache_disable_2'));
440
 
441
		Configure::write('Cache.disable', false);
442
 
443
		$this->assertTrue(Cache::write('key_5', 'hello', 'test_cache_disable_2'));
444
		$this->assertSame(Cache::read('key_5', 'test_cache_disable_2'), 'hello');
445
 
446
		Configure::write('Cache.disable', true);
447
 
448
		$this->assertFalse(Cache::write('key_6', 'hello', 'test_cache_disable_2'));
449
		$this->assertFalse(Cache::read('key_6', 'test_cache_disable_2'));
450
	}
451
 
452
/**
453
 * testSet method
454
 *
455
 * @return void
456
 */
457
	public function testSet() {
458
		$_cacheSet = Cache::set();
459
 
460
		Cache::set(array('duration' => '+1 year'));
461
		$data = Cache::read('test_cache');
462
		$this->assertFalse($data);
463
 
464
		$data = 'this is just a simple test of the cache system';
465
		$write = Cache::write('test_cache', $data);
466
		$this->assertTrue($write);
467
 
468
		Cache::set(array('duration' => '+1 year'));
469
		$data = Cache::read('test_cache');
470
		$this->assertEquals('this is just a simple test of the cache system', $data);
471
 
472
		Cache::delete('test_cache');
473
 
474
		Cache::settings();
475
 
476
		Cache::set($_cacheSet);
477
	}
478
 
479
/**
480
 * test set() parameter handling for user cache configs.
481
 *
482
 * @return void
483
 */
484
	public function testSetOnAlternateConfigs() {
485
		Cache::config('file_config', array('engine' => 'File', 'prefix' => 'test_file_'));
486
		Cache::set(array('duration' => '+1 year'), 'file_config');
487
		$settings = Cache::settings('file_config');
488
 
489
		$this->assertEquals('test_file_', $settings['prefix']);
490
		$this->assertEquals(strtotime('+1 year') - time(), $settings['duration']);
491
	}
492
}