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
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Cache
* @subpackage UnitTests
* @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id: CommonBackendTest.php 23775 2011-03-01 17:25:24Z ralph $
*/
// backward compatibility (https://stackoverflow.com/a/42828632/187780)
if (!class_exists('\PHPUnit\Framework\TestCase') && class_exists('\PHPUnit_Framework_TestCase')) {
class_alias('\PHPUnit_Framework_TestCase', '\PHPUnit\Framework\TestCase');
}
/**
* @category Zend
* @package Zend_Cache
* @subpackage UnitTests
* @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @group Zend_Cache
*/
abstract class Zend_Cache_CommonBackendTest extends \PHPUnit\Framework\TestCase {
protected $_instance;
protected $_className;
protected $_root;
public function __construct($name = null, array $data = array(), $dataName = '')
{
$this->_className = $name;
$this->_root = dirname(__FILE__);
date_default_timezone_set('UTC');
parent::__construct($name, $data, $dataName);
}
public function setUp($notag = false)
{
$this->mkdir();
$this->_instance->setDirectives(array('logging' => true));
if ($notag) {
$this->_instance->save('bar : data to cache', 'bar');
$this->_instance->save('bar2 : data to cache', 'bar2');
$this->_instance->save('bar3 : data to cache', 'bar3');
} else {
$this->_instance->save('bar : data to cache', 'bar', array('tag3', 'tag4'));
$this->_instance->save('bar2 : data to cache', 'bar2', array('tag3', 'tag1'));
$this->_instance->save('bar3 : data to cache', 'bar3', array('tag2', 'tag3'));
}
}
public function mkdir()
{
@mkdir($this->getTmpDir());
}
public function rmdir()
{
$tmpDir = $this->getTmpDir(false);
foreach (glob("$tmpDir*") as $dirname) {
@rmdir($dirname);
}
}
public function getTmpDir($date = true)
{
$suffix = '';
if ($date) {
$suffix = date('mdyHis');
}
if (is_writeable($this->_root)) {
return $this->_root . DIRECTORY_SEPARATOR . 'zend_cache_tmp_dir_' . $suffix;
} else if (getenv('TMPDIR')){
return getenv('TMPDIR') . DIRECTORY_SEPARATOR . 'zend_cache_tmp_dir_' . $suffix;
} else {
die("no writable tmpdir found");
}
}
public function tearDown()
{
if ($this->_instance) {
$this->_instance->clean();
}
$this->rmdir();
}
public function testConstructorCorrectCall()
{
$this->fail('PLEASE IMPLEMENT A testConstructorCorrectCall !!!');
}
public function testConstructorBadOption()
{
try {
$class = $this->_className;
$test = new $class(array(1 => 'bar'));
} catch (Zend_Cache_Exception $e) {
return;
}
$this->fail('Zend_Cache_Exception was expected but not thrown');
}
public function testSetDirectivesCorrectCall()
{
$this->_instance->setDirectives(array('lifetime' => 3600, 'logging' => true));
}
public function testSetDirectivesBadArgument()
{
try {
$this->_instance->setDirectives('foo');
} catch (Zend_Cache_Exception $e) {
return;
}
$this->fail('Zend_Cache_Exception was expected but not thrown');
}
public function testSetDirectivesBadDirective()
{
// A bad directive (not known by a specific backend) is possible
// => so no exception here
$this->_instance->setDirectives(array('foo' => true, 'lifetime' => 3600));
}
public function testSetDirectivesBadDirective2()
{
try {
$this->_instance->setDirectives(array('foo' => true, 12 => 3600));
} catch (Zend_Cache_Exception $e) {
return;
}
$this->fail('Zend_Cache_Exception was expected but not thrown');
}
public function testSaveCorrectCall()
{
$res = $this->_instance->save('data to cache', 'foo', array('tag1', 'tag2'));
$this->assertTrue($res);
}
public function testSaveWithNullLifeTime()
{
$this->_instance->setDirectives(array('lifetime' => null));
$res = $this->_instance->save('data to cache', 'foo', array('tag1', 'tag2'));
$this->assertTrue($res);
}
public function testSaveWithSpecificLifeTime()
{
$this->_instance->setDirectives(array('lifetime' => 3600));
$res = $this->_instance->save('data to cache', 'foo', array('tag1', 'tag2'), 10);
$this->assertTrue($res);
}
public function testRemoveCorrectCall()
{
$this->assertTrue($this->_instance->remove('bar'));
$this->assertFalse($this->_instance->test('bar'));
$this->assertFalse($this->_instance->remove('barbar'));
$this->assertFalse($this->_instance->test('barbar'));
}
public function testTestWithAnExistingCacheId()
{
$res = $this->_instance->test('bar');
if (!$res) {
$this->fail('test() return false');
}
if (!($res > 999999)) {
$this->fail('test() return an incorrect integer');
}
return;
}
public function testTestWithANonExistingCacheId()
{
$this->assertFalse($this->_instance->test('barbar'));
}
public function testTestWithAnExistingCacheIdAndANullLifeTime()
{
$this->_instance->setDirectives(array('lifetime' => null));
$res = $this->_instance->test('bar');
if (!$res) {
$this->fail('test() return false');
}
if (!($res > 999999)) {
$this->fail('test() return an incorrect integer');
}
return;
}
public function testGetWithANonExistingCacheId()
{
$this->assertFalse($this->_instance->load('barbar'));
}
public function testGetWithAnExistingCacheId()
{
$this->assertEquals('bar : data to cache', $this->_instance->load('bar'));
}
public function testGetWithAnExistingCacheIdAndUTFCharacters()
{
$data = '"""""' . "'" . '\n' . 'ééééé';
$this->_instance->save($data, 'foo');
$this->assertEquals($data, $this->_instance->load('foo'));
}
public function testGetWithAnExpiredCacheId()
{
$this->_instance->___expire('bar');
$this->_instance->setDirectives(array('lifetime' => -1));
$this->assertFalse($this->_instance->load('bar'));
$this->assertEquals('bar : data to cache', $this->_instance->load('bar', true));
}
public function testCleanModeAll()
{
$this->assertTrue($this->_instance->clean('all'));
$this->assertFalse($this->_instance->test('bar'));
$this->assertFalse($this->_instance->test('bar2'));
}
public function testCleanModeOld()
{
$this->_instance->___expire('bar2');
$this->assertTrue($this->_instance->clean('old'));
$this->assertTrue($this->_instance->test('bar') > 999999);
$this->assertFalse($this->_instance->test('bar2'));
}
public function testCleanModeMatchingTags()
{
$this->assertTrue($this->_instance->clean('matchingTag', array('tag3')));
$this->assertFalse($this->_instance->test('bar'));
$this->assertFalse($this->_instance->test('bar2'));
}
public function testCleanModeMatchingTags2()
{
$this->assertTrue($this->_instance->clean('matchingTag', array('tag3', 'tag4')));
$this->assertFalse($this->_instance->test('bar'));
$this->assertTrue($this->_instance->test('bar2') > 999999);
}
public function testCleanModeNotMatchingTags()
{
$this->assertTrue($this->_instance->clean('notMatchingTag', array('tag3')));
$this->assertTrue($this->_instance->test('bar') > 999999);
$this->assertTrue($this->_instance->test('bar2') > 999999);
}
public function testCleanModeNotMatchingTags2()
{
$this->assertTrue($this->_instance->clean('notMatchingTag', array('tag4')));
$this->assertTrue($this->_instance->test('bar') > 999999);
$this->assertFalse($this->_instance->test('bar2'));
}
public function testCleanModeNotMatchingTags3()
{
$this->assertTrue($this->_instance->clean('notMatchingTag', array('tag4', 'tag1')));
$this->assertTrue($this->_instance->test('bar') > 999999);
$this->assertTrue($this->_instance->test('bar2') > 999999);
$this->assertFalse($this->_instance->test('bar3'));
}
}