/
CacheTest.php
457 lines (388 loc) · 12.2 KB
/
CacheTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
<?php
/**
* CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
* Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice
*
* @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
* @since CakePHP(tm) v 1.2.0.5432
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace Cake\Test\TestCase\Cache;
use Cake\Cache\Cache;
use Cake\Core\App;
use Cake\Core\Configure;
use Cake\Core\Plugin;
use Cake\TestSuite\TestCase;
/**
* CacheTest class
*
* @package Cake.Test.Case.Cache
*/
class CacheTest extends TestCase {
/**
* setUp method
*
* @return void
*/
public function setUp() {
parent::setUp();
Configure::write('Cache.disable', false);
Configure::write('Cache.default', [
'engine' => 'File',
'path' => TMP . 'tests'
]);
}
/**
* testEngine method
*
* @return void
*/
public function testEngine() {
$settings = [
'engine' => 'File',
'path' => TMP . 'tests',
'prefix' => 'cake_test_'
];
Configure::write('Cache.test_config', $settings);
$engine = Cache::engine('test_config');
$this->assertInstanceOf('Cake\Cache\Engine\FileEngine', $engine);
}
/**
* Check that no fatal errors are issued doing normal things when Cache.disable is true.
*
* @return void
*/
public function testNonFatalErrorsWithCachedisable() {
Configure::write('Cache.disable', true);
Configure::write('Cache.test', [
'engine' => 'File',
'path' => TMP, 'prefix' => 'error_test_'
]);
Cache::write('no_save', 'Noooo!', 'test');
Cache::read('no_save', 'test');
Cache::delete('no_save', 'test');
Cache::set('duration', '+10 minutes');
Configure::write('Cache.disable', false);
}
/**
* test configuring CacheEngines in App/libs
*
* @return void
*/
public function testConfigWithLibAndPluginEngines() {
App::build(array(
'Lib' => array(CAKE . 'Test/TestApp/Lib/'),
'Plugin' => array(CAKE . 'Test/TestApp/Plugin/')
), App::RESET);
Configure::write('App.namespace', 'TestApp');
Plugin::load('TestPlugin');
$settings = ['engine' => 'TestAppCache', 'path' => TMP, 'prefix' => 'cake_test_'];
Configure::write('Cache.libEngine', $settings);
$engine = Cache::engine('libEngine');
$this->assertInstanceOf('\TestApp\Cache\Engine\TestAppCacheEngine', $engine);
$settings = ['engine' => 'TestPlugin.TestPluginCache', 'path' => TMP, 'prefix' => 'cake_test_'];
$result = Configure::write('Cache.pluginLibEngine', $settings);
$engine = Cache::engine('pluginLibEngine');
$this->assertInstanceOf('\TestPlugin\Cache\Engine\TestPluginCacheEngine', $engine);
Cache::drop('libEngine');
Cache::drop('pluginLibEngine');
Plugin::unload();
}
/**
* Test reading from a config that is undefined.
*
* @expectedException PHPUnit_Framework_Error_Warning
* @return void
*/
public function testReadNonExistingConfig() {
$this->assertFalse(Cache::read('key', 'totally fake'));
}
/**
* Test write from a config that is undefined.
*
* @expectedException PHPUnit_Framework_Error_Warning
* @return void
*/
public function testWriteNonExistingConfig() {
$this->assertFalse(Cache::write('key', 'value', 'totally fake'));
}
/**
* Test write from a config that is undefined.
*
* @expectedException PHPUnit_Framework_Error_Warning
* @return void
*/
public function testIncrementNonExistingConfig() {
$this->assertFalse(Cache::increment('key', 1, 'totally fake'));
}
/**
* Test write from a config that is undefined.
*
* @expectedException PHPUnit_Framework_Error_Warning
* @return void
*/
public function testDecrementNonExistingConfig() {
$this->assertFalse(Cache::decrement('key', 1, 'totally fake'));
}
/**
* test that trying to configure classes that don't extend CacheEngine fail.
*
* @expectedException Cake\Error\Exception
* @return void
*/
public function testAttemptingToConfigureANonCacheEngineClass() {
$this->getMock('\StdClass', array(), array(), 'RubbishEngine');
Configure::write('Cache.wrong', array(
'engine' => '\RubbishEngine'
));
Cache::engine('wrong');
}
/**
* Test that engine() can be used to inject instances.
*
* @return void
*/
public function testSetEngineValid() {
$engine = $this->getMockForAbstractClass('\Cake\Cache\CacheEngine');
Cache::engine('test', $engine);
$this->assertSame($engine, Cache::engine('test'));
}
/**
* test that calling config() sets the 'default' configuration up.
*
* @return void
*/
public function testConfigSettingDefaultConfigKey() {
Configure::write('Cache.test_name', [
'engine' => 'File',
'prefix' => 'test_name_'
]);
Cache::write('value_one', 'I am cached', 'test_name');
$result = Cache::read('value_one', 'test_name');
$this->assertEquals('I am cached', $result);
$result = Cache::read('value_one');
$this->assertEquals(null, $result);
Cache::write('value_one', 'I am in default config!');
$result = Cache::read('value_one');
$this->assertEquals('I am in default config!', $result);
$result = Cache::read('value_one', 'test_name');
$this->assertEquals('I am cached', $result);
Cache::delete('value_one', 'test_name');
Cache::delete('value_one', 'default');
}
/**
* test that configured returns an array of the currently constrcuted cache
* engines
*
* @return void
*/
public function testConfigured() {
Cache::drop('default');
$result = Cache::configured();
$this->assertContains('_cake_core_', $result);
$this->assertNotContains('default', $result, 'Unconnected engines should not display.');
Cache::engine('default');
$result = Cache::configured();
$this->assertContains('default', $result, 'default should exist now.');
}
/**
* test that drop removes cache configs, and that further attempts to use that config
* do not work.
*
* @return void
*/
public function testDrop() {
App::build(array(
'Lib' => array(CAKE . 'Test/TestApp/Lib/'),
'Plugin' => array(CAKE . 'Test/TestApp/Plugin/')
), App::RESET);
Configure::write('App.namespace', 'TestApp');
$result = Cache::drop('some_config_that_does_not_exist');
$this->assertFalse($result);
Cache::engine('default');
$result = Cache::drop('default');
$this->assertTrue($result, 'Built engines should be dropped');
$config = Configure::read('Cache.default');
$this->assertEquals('File', $config['engine'], 'Config data should not be removed.');
Configure::write('Cache.unconfigTest', [
'engine' => 'TestAppCache'
]);
$this->assertInstanceOf(
'TestApp\Cache\Engine\TestAppCacheEngine',
Cache::engine('unconfigTest')
);
$this->assertTrue(Cache::drop('unconfigTest'));
}
/**
* Test that dropping a cache config refreshes its configuration and
* creates a new instance.
*
* @return void
*/
public function testDropChangeConfig() {
Configure::write('Cache.tests', [
'engine' => 'File',
]);
$result = Cache::engine('tests');
$settings = Cache::settings('tests');
$this->assertEquals(CACHE, $settings['path']);
$id = spl_object_hash($result);
Cache::drop('tests');
Configure::write('Cache.tests', [
'engine' => 'File',
'extra' => 'value'
]);
$result = Cache::engine('tests');
$this->assertNotEquals($id, spl_object_hash($result));
}
/**
* testWriteEmptyValues method
*
* @return void
*/
public function testWriteEmptyValues() {
Cache::write('App.falseTest', false);
$this->assertSame(Cache::read('App.falseTest'), false);
Cache::write('App.trueTest', true);
$this->assertSame(Cache::read('App.trueTest'), true);
Cache::write('App.nullTest', null);
$this->assertSame(Cache::read('App.nullTest'), null);
Cache::write('App.zeroTest', 0);
$this->assertSame(Cache::read('App.zeroTest'), 0);
Cache::write('App.zeroTest2', '0');
$this->assertSame(Cache::read('App.zeroTest2'), '0');
}
/**
* Test that failed writes cause errors to be triggered.
*
* @return void
*/
public function testWriteTriggerError() {
App::build(array(
'Lib' => array(CAKE . 'Test/TestApp/Lib/'),
'Plugin' => array(CAKE . 'Test/TestApp/Plugin/')
), App::RESET);
Configure::write('App.namespace', 'TestApp');
Configure::write('Cache.test_trigger', [
'engine' => 'TestAppCache',
'prefix' => ''
]);
try {
Cache::write('fail', 'value', 'test_trigger');
$this->fail('No exception thrown');
} catch (\PHPUnit_Framework_Error $e) {
$this->assertTrue(true);
}
Cache::drop('test_trigger');
App::build();
}
/**
* testCacheDisable method
*
* Check that the "Cache.disable" configuration and a change to it
* (even after a cache config has been setup) is taken into account.
*
* @return void
*/
public function testCacheDisable() {
Configure::write('Cache.disable', false);
Configure::write('Cache.test_cache_disable_1', [
'engine' => 'File',
'path' => TMP . 'tests'
]);
$this->assertTrue(Cache::write('key_1', 'hello', 'test_cache_disable_1'));
$this->assertSame(Cache::read('key_1', 'test_cache_disable_1'), 'hello');
Configure::write('Cache.disable', true);
$this->assertFalse(Cache::write('key_2', 'hello', 'test_cache_disable_1'));
$this->assertFalse(Cache::read('key_2', 'test_cache_disable_1'));
Configure::write('Cache.disable', false);
$this->assertTrue(Cache::write('key_3', 'hello', 'test_cache_disable_1'));
$this->assertSame(Cache::read('key_3', 'test_cache_disable_1'), 'hello');
Configure::write('Cache.disable', true);
Configure::write('Cache.test_cache_disable_2', [
'engine' => 'File',
'path' => TMP . 'tests'
]);
$this->assertFalse(Cache::write('key_4', 'hello', 'test_cache_disable_2'));
$this->assertFalse(Cache::read('key_4', 'test_cache_disable_2'));
Configure::write('Cache.disable', false);
$this->assertTrue(Cache::write('key_5', 'hello', 'test_cache_disable_2'));
$this->assertSame(Cache::read('key_5', 'test_cache_disable_2'), 'hello');
Configure::write('Cache.disable', true);
$this->assertFalse(Cache::write('key_6', 'hello', 'test_cache_disable_2'));
$this->assertFalse(Cache::read('key_6', 'test_cache_disable_2'));
}
/**
* testSet method
*
* @return void
*/
public function testSet() {
$_cacheSet = Cache::set();
Cache::set(array('duration' => '+1 year'));
$data = Cache::read('test_cache');
$this->assertFalse($data);
$data = 'this is just a simple test of the cache system';
$write = Cache::write('test_cache', $data);
$this->assertTrue($write);
Cache::set(array('duration' => '+1 year'));
$data = Cache::read('test_cache');
$this->assertEquals('this is just a simple test of the cache system', $data);
Cache::delete('test_cache');
Cache::set($_cacheSet);
}
/**
* Test that set() modifies settings, which can be read back with
* settings().
*
* @return void
*/
public function testSetModifySettings() {
Configure::write('Cache.tests', [
'engine' => 'File',
'duration' => '+1 minute'
]);
Cache::drop('tests');
$result = Cache::set(['duration' => '+1 year'], 'tests');
$this->assertEquals(strtotime('+1 year') - time(), $result['duration']);
$result = Cache::set('duration', '+1 month', 'tests');
$this->assertEquals(strtotime('+1 month') - time(), $result['duration']);
$settings = Cache::settings('tests');
$this->assertEquals($result, $settings, 'set() and settings() should be the same.');
}
/**
* Test that calling set() with null, config restores old settings.
*
* @return void
*/
public function testSetModifyAndResetSettings() {
Configure::write('Cache.tests', [
'engine' => 'File',
'duration' => '+1 minute'
]);
Cache::drop('tests');
$result = Cache::set('duration', '+1 year', 'tests');
$this->assertEquals(strtotime('+1 year') - time(), $result['duration']);
$result = Cache::set(null, 'tests');
$this->assertEquals(strtotime('+1 minute') - time(), $result['duration']);
}
/**
* test set() parameter handling for user cache configs.
*
* @return void
*/
public function testSetOnAlternateConfigs() {
Configure::write('Cache.file_config', [
'engine' => 'File',
'prefix' => 'test_file_'
]);
Cache::set(['duration' => '+1 year'], 'file_config');
$settings = Cache::settings('file_config');
$this->assertEquals('test_file_', $settings['prefix']);
$this->assertEquals(strtotime('+1 year') - time(), $settings['duration']);
}
}