7
* CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
8
* Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
10
* Licensed under The MIT License
11
* Redistributions of files must retain the above copyright notice
13
* @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
14
* @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
15
* @package Cake.Test.Case.Cache
16
* @since CakePHP(tm) v 1.2.0.5432
17
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
20
App::uses('Cache', 'Cache');
25
* @package Cake.Test.Case.Cache
27
class CacheTest extends CakeTestCase {
34
public function setUp() {
36
$this->_cacheDisable = Configure::read('Cache.disable');
37
Configure::write('Cache.disable', false);
39
$this->_defaultCacheConfig = Cache::config('default');
40
Cache::config('default', array('engine' => 'File', 'path' => TMP . 'tests'));
48
public function tearDown() {
50
Configure::write('Cache.disable', $this->_cacheDisable);
51
Cache::config('default', $this->_defaultCacheConfig['settings']);
59
public function testConfig() {
60
$settings = array('engine' => 'File', 'path' => TMP . 'tests', 'prefix' => 'cake_test_');
61
$results = Cache::config('new', $settings);
62
$this->assertEquals(Cache::config('new'), $results);
63
$this->assertTrue(isset($results['engine']));
64
$this->assertTrue(isset($results['settings']));
68
* Check that no fatal errors are issued doing normal things when Cache.disable is true.
72
public function testNonFatalErrorsWithCachedisable() {
73
Configure::write('Cache.disable', true);
74
Cache::config('test', array('engine' => 'File', 'path' => TMP, 'prefix' => 'error_test_'));
76
Cache::write('no_save', 'Noooo!', 'test');
77
Cache::read('no_save', 'test');
78
Cache::delete('no_save', 'test');
79
Cache::set('duration', '+10 minutes');
81
Configure::write('Cache.disable', false);
85
* test configuring CacheEngines in App/libs
89
public function testConfigWithLibAndPluginEngines() {
91
'Lib' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Lib' . DS),
92
'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
94
CakePlugin::load('TestPlugin');
96
$settings = array('engine' => 'TestAppCache', 'path' => TMP, 'prefix' => 'cake_test_');
97
$result = Cache::config('libEngine', $settings);
98
$this->assertEquals(Cache::config('libEngine'), $result);
100
$settings = array('engine' => 'TestPlugin.TestPluginCache', 'path' => TMP, 'prefix' => 'cake_test_');
101
$result = Cache::config('pluginLibEngine', $settings);
102
$this->assertEquals(Cache::config('pluginLibEngine'), $result);
104
Cache::drop('libEngine');
105
Cache::drop('pluginLibEngine');
108
CakePlugin::unload();
112
* testInvalidConfig method
114
* Test that the cache class doesn't cause fatal errors with a partial path
116
* @expectedException PHPUnit_Framework_Error_Warning
119
public function testInvalidConfig() {
120
Cache::config('invalid', array(
122
'duration' => '+1 year',
123
'prefix' => 'testing_invalid_',
128
$read = Cache::read('Test', 'invalid');
132
* Test reading from a config that is undefined.
136
public function testReadNonExistingConfig() {
137
$this->assertFalse(Cache::read('key', 'totally fake'));
138
$this->assertFalse(Cache::write('key', 'value', 'totally fake'));
139
$this->assertFalse(Cache::increment('key', 1, 'totally fake'));
140
$this->assertFalse(Cache::decrement('key', 1, 'totally fake'));
144
* test that trying to configure classes that don't extend CacheEngine fail.
146
* @expectedException CacheException
149
public function testAttemptingToConfigureANonCacheEngineClass() {
150
$this->getMock('StdClass', array(), array(), 'RubbishEngine');
151
Cache::config('Garbage', array(
152
'engine' => 'Rubbish'
157
* testConfigChange method
161
public function testConfigChange() {
162
$_cacheConfigSessions = Cache::config('sessions');
163
$_cacheConfigTests = Cache::config('tests');
165
$result = Cache::config('sessions', array('engine' => 'File', 'path' => TMP . 'sessions'));
166
$this->assertEquals(Cache::settings('sessions'), $result['settings']);
168
$result = Cache::config('tests', array('engine' => 'File', 'path' => TMP . 'tests'));
169
$this->assertEquals(Cache::settings('tests'), $result['settings']);
171
Cache::config('sessions', $_cacheConfigSessions['settings']);
172
Cache::config('tests', $_cacheConfigTests['settings']);
176
* test that calling config() sets the 'default' configuration up.
180
public function testConfigSettingDefaultConfigKey() {
181
Cache::config('test_name', array('engine' => 'File', 'prefix' => 'test_name_'));
183
Cache::write('value_one', 'I am cached', 'test_name');
184
$result = Cache::read('value_one', 'test_name');
185
$this->assertEquals('I am cached', $result);
187
$result = Cache::read('value_one');
188
$this->assertEquals(null, $result);
190
Cache::write('value_one', 'I am in default config!');
191
$result = Cache::read('value_one');
192
$this->assertEquals('I am in default config!', $result);
194
$result = Cache::read('value_one', 'test_name');
195
$this->assertEquals('I am cached', $result);
197
Cache::delete('value_one', 'test_name');
198
Cache::delete('value_one', 'default');
202
* testWritingWithConfig method
206
public function testWritingWithConfig() {
207
$_cacheConfigSessions = Cache::config('sessions');
209
Cache::write('test_something', 'this is the test data', 'tests');
212
'path' => TMP . 'sessions' . DS,
217
'probability' => 100,
219
'isWindows' => DIRECTORY_SEPARATOR == '\\',
223
$this->assertEquals($expected, Cache::settings('sessions'));
225
Cache::config('sessions', $_cacheConfigSessions['settings']);
229
* test that configured returns an array of the currently configured cache
234
public function testConfigured() {
235
$result = Cache::configured();
236
$this->assertTrue(in_array('_cake_core_', $result));
237
$this->assertTrue(in_array('default', $result));
241
* testInitSettings method
245
public function testInitSettings() {
246
$initial = Cache::settings();
247
$override = array('engine' => 'File', 'path' => TMP . 'tests');
248
Cache::config('for_test', $override);
250
$settings = Cache::settings();
251
$expecting = $override + $initial;
252
$this->assertEquals($settings, $expecting);
256
* test that drop removes cache configs, and that further attempts to use that config
261
public function testDrop() {
263
'Lib' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Lib' . DS),
264
'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
267
$result = Cache::drop('some_config_that_does_not_exist');
268
$this->assertFalse($result);
270
$_testsConfig = Cache::config('tests');
271
$result = Cache::drop('tests');
272
$this->assertTrue($result);
274
Cache::config('unconfigTest', array(
275
'engine' => 'TestAppCache'
277
$this->assertTrue(Cache::isInitialized('unconfigTest'));
279
$this->assertTrue(Cache::drop('unconfigTest'));
280
$this->assertFalse(Cache::isInitialized('TestAppCache'));
282
Cache::config('tests', $_testsConfig);
287
* testWriteEmptyValues method
291
public function testWriteEmptyValues() {
292
Cache::write('App.falseTest', false);
293
$this->assertSame(Cache::read('App.falseTest'), false);
295
Cache::write('App.trueTest', true);
296
$this->assertSame(Cache::read('App.trueTest'), true);
298
Cache::write('App.nullTest', null);
299
$this->assertSame(Cache::read('App.nullTest'), null);
301
Cache::write('App.zeroTest', 0);
302
$this->assertSame(Cache::read('App.zeroTest'), 0);
304
Cache::write('App.zeroTest2', '0');
305
$this->assertSame(Cache::read('App.zeroTest2'), '0');
309
* Test that failed writes cause errors to be triggered.
313
public function testWriteTriggerError() {
315
'Lib' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Lib' . DS),
316
'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
319
Cache::config('test_trigger', array('engine' => 'TestAppCache', 'prefix' => ''));
321
Cache::write('fail', 'value', 'test_trigger');
322
$this->fail('No exception thrown');
323
} catch (PHPUnit_Framework_Error $e) {
324
$this->assertTrue(true);
326
Cache::drop('test_trigger');
331
* testCacheDisable method
333
* Check that the "Cache.disable" configuration and a change to it
334
* (even after a cache config has been setup) is taken into account.
338
public function testCacheDisable() {
339
Configure::write('Cache.disable', false);
340
Cache::config('test_cache_disable_1', array('engine' => 'File', 'path' => TMP . 'tests'));
342
$this->assertTrue(Cache::write('key_1', 'hello', 'test_cache_disable_1'));
343
$this->assertSame(Cache::read('key_1', 'test_cache_disable_1'), 'hello');
345
Configure::write('Cache.disable', true);
347
$this->assertFalse(Cache::write('key_2', 'hello', 'test_cache_disable_1'));
348
$this->assertFalse(Cache::read('key_2', 'test_cache_disable_1'));
350
Configure::write('Cache.disable', false);
352
$this->assertTrue(Cache::write('key_3', 'hello', 'test_cache_disable_1'));
353
$this->assertSame(Cache::read('key_3', 'test_cache_disable_1'), 'hello');
355
Configure::write('Cache.disable', true);
356
Cache::config('test_cache_disable_2', array('engine' => 'File', 'path' => TMP . 'tests'));
358
$this->assertFalse(Cache::write('key_4', 'hello', 'test_cache_disable_2'));
359
$this->assertFalse(Cache::read('key_4', 'test_cache_disable_2'));
361
Configure::write('Cache.disable', false);
363
$this->assertTrue(Cache::write('key_5', 'hello', 'test_cache_disable_2'));
364
$this->assertSame(Cache::read('key_5', 'test_cache_disable_2'), 'hello');
366
Configure::write('Cache.disable', true);
368
$this->assertFalse(Cache::write('key_6', 'hello', 'test_cache_disable_2'));
369
$this->assertFalse(Cache::read('key_6', 'test_cache_disable_2'));
377
public function testSet() {
378
$_cacheSet = Cache::set();
380
Cache::set(array('duration' => '+1 year'));
381
$data = Cache::read('test_cache');
382
$this->assertFalse($data);
384
$data = 'this is just a simple test of the cache system';
385
$write = Cache::write('test_cache', $data);
386
$this->assertTrue($write);
388
Cache::set(array('duration' => '+1 year'));
389
$data = Cache::read('test_cache');
390
$this->assertEquals('this is just a simple test of the cache system', $data);
392
Cache::delete('test_cache');
394
$global = Cache::settings();
396
Cache::set($_cacheSet);
400
* test set() parameter handling for user cache configs.
404
public function testSetOnAlternateConfigs() {
405
Cache::config('file_config', array('engine' => 'File', 'prefix' => 'test_file_'));
406
Cache::set(array('duration' => '+1 year'), 'file_config');
407
$settings = Cache::settings('file_config');
409
$this->assertEquals('test_file_', $settings['prefix']);
410
$this->assertEquals(strtotime('+1 year') - time(), $settings['duration']);