~artur-barczynski/azsystem/trunk

« back to all changes in this revision

Viewing changes to lib/Cake/Test/Case/Cache/CacheTest.php

  • Committer: Artur Barczynski
  • Date: 2012-09-20 16:31:07 UTC
  • Revision ID: artur@arturkb.pl-20120920163107-oakeg1a4h9e6d37f
Init

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
 * CacheTest file
 
4
 *
 
5
 * PHP 5
 
6
 *
 
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)
 
9
 *
 
10
 * Licensed under The MIT License
 
11
 * Redistributions of files must retain the above copyright notice
 
12
 *
 
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)
 
18
 */
 
19
 
 
20
App::uses('Cache', 'Cache');
 
21
 
 
22
/**
 
23
 * CacheTest class
 
24
 *
 
25
 * @package       Cake.Test.Case.Cache
 
26
 */
 
27
class CacheTest extends CakeTestCase {
 
28
 
 
29
/**
 
30
 * setUp method
 
31
 *
 
32
 * @return void
 
33
 */
 
34
        public function setUp() {
 
35
                parent::setUp();
 
36
                $this->_cacheDisable = Configure::read('Cache.disable');
 
37
                Configure::write('Cache.disable', false);
 
38
 
 
39
                $this->_defaultCacheConfig = Cache::config('default');
 
40
                Cache::config('default', array('engine' => 'File', 'path' => TMP . 'tests'));
 
41
        }
 
42
 
 
43
/**
 
44
 * tearDown method
 
45
 *
 
46
 * @return void
 
47
 */
 
48
        public function tearDown() {
 
49
                parent::tearDown();
 
50
                Configure::write('Cache.disable', $this->_cacheDisable);
 
51
                Cache::config('default', $this->_defaultCacheConfig['settings']);
 
52
        }
 
53
 
 
54
/**
 
55
 * testConfig method
 
56
 *
 
57
 * @return void
 
58
 */
 
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']));
 
65
        }
 
66
 
 
67
/**
 
68
 * Check that no fatal errors are issued doing normal things when Cache.disable is true.
 
69
 *
 
70
 * @return void
 
71
 */
 
72
        public function testNonFatalErrorsWithCachedisable() {
 
73
                Configure::write('Cache.disable', true);
 
74
                Cache::config('test', array('engine' => 'File', 'path' => TMP, 'prefix' => 'error_test_'));
 
75
 
 
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');
 
80
 
 
81
                Configure::write('Cache.disable', false);
 
82
        }
 
83
 
 
84
/**
 
85
 * test configuring CacheEngines in App/libs
 
86
 *
 
87
 * @return void
 
88
 */
 
89
        public function testConfigWithLibAndPluginEngines() {
 
90
                App::build(array(
 
91
                        'Lib' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Lib' . DS),
 
92
                        'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
 
93
                ), App::RESET);
 
94
                CakePlugin::load('TestPlugin');
 
95
 
 
96
                $settings = array('engine' => 'TestAppCache', 'path' => TMP, 'prefix' => 'cake_test_');
 
97
                $result = Cache::config('libEngine', $settings);
 
98
                $this->assertEquals(Cache::config('libEngine'), $result);
 
99
 
 
100
                $settings = array('engine' => 'TestPlugin.TestPluginCache', 'path' => TMP, 'prefix' => 'cake_test_');
 
101
                $result = Cache::config('pluginLibEngine', $settings);
 
102
                $this->assertEquals(Cache::config('pluginLibEngine'), $result);
 
103
 
 
104
                Cache::drop('libEngine');
 
105
                Cache::drop('pluginLibEngine');
 
106
 
 
107
                App::build();
 
108
                CakePlugin::unload();
 
109
        }
 
110
 
 
111
/**
 
112
 * testInvalidConfig method
 
113
 *
 
114
 * Test that the cache class doesn't cause fatal errors with a partial path
 
115
 *
 
116
 * @expectedException PHPUnit_Framework_Error_Warning
 
117
 * @return void
 
118
 */
 
119
        public function testInvalidConfig() {
 
120
                Cache::config('invalid', array(
 
121
                        'engine' => 'File',
 
122
                        'duration' => '+1 year',
 
123
                        'prefix' => 'testing_invalid_',
 
124
                        'path' => 'data/',
 
125
                        'serialize' => true,
 
126
                        'random' => 'wii'
 
127
                ));
 
128
                $read = Cache::read('Test', 'invalid');
 
129
        }
 
130
 
 
131
/**
 
132
 * Test reading from a config that is undefined.
 
133
 *
 
134
 * @return void
 
135
 */
 
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'));
 
141
        }
 
142
 
 
143
/**
 
144
 * test that trying to configure classes that don't extend CacheEngine fail.
 
145
 *
 
146
 * @expectedException CacheException
 
147
 * @return void
 
148
 */
 
149
        public function testAttemptingToConfigureANonCacheEngineClass() {
 
150
                $this->getMock('StdClass', array(), array(), 'RubbishEngine');
 
151
                Cache::config('Garbage', array(
 
152
                        'engine' => 'Rubbish'
 
153
                ));
 
154
        }
 
155
 
 
156
/**
 
157
 * testConfigChange method
 
158
 *
 
159
 * @return void
 
160
 */
 
161
        public function testConfigChange() {
 
162
                $_cacheConfigSessions = Cache::config('sessions');
 
163
                $_cacheConfigTests = Cache::config('tests');
 
164
 
 
165
                $result = Cache::config('sessions', array('engine' => 'File', 'path' => TMP . 'sessions'));
 
166
                $this->assertEquals(Cache::settings('sessions'), $result['settings']);
 
167
 
 
168
                $result = Cache::config('tests', array('engine' => 'File', 'path' => TMP . 'tests'));
 
169
                $this->assertEquals(Cache::settings('tests'), $result['settings']);
 
170
 
 
171
                Cache::config('sessions', $_cacheConfigSessions['settings']);
 
172
                Cache::config('tests', $_cacheConfigTests['settings']);
 
173
        }
 
174
 
 
175
/**
 
176
 * test that calling config() sets the 'default' configuration up.
 
177
 *
 
178
 * @return void
 
179
 */
 
180
        public function testConfigSettingDefaultConfigKey() {
 
181
                Cache::config('test_name', array('engine' => 'File', 'prefix' => 'test_name_'));
 
182
 
 
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);
 
186
 
 
187
                $result = Cache::read('value_one');
 
188
                $this->assertEquals(null, $result);
 
189
 
 
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);
 
193
 
 
194
                $result = Cache::read('value_one', 'test_name');
 
195
                $this->assertEquals('I am cached', $result);
 
196
 
 
197
                Cache::delete('value_one', 'test_name');
 
198
                Cache::delete('value_one', 'default');
 
199
        }
 
200
 
 
201
/**
 
202
 * testWritingWithConfig method
 
203
 *
 
204
 * @return void
 
205
 */
 
206
        public function testWritingWithConfig() {
 
207
                $_cacheConfigSessions = Cache::config('sessions');
 
208
 
 
209
                Cache::write('test_something', 'this is the test data', 'tests');
 
210
 
 
211
                $expected = array(
 
212
                        'path' => TMP . 'sessions' . DS,
 
213
                        'prefix' => 'cake_',
 
214
                        'lock' => true,
 
215
                        'serialize' => true,
 
216
                        'duration' => 3600,
 
217
                        'probability' => 100,
 
218
                        'engine' => 'File',
 
219
                        'isWindows' => DIRECTORY_SEPARATOR == '\\',
 
220
                        'mask' => 0664,
 
221
                        'groups' => array()
 
222
                );
 
223
                $this->assertEquals($expected, Cache::settings('sessions'));
 
224
 
 
225
                Cache::config('sessions', $_cacheConfigSessions['settings']);
 
226
        }
 
227
 
 
228
/**
 
229
 * test that configured returns an array of the currently configured cache
 
230
 * settings
 
231
 *
 
232
 * @return void
 
233
 */
 
234
        public function testConfigured() {
 
235
                $result = Cache::configured();
 
236
                $this->assertTrue(in_array('_cake_core_', $result));
 
237
                $this->assertTrue(in_array('default', $result));
 
238
        }
 
239
 
 
240
/**
 
241
 * testInitSettings method
 
242
 *
 
243
 * @return void
 
244
 */
 
245
        public function testInitSettings() {
 
246
                $initial = Cache::settings();
 
247
                $override = array('engine' => 'File', 'path' => TMP . 'tests');
 
248
                Cache::config('for_test', $override);
 
249
 
 
250
                $settings = Cache::settings();
 
251
                $expecting = $override + $initial;
 
252
                $this->assertEquals($settings, $expecting);
 
253
        }
 
254
 
 
255
/**
 
256
 * test that drop removes cache configs, and that further attempts to use that config
 
257
 * do not work.
 
258
 *
 
259
 * @return void
 
260
 */
 
261
        public function testDrop() {
 
262
                App::build(array(
 
263
                        'Lib' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Lib' . DS),
 
264
                        'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
 
265
                ), App::RESET);
 
266
 
 
267
                $result = Cache::drop('some_config_that_does_not_exist');
 
268
                $this->assertFalse($result);
 
269
 
 
270
                $_testsConfig = Cache::config('tests');
 
271
                $result = Cache::drop('tests');
 
272
                $this->assertTrue($result);
 
273
 
 
274
                Cache::config('unconfigTest', array(
 
275
                        'engine' => 'TestAppCache'
 
276
                ));
 
277
                $this->assertTrue(Cache::isInitialized('unconfigTest'));
 
278
 
 
279
                $this->assertTrue(Cache::drop('unconfigTest'));
 
280
                $this->assertFalse(Cache::isInitialized('TestAppCache'));
 
281
 
 
282
                Cache::config('tests', $_testsConfig);
 
283
                App::build();
 
284
        }
 
285
 
 
286
/**
 
287
 * testWriteEmptyValues method
 
288
 *
 
289
 * @return void
 
290
 */
 
291
        public function testWriteEmptyValues() {
 
292
                Cache::write('App.falseTest', false);
 
293
                $this->assertSame(Cache::read('App.falseTest'), false);
 
294
 
 
295
                Cache::write('App.trueTest', true);
 
296
                $this->assertSame(Cache::read('App.trueTest'), true);
 
297
 
 
298
                Cache::write('App.nullTest', null);
 
299
                $this->assertSame(Cache::read('App.nullTest'), null);
 
300
 
 
301
                Cache::write('App.zeroTest', 0);
 
302
                $this->assertSame(Cache::read('App.zeroTest'), 0);
 
303
 
 
304
                Cache::write('App.zeroTest2', '0');
 
305
                $this->assertSame(Cache::read('App.zeroTest2'), '0');
 
306
        }
 
307
 
 
308
/**
 
309
 * Test that failed writes cause errors to be triggered.
 
310
 *
 
311
 * @return void
 
312
 */
 
313
        public function testWriteTriggerError() {
 
314
                App::build(array(
 
315
                        'Lib' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Lib' . DS),
 
316
                        'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
 
317
                ), App::RESET);
 
318
 
 
319
                Cache::config('test_trigger', array('engine' => 'TestAppCache', 'prefix' => ''));
 
320
                try {
 
321
                        Cache::write('fail', 'value', 'test_trigger');
 
322
                        $this->fail('No exception thrown');
 
323
                } catch (PHPUnit_Framework_Error $e) {
 
324
                        $this->assertTrue(true);
 
325
                }
 
326
                Cache::drop('test_trigger');
 
327
                App::build();
 
328
        }
 
329
 
 
330
/**
 
331
 * testCacheDisable method
 
332
 *
 
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.
 
335
 *
 
336
 * @return void
 
337
 */
 
338
        public function testCacheDisable() {
 
339
                Configure::write('Cache.disable', false);
 
340
                Cache::config('test_cache_disable_1', array('engine' => 'File', 'path' => TMP . 'tests'));
 
341
 
 
342
                $this->assertTrue(Cache::write('key_1', 'hello', 'test_cache_disable_1'));
 
343
                $this->assertSame(Cache::read('key_1', 'test_cache_disable_1'), 'hello');
 
344
 
 
345
                Configure::write('Cache.disable', true);
 
346
 
 
347
                $this->assertFalse(Cache::write('key_2', 'hello', 'test_cache_disable_1'));
 
348
                $this->assertFalse(Cache::read('key_2', 'test_cache_disable_1'));
 
349
 
 
350
                Configure::write('Cache.disable', false);
 
351
 
 
352
                $this->assertTrue(Cache::write('key_3', 'hello', 'test_cache_disable_1'));
 
353
                $this->assertSame(Cache::read('key_3', 'test_cache_disable_1'), 'hello');
 
354
 
 
355
                Configure::write('Cache.disable', true);
 
356
                Cache::config('test_cache_disable_2', array('engine' => 'File', 'path' => TMP . 'tests'));
 
357
 
 
358
                $this->assertFalse(Cache::write('key_4', 'hello', 'test_cache_disable_2'));
 
359
                $this->assertFalse(Cache::read('key_4', 'test_cache_disable_2'));
 
360
 
 
361
                Configure::write('Cache.disable', false);
 
362
 
 
363
                $this->assertTrue(Cache::write('key_5', 'hello', 'test_cache_disable_2'));
 
364
                $this->assertSame(Cache::read('key_5', 'test_cache_disable_2'), 'hello');
 
365
 
 
366
                Configure::write('Cache.disable', true);
 
367
 
 
368
                $this->assertFalse(Cache::write('key_6', 'hello', 'test_cache_disable_2'));
 
369
                $this->assertFalse(Cache::read('key_6', 'test_cache_disable_2'));
 
370
        }
 
371
 
 
372
/**
 
373
 * testSet method
 
374
 *
 
375
 * @return void
 
376
 */
 
377
        public function testSet() {
 
378
                $_cacheSet = Cache::set();
 
379
 
 
380
                Cache::set(array('duration' => '+1 year'));
 
381
                $data = Cache::read('test_cache');
 
382
                $this->assertFalse($data);
 
383
 
 
384
                $data = 'this is just a simple test of the cache system';
 
385
                $write = Cache::write('test_cache', $data);
 
386
                $this->assertTrue($write);
 
387
 
 
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);
 
391
 
 
392
                Cache::delete('test_cache');
 
393
 
 
394
                $global = Cache::settings();
 
395
 
 
396
                Cache::set($_cacheSet);
 
397
        }
 
398
 
 
399
/**
 
400
 * test set() parameter handling for user cache configs.
 
401
 *
 
402
 * @return void
 
403
 */
 
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');
 
408
 
 
409
                $this->assertEquals('test_file_', $settings['prefix']);
 
410
                $this->assertEquals(strtotime('+1 year') - time(), $settings['duration']);
 
411
        }
 
412
}