Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
12345 anikendra 1
<?php
2
/**
3
 * RedisEngineTest file
4
 *
5
 * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
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/view/1196/Testing CakePHP(tm) Tests
14
 * @package       Cake.Test.Case.Cache.Engine
15
 * @since         CakePHP(tm) v 2.2
16
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
17
 */
18
 
19
App::uses('Cache', 'Cache');
20
App::uses('RedisEngine', 'Cache/Engine');
21
 
22
/**
23
 * RedisEngineTest class
24
 *
25
 * @package       Cake.Test.Case.Cache.Engine
26
 */
27
class RedisEngineTest extends CakeTestCase {
28
 
29
/**
30
 * setUp method
31
 *
32
 * @return void
33
 */
34
	public function setUp() {
35
		parent::setUp();
36
		$this->skipIf(!class_exists('Redis'), 'Redis is not installed or configured properly.');
37
 
38
		$this->_cacheDisable = Configure::read('Cache.disable');
39
		Configure::write('Cache.disable', false);
40
		Cache::config('redis', array(
41
			'engine' => 'Redis',
42
			'prefix' => 'cake_',
43
			'duration' => 3600
44
		));
45
	}
46
 
47
/**
48
 * tearDown method
49
 *
50
 * @return void
51
 */
52
	public function tearDown() {
53
		parent::tearDown();
54
		Configure::write('Cache.disable', $this->_cacheDisable);
55
		Cache::drop('');
56
		Cache::drop('redis_groups');
57
		Cache::drop('redis_helper');
58
		Cache::config('default');
59
	}
60
 
61
/**
62
 * testSettings method
63
 *
64
 * @return void
65
 */
66
	public function testSettings() {
67
		$settings = Cache::settings('redis');
68
		$expecting = array(
69
			'prefix' => 'cake_',
70
			'duration' => 3600,
71
			'probability' => 100,
72
			'groups' => array(),
73
			'engine' => 'Redis',
74
			'server' => '127.0.0.1',
75
			'port' => 6379,
76
			'timeout' => 0,
77
			'persistent' => true,
78
			'password' => false,
79
			'database' => 0,
80
		);
81
		$this->assertEquals($expecting, $settings);
82
	}
83
 
84
/**
85
 * testConnect method
86
 *
87
 * @return void
88
 */
89
	public function testConnect() {
90
		$Redis = new RedisEngine();
91
		$this->assertTrue($Redis->init(Cache::settings('redis')));
92
	}
93
 
94
/**
95
 * testMultiDatabaseOperations method
96
 *
97
 * @return void
98
 */
99
	public function testMultiDatabaseOperations() {
100
		Cache::config('redisdb0', array(
101
			'engine' => 'Redis',
102
			'prefix' => 'cake2_',
103
			'duration' => 3600,
104
			'persistent' => false,
105
		));
106
 
107
		Cache::config('redisdb1', array(
108
			'engine' => 'Redis',
109
			'database' => 1,
110
			'prefix' => 'cake2_',
111
			'duration' => 3600,
112
			'persistent' => false,
113
		));
114
 
115
		$result = Cache::write('save_in_0', true, 'redisdb0');
116
		$exist = Cache::read('save_in_0', 'redisdb0');
117
		$this->assertTrue($result);
118
		$this->assertTrue($exist);
119
 
120
		$result = Cache::write('save_in_1', true, 'redisdb1');
121
		$this->assertTrue($result);
122
		$exist = Cache::read('save_in_0', 'redisdb1');
123
		$this->assertFalse($exist);
124
		$exist = Cache::read('save_in_1', 'redisdb1');
125
		$this->assertTrue($exist);
126
 
127
		Cache::delete('save_in_0', 'redisdb0');
128
		$exist = Cache::read('save_in_0', 'redisdb0');
129
		$this->assertFalse($exist);
130
 
131
		Cache::delete('save_in_1', 'redisdb1');
132
		$exist = Cache::read('save_in_1', 'redisdb1');
133
		$this->assertFalse($exist);
134
 
135
		Cache::drop('redisdb0');
136
		Cache::drop('redisdb1');
137
	}
138
 
139
/**
140
 * testReadAndWriteCache method
141
 *
142
 * @return void
143
 */
144
	public function testReadAndWriteCache() {
145
		Cache::set(array('duration' => 1), null, 'redis');
146
 
147
		$result = Cache::read('test', 'redis');
148
		$expecting = '';
149
		$this->assertEquals($expecting, $result);
150
 
151
		$data = 'this is a test of the emergency broadcasting system';
152
		$result = Cache::write('test', $data, 'redis');
153
		$this->assertTrue($result);
154
 
155
		$result = Cache::read('test', 'redis');
156
		$expecting = $data;
157
		$this->assertEquals($expecting, $result);
158
 
159
		$data = array(1, 2, 3);
160
		$this->assertTrue(Cache::write('array_data', $data, 'redis'));
161
		$this->assertEquals($data, Cache::read('array_data', 'redis'));
162
 
163
		Cache::delete('test', 'redis');
164
	}
165
 
166
/**
167
 * testExpiry method
168
 *
169
 * @return void
170
 */
171
	public function testExpiry() {
172
		Cache::set(array('duration' => 1), 'redis');
173
 
174
		$result = Cache::read('test', 'redis');
175
		$this->assertFalse($result);
176
 
177
		$data = 'this is a test of the emergency broadcasting system';
178
		$result = Cache::write('other_test', $data, 'redis');
179
		$this->assertTrue($result);
180
 
181
		sleep(2);
182
		$result = Cache::read('other_test', 'redis');
183
		$this->assertFalse($result);
184
 
185
		Cache::set(array('duration' => "+1 second"), 'redis');
186
 
187
		$data = 'this is a test of the emergency broadcasting system';
188
		$result = Cache::write('other_test', $data, 'redis');
189
		$this->assertTrue($result);
190
 
191
		sleep(2);
192
		$result = Cache::read('other_test', 'redis');
193
		$this->assertFalse($result);
194
 
195
		Cache::config('redis', array('duration' => '+1 second'));
196
		sleep(2);
197
 
198
		$result = Cache::read('other_test', 'redis');
199
		$this->assertFalse($result);
200
 
201
		Cache::config('redis', array('duration' => '+29 days'));
202
		$data = 'this is a test of the emergency broadcasting system';
203
		$result = Cache::write('long_expiry_test', $data, 'redis');
204
		$this->assertTrue($result);
205
 
206
		sleep(2);
207
		$result = Cache::read('long_expiry_test', 'redis');
208
		$expecting = $data;
209
		$this->assertEquals($expecting, $result);
210
 
211
		Cache::config('redis', array('duration' => 3600));
212
	}
213
 
214
/**
215
 * testDeleteCache method
216
 *
217
 * @return void
218
 */
219
	public function testDeleteCache() {
220
		$data = 'this is a test of the emergency broadcasting system';
221
		$result = Cache::write('delete_test', $data, 'redis');
222
		$this->assertTrue($result);
223
 
224
		$result = Cache::delete('delete_test', 'redis');
225
		$this->assertTrue($result);
226
	}
227
 
228
/**
229
 * testDecrement method
230
 *
231
 * @return void
232
 */
233
	public function testDecrement() {
234
		Cache::delete('test_decrement', 'redis');
235
		$result = Cache::write('test_decrement', 5, 'redis');
236
		$this->assertTrue($result);
237
 
238
		$result = Cache::decrement('test_decrement', 1, 'redis');
239
		$this->assertEquals(4, $result);
240
 
241
		$result = Cache::read('test_decrement', 'redis');
242
		$this->assertEquals(4, $result);
243
 
244
		$result = Cache::decrement('test_decrement', 2, 'redis');
245
		$this->assertEquals(2, $result);
246
 
247
		$result = Cache::read('test_decrement', 'redis');
248
		$this->assertEquals(2, $result);
249
	}
250
 
251
/**
252
 * testIncrement method
253
 *
254
 * @return void
255
 */
256
	public function testIncrement() {
257
		Cache::delete('test_increment', 'redis');
258
		$result = Cache::increment('test_increment', 1, 'redis');
259
		$this->assertEquals(1, $result);
260
 
261
		$result = Cache::read('test_increment', 'redis');
262
		$this->assertEquals(1, $result);
263
 
264
		$result = Cache::increment('test_increment', 2, 'redis');
265
		$this->assertEquals(3, $result);
266
 
267
		$result = Cache::read('test_increment', 'redis');
268
		$this->assertEquals(3, $result);
269
	}
270
 
271
/**
272
 * test clearing redis.
273
 *
274
 * @return void
275
 */
276
	public function testClear() {
277
		Cache::config('redis2', array(
278
			'engine' => 'Redis',
279
			'prefix' => 'cake2_',
280
			'duration' => 3600
281
		));
282
 
283
		Cache::write('some_value', 'cache1', 'redis');
284
		$result = Cache::clear(true, 'redis');
285
		$this->assertTrue($result);
286
		$this->assertEquals('cache1', Cache::read('some_value', 'redis'));
287
 
288
		Cache::write('some_value', 'cache2', 'redis2');
289
		$result = Cache::clear(false, 'redis');
290
		$this->assertTrue($result);
291
		$this->assertFalse(Cache::read('some_value', 'redis'));
292
		$this->assertEquals('cache2', Cache::read('some_value', 'redis2'));
293
 
294
		Cache::clear(false, 'redis2');
295
	}
296
 
297
/**
298
 * test that a 0 duration can successfully write.
299
 *
300
 * @return void
301
 */
302
	public function testZeroDuration() {
303
		Cache::config('redis', array('duration' => 0));
304
		$result = Cache::write('test_key', 'written!', 'redis');
305
 
306
		$this->assertTrue($result);
307
		$result = Cache::read('test_key', 'redis');
308
		$this->assertEquals('written!', $result);
309
	}
310
 
311
/**
312
 * Tests that configuring groups for stored keys return the correct values when read/written
313
 * Shows that altering the group value is equivalent to deleting all keys under the same
314
 * group
315
 *
316
 * @return void
317
 */
318
	public function testGroupReadWrite() {
319
		Cache::config('redis_groups', array(
320
			'engine' => 'Redis',
321
			'duration' => 3600,
322
			'groups' => array('group_a', 'group_b'),
323
			'prefix' => 'test_'
324
		));
325
		Cache::config('redis_helper', array(
326
			'engine' => 'Redis',
327
			'duration' => 3600,
328
			'prefix' => 'test_'
329
		));
330
		$this->assertTrue(Cache::write('test_groups', 'value', 'redis_groups'));
331
		$this->assertEquals('value', Cache::read('test_groups', 'redis_groups'));
332
 
333
		Cache::increment('group_a', 1, 'redis_helper');
334
		$this->assertFalse(Cache::read('test_groups', 'redis_groups'));
335
		$this->assertTrue(Cache::write('test_groups', 'value2', 'redis_groups'));
336
		$this->assertEquals('value2', Cache::read('test_groups', 'redis_groups'));
337
 
338
		Cache::increment('group_b', 1, 'redis_helper');
339
		$this->assertFalse(Cache::read('test_groups', 'redis_groups'));
340
		$this->assertTrue(Cache::write('test_groups', 'value3', 'redis_groups'));
341
		$this->assertEquals('value3', Cache::read('test_groups', 'redis_groups'));
342
	}
343
 
344
/**
345
 * Tests that deleteing from a groups-enabled config is possible
346
 *
347
 * @return void
348
 */
349
	public function testGroupDelete() {
350
		Cache::config('redis_groups', array(
351
			'engine' => 'Redis',
352
			'duration' => 3600,
353
			'groups' => array('group_a', 'group_b')
354
		));
355
		$this->assertTrue(Cache::write('test_groups', 'value', 'redis_groups'));
356
		$this->assertEquals('value', Cache::read('test_groups', 'redis_groups'));
357
		$this->assertTrue(Cache::delete('test_groups', 'redis_groups'));
358
 
359
		$this->assertFalse(Cache::read('test_groups', 'redis_groups'));
360
	}
361
 
362
/**
363
 * Test clearing a cache group
364
 *
365
 * @return void
366
 */
367
	public function testGroupClear() {
368
		Cache::config('redis_groups', array(
369
			'engine' => 'Redis',
370
			'duration' => 3600,
371
			'groups' => array('group_a', 'group_b')
372
		));
373
 
374
		$this->assertTrue(Cache::write('test_groups', 'value', 'redis_groups'));
375
		$this->assertTrue(Cache::clearGroup('group_a', 'redis_groups'));
376
		$this->assertFalse(Cache::read('test_groups', 'redis_groups'));
377
 
378
		$this->assertTrue(Cache::write('test_groups', 'value2', 'redis_groups'));
379
		$this->assertTrue(Cache::clearGroup('group_b', 'redis_groups'));
380
		$this->assertFalse(Cache::read('test_groups', 'redis_groups'));
381
	}
382
 
383
}