Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
13532 anikendra 1
<?php
2
/**
3
 * WincacheEngineTest 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
 
21
/**
22
 * WincacheEngineTest class
23
 *
24
 * @package       Cake.Test.Case.Cache.Engine
25
 */
26
class WincacheEngineTest extends CakeTestCase {
27
 
28
/**
29
 * setUp method
30
 *
31
 * @return void
32
 */
33
	public function setUp() {
34
		parent::setUp();
35
		$this->skipIf(!function_exists('wincache_ucache_set'), 'Wincache is not installed or configured properly.');
36
		$this->_cacheDisable = Configure::read('Cache.disable');
37
		Configure::write('Cache.disable', false);
38
		Cache::config('wincache', array('engine' => 'Wincache', 'prefix' => 'cake_'));
39
	}
40
 
41
/**
42
 * tearDown method
43
 *
44
 * @return void
45
 */
46
	public function tearDown() {
47
		parent::tearDown();
48
		Configure::write('Cache.disable', $this->_cacheDisable);
49
		Cache::drop('wincache');
50
		Cache::drop('wincache_groups');
51
		Cache::config('default');
52
	}
53
 
54
/**
55
 * testReadAndWriteCache method
56
 *
57
 * @return void
58
 */
59
	public function testReadAndWriteCache() {
60
		Cache::set(array('duration' => 1), 'wincache');
61
 
62
		$result = Cache::read('test', 'wincache');
63
		$expecting = '';
64
		$this->assertEquals($expecting, $result);
65
 
66
		$data = 'this is a test of the emergency broadcasting system';
67
		$result = Cache::write('test', $data, 'wincache');
68
		$this->assertTrue($result);
69
 
70
		$result = Cache::read('test', 'wincache');
71
		$expecting = $data;
72
		$this->assertEquals($expecting, $result);
73
 
74
		Cache::delete('test', 'wincache');
75
	}
76
 
77
/**
78
 * testExpiry method
79
 *
80
 * @return void
81
 */
82
	public function testExpiry() {
83
		Cache::set(array('duration' => 1), 'wincache');
84
 
85
		$result = Cache::read('test', 'wincache');
86
		$this->assertFalse($result);
87
 
88
		$data = 'this is a test of the emergency broadcasting system';
89
		$result = Cache::write('other_test', $data, 'wincache');
90
		$this->assertTrue($result);
91
 
92
		sleep(2);
93
		$result = Cache::read('other_test', 'wincache');
94
		$this->assertFalse($result);
95
 
96
		Cache::set(array('duration' => 1), 'wincache');
97
 
98
		$data = 'this is a test of the emergency broadcasting system';
99
		$result = Cache::write('other_test', $data, 'wincache');
100
		$this->assertTrue($result);
101
 
102
		sleep(2);
103
		$result = Cache::read('other_test', 'wincache');
104
		$this->assertFalse($result);
105
 
106
		sleep(2);
107
		$result = Cache::read('other_test', 'wincache');
108
		$this->assertFalse($result);
109
	}
110
 
111
/**
112
 * testDeleteCache method
113
 *
114
 * @return void
115
 */
116
	public function testDeleteCache() {
117
		$data = 'this is a test of the emergency broadcasting system';
118
		$result = Cache::write('delete_test', $data, 'wincache');
119
		$this->assertTrue($result);
120
 
121
		$result = Cache::delete('delete_test', 'wincache');
122
		$this->assertTrue($result);
123
	}
124
 
125
/**
126
 * testDecrement method
127
 *
128
 * @return void
129
 */
130
	public function testDecrement() {
131
		$this->skipIf(
132
			!function_exists('wincache_ucache_dec'),
133
			'No wincache_ucache_dec() function, cannot test decrement().'
134
		);
135
 
136
		$result = Cache::write('test_decrement', 5, 'wincache');
137
		$this->assertTrue($result);
138
 
139
		$result = Cache::decrement('test_decrement', 1, 'wincache');
140
		$this->assertEquals(4, $result);
141
 
142
		$result = Cache::read('test_decrement', 'wincache');
143
		$this->assertEquals(4, $result);
144
 
145
		$result = Cache::decrement('test_decrement', 2, 'wincache');
146
		$this->assertEquals(2, $result);
147
 
148
		$result = Cache::read('test_decrement', 'wincache');
149
		$this->assertEquals(2, $result);
150
	}
151
 
152
/**
153
 * testIncrement method
154
 *
155
 * @return void
156
 */
157
	public function testIncrement() {
158
		$this->skipIf(
159
			!function_exists('wincache_ucache_inc'),
160
			'No wincache_inc() function, cannot test increment().'
161
		);
162
 
163
		$result = Cache::write('test_increment', 5, 'wincache');
164
		$this->assertTrue($result);
165
 
166
		$result = Cache::increment('test_increment', 1, 'wincache');
167
		$this->assertEquals(6, $result);
168
 
169
		$result = Cache::read('test_increment', 'wincache');
170
		$this->assertEquals(6, $result);
171
 
172
		$result = Cache::increment('test_increment', 2, 'wincache');
173
		$this->assertEquals(8, $result);
174
 
175
		$result = Cache::read('test_increment', 'wincache');
176
		$this->assertEquals(8, $result);
177
	}
178
 
179
/**
180
 * test the clearing of cache keys
181
 *
182
 * @return void
183
 */
184
	public function testClear() {
185
		wincache_ucache_set('not_cake', 'safe');
186
		Cache::write('some_value', 'value', 'wincache');
187
 
188
		$result = Cache::clear(false, 'wincache');
189
		$this->assertTrue($result);
190
		$this->assertFalse(Cache::read('some_value', 'wincache'));
191
		$this->assertEquals('safe', wincache_ucache_get('not_cake'));
192
	}
193
 
194
/**
195
 * Tests that configuring groups for stored keys return the correct values when read/written
196
 * Shows that altering the group value is equivalent to deleting all keys under the same
197
 * group
198
 *
199
 * @return void
200
 */
201
	public function testGroupsReadWrite() {
202
		Cache::config('wincache_groups', array(
203
			'engine' => 'Wincache',
204
			'duration' => 0,
205
			'groups' => array('group_a', 'group_b'),
206
			'prefix' => 'test_'
207
		));
208
		$this->assertTrue(Cache::write('test_groups', 'value', 'wincache_groups'));
209
		$this->assertEquals('value', Cache::read('test_groups', 'wincache_groups'));
210
 
211
		wincache_ucache_inc('test_group_a');
212
		$this->assertFalse(Cache::read('test_groups', 'wincache_groups'));
213
		$this->assertTrue(Cache::write('test_groups', 'value2', 'wincache_groups'));
214
		$this->assertEquals('value2', Cache::read('test_groups', 'wincache_groups'));
215
 
216
		wincache_ucache_inc('test_group_b');
217
		$this->assertFalse(Cache::read('test_groups', 'wincache_groups'));
218
		$this->assertTrue(Cache::write('test_groups', 'value3', 'wincache_groups'));
219
		$this->assertEquals('value3', Cache::read('test_groups', 'wincache_groups'));
220
	}
221
 
222
/**
223
 * Tests that deleteing from a groups-enabled config is possible
224
 *
225
 * @return void
226
 */
227
	public function testGroupDelete() {
228
		Cache::config('wincache_groups', array(
229
			'engine' => 'Wincache',
230
			'duration' => 0,
231
			'groups' => array('group_a', 'group_b'),
232
			'prefix' => 'test_'
233
		));
234
		$this->assertTrue(Cache::write('test_groups', 'value', 'wincache_groups'));
235
		$this->assertEquals('value', Cache::read('test_groups', 'wincache_groups'));
236
		$this->assertTrue(Cache::delete('test_groups', 'wincache_groups'));
237
 
238
		$this->assertFalse(Cache::read('test_groups', 'wincache_groups'));
239
	}
240
 
241
/**
242
 * Test clearing a cache group
243
 *
244
 * @return void
245
 */
246
	public function testGroupClear() {
247
		Cache::config('wincache_groups', array(
248
			'engine' => 'Wincache',
249
			'duration' => 0,
250
			'groups' => array('group_a', 'group_b'),
251
			'prefix' => 'test_'
252
		));
253
 
254
		$this->assertTrue(Cache::write('test_groups', 'value', 'wincache_groups'));
255
		$this->assertTrue(Cache::clearGroup('group_a', 'wincache_groups'));
256
		$this->assertFalse(Cache::read('test_groups', 'wincache_groups'));
257
 
258
		$this->assertTrue(Cache::write('test_groups', 'value2', 'wincache_groups'));
259
		$this->assertTrue(Cache::clearGroup('group_b', 'wincache_groups'));
260
		$this->assertFalse(Cache::read('test_groups', 'wincache_groups'));
261
	}
262
}