1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Test - Cache class |
4
|
|
|
* @package fwolflib |
5
|
|
|
* @subpackage class-test |
6
|
|
|
* @copyright Copyright 2010, Fwolf |
7
|
|
|
* @author Fwolf <[email protected]> |
8
|
|
|
* @since 2010-01-07 |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
// Define like this, so test can run both under eclipse and web alone. |
12
|
|
|
// {{{ |
13
|
|
|
if (! defined('SIMPLE_TEST')) { |
14
|
|
|
define('SIMPLE_TEST', 'simpletest/'); |
15
|
|
|
require_once(SIMPLE_TEST . 'autorun.php'); |
16
|
|
|
} |
17
|
|
|
// Then set output encoding |
18
|
|
|
//header('Content-Type: text/html; charset=utf-8'); |
19
|
|
|
// }}} |
20
|
|
|
|
21
|
|
|
// Require library define file which need test |
22
|
|
|
require_once(dirname(__FILE__) . '/fwolflib.php'); |
23
|
|
|
require_once(FWOLFLIB . 'class/cache.php'); |
24
|
|
|
require_once(FWOLFLIB . 'func/ecl.php'); |
25
|
|
|
require_once(FWOLFLIB . 'func/request.php'); |
26
|
|
|
require_once(FWOLFLIB . 'func/string.php'); |
27
|
|
|
|
28
|
|
|
class TestCache extends UnitTestCase { |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Cache object |
32
|
|
|
* @var object |
33
|
|
|
*/ |
34
|
|
|
protected $oCh = null; |
35
|
|
|
|
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Constructor |
39
|
|
|
*/ |
40
|
|
|
public function __construct () { |
41
|
|
|
$ar_cfg = array( |
42
|
|
|
'cache-file-dir' => '/tmp/cache/', |
43
|
|
|
'cache-file-rule' => '1142', |
44
|
|
|
); |
45
|
|
|
$this->oCh = new CacheTest($ar_cfg); |
46
|
|
|
} // end of func __construct |
47
|
|
|
|
48
|
|
|
|
49
|
|
|
function TestCacheFilePath () { |
|
|
|
|
50
|
|
|
$this->oCh->SetCfg(array( |
51
|
|
|
'cache-type' => 'file', |
52
|
|
|
'cache-file-dir' => '/tmp/cache/', |
53
|
|
|
'cache-file-rule' => '1140', |
54
|
|
|
)); |
55
|
|
|
$key = 'site/index'; |
56
|
|
|
|
57
|
|
|
//Ecl(md5($key)); |
58
|
|
|
|
59
|
|
|
$x = '/tmp/cache/d0/ex/3ed0dc6e'; |
60
|
|
|
$y = $this->oCh->CacheFilePath($key); |
61
|
|
|
$this->assertEqual($x, $y); |
62
|
|
|
|
63
|
|
|
$this->oCh->SetCfg(array('cache-file-rule' => '1131')); |
64
|
|
|
$x = '/tmp/cache/d0/te/3ed0dc6e'; |
65
|
|
|
$y = $this->oCh->CacheFilePath($key); |
66
|
|
|
$this->assertEqual($x, $y); |
67
|
|
|
|
68
|
|
|
// Notice: Directly use key's part as path may cause wrong |
69
|
|
|
$this->oCh->SetCfg(array('cache-file-rule' => '2342')); |
70
|
|
|
$x = '/tmp/cache/57//i/3ed0dc6e'; |
71
|
|
|
$y = $this->oCh->CacheFilePath($key); |
72
|
|
|
$this->assertEqual($x, $y); |
73
|
|
|
|
74
|
|
|
// Common usage |
75
|
|
|
$this->oCh->SetCfg(array('cache-file-rule' => '1011')); |
76
|
|
|
$x = '/tmp/cache/3e/d0/3ed0dc6e'; |
77
|
|
|
$y = $this->oCh->CacheFilePath($key); |
78
|
|
|
$this->assertEqual($x, $y); |
79
|
|
|
|
80
|
|
|
// Common usage 2 |
81
|
|
|
$this->oCh->SetCfg(array('cache-file-rule' => '2021')); |
82
|
|
|
$x = '/tmp/cache/b6/9c/3ed0dc6e'; |
83
|
|
|
$y = $this->oCh->CacheFilePath($key); |
84
|
|
|
$this->assertEqual($x, $y); |
85
|
|
|
|
86
|
|
|
// Common usage 3 |
87
|
|
|
$this->oCh->SetCfg(array('cache-file-rule' => '55')); |
88
|
|
|
$x = '/tmp/cache/89/3ed0dc6e'; |
89
|
|
|
$y = $this->oCh->CacheFilePath($key); |
90
|
|
|
$this->assertEqual($x, $y); |
91
|
|
|
|
92
|
|
|
//Ecl($y); |
93
|
|
|
|
94
|
|
|
// Read/write |
95
|
|
|
$v = $this->oCh->CacheGet($key, 1); |
96
|
|
|
var_dump($v); |
|
|
|
|
97
|
|
|
Ecl(hash('crc32', $key) |
|
|
|
|
98
|
|
|
. '|' |
99
|
|
|
. $this->oCh->CacheFilePath($key)); |
100
|
|
|
} // end of func TestCacheFilePath |
101
|
|
|
|
102
|
|
|
|
103
|
|
|
} // end of class TestCache |
104
|
|
|
|
105
|
|
|
|
106
|
|
|
class CacheTest extends Cache { |
|
|
|
|
107
|
|
|
protected function CacheGenVal ($key) { |
108
|
|
|
$this->sDummy = RandomString(30, 'a0'); |
|
|
|
|
109
|
|
|
return $this; |
|
|
|
|
110
|
|
|
} // end of func CacheGenVal |
111
|
|
|
|
112
|
|
|
public function CacheLifetime ($key) { |
113
|
|
|
return 0; |
114
|
|
|
} // end of func CacheLifetime |
115
|
|
|
} // end of class CacheTest |
116
|
|
|
|
117
|
|
|
|
118
|
|
|
// Change output charset in this way. |
119
|
|
|
// {{{ |
120
|
|
|
$s_url = GetSelfUrl(false); |
|
|
|
|
121
|
|
|
$s_url = substr($s_url, strrpos($s_url, '/') + 1); |
122
|
|
|
if ('cache.test.php' == $s_url) { |
123
|
|
|
$test = new TestCache(); |
124
|
|
|
$test->run(new HtmlReporter('utf-8')); |
125
|
|
|
} |
126
|
|
|
// }}} |
127
|
|
|
?> |
|
|
|
|
128
|
|
|
|
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.