Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
13532 anikendra 1
<?php
2
/**
3
 * MemcacheEngineTest 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.Engine
15
 * @since         CakePHP(tm) v 1.2.0.5434
16
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
17
 */
18
 
19
App::uses('Cache', 'Cache');
20
App::uses('MemcacheEngine', 'Cache/Engine');
21
 
22
/**
23
 * Class TestMemcacheEngine
24
 *
25
 * @package       Cake.Test.Case.Cache.Engine
26
 */
27
class TestMemcacheEngine extends MemcacheEngine {
28
 
29
/**
30
 * public accessor to _parseServerString
31
 *
32
 * @param string $server
33
 * @return array
34
 */
35
	public function parseServerString($server) {
36
		return $this->_parseServerString($server);
37
	}
38
 
39
	public function setMemcache($memcache) {
40
		$this->_Memcache = $memcache;
41
	}
42
 
43
}
44
 
45
/**
46
 * MemcacheEngineTest class
47
 *
48
 * @package       Cake.Test.Case.Cache.Engine
49
 */
50
class MemcacheEngineTest extends CakeTestCase {
51
 
52
/**
53
 * setUp method
54
 *
55
 * @return void
56
 */
57
	public function setUp() {
58
		parent::setUp();
59
		$this->skipIf(!class_exists('Memcache'), 'Memcache is not installed or configured properly.');
60
 
61
		$this->_cacheDisable = Configure::read('Cache.disable');
62
		Configure::write('Cache.disable', false);
63
		Cache::config('memcache', array(
64
			'engine' => 'Memcache',
65
			'prefix' => 'cake_',
66
			'duration' => 3600
67
		));
68
	}
69
 
70
/**
71
 * tearDown method
72
 *
73
 * @return void
74
 */
75
	public function tearDown() {
76
		parent::tearDown();
77
		Configure::write('Cache.disable', $this->_cacheDisable);
78
		Cache::drop('memcache');
79
		Cache::drop('memcache_groups');
80
		Cache::drop('memcache_helper');
81
		Cache::config('default');
82
	}
83
 
84
/**
85
 * testSettings method
86
 *
87
 * @return void
88
 */
89
	public function testSettings() {
90
		$settings = Cache::settings('memcache');
91
		unset($settings['serialize'], $settings['path']);
92
		$expecting = array(
93
			'prefix' => 'cake_',
94
			'duration' => 3600,
95
			'probability' => 100,
96
			'servers' => array('127.0.0.1'),
97
			'persistent' => true,
98
			'compress' => false,
99
			'engine' => 'Memcache',
100
			'groups' => array()
101
		);
102
		$this->assertEquals($expecting, $settings);
103
	}
104
 
105
/**
106
 * testSettings method
107
 *
108
 * @return void
109
 */
110
	public function testMultipleServers() {
111
		$servers = array('127.0.0.1:11211', '127.0.0.1:11222');
112
		$available = true;
113
		$Memcache = new Memcache();
114
 
115
		foreach ($servers as $server) {
116
			list($host, $port) = explode(':', $server);
117
			//@codingStandardsIgnoreStart
118
			if (!@$Memcache->connect($host, $port)) {
119
				$available = false;
120
			}
121
			//@codingStandardsIgnoreEnd
122
		}
123
 
124
		$this->skipIf(!$available, 'Need memcache servers at ' . implode(', ', $servers) . ' to run this test.');
125
 
126
		$Memcache = new MemcacheEngine();
127
		$Memcache->init(array('engine' => 'Memcache', 'servers' => $servers));
128
 
129
		$settings = $Memcache->settings();
130
		$this->assertEquals($settings['servers'], $servers);
131
		Cache::drop('dual_server');
132
	}
133
 
134
/**
135
 * testConnect method
136
 *
137
 * @return void
138
 */
139
	public function testConnect() {
140
		$Memcache = new MemcacheEngine();
141
		$Memcache->init(Cache::settings('memcache'));
142
		$result = $Memcache->connect('127.0.0.1');
143
		$this->assertTrue($result);
144
	}
145
 
146
/**
147
 * test connecting to an ipv6 server.
148
 *
149
 * @return void
150
 */
151
	public function testConnectIpv6() {
152
		$Memcache = new MemcacheEngine();
153
		$result = $Memcache->init(array(
154
			'prefix' => 'cake_',
155
			'duration' => 200,
156
			'engine' => 'Memcache',
157
			'servers' => array(
158
				'[::1]:11211'
159
			)
160
		));
161
		$this->assertTrue($result);
162
	}
163
 
164
/**
165
 * test non latin domains.
166
 *
167
 * @return void
168
 */
169
	public function testParseServerStringNonLatin() {
170
		$Memcache = new TestMemcacheEngine();
171
		$result = $Memcache->parseServerString('schülervz.net:13211');
172
		$this->assertEquals(array('schülervz.net', '13211'), $result);
173
 
174
		$result = $Memcache->parseServerString('sülül:1111');
175
		$this->assertEquals(array('sülül', '1111'), $result);
176
	}
177
 
178
/**
179
 * test unix sockets.
180
 *
181
 * @return void
182
 */
183
	public function testParseServerStringUnix() {
184
		$Memcache = new TestMemcacheEngine();
185
		$result = $Memcache->parseServerString('unix:///path/to/memcached.sock');
186
		$this->assertEquals(array('unix:///path/to/memcached.sock', 0), $result);
187
	}
188
 
189
/**
190
 * testReadAndWriteCache method
191
 *
192
 * @return void
193
 */
194
	public function testReadAndWriteCache() {
195
		Cache::set(array('duration' => 1), null, 'memcache');
196
 
197
		$result = Cache::read('test', 'memcache');
198
		$expecting = '';
199
		$this->assertEquals($expecting, $result);
200
 
201
		$data = 'this is a test of the emergency broadcasting system';
202
		$result = Cache::write('test', $data, 'memcache');
203
		$this->assertTrue($result);
204
 
205
		$result = Cache::read('test', 'memcache');
206
		$expecting = $data;
207
		$this->assertEquals($expecting, $result);
208
 
209
		Cache::delete('test', 'memcache');
210
	}
211
 
212
/**
213
 * testExpiry method
214
 *
215
 * @return void
216
 */
217
	public function testExpiry() {
218
		Cache::set(array('duration' => 1), 'memcache');
219
 
220
		$result = Cache::read('test', 'memcache');
221
		$this->assertFalse($result);
222
 
223
		$data = 'this is a test of the emergency broadcasting system';
224
		$result = Cache::write('other_test', $data, 'memcache');
225
		$this->assertTrue($result);
226
 
227
		sleep(2);
228
		$result = Cache::read('other_test', 'memcache');
229
		$this->assertFalse($result);
230
 
231
		Cache::set(array('duration' => "+1 second"), 'memcache');
232
 
233
		$data = 'this is a test of the emergency broadcasting system';
234
		$result = Cache::write('other_test', $data, 'memcache');
235
		$this->assertTrue($result);
236
 
237
		sleep(3);
238
		$result = Cache::read('other_test', 'memcache');
239
		$this->assertFalse($result);
240
 
241
		Cache::config('memcache', array('duration' => '+1 second'));
242
 
243
		$result = Cache::read('other_test', 'memcache');
244
		$this->assertFalse($result);
245
 
246
		Cache::config('memcache', array('duration' => '+29 days'));
247
		$data = 'this is a test of the emergency broadcasting system';
248
		$result = Cache::write('long_expiry_test', $data, 'memcache');
249
		$this->assertTrue($result);
250
 
251
		sleep(2);
252
		$result = Cache::read('long_expiry_test', 'memcache');
253
		$expecting = $data;
254
		$this->assertEquals($expecting, $result);
255
 
256
		Cache::config('memcache', array('duration' => 3600));
257
	}
258
 
259
/**
260
 * testDeleteCache method
261
 *
262
 * @return void
263
 */
264
	public function testDeleteCache() {
265
		$data = 'this is a test of the emergency broadcasting system';
266
		$result = Cache::write('delete_test', $data, 'memcache');
267
		$this->assertTrue($result);
268
 
269
		$result = Cache::delete('delete_test', 'memcache');
270
		$this->assertTrue($result);
271
	}
272
 
273
/**
274
 * testDecrement method
275
 *
276
 * @return void
277
 */
278
	public function testDecrement() {
279
		$result = Cache::write('test_decrement', 5, 'memcache');
280
		$this->assertTrue($result);
281
 
282
		$result = Cache::decrement('test_decrement', 1, 'memcache');
283
		$this->assertEquals(4, $result);
284
 
285
		$result = Cache::read('test_decrement', 'memcache');
286
		$this->assertEquals(4, $result);
287
 
288
		$result = Cache::decrement('test_decrement', 2, 'memcache');
289
		$this->assertEquals(2, $result);
290
 
291
		$result = Cache::read('test_decrement', 'memcache');
292
		$this->assertEquals(2, $result);
293
	}
294
 
295
/**
296
 * testIncrement method
297
 *
298
 * @return void
299
 */
300
	public function testIncrement() {
301
		$result = Cache::write('test_increment', 5, 'memcache');
302
		$this->assertTrue($result);
303
 
304
		$result = Cache::increment('test_increment', 1, 'memcache');
305
		$this->assertEquals(6, $result);
306
 
307
		$result = Cache::read('test_increment', 'memcache');
308
		$this->assertEquals(6, $result);
309
 
310
		$result = Cache::increment('test_increment', 2, 'memcache');
311
		$this->assertEquals(8, $result);
312
 
313
		$result = Cache::read('test_increment', 'memcache');
314
		$this->assertEquals(8, $result);
315
	}
316
 
317
/**
318
 * test that configurations don't conflict, when a file engine is declared after a memcache one.
319
 *
320
 * @return void
321
 */
322
	public function testConfigurationConflict() {
323
		Cache::config('long_memcache', array(
324
			'engine' => 'Memcache',
325
			'duration' => '+2 seconds',
326
			'servers' => array('127.0.0.1:11211'),
327
		));
328
		Cache::config('short_memcache', array(
329
			'engine' => 'Memcache',
330
			'duration' => '+1 seconds',
331
			'servers' => array('127.0.0.1:11211'),
332
		));
333
		Cache::config('some_file', array('engine' => 'File'));
334
 
335
		$this->assertTrue(Cache::write('duration_test', 'yay', 'long_memcache'));
336
		$this->assertTrue(Cache::write('short_duration_test', 'boo', 'short_memcache'));
337
 
338
		$this->assertEquals('yay', Cache::read('duration_test', 'long_memcache'), 'Value was not read %s');
339
		$this->assertEquals('boo', Cache::read('short_duration_test', 'short_memcache'), 'Value was not read %s');
340
 
341
		sleep(1);
342
		$this->assertEquals('yay', Cache::read('duration_test', 'long_memcache'), 'Value was not read %s');
343
 
344
		sleep(2);
345
		$this->assertFalse(Cache::read('short_duration_test', 'short_memcache'), 'Cache was not invalidated %s');
346
		$this->assertFalse(Cache::read('duration_test', 'long_memcache'), 'Value did not expire %s');
347
 
348
		Cache::delete('duration_test', 'long_memcache');
349
		Cache::delete('short_duration_test', 'short_memcache');
350
	}
351
 
352
/**
353
 * test clearing memcache.
354
 *
355
 * @return void
356
 */
357
	public function testClear() {
358
		Cache::config('memcache2', array(
359
			'engine' => 'Memcache',
360
			'prefix' => 'cake2_',
361
			'duration' => 3600
362
		));
363
 
364
		Cache::write('some_value', 'cache1', 'memcache');
365
		$result = Cache::clear(true, 'memcache');
366
		$this->assertTrue($result);
367
		$this->assertEquals('cache1', Cache::read('some_value', 'memcache'));
368
 
369
		Cache::write('some_value', 'cache2', 'memcache2');
370
		$result = Cache::clear(false, 'memcache');
371
		$this->assertTrue($result);
372
		$this->assertFalse(Cache::read('some_value', 'memcache'));
373
		$this->assertEquals('cache2', Cache::read('some_value', 'memcache2'));
374
 
375
		Cache::clear(false, 'memcache2');
376
	}
377
 
378
/**
379
 * test that a 0 duration can successfully write.
380
 *
381
 * @return void
382
 */
383
	public function testZeroDuration() {
384
		Cache::config('memcache', array('duration' => 0));
385
		$result = Cache::write('test_key', 'written!', 'memcache');
386
 
387
		$this->assertTrue($result);
388
		$result = Cache::read('test_key', 'memcache');
389
		$this->assertEquals('written!', $result);
390
	}
391
 
392
/**
393
 * test that durations greater than 30 days never expire
394
 *
395
 * @return void
396
 */
397
	public function testLongDurationEqualToZero() {
398
		$memcache = new TestMemcacheEngine();
399
		$memcache->settings['compress'] = false;
400
 
401
		$mock = $this->getMock('Memcache');
402
		$memcache->setMemcache($mock);
403
		$mock->expects($this->once())
404
			->method('set')
405
			->with('key', 'value', false, 0);
406
 
407
		$value = 'value';
408
		$memcache->write('key', $value, 50 * DAY);
409
	}
410
 
411
/**
412
 * Tests that configuring groups for stored keys return the correct values when read/written
413
 * Shows that altering the group value is equivalent to deleting all keys under the same
414
 * group
415
 *
416
 * @return void
417
 */
418
	public function testGroupReadWrite() {
419
		Cache::config('memcache_groups', array(
420
			'engine' => 'Memcache',
421
			'duration' => 3600,
422
			'groups' => array('group_a', 'group_b'),
423
			'prefix' => 'test_'
424
		));
425
		Cache::config('memcache_helper', array(
426
			'engine' => 'Memcache',
427
			'duration' => 3600,
428
			'prefix' => 'test_'
429
		));
430
		$this->assertTrue(Cache::write('test_groups', 'value', 'memcache_groups'));
431
		$this->assertEquals('value', Cache::read('test_groups', 'memcache_groups'));
432
 
433
		Cache::increment('group_a', 1, 'memcache_helper');
434
		$this->assertFalse(Cache::read('test_groups', 'memcache_groups'));
435
		$this->assertTrue(Cache::write('test_groups', 'value2', 'memcache_groups'));
436
		$this->assertEquals('value2', Cache::read('test_groups', 'memcache_groups'));
437
 
438
		Cache::increment('group_b', 1, 'memcache_helper');
439
		$this->assertFalse(Cache::read('test_groups', 'memcache_groups'));
440
		$this->assertTrue(Cache::write('test_groups', 'value3', 'memcache_groups'));
441
		$this->assertEquals('value3', Cache::read('test_groups', 'memcache_groups'));
442
	}
443
 
444
/**
445
 * Tests that deleteing from a groups-enabled config is possible
446
 *
447
 * @return void
448
 */
449
	public function testGroupDelete() {
450
		Cache::config('memcache_groups', array(
451
			'engine' => 'Memcache',
452
			'duration' => 3600,
453
			'groups' => array('group_a', 'group_b')
454
		));
455
		$this->assertTrue(Cache::write('test_groups', 'value', 'memcache_groups'));
456
		$this->assertEquals('value', Cache::read('test_groups', 'memcache_groups'));
457
		$this->assertTrue(Cache::delete('test_groups', 'memcache_groups'));
458
 
459
		$this->assertFalse(Cache::read('test_groups', 'memcache_groups'));
460
	}
461
 
462
/**
463
 * Test clearing a cache group
464
 *
465
 * @return void
466
 */
467
	public function testGroupClear() {
468
		Cache::config('memcache_groups', array(
469
			'engine' => 'Memcache',
470
			'duration' => 3600,
471
			'groups' => array('group_a', 'group_b')
472
		));
473
 
474
		$this->assertTrue(Cache::write('test_groups', 'value', 'memcache_groups'));
475
		$this->assertTrue(Cache::clearGroup('group_a', 'memcache_groups'));
476
		$this->assertFalse(Cache::read('test_groups', 'memcache_groups'));
477
 
478
		$this->assertTrue(Cache::write('test_groups', 'value2', 'memcache_groups'));
479
		$this->assertTrue(Cache::clearGroup('group_b', 'memcache_groups'));
480
		$this->assertFalse(Cache::read('test_groups', 'memcache_groups'));
481
	}
482
}