comparison cake/tests/cases/libs/cache.test.php @ 0:261e66bd5a0c

hg init
author Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
date Sun, 24 Jul 2011 21:08:31 +0900
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:261e66bd5a0c
1 <?php
2 /**
3 * CacheTest file
4 *
5 * PHP versions 4 and 5
6 *
7 * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
8 * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
9 *
10 * Licensed under The Open Group Test Suite License
11 * Redistributions of files must retain the above copyright notice.
12 *
13 * @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
14 * @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
15 * @package cake
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
19 */
20 if (!class_exists('Cache')) {
21 require LIBS . 'cache.php';
22 }
23
24 /**
25 * CacheTest class
26 *
27 * @package cake
28 * @subpackage cake.tests.cases.libs
29 */
30 class CacheTest extends CakeTestCase {
31
32 /**
33 * setUp method
34 *
35 * @access public
36 * @return void
37 */
38 function setUp() {
39 $this->_cacheDisable = Configure::read('Cache.disable');
40 Configure::write('Cache.disable', false);
41
42 $this->_defaultCacheConfig = Cache::config('default');
43 Cache::config('default', array('engine' => 'File', 'path' => TMP . 'tests'));
44 }
45
46 /**
47 * tearDown method
48 *
49 * @access public
50 * @return void
51 */
52 function tearDown() {
53 Configure::write('Cache.disable', $this->_cacheDisable);
54 Cache::config('default', $this->_defaultCacheConfig['settings']);
55 }
56
57 /**
58 * testConfig method
59 *
60 * @access public
61 * @return void
62 */
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']));
69 }
70
71 /**
72 * Check that no fatal errors are issued doing normal things when Cache.disable is true.
73 *
74 * @return void
75 */
76 function testNonFatalErrorsWithCachedisable() {
77 Configure::write('Cache.disable', true);
78 Cache::config('test', array('engine' => 'File', 'path' => TMP, 'prefix' => 'error_test_'));
79
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');
84
85 Configure::write('Cache.disable', false);
86 }
87
88 /**
89 * test configuring CacheEngines in App/libs
90 *
91 * @return void
92 */
93 function testConfigWithLibAndPluginEngines() {
94 App::build(array(
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)
97 ), true);
98
99 $settings = array('engine' => 'TestAppCache', 'path' => TMP, 'prefix' => 'cake_test_');
100 $result = Cache::config('libEngine', $settings);
101 $this->assertEqual($result, Cache::config('libEngine'));
102
103 $settings = array('engine' => 'TestPlugin.TestPluginCache', 'path' => TMP, 'prefix' => 'cake_test_');
104 $result = Cache::config('pluginLibEngine', $settings);
105 $this->assertEqual($result, Cache::config('pluginLibEngine'));
106
107 Cache::drop('libEngine');
108 Cache::drop('pluginLibEngine');
109
110 App::build();
111 }
112
113 /**
114 * testInvalidConfig method
115 *
116 * Test that the cache class doesn't cause fatal errors with a partial path
117 *
118 * @access public
119 * @return void
120 */
121 function testInvaidConfig() {
122 $this->expectError();
123 Cache::config('invalid', array(
124 'engine' => 'File',
125 'duration' => '+1 year',
126 'prefix' => 'testing_invalid_',
127 'path' => 'data/',
128 'serialize' => true,
129 'random' => 'wii'
130 ));
131 $read = Cache::read('Test', 'invalid');
132 $this->assertEqual($read, null);
133 }
134
135 /**
136 * testConfigChange method
137 *
138 * @access public
139 * @return void
140 */
141 function testConfigChange() {
142 $_cacheConfigSessions = Cache::config('sessions');
143 $_cacheConfigTests = Cache::config('tests');
144
145 $result = Cache::config('sessions', array('engine'=> 'File', 'path' => TMP . 'sessions'));
146 $this->assertEqual($result['settings'], Cache::settings('sessions'));
147
148 $result = Cache::config('tests', array('engine'=> 'File', 'path' => TMP . 'tests'));
149 $this->assertEqual($result['settings'], Cache::settings('tests'));
150
151 Cache::config('sessions', $_cacheConfigSessions['settings']);
152 Cache::config('tests', $_cacheConfigTests['settings']);
153 }
154
155 /**
156 * test that calling config() sets the 'default' configuration up.
157 *
158 * @return void
159 */
160 function testConfigSettingDefaultConfigKey() {
161 Cache::config('test_name', array('engine' => 'File', 'prefix' => 'test_name_'));
162
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');
167
168 Cache::config('default');
169 $result = Cache::read('value_one');
170 $this->assertEqual($result, null);
171
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!');
175
176 Cache::config('test_name');
177 $result = Cache::read('value_one');
178 $this->assertEqual($result, 'I am cached');
179
180 Cache::delete('value_one');
181 Cache::config('default');
182 Cache::delete('value_one');
183 }
184
185 /**
186 * testWritingWithConfig method
187 *
188 * @access public
189 * @return void
190 */
191 function testWritingWithConfig() {
192 $_cacheConfigSessions = Cache::config('sessions');
193
194 Cache::write('test_somthing', 'this is the test data', 'tests');
195
196 $expected = array(
197 'path' => TMP . 'sessions',
198 'prefix' => 'cake_',
199 'lock' => false,
200 'serialize' => true,
201 'duration' => 3600,
202 'probability' => 100,
203 'engine' => 'File',
204 'isWindows' => DIRECTORY_SEPARATOR == '\\'
205 );
206 $this->assertEqual($expected, Cache::settings('sessions'));
207
208 Cache::config('sessions', $_cacheConfigSessions['settings']);
209 }
210
211 /**
212 * test that configured returns an array of the currently configured cache
213 * settings
214 *
215 * @return void
216 */
217 function testConfigured() {
218 $result = Cache::configured();
219 $this->assertTrue(in_array('_cake_core_', $result));
220 $this->assertTrue(in_array('default', $result));
221 }
222
223 /**
224 * testInitSettings method
225 *
226 * @access public
227 * @return void
228 */
229 function testInitSettings() {
230 Cache::config('for_test', array('engine' => 'File', 'path' => TMP . 'tests'));
231
232 $settings = Cache::settings();
233 $expecting = array(
234 'engine' => 'File',
235 'duration'=> 3600,
236 'probability' => 100,
237 'path'=> TMP . 'tests',
238 'prefix'=> 'cake_',
239 'lock' => false,
240 'serialize'=> true,
241 'isWindows' => DIRECTORY_SEPARATOR == '\\'
242 );
243 $this->assertEqual($settings, $expecting);
244 }
245
246 /**
247 * test that drop removes cache configs, and that further attempts to use that config
248 * do not work.
249 *
250 * @return void
251 */
252 function testDrop() {
253 App::build(array(
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)
256 ), true);
257
258 $result = Cache::drop('some_config_that_does_not_exist');
259 $this->assertFalse($result);
260
261 $_testsConfig = Cache::config('tests');
262 $result = Cache::drop('tests');
263 $this->assertTrue($result);
264
265 Cache::config('unconfigTest', array(
266 'engine' => 'TestAppCache'
267 ));
268 $this->assertTrue(Cache::isInitialized('unconfigTest'));
269
270 $this->assertTrue(Cache::drop('unconfigTest'));
271 $this->assertFalse(Cache::isInitialized('TestAppCache'));
272
273 Cache::config('tests', $_testsConfig);
274 App::build();
275 }
276
277 /**
278 * testWriteEmptyValues method
279 *
280 * @access public
281 * @return void
282 */
283 function testWriteEmptyValues() {
284 Cache::write('App.falseTest', false);
285 $this->assertIdentical(Cache::read('App.falseTest'), false);
286
287 Cache::write('App.trueTest', true);
288 $this->assertIdentical(Cache::read('App.trueTest'), true);
289
290 Cache::write('App.nullTest', null);
291 $this->assertIdentical(Cache::read('App.nullTest'), null);
292
293 Cache::write('App.zeroTest', 0);
294 $this->assertIdentical(Cache::read('App.zeroTest'), 0);
295
296 Cache::write('App.zeroTest2', '0');
297 $this->assertIdentical(Cache::read('App.zeroTest2'), '0');
298 }
299
300 /**
301 * testCacheDisable method
302 *
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.
305 *
306 * @link https://trac.cakephp.org/ticket/6236
307 * @access public
308 * @return void
309 */
310 function testCacheDisable() {
311 Configure::write('Cache.disable', false);
312 Cache::config('test_cache_disable_1', array('engine'=> 'File', 'path' => TMP . 'tests'));
313
314 $this->assertTrue(Cache::write('key_1', 'hello'));
315 $this->assertIdentical(Cache::read('key_1'), 'hello');
316
317 Configure::write('Cache.disable', true);
318
319 $this->assertFalse(Cache::write('key_2', 'hello'));
320 $this->assertFalse(Cache::read('key_2'));
321
322 Configure::write('Cache.disable', false);
323
324 $this->assertTrue(Cache::write('key_3', 'hello'));
325 $this->assertIdentical(Cache::read('key_3'), 'hello');
326
327 Configure::write('Cache.disable', true);
328 Cache::config('test_cache_disable_2', array('engine'=> 'File', 'path' => TMP . 'tests'));
329
330 $this->assertFalse(Cache::write('key_4', 'hello'));
331 $this->assertFalse(Cache::read('key_4'));
332
333 Configure::write('Cache.disable', false);
334
335 $this->assertTrue(Cache::write('key_5', 'hello'));
336 $this->assertIdentical(Cache::read('key_5'), 'hello');
337
338 Configure::write('Cache.disable', true);
339
340 $this->assertFalse(Cache::write('key_6', 'hello'));
341 $this->assertFalse(Cache::read('key_6'));
342 }
343
344 /**
345 * testSet method
346 *
347 * @access public
348 * @return void
349 */
350 function testSet() {
351 $_cacheSet = Cache::set();
352
353 Cache::set(array('duration' => '+1 year'));
354 $data = Cache::read('test_cache');
355 $this->assertFalse($data);
356
357 $data = 'this is just a simple test of the cache system';
358 $write = Cache::write('test_cache', $data);
359 $this->assertTrue($write);
360
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');
364
365 Cache::delete('test_cache');
366
367 $global = Cache::settings();
368
369 Cache::set($_cacheSet);
370 }
371
372 }