My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
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
<?php
/**
* Phpwell Google Trends Trendscraper Test
*/

/**
* Imports
*/
$phpwell_dir = dirname(dirname(dirname(__FILE__)));
require_once(sprintf('%s/%s', $phpwell_dir, 'import.php'));
import_well('test/functional');
import_well('Cache_Lite/Lite.php');


/**
* Test Class
*/
class PearCacheLiteCase extends PhpwellFunctionalTestCase {

var $Cache = NULL;
var $ConfigOptions = array();
var $ErrorList = array();

function startAll() {
}

function endAll() {
}

function startUp() {
$this->ConfigOptions = array(
'cacheDir' => '/tmp/',
'lifeTime' => 30
);
$this->Cache = new Cache_Lite($this->ConfigOptions);
}

function tearDown() {
$this->Cache->clean();
$this->Cache = NULL;
}


/**
* Tests
*/
function testInstance() {
$this->assert($this->is_instance($this->Cache, 'Cache_Lite'));
}

function testConfiguration() {
$this->assert($this->Cache->_cacheDir === $this->ConfigOptions['cacheDir']);
$this->assert($this->Cache->_lifeTime === $this->ConfigOptions['lifeTime']);
$this->log($this->Cache->_cacheDir);
}

function testSetOptions() {
$this->Cache->setOption('cacheDir', dirname(__FILE__));
$this->Cache->setOption('lifeTime', NULL);
$this->Cache->setOption('caching', FALSE);

$this->assert($this->Cache->_cacheDir === dirname(__FILE__));
$this->assert($this->Cache->_lifeTime === NULL);
$this->assert($this->Cache->_caching === FALSE);
}

function testCacheDebug() {
$this->Cache->setToDebug();
$this->assertEqual($this->Cache->_pearErrorMode, CACHE_LITE_ERROR_DIE);
}

function testCache() {
$payload = 'a test cache';
$cache_id = __FUNCTION__;

# save cache
$cached = $this->Cache->save($payload, $cache_id);
$this->assertTrue($cached);

# retrieve cache
$retrieved = $this->Cache->get($cache_id);
$this->assertEqual($retrieved, $payload);

# clean cache
$removed = $this->Cache->remove($cache_id);
$retrieved = $this->Cache->get($cache_id);
$this->assert(! $retrieved);
}

function testCacheSerialization() {
$cache_id = __FUNCTION__;

# create an object object
$AnObject = new stdClass();
$AnObject->foo = 'some value';
$AnObject->Cacher = new Cache_Lite();
$payload = $AnObject;

$this->Cache->setOption('automaticSerialization', TRUE);
$cached = $this->Cache->save($payload, $cache_id);
$this->assertTrue($cached);

$retrieved = $this->Cache->get($cache_id);
$this->assertEqual($retrieved, $payload);
$this->assertEqual($retrieved->foo, $payload->foo);
$this->assertEqual($retrieved->Cacher, $payload->Cacher);
$this->assertTrue($this->is_instance($retrieved->Cacher, 'Cache_Lite'));
}

function testWikiExample() {
# import
#require_once "Cache/Lite.php";

# config and instantiate
$cache_config = array(
'cacheDir' => '/tmp/',
'lifeTime' => 300,
'automaticCleaningFactor' => 200
);
$Cache = new Cache_Lite($cache_config);

# change a config
$Cache->setOption('automaticSerialization', TRUE);

# get a config (workaround)
$auto_serialize = $Cache->_automaticSerialization;
assert($auto_serialize);

# save cache
$cache_id = 'your cache key';
$payload = 'a string or, with automaticSerialization enabled, mixed value';
$cached = $Cache->save($payload, $cache_id);
assert($cached);

# retrieve cache
$retrieved = $Cache->get($cache_id);
assert($retrieved == $payload);

# clean cache
$removed = $Cache->remove($cache_id);
$retrieved = $Cache->get($cache_id);
assert(! $retrieved);
}
}

$Test = &new PearCacheLiteCase();
$Test->run();
print $Test->report();

Change log

r263 by klenwell on Aug 8, 2010   Diff
T650: added pear lite cache with test
Go to: 
Project members, sign in to write a code review

Older revisions

All revisions of this file

File info

Size: 4129 bytes, 148 lines
Powered by Google Project Hosting