7
* CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
8
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
10
* Licensed under The Open Group Test Suite License
11
* Redistributions of files must retain the above copyright notice.
13
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
14
* @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
16
* @subpackage cake.tests.cases.libs
17
* @since CakePHP(tm) v 1.2.0.5432
18
* @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
20
if (!class_exists('Cache')) {
21
require LIBS . 'cache.php';
28
* @subpackage cake.tests.cases.libs
30
class CacheTest extends CakeTestCase {
39
$this->_cacheDisable = Configure::read('Cache.disable');
40
Configure::write('Cache.disable', false);
42
$this->_defaultCacheConfig = Cache::config('default');
43
Cache::config('default', array('engine' => 'File', 'path' => TMP . 'tests'));
53
Configure::write('Cache.disable', $this->_cacheDisable);
54
Cache::config('default', $this->_defaultCacheConfig['settings']);
63
function testConfig() {
64
$settings = array('engine' => 'File', 'path' => TMP . 'tests', 'prefix' => 'cake_test_');
65
$results = Cache::config('new', $settings);
66
$this->assertEqual($results, Cache::config('new'));
67
$this->assertTrue(isset($results['engine']));
68
$this->assertTrue(isset($results['settings']));
72
* Check that no fatal errors are issued doing normal things when Cache.disable is true.
76
function testNonFatalErrorsWithCachedisable() {
77
Configure::write('Cache.disable', true);
78
Cache::config('test', array('engine' => 'File', 'path' => TMP, 'prefix' => 'error_test_'));
80
Cache::write('no_save', 'Noooo!', 'test');
81
Cache::read('no_save', 'test');
82
Cache::delete('no_save', 'test');
83
Cache::set('duration', '+10 minutes');
85
Configure::write('Cache.disable', false);
89
* test configuring CacheEngines in App/libs
93
function testConfigWithLibAndPluginEngines() {
95
'libs' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'libs' . DS),
96
'plugins' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS)
99
$settings = array('engine' => 'TestAppCache', 'path' => TMP, 'prefix' => 'cake_test_');
100
$result = Cache::config('libEngine', $settings);
101
$this->assertEqual($result, Cache::config('libEngine'));
103
$settings = array('engine' => 'TestPlugin.TestPluginCache', 'path' => TMP, 'prefix' => 'cake_test_');
104
$result = Cache::config('pluginLibEngine', $settings);
105
$this->assertEqual($result, Cache::config('pluginLibEngine'));
107
Cache::drop('libEngine');
108
Cache::drop('pluginLibEngine');
114
* testInvalidConfig method
116
* Test that the cache class doesn't cause fatal errors with a partial path
121
function testInvaidConfig() {
122
$this->expectError();
123
Cache::config('invalid', array(
125
'duration' => '+1 year',
126
'prefix' => 'testing_invalid_',
131
$read = Cache::read('Test', 'invalid');
132
$this->assertEqual($read, null);
136
* testConfigChange method
141
function testConfigChange() {
142
$_cacheConfigSessions = Cache::config('sessions');
143
$_cacheConfigTests = Cache::config('tests');
145
$result = Cache::config('sessions', array('engine'=> 'File', 'path' => TMP . 'sessions'));
146
$this->assertEqual($result['settings'], Cache::settings('sessions'));
148
$result = Cache::config('tests', array('engine'=> 'File', 'path' => TMP . 'tests'));
149
$this->assertEqual($result['settings'], Cache::settings('tests'));
151
Cache::config('sessions', $_cacheConfigSessions['settings']);
152
Cache::config('tests', $_cacheConfigTests['settings']);
156
* test that calling config() sets the 'default' configuration up.
160
function testConfigSettingDefaultConfigKey() {
161
Cache::config('test_name', array('engine' => 'File', 'prefix' => 'test_name_'));
163
Cache::config('test_name');
164
Cache::write('value_one', 'I am cached');
165
$result = Cache::read('value_one');
166
$this->assertEqual($result, 'I am cached');
168
Cache::config('default');
169
$result = Cache::read('value_one');
170
$this->assertEqual($result, null);
172
Cache::write('value_one', 'I am in default config!');
173
$result = Cache::read('value_one');
174
$this->assertEqual($result, 'I am in default config!');
176
Cache::config('test_name');
177
$result = Cache::read('value_one');
178
$this->assertEqual($result, 'I am cached');
180
Cache::delete('value_one');
181
Cache::config('default');
182
Cache::delete('value_one');
186
* testWritingWithConfig method
191
function testWritingWithConfig() {
192
$_cacheConfigSessions = Cache::config('sessions');
194
Cache::write('test_somthing', 'this is the test data', 'tests');
197
'path' => TMP . 'sessions',
202
'probability' => 100,
204
'isWindows' => DIRECTORY_SEPARATOR == '\\'
206
$this->assertEqual($expected, Cache::settings('sessions'));
208
Cache::config('sessions', $_cacheConfigSessions['settings']);
212
* test that configured returns an array of the currently configured cache
217
function testConfigured() {
218
$result = Cache::configured();
219
$this->assertTrue(in_array('_cake_core_', $result));
220
$this->assertTrue(in_array('default', $result));
224
* testInitSettings method
229
function testInitSettings() {
230
Cache::config('default', array('engine' => 'File', 'path' => TMP . 'tests'));
232
$settings = Cache::settings();
236
'probability' => 100,
237
'path'=> TMP . 'tests',
241
'isWindows' => DIRECTORY_SEPARATOR == '\\'
243
$this->assertEqual($settings, $expecting);
247
* test that drop removes cache configs, and that further attempts to use that config
252
function testDrop() {
254
'libs' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'libs' . DS),
255
'plugins' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS)
258
$result = Cache::drop('some_config_that_does_not_exist');
259
$this->assertFalse($result);
261
$_testsConfig = Cache::config('tests');
262
$result = Cache::drop('tests');
263
$this->assertTrue($result);
265
Cache::config('unconfigTest', array(
266
'engine' => 'TestAppCache'
268
$this->assertTrue(Cache::isInitialized('unconfigTest'));
270
$this->assertTrue(Cache::drop('unconfigTest'));
271
$this->assertFalse(Cache::isInitialized('TestAppCache'));
273
Cache::config('tests', $_testsConfig);
278
* testWriteEmptyValues method
283
function testWriteEmptyValues() {
284
Cache::write('App.falseTest', false);
285
$this->assertIdentical(Cache::read('App.falseTest'), false);
287
Cache::write('App.trueTest', true);
288
$this->assertIdentical(Cache::read('App.trueTest'), true);
290
Cache::write('App.nullTest', null);
291
$this->assertIdentical(Cache::read('App.nullTest'), null);
293
Cache::write('App.zeroTest', 0);
294
$this->assertIdentical(Cache::read('App.zeroTest'), 0);
296
Cache::write('App.zeroTest2', '0');
297
$this->assertIdentical(Cache::read('App.zeroTest2'), '0');
301
* testCacheDisable method
303
* Check that the "Cache.disable" configuration and a change to it
304
* (even after a cache config has been setup) is taken into account.
306
* @link https://trac.cakephp.org/ticket/6236
310
function testCacheDisable() {
311
Configure::write('Cache.disable', false);
312
Cache::config('test_cache_disable_1', array('engine'=> 'File', 'path' => TMP . 'tests'));
314
$this->assertTrue(Cache::write('key_1', 'hello'));
315
$this->assertIdentical(Cache::read('key_1'), 'hello');
317
Configure::write('Cache.disable', true);
319
$this->assertFalse(Cache::write('key_2', 'hello'));
320
$this->assertFalse(Cache::read('key_2'));
322
Configure::write('Cache.disable', false);
324
$this->assertTrue(Cache::write('key_3', 'hello'));
325
$this->assertIdentical(Cache::read('key_3'), 'hello');
327
Configure::write('Cache.disable', true);
328
Cache::config('test_cache_disable_2', array('engine'=> 'File', 'path' => TMP . 'tests'));
330
$this->assertFalse(Cache::write('key_4', 'hello'));
331
$this->assertFalse(Cache::read('key_4'));
333
Configure::write('Cache.disable', false);
335
$this->assertTrue(Cache::write('key_5', 'hello'));
336
$this->assertIdentical(Cache::read('key_5'), 'hello');
338
Configure::write('Cache.disable', true);
340
$this->assertFalse(Cache::write('key_6', 'hello'));
341
$this->assertFalse(Cache::read('key_6'));
351
$_cacheSet = Cache::set();
353
Cache::set(array('duration' => '+1 year'));
354
$data = Cache::read('test_cache');
355
$this->assertFalse($data);
357
$data = 'this is just a simple test of the cache system';
358
$write = Cache::write('test_cache', $data);
359
$this->assertTrue($write);
361
Cache::set(array('duration' => '+1 year'));
362
$data = Cache::read('test_cache');
363
$this->assertEqual($data, 'this is just a simple test of the cache system');
365
Cache::delete('test_cache');
367
$global = Cache::settings();
369
Cache::set($_cacheSet);