-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCacheBasicMethodsTest.php
More file actions
executable file
·332 lines (309 loc) · 6.79 KB
/
CacheBasicMethodsTest.php
File metadata and controls
executable file
·332 lines (309 loc) · 6.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
<?php
/**
* @package Kohana/Cache
* @group kohana
* @group kohana.cache
* @category Test
* @author Kohana Team
* @copyright (c) 2009-2012 Kohana Team
* @license http://kohanaphp.com/license
*/
abstract class Kohana_CacheBasicMethodsTest extends PHPUnit_Framework_TestCase {
/**
* @var Cache driver for this test
*/
protected $_cache_driver;
/**
* This method MUST be implemented by each driver to setup the `Cache`
* instance for each test.
*
* This method should do the following tasks for each driver test:
*
* - Test the Cache instance driver is available, skip test otherwise
* - Setup the Cache instance
* - Call the parent setup method, `parent::setUp()`
*
* @return void
*/
public function setUp()
{
parent::setUp();
}
/**
* Accessor method to `$_cache_driver`.
*
* @return Cache
* @return self
*/
public function cache(Cache $cache = NULL)
{
if ($cache === NULL)
return $this->_cache_driver;
$this->_cache_driver = $cache;
return $this;
}
/**
* Data provider for test_set_get()
*
* @return array
*/
public function provider_set_get()
{
$object = new StdClass;
$object->foo = 'foo';
$object->bar = 'bar';
$html_text = <<<TESTTEXT
<!doctype html>
<head>
</head>
<body>
</body>
</html>
TESTTEXT;
return array(
array(
array(
'id' => 'string', // Key to set to cache
'value' => 'foobar', // Value to set to key
'ttl' => 0, // Time to live
'wait' => FALSE, // Test wait time to let cache expire
'type' => 'string', // Type test
'default' => NULL // Default value get should return
),
'foobar'
),
array(
array(
'id' => 'integer',
'value' => 101010,
'ttl' => 0,
'wait' => FALSE,
'type' => 'integer',
'default' => NULL
),
101010
),
array(
array(
'id' => 'float',
'value' => 10.00,
'ttl' => 0,
'wait' => FALSE,
'type' => 'float',
'default' => NULL
),
10.00
),
array(
array(
'id' => 'array',
'value' => array(
'key' => 'foo',
'value' => 'bar'
),
'ttl' => 0,
'wait' => FALSE,
'type' => 'array',
'default' => NULL
),
array(
'key' => 'foo',
'value' => 'bar'
)
),
array(
array(
'id' => 'boolean',
'value' => TRUE,
'ttl' => 0,
'wait' => FALSE,
'type' => 'boolean',
'default' => NULL
),
TRUE
),
array(
array(
'id' => 'null',
'value' => NULL,
'ttl' => 0,
'wait' => FALSE,
'type' => 'null',
'default' => NULL
),
NULL
),
array(
array(
'id' => 'object',
'value' => $object,
'ttl' => 0,
'wait' => FALSE,
'type' => 'object',
'default' => NULL
),
$object
),
array(
array(
'id' => 'bar\\ with / troublesome key',
'value' => 'foo bar snafu',
'ttl' => 0,
'wait' => FALSE,
'type' => 'string',
'default' => NULL
),
'foo bar snafu'
),
array(
array(
'id' => 'test ttl 0 means never expire',
'value' => 'cache value that should last',
'ttl' => 0,
'wait' => 1,
'type' => 'string',
'default' => NULL
),
'cache value that should last'
),
array(
array(
'id' => 'bar',
'value' => 'foo',
'ttl' => 3,
'wait' => 5,
'type' => 'null',
'default' => NULL
),
NULL
),
array(
array(
'id' => 'snafu',
'value' => 'fubar',
'ttl' => 3,
'wait' => 5,
'type' => 'string',
'default' => 'something completely different!'
),
'something completely different!'
),
array(
array(
'id' => 'new line test with HTML',
'value' => $html_text,
'ttl' => 10,
'wait' => FALSE,
'type' => 'string',
'default' => NULL,
),
$html_text
),
array(
array(
'id' => 'test with 60*5',
'value' => 'blabla',
'ttl' => 60*5,
'wait' => FALSE,
'type' => 'string',
'default' => NULL,
),
'blabla'
),
array(
array(
'id' => 'test with 60*50',
'value' => 'bla bla',
'ttl' => 60*50,
'wait' => FALSE,
'type' => 'string',
'default' => NULL,
),
'bla bla'
)
);
}
/**
* Tests the [Cache::set()] method, testing;
*
* - The value is cached
* - The lifetime is respected
* - The returned value type is as expected
* - The default not-found value is respected
*
* @dataProvider provider_set_get
*
* @param array data
* @param mixed expected
* @return void
*/
public function test_set_get(array $data, $expected)
{
$cache = $this->cache();
extract($data);
$this->assertTrue($cache->set($id, $value, $ttl));
if ($wait !== FALSE)
{
// Lets let the cache expire
sleep($wait);
}
$result = $cache->get($id, $default);
$this->assertEquals($expected, $result);
$this->assertInternalType($type, $result);
unset($id, $value, $ttl, $wait, $type, $default);
}
/**
* Tests the [Cache::delete()] method, testing;
*
* - The a cached value is deleted from cache
* - The cache returns a TRUE value upon deletion
* - The cache returns a FALSE value if no value exists to delete
*
* @return void
*/
public function test_delete()
{
// Init
$cache = $this->cache();
$cache->delete_all();
// Test deletion if real cached value
if ( ! $cache->set('test_delete_1', 'This should not be here!', 0))
{
$this->fail('Unable to set cache value to delete!');
}
// Test delete returns TRUE and check the value is gone
$this->assertTrue($cache->delete('test_delete_1'));
$this->assertNull($cache->get('test_delete_1'));
// Test non-existant cache value returns FALSE if no error
$this->assertFalse($cache->delete('test_delete_1'));
}
/**
* Tests [Cache::delete_all()] works as specified
*
* @return void
* @uses Kohana_CacheBasicMethodsTest::provider_set_get()
*/
public function test_delete_all()
{
// Init
$cache = $this->cache();
$data = $this->provider_set_get();
foreach ($data as $key => $values)
{
extract($values[0]);
if ( ! $cache->set($id, $value))
{
$this->fail('Unable to set: '.$key.' => '.$value.' to cache');
}
unset($id, $value, $ttl, $wait, $type, $default);
}
// Test delete_all is successful
$this->assertTrue($cache->delete_all());
foreach ($data as $key => $values)
{
// Verify data has been purged
$this->assertSame('Cache Deleted!', $cache->get($values[0]['id'],
'Cache Deleted!'));
}
}
} // End Kohana_CacheBasicMethodsTest