統計
| ブランチ: | リビジョン:

pictcode / lib / Cake / Test / Case / Cache / Engine / XcacheEngineTest.php @ 635eef61

履歴 | 表示 | アノテート | ダウンロード (7.044 KB)

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