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