comparison cake/tests/cases/libs/cache/file.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 * FileEngineTest 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.cache
17 * @since CakePHP(tm) v 1.2.0.5434
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 * FileEngineTest class
26 *
27 * @package cake
28 * @subpackage cake.tests.cases.libs.cache
29 */
30 class FileEngineTest extends CakeTestCase {
31
32 /**
33 * config property
34 *
35 * @var array
36 * @access public
37 */
38 var $config = array();
39
40 /**
41 * startCase method
42 *
43 * @access public
44 * @return void
45 */
46 function startCase() {
47 $this->_cacheDisable = Configure::read('Cache.disable');
48 $this->_cacheConfig = Cache::config('default');
49 Configure::write('Cache.disable', false);
50 Cache::config('default', array('engine' => 'File', 'path' => CACHE));
51 }
52
53 /**
54 * endCase method
55 *
56 * @access public
57 * @return void
58 */
59 function endCase() {
60 Configure::write('Cache.disable', $this->_cacheDisable);
61 Cache::config('default', $this->_cacheConfig['settings']);
62 }
63
64 /**
65 * testCacheDirChange method
66 *
67 * @access public
68 * @return void
69 */
70 function testCacheDirChange() {
71 $result = Cache::config('sessions', array('engine'=> 'File', 'path' => TMP . 'sessions'));
72 $this->assertEqual($result['settings'], Cache::settings('sessions'));
73
74 $result = Cache::config('sessions', array('engine'=> 'File', 'path' => TMP . 'tests'));
75 $this->assertEqual($result['settings'], Cache::settings('sessions'));
76 $this->assertNotEqual($result['settings'], Cache::settings('default'));
77 }
78
79 /**
80 * testReadAndWriteCache method
81 *
82 * @access public
83 * @return void
84 */
85 function testReadAndWriteCache() {
86 Cache::config('default');
87
88 $result = Cache::write(null, 'here');
89 $this->assertFalse($result);
90
91 Cache::set(array('duration' => 1));
92
93 $result = Cache::read('test');
94 $expecting = '';
95 $this->assertEqual($result, $expecting);
96
97 $data = 'this is a test of the emergency broadcasting system';
98 $result = Cache::write('test', $data);
99 $this->assertTrue(file_exists(CACHE . 'cake_test'));
100
101 $result = Cache::read('test');
102 $expecting = $data;
103 $this->assertEqual($result, $expecting);
104
105 Cache::delete('test');
106 }
107
108 /**
109 * testExpiry method
110 *
111 * @access public
112 * @return void
113 */
114 function testExpiry() {
115 Cache::set(array('duration' => 1));
116
117 $result = Cache::read('test');
118 $this->assertFalse($result);
119
120 $data = 'this is a test of the emergency broadcasting system';
121 $result = Cache::write('other_test', $data);
122 $this->assertTrue($result);
123
124 sleep(2);
125 $result = Cache::read('other_test');
126 $this->assertFalse($result);
127
128 Cache::set(array('duration' => "+1 second"));
129
130 $data = 'this is a test of the emergency broadcasting system';
131 $result = Cache::write('other_test', $data);
132 $this->assertTrue($result);
133
134 sleep(2);
135 $result = Cache::read('other_test');
136 $this->assertFalse($result);
137 }
138
139 /**
140 * testDeleteCache method
141 *
142 * @access public
143 * @return void
144 */
145 function testDeleteCache() {
146 $data = 'this is a test of the emergency broadcasting system';
147 $result = Cache::write('delete_test', $data);
148 $this->assertTrue($result);
149
150 $result = Cache::delete('delete_test');
151 $this->assertTrue($result);
152 $this->assertFalse(file_exists(TMP . 'tests' . DS . 'delete_test'));
153
154 $result = Cache::delete('delete_test');
155 $this->assertFalse($result);
156 }
157
158 /**
159 * testSerialize method
160 *
161 * @access public
162 * @return void
163 */
164 function testSerialize() {
165 Cache::config('default', array('engine' => 'File', 'serialize' => true));
166 $data = 'this is a test of the emergency broadcasting system';
167 $write = Cache::write('serialize_test', $data);
168 $this->assertTrue($write);
169
170 Cache::config('default', array('serialize' => false));
171 $read = Cache::read('serialize_test');
172
173 $newread = Cache::read('serialize_test');
174
175 $delete = Cache::delete('serialize_test');
176
177 $this->assertIdentical($read, serialize($data));
178
179 $this->assertIdentical(unserialize($newread), $data);
180 }
181
182 /**
183 * testClear method
184 *
185 * @access public
186 * @return void
187 */
188 function testClear() {
189 Cache::config('default', array('engine' => 'File', 'duration' => 1));
190 $data = 'this is a test of the emergency broadcasting system';
191 $write = Cache::write('serialize_test1', $data);
192 $write = Cache::write('serialize_test2', $data);
193 $write = Cache::write('serialize_test3', $data);
194 $this->assertTrue(file_exists(CACHE . 'cake_serialize_test1'));
195 $this->assertTrue(file_exists(CACHE . 'cake_serialize_test2'));
196 $this->assertTrue(file_exists(CACHE . 'cake_serialize_test3'));
197 sleep(2);
198 $result = Cache::clear(true);
199 $this->assertTrue($result);
200 $this->assertFalse(file_exists(CACHE . 'cake_serialize_test1'));
201 $this->assertFalse(file_exists(CACHE . 'cake_serialize_test2'));
202 $this->assertFalse(file_exists(CACHE . 'cake_serialize_test3'));
203
204 $data = 'this is a test of the emergency broadcasting system';
205 $write = Cache::write('serialize_test1', $data);
206 $write = Cache::write('serialize_test2', $data);
207 $write = Cache::write('serialize_test3', $data);
208 $this->assertTrue(file_exists(CACHE . 'cake_serialize_test1'));
209 $this->assertTrue(file_exists(CACHE . 'cake_serialize_test2'));
210 $this->assertTrue(file_exists(CACHE . 'cake_serialize_test3'));
211
212 $result = Cache::clear();
213 $this->assertTrue($result);
214 $this->assertFalse(file_exists(CACHE . 'cake_serialize_test1'));
215 $this->assertFalse(file_exists(CACHE . 'cake_serialize_test2'));
216 $this->assertFalse(file_exists(CACHE . 'cake_serialize_test3'));
217
218 Cache::config('default', array('engine' => 'File', 'path' => CACHE . 'views'));
219
220 $data = 'this is a test of the emergency broadcasting system';
221 $write = Cache::write('controller_view_1', $data);
222 $write = Cache::write('controller_view_2', $data);
223 $write = Cache::write('controller_view_3', $data);
224 $write = Cache::write('controller_view_10', $data);
225 $write = Cache::write('controller_view_11', $data);
226 $write = Cache::write('controller_view_12', $data);
227 $this->assertTrue(file_exists(CACHE . 'views'. DS . 'cake_controller_view_1'));
228 $this->assertTrue(file_exists(CACHE . 'views'. DS . 'cake_controller_view_2'));
229 $this->assertTrue(file_exists(CACHE . 'views'. DS . 'cake_controller_view_3'));
230 $this->assertTrue(file_exists(CACHE . 'views'. DS . 'cake_controller_view_10'));
231 $this->assertTrue(file_exists(CACHE . 'views'. DS . 'cake_controller_view_11'));
232 $this->assertTrue(file_exists(CACHE . 'views'. DS . 'cake_controller_view_12'));
233
234 clearCache('controller_view_1', 'views', '');
235 $this->assertFalse(file_exists(CACHE . 'views'. DS . 'cake_controller_view_1'));
236 $this->assertTrue(file_exists(CACHE . 'views'. DS . 'cake_controller_view_2'));
237 $this->assertTrue(file_exists(CACHE . 'views'. DS . 'cake_controller_view_3'));
238 $this->assertTrue(file_exists(CACHE . 'views'. DS . 'cake_controller_view_10'));
239 $this->assertTrue(file_exists(CACHE . 'views'. DS . 'cake_controller_view_11'));
240 $this->assertTrue(file_exists(CACHE . 'views'. DS . 'cake_controller_view_12'));
241
242 clearCache('controller_view', 'views', '');
243 $this->assertFalse(file_exists(CACHE . 'views'. DS . 'cake_controller_view_1'));
244 $this->assertFalse(file_exists(CACHE . 'views'. DS . 'cake_controller_view_2'));
245 $this->assertFalse(file_exists(CACHE . 'views'. DS . 'cake_controller_view_3'));
246 $this->assertFalse(file_exists(CACHE . 'views'. DS . 'cake_controller_view_10'));
247 $this->assertFalse(file_exists(CACHE . 'views'. DS . 'cake_controller_view_11'));
248 $this->assertFalse(file_exists(CACHE . 'views'. DS . 'cake_controller_view_12'));
249
250 $write = Cache::write('controller_view_1', $data);
251 $write = Cache::write('controller_view_2', $data);
252 $write = Cache::write('controller_view_3', $data);
253 $write = Cache::write('controller_view_10', $data);
254 $write = Cache::write('controller_view_11', $data);
255 $write = Cache::write('controller_view_12', $data);
256 $this->assertTrue(file_exists(CACHE . 'views'. DS . 'cake_controller_view_1'));
257 $this->assertTrue(file_exists(CACHE . 'views'. DS . 'cake_controller_view_2'));
258 $this->assertTrue(file_exists(CACHE . 'views'. DS . 'cake_controller_view_3'));
259 $this->assertTrue(file_exists(CACHE . 'views'. DS . 'cake_controller_view_10'));
260 $this->assertTrue(file_exists(CACHE . 'views'. DS . 'cake_controller_view_11'));
261 $this->assertTrue(file_exists(CACHE . 'views'. DS . 'cake_controller_view_12'));
262
263 clearCache(array('controller_view_2', 'controller_view_11', 'controller_view_12'), 'views', '');
264 $this->assertTrue(file_exists(CACHE . 'views'. DS . 'cake_controller_view_1'));
265 $this->assertFalse(file_exists(CACHE . 'views'. DS . 'cake_controller_view_2'));
266 $this->assertTrue(file_exists(CACHE . 'views'. DS . 'cake_controller_view_3'));
267 $this->assertTrue(file_exists(CACHE . 'views'. DS . 'cake_controller_view_10'));
268 $this->assertFalse(file_exists(CACHE . 'views'. DS . 'cake_controller_view_11'));
269 $this->assertFalse(file_exists(CACHE . 'views'. DS . 'cake_controller_view_12'));
270
271 clearCache('controller_view');
272
273 Cache::config('default', array('engine' => 'File', 'path' => CACHE));
274 }
275
276 /**
277 * test that clear() doesn't wipe files not in the current engine's prefix.
278 *
279 * @return void
280 */
281 function testClearWithPrefixes() {
282 $FileOne =& new FileEngine();
283 $FileOne->init(array(
284 'prefix' => 'prefix_one_',
285 'duration' => DAY
286 ));
287 $FileTwo =& new FileEngine();
288 $FileTwo->init(array(
289 'prefix' => 'prefix_two_',
290 'duration' => DAY
291 ));
292
293 $data1 = $data2 = $expected = 'content to cache';
294 $FileOne->write('key_one', $data1, DAY);
295 $FileTwo->write('key_two', $data2, DAY);
296
297 $this->assertEqual($FileOne->read('key_one'), $expected);
298 $this->assertEqual($FileTwo->read('key_two'), $expected);
299
300 $FileOne->clear(false);
301 $this->assertEqual($FileTwo->read('key_two'), $expected, 'secondary config was cleared by accident.');
302 }
303
304 /**
305 * testKeyPath method
306 *
307 * @access public
308 * @return void
309 */
310 function testKeyPath() {
311 $result = Cache::write('views.countries.something', 'here');
312 $this->assertTrue($result);
313 $this->assertTrue(file_exists(CACHE . 'cake_views_countries_something'));
314
315 $result = Cache::read('views.countries.something');
316 $this->assertEqual($result, 'here');
317
318 $result = Cache::clear();
319 $this->assertTrue($result);
320 }
321
322 /**
323 * testRemoveWindowsSlashesFromCache method
324 *
325 * @access public
326 * @return void
327 */
328 function testRemoveWindowsSlashesFromCache() {
329 Cache::config('windows_test', array('engine' => 'File', 'isWindows' => true, 'prefix' => null, 'path' => TMP));
330
331 $expected = array (
332 'C:\dev\prj2\sites\cake\libs' => array(
333 0 => 'C:\dev\prj2\sites\cake\libs', 1 => 'C:\dev\prj2\sites\cake\libs\view',
334 2 => 'C:\dev\prj2\sites\cake\libs\view\scaffolds', 3 => 'C:\dev\prj2\sites\cake\libs\view\pages',
335 4 => 'C:\dev\prj2\sites\cake\libs\view\layouts', 5 => 'C:\dev\prj2\sites\cake\libs\view\layouts\xml',
336 6 => 'C:\dev\prj2\sites\cake\libs\view\layouts\rss', 7 => 'C:\dev\prj2\sites\cake\libs\view\layouts\js',
337 8 => 'C:\dev\prj2\sites\cake\libs\view\layouts\email', 9 => 'C:\dev\prj2\sites\cake\libs\view\layouts\email\text',
338 10 => 'C:\dev\prj2\sites\cake\libs\view\layouts\email\html', 11 => 'C:\dev\prj2\sites\cake\libs\view\helpers',
339 12 => 'C:\dev\prj2\sites\cake\libs\view\errors', 13 => 'C:\dev\prj2\sites\cake\libs\view\elements',
340 14 => 'C:\dev\prj2\sites\cake\libs\view\elements\email', 15 => 'C:\dev\prj2\sites\cake\libs\view\elements\email\text',
341 16 => 'C:\dev\prj2\sites\cake\libs\view\elements\email\html', 17 => 'C:\dev\prj2\sites\cake\libs\model',
342 18 => 'C:\dev\prj2\sites\cake\libs\model\datasources', 19 => 'C:\dev\prj2\sites\cake\libs\model\datasources\dbo',
343 20 => 'C:\dev\prj2\sites\cake\libs\model\behaviors', 21 => 'C:\dev\prj2\sites\cake\libs\controller',
344 22 => 'C:\dev\prj2\sites\cake\libs\controller\components', 23 => 'C:\dev\prj2\sites\cake\libs\cache'),
345 'C:\dev\prj2\sites\main_site\vendors' => array(
346 0 => 'C:\dev\prj2\sites\main_site\vendors', 1 => 'C:\dev\prj2\sites\main_site\vendors\shells',
347 2 => 'C:\dev\prj2\sites\main_site\vendors\shells\templates', 3 => 'C:\dev\prj2\sites\main_site\vendors\shells\templates\cdc_project',
348 4 => 'C:\dev\prj2\sites\main_site\vendors\shells\tasks', 5 => 'C:\dev\prj2\sites\main_site\vendors\js',
349 6 => 'C:\dev\prj2\sites\main_site\vendors\css'),
350 'C:\dev\prj2\sites\vendors' => array(
351 0 => 'C:\dev\prj2\sites\vendors', 1 => 'C:\dev\prj2\sites\vendors\simpletest',
352 2 => 'C:\dev\prj2\sites\vendors\simpletest\test', 3 => 'C:\dev\prj2\sites\vendors\simpletest\test\support',
353 4 => 'C:\dev\prj2\sites\vendors\simpletest\test\support\collector', 5 => 'C:\dev\prj2\sites\vendors\simpletest\extensions',
354 6 => 'C:\dev\prj2\sites\vendors\simpletest\extensions\testdox', 7 => 'C:\dev\prj2\sites\vendors\simpletest\docs',
355 8 => 'C:\dev\prj2\sites\vendors\simpletest\docs\fr', 9 => 'C:\dev\prj2\sites\vendors\simpletest\docs\en'),
356 'C:\dev\prj2\sites\main_site\views\helpers' => array(
357 0 => 'C:\dev\prj2\sites\main_site\views\helpers')
358 );
359
360 Cache::write('test_dir_map', $expected, 'windows_test');
361 $data = Cache::read('test_dir_map', 'windows_test');
362 Cache::delete('test_dir_map', 'windows_test');
363 $this->assertEqual($expected, $data);
364
365 Cache::drop('windows_test');
366 }
367
368 /**
369 * testWriteQuotedString method
370 *
371 * @access public
372 * @return void
373 */
374 function testWriteQuotedString() {
375 Cache::config('default', array('engine' => 'File', 'path' => TMP . 'tests'));
376 Cache::write('App.doubleQuoteTest', '"this is a quoted string"');
377 $this->assertIdentical(Cache::read('App.doubleQuoteTest'), '"this is a quoted string"');
378 Cache::write('App.singleQuoteTest', "'this is a quoted string'");
379 $this->assertIdentical(Cache::read('App.singleQuoteTest'), "'this is a quoted string'");
380
381 Cache::config('default', array('isWindows' => true, 'path' => TMP . 'tests'));
382 $this->assertIdentical(Cache::read('App.doubleQuoteTest'), '"this is a quoted string"');
383 Cache::write('App.singleQuoteTest', "'this is a quoted string'");
384 $this->assertIdentical(Cache::read('App.singleQuoteTest'), "'this is a quoted string'");
385 }
386
387 /**
388 * check that FileEngine generates an error when a configured Path does not exist.
389 *
390 * @return void
391 */
392 function testErrorWhenPathDoesNotExist() {
393 if ($this->skipIf(is_dir(TMP . 'tests' . DS . 'file_failure'), 'Cannot run test directory exists. %s')) {
394 return;
395 }
396 $this->expectError();
397 Cache::config('failure', array(
398 'engine' => 'File',
399 'path' => TMP . 'tests' . DS . 'file_failure'
400 ));
401
402 Cache::drop('failure');
403 }
404 }