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

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

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

1
<?php
2
/**
3
 * ApcEngineTest 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
 * ApcEngineTest class
23
 *
24
 * @package       Cake.Test.Case.Cache.Engine
25
 */
26
class ApcEngineTest extends CakeTestCase {
27

    
28
/**
29
 * setUp method
30
 *
31
 * @return void
32
 */
33
        public function setUp() {
34
                parent::setUp();
35
                $this->skipIf(!function_exists('apc_store'), 'Apc is not installed or configured properly.');
36

    
37
                if (PHP_SAPI === 'cli') {
38
                        $this->skipIf(!ini_get('apc.enable_cli'), 'APC is not enabled for the CLI.');
39
                }
40

    
41
                $this->_cacheDisable = Configure::read('Cache.disable');
42
                Configure::write('Cache.disable', false);
43
                Cache::config('apc', array('engine' => 'Apc', 'prefix' => 'cake_'));
44
        }
45

    
46
/**
47
 * tearDown method
48
 *
49
 * @return void
50
 */
51
        public function tearDown() {
52
                parent::tearDown();
53
                Configure::write('Cache.disable', $this->_cacheDisable);
54
                Cache::drop('apc');
55
                Cache::drop('apc_groups');
56
                Cache::config('default');
57
        }
58

    
59
/**
60
 * testReadAndWriteCache method
61
 *
62
 * @return void
63
 */
64
        public function testReadAndWriteCache() {
65
                Cache::set(array('duration' => 1), 'apc');
66

    
67
                $result = Cache::read('test', 'apc');
68
                $expecting = '';
69
                $this->assertEquals($expecting, $result);
70

    
71
                $data = 'this is a test of the emergency broadcasting system';
72
                $result = Cache::write('test', $data, 'apc');
73
                $this->assertTrue($result);
74

    
75
                $result = Cache::read('test', 'apc');
76
                $expecting = $data;
77
                $this->assertEquals($expecting, $result);
78

    
79
                Cache::delete('test', 'apc');
80
        }
81

    
82
/**
83
 * Writing cache entries with duration = 0 (forever) should work.
84
 *
85
 * @return void
86
 */
87
        public function testReadWriteDurationZero() {
88
                Cache::config('apc', array('engine' => 'Apc', 'duration' => 0, 'prefix' => 'cake_'));
89
                Cache::write('zero', 'Should save', 'apc');
90
                sleep(1);
91

    
92
                $result = Cache::read('zero', 'apc');
93
                $this->assertEquals('Should save', $result);
94
        }
95

    
96
/**
97
 * testExpiry method
98
 *
99
 * @return void
100
 */
101
        public function testExpiry() {
102
                Cache::set(array('duration' => 1), 'apc');
103

    
104
                $result = Cache::read('test', 'apc');
105
                $this->assertFalse($result);
106

    
107
                $data = 'this is a test of the emergency broadcasting system';
108
                $result = Cache::write('other_test', $data, 'apc');
109
                $this->assertTrue($result);
110

    
111
                sleep(2);
112
                $result = Cache::read('other_test', 'apc');
113
                $this->assertFalse($result);
114

    
115
                Cache::set(array('duration' => 1), 'apc');
116

    
117
                $data = 'this is a test of the emergency broadcasting system';
118
                $result = Cache::write('other_test', $data, 'apc');
119
                $this->assertTrue($result);
120

    
121
                sleep(2);
122
                $result = Cache::read('other_test', 'apc');
123
                $this->assertFalse($result);
124

    
125
                sleep(2);
126
                $result = Cache::read('other_test', 'apc');
127
                $this->assertFalse($result);
128
        }
129

    
130
/**
131
 * testDeleteCache method
132
 *
133
 * @return void
134
 */
135
        public function testDeleteCache() {
136
                $data = 'this is a test of the emergency broadcasting system';
137
                $result = Cache::write('delete_test', $data, 'apc');
138
                $this->assertTrue($result);
139

    
140
                $result = Cache::delete('delete_test', 'apc');
141
                $this->assertTrue($result);
142
        }
143

    
144
/**
145
 * testDecrement method
146
 *
147
 * @return void
148
 */
149
        public function testDecrement() {
150
                $this->skipIf(!function_exists('apc_dec'), 'No apc_dec() function, cannot test decrement().');
151

    
152
                $result = Cache::write('test_decrement', 5, 'apc');
153
                $this->assertTrue($result);
154

    
155
                $result = Cache::decrement('test_decrement', 1, 'apc');
156
                $this->assertEquals(4, $result);
157

    
158
                $result = Cache::read('test_decrement', 'apc');
159
                $this->assertEquals(4, $result);
160

    
161
                $result = Cache::decrement('test_decrement', 2, 'apc');
162
                $this->assertEquals(2, $result);
163

    
164
                $result = Cache::read('test_decrement', 'apc');
165
                $this->assertEquals(2, $result);
166
        }
167

    
168
/**
169
 * testIncrement method
170
 *
171
 * @return void
172
 */
173
        public function testIncrement() {
174
                $this->skipIf(!function_exists('apc_inc'), 'No apc_inc() function, cannot test increment().');
175

    
176
                $result = Cache::write('test_increment', 5, 'apc');
177
                $this->assertTrue($result);
178

    
179
                $result = Cache::increment('test_increment', 1, 'apc');
180
                $this->assertEquals(6, $result);
181

    
182
                $result = Cache::read('test_increment', 'apc');
183
                $this->assertEquals(6, $result);
184

    
185
                $result = Cache::increment('test_increment', 2, 'apc');
186
                $this->assertEquals(8, $result);
187

    
188
                $result = Cache::read('test_increment', 'apc');
189
                $this->assertEquals(8, $result);
190
        }
191

    
192
/**
193
 * test the clearing of cache keys
194
 *
195
 * @return void
196
 */
197
        public function testClear() {
198
                apc_store('not_cake', 'survive');
199
                Cache::write('some_value', 'value', 'apc');
200

    
201
                $result = Cache::clear(false, 'apc');
202
                $this->assertTrue($result);
203
                $this->assertFalse(Cache::read('some_value', 'apc'));
204
                $this->assertEquals('survive', apc_fetch('not_cake'));
205
                apc_delete('not_cake');
206
        }
207

    
208
/**
209
 * Tests that configuring groups for stored keys return the correct values when read/written
210
 * Shows that altering the group value is equivalent to deleting all keys under the same
211
 * group
212
 *
213
 * @return void
214
 */
215
        public function testGroupsReadWrite() {
216
                Cache::config('apc_groups', array(
217
                        'engine' => 'Apc',
218
                        'duration' => 0,
219
                        'groups' => array('group_a', 'group_b'),
220
                        'prefix' => 'test_'
221
                ));
222
                $this->assertTrue(Cache::write('test_groups', 'value', 'apc_groups'));
223
                $this->assertEquals('value', Cache::read('test_groups', 'apc_groups'));
224

    
225
                apc_inc('test_group_a');
226
                $this->assertFalse(Cache::read('test_groups', 'apc_groups'));
227
                $this->assertTrue(Cache::write('test_groups', 'value2', 'apc_groups'));
228
                $this->assertEquals('value2', Cache::read('test_groups', 'apc_groups'));
229

    
230
                apc_inc('test_group_b');
231
                $this->assertFalse(Cache::read('test_groups', 'apc_groups'));
232
                $this->assertTrue(Cache::write('test_groups', 'value3', 'apc_groups'));
233
                $this->assertEquals('value3', Cache::read('test_groups', 'apc_groups'));
234
        }
235

    
236
/**
237
 * Tests that deleteing from a groups-enabled config is possible
238
 *
239
 * @return void
240
 */
241
        public function testGroupDelete() {
242
                Cache::config('apc_groups', array(
243
                        'engine' => 'Apc',
244
                        'duration' => 0,
245
                        'groups' => array('group_a', 'group_b'),
246
                        'prefix' => 'test_'
247
                ));
248
                $this->assertTrue(Cache::write('test_groups', 'value', 'apc_groups'));
249
                $this->assertEquals('value', Cache::read('test_groups', 'apc_groups'));
250
                $this->assertTrue(Cache::delete('test_groups', 'apc_groups'));
251

    
252
                $this->assertFalse(Cache::read('test_groups', 'apc_groups'));
253
        }
254

    
255
/**
256
 * Test clearing a cache group
257
 *
258
 * @return void
259
 */
260
        public function testGroupClear() {
261
                Cache::config('apc_groups', array(
262
                        'engine' => 'Apc',
263
                        'duration' => 0,
264
                        'groups' => array('group_a', 'group_b'),
265
                        'prefix' => 'test_'
266
                ));
267

    
268
                $this->assertTrue(Cache::write('test_groups', 'value', 'apc_groups'));
269
                $this->assertTrue(Cache::clearGroup('group_a', 'apc_groups'));
270
                $this->assertFalse(Cache::read('test_groups', 'apc_groups'));
271

    
272
                $this->assertTrue(Cache::write('test_groups', 'value2', 'apc_groups'));
273
                $this->assertTrue(Cache::clearGroup('group_b', 'apc_groups'));
274
                $this->assertFalse(Cache::read('test_groups', 'apc_groups'));
275
        }
276
}