4
* @subpackage UnitTests
10
require_once 'Zend/Cache.php';
11
require_once 'Zend/Cache/Core.php';
12
require_once 'Zend/Cache/Backend/File.php'; // TODO : use only Test backend ?
13
require_once 'Zend/Cache/Backend/Test.php';
18
require_once 'PHPUnit/Framework/TestCase.php';
22
* @subpackage UnitTests
24
class Zend_Cache_CoreTest extends PHPUnit_Framework_TestCase
28
public function setUp()
30
if (!$this->_instance) {
31
$this->_instance = new Zend_Cache_Core(array());
32
$this->_backend = new Zend_Cache_Backend_Test();
33
$this->_instance->setBackend($this->_backend);
37
public function tearDown()
39
unset($this->_instance);
42
public function testConstructorCorrectCall()
44
$test = new Zend_Cache_Core(array('lifetime' => 3600, 'caching' => true));
47
public function testConstructorBadOption()
50
$test = new Zend_Cache_Core(array(0 => 'bar', 'lifetime' => 3600));
51
} catch (Zend_Cache_Exception $e) {
54
$this->fail('Zend_Cache_Exception was expected but not thrown');
57
public function testSetLifeTime()
59
$this->_instance->setLifeTime(3600);
62
public function testSetBackendCorrectCall1()
64
$backend = new Zend_Cache_Backend_File(array());
65
$this->_instance->setBackend($backend);
68
public function testSetBackendCorrectCall2()
70
$backend = new Zend_Cache_Backend_Test(array());
71
$this->_instance->setBackend($backend);
72
$log = $backend->getLastLog();
73
$this->assertEquals('setDirectives', $log['methodName']);
74
$this->assertType('array', $log['args'][0]);
77
public function testSetOptionCorrectCall()
79
$this->_instance->setOption('caching', false);
82
public function testSetOptionBadCall()
85
$this->_instance->setOption(array('lifetime'), 1200);
86
} catch (Zend_Cache_Exception $e) {
89
$this->fail('Zend_Cache_Exception was expected but not thrown');
93
* Unknown options are okay and should be silently ignored. Non-string
94
* options, however, should throw exceptions.
98
public function testSetOptionUnknownOption()
101
$this->_instance->setOption(0, 1200);
102
$this->fail('Zend_Cache_Exception was expected but not thrown');
103
} catch (Zend_Cache_Exception $e) {
107
$this->_instance->setOption('foo', 1200);
108
} catch (Zend_Cache_Exception $e) {
109
$this->fail('Zend_Cache_Exception was thrown but should not have been');
113
public function testSaveCorrectBadCall1()
116
$this->_instance->save('data', 'foo bar');
117
} catch (Zend_Cache_Exception $e) {
120
$this->fail('Zend_Cache_Exception was expected but not thrown');
123
public function testSaveCorrectBadCall2()
126
$this->_instance->save('data', 'foobar', array('tag1', 'foo bar'));
127
} catch (Zend_Cache_Exception $e) {
130
$this->fail('Zend_Cache_Exception was expected but not thrown');
133
public function testSaveCorrectBadCall3()
136
$this->_instance->save(array('data'), 'foobar');
137
} catch (Zend_Cache_Exception $e) {
140
$this->fail('Zend_Cache_Exception was expected but not thrown');
143
public function testSaveWithABadCacheId()
146
$this->_instance->save(array('data'), true);
147
} catch (Zend_Cache_Exception $e) {
150
$this->fail('Zend_Cache_Exception was expected but not thrown');
153
public function testSaveWithABadCacheId2()
156
$this->_instance->save(array('data'), 'internal_foo');
157
} catch (Zend_Cache_Exception $e) {
160
$this->fail('Zend_Cache_Exception was expected but not thrown');
163
public function testSaveWithABadTags()
166
$this->_instance->save(array('data'), 'foo', 'foobar');
167
} catch (Zend_Cache_Exception $e) {
170
$this->fail('Zend_Cache_Exception was expected but not thrown');
173
public function testSaveCorrectCallNoCaching()
175
$i1 = $this->_backend->getLogIndex();
176
$this->_instance->setOption('caching', false);
177
$res = $this->_instance->save('data', 'foo');
178
$i2 = $this->_backend->getLogIndex();
179
$this->assertTrue($res);
180
$this->assertEquals($i1, $i2);
183
public function testSaveCorrectCallNoWriteControl()
185
$this->_instance->setOption('write_control', false);
186
$res = $this->_instance->save('data', 'foo', array('tag1', 'tag2'));
187
$log = $this->_backend->getLastLog();
189
'methodName' => 'save',
199
$this->assertEquals($expected, $log);
202
public function testSaveCorrectCall()
204
$res = $this->_instance->save('data', 'foo', array('tag1', 'tag2'));
205
$logs = $this->_backend->getAllLogs();
207
'methodName' => 'save',
218
'methodName' => 'get',
225
'methodName' => 'remove',
230
$this->assertFalse($res);
231
$this->assertEquals($expected1, $logs[count($logs) - 3]);
232
$this->assertEquals($expected2, $logs[count($logs) - 2]);
233
$this->assertEquals($expected3, $logs[count($logs) - 1]);
236
public function testSaveCorrectCallButFileCorruption()
238
$res = $this->_instance->save('data', 'false', array('tag1', 'tag2'));
239
$logs = $this->_backend->getAllLogs();
241
'methodName' => 'save',
252
'methodName' => 'remove',
257
$this->assertFalse($res);
258
$this->assertEquals($expected1, $logs[count($logs) - 2]);
259
$this->assertEquals($expected2, $logs[count($logs) - 1]);
262
public function testSaveCorrectCallWithAutomaticCleaning()
264
$this->_instance->setOption('automatic_cleaning_factor', 1);
265
$res = $this->_instance->save('data', 'false', array('tag1', 'tag2'));
266
$logs = $this->_backend->getAllLogs();
268
'methodName' => 'clean',
274
$this->assertFalse($res);
275
$this->assertEquals($expected, $logs[count($logs) - 3]);
278
public function testTestCorrectCallNoCaching()
280
$i1 = $this->_backend->getLogIndex();
281
$this->_instance->setOption('caching', false);
282
$res = $this->_instance->test('foo');
283
$i2 = $this->_backend->getLogIndex();
284
$this->assertFalse($res);
285
$this->assertEquals($i1, $i2);
288
public function testTestBadCall()
291
$this->_instance->test('foo bar');
292
} catch (Zend_Cache_Exception $e) {
295
$this->fail('Zend_Cache_Exception was expected but not thrown');
298
public function testTestCorrectCall1()
300
$res = $this->_instance->test('foo');
301
$log = $this->_backend->getLastLog();
303
'methodName' => 'test',
308
$this->assertEquals(123456, $res);
309
$this->assertEquals($expected, $log);
312
public function testTestCorrectCall2()
314
$res = $this->_instance->test('false');
315
$this->assertFalse($res);
318
public function testGetCorrectCallNoCaching()
320
$i1 = $this->_backend->getLogIndex();
321
$this->_instance->setOption('caching', false);
322
$res = $this->_instance->load('foo');
323
$i2 = $this->_backend->getLogIndex();
324
$this->assertFalse($res);
325
$this->assertEquals($i1, $i2);
328
public function testGetBadCall()
331
$res = $this->_instance->load('foo bar');
332
} catch (Zend_Cache_Exception $e) {
335
$this->fail('Zend_Cache_Exception was expected but not thrown');
338
public function testGetCorrectCall1()
340
$res = $this->_instance->load('false');
341
$this->assertFalse($res);
344
public function testGetCorrectCall2()
346
$res = $this->_instance->load('bar');
347
$this->assertEquals('foo', 'foo');
350
public function testGetCorrectCallWithAutomaticSerialization()
352
$this->_instance->setOption('automatic_serialization', true);
353
$res = $this->_instance->load('serialized');
354
$this->assertEquals(array('foo'), $res);
357
public function testRemoveBadCall()
360
$res = $this->_instance->remove('foo bar');
361
} catch (Zend_Cache_Exception $e) {
364
$this->fail('Zend_Cache_Exception was expected but not thrown');
367
public function testRemoveCorrectCallNoCaching()
369
$i1 = $this->_backend->getLogIndex();
370
$this->_instance->setOption('caching', false);
371
$res = $this->_instance->remove('foo');
372
$i2 = $this->_backend->getLogIndex();
373
$this->assertTrue($res);
374
$this->assertEquals($i1, $i2);
377
public function testRemoveCorrectCall()
379
$res = $this->_instance->remove('foo');
380
$log = $this->_backend->getLastLog();
382
'methodName' => 'remove',
387
$this->assertTrue($res);
388
$this->assertEquals($expected, $log);
391
public function testCleanBadCall1()
394
$res = $this->_instance->clean('matchingTag', array('foo bar', 'foo'));
395
} catch (Zend_Cache_Exception $e) {
398
$this->fail('Zend_Cache_Exception was expected but not thrown');
401
public function testCleanBadCall2()
404
$res = $this->_instance->clean('foo');
405
} catch (Zend_Cache_Exception $e) {
408
$this->fail('Zend_Cache_Exception was expected but not thrown');
411
public function testCleanCorrectCallNoCaching()
413
$i1 = $this->_backend->getLogIndex();
414
$this->_instance->setOption('caching', false);
415
$res = $this->_instance->clean('all');
416
$i2 = $this->_backend->getLogIndex();
417
$this->assertTrue($res);
418
$this->assertEquals($i1, $i2);
421
public function testCleanCorrectCall()
423
$res = $this->_instance->clean('matchingTag', array('tag1', 'tag2'));
424
$log = $this->_backend->getLastLog();
426
'methodName' => 'clean',
435
$this->assertTrue($res);
436
$this->assertEquals($expected, $log);