This repository has been archived by the owner on Dec 19, 2023. It is now read-only.
/
SagFileCacheTest.php
135 lines (108 loc) · 3.74 KB
/
SagFileCacheTest.php
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
<?php
/*
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
require_once('SagFileCache.php');
class SagFileTest extends PHPUnit_Framework_TestCase
{
protected $cache;
private $FILE_EXT = '.sag';
public function setUp()
{
$this->cache = new SagFileCache('/tmp/sag');
}
public function test_createNew()
{
foreach(array('/bwah', '/hithere', '/yup') as $url)
{
$prevSize = $this->cache->getUsage();
$item = new stdClass();
$item->body = new stdClass();
$item->body->foo = "bar";
$item->headers = new stdClass();
$item->headers->etag = "\"asdfasfsadfsadf\"";
$res = $this->cache->set($url, $item);
$this->assertTrue($res === true || is_object($res));
$file = '/tmp/sag/'.$this->cache->makeKey($url).$this->FILE_EXT;
$this->assertTrue(is_file($file));
$this->assertTrue(is_readable($file));
$this->assertTrue(is_writable($file));
$fileContents = file_get_contents($file);
//compare objects as PHP classes
$this->assertEquals(json_decode(file_get_contents($file)), $item);
//compare objects as JSON
$this->assertEquals($fileContents, json_encode($item));
//compare sizes as JSON
$this->assertEquals(strlen($fileContents), strlen(json_encode($item)));
//compare size on disk with cache's reported size
$this->assertEquals(filesize($file), $this->cache->getUsage() - $prevSize);
}
}
public function test_get()
{
$this->assertNotNull($this->cache->get('/bwah'));
$this->assertNull($this->cache->get(rand(0,100)));
}
public function test_newVersion()
{
$new = new stdClass();
$new->body = new stdClass();
$new->body->titFor = "tat";
$new->headers = new stdClass();
$new->headers->etag = "\"asdfasdfasdfasdf\"";
$file = $this->cache->makeFilename('/bwah');
$oldContents = file_get_contents($file);
$oldCopy = $this->cache->set('/bwah', $new);
$this->assertEquals(json_encode($oldCopy), $oldContents);
$this->assertEquals($new, json_decode(file_get_contents($file)));
}
public function test_partialClear()
{
$files = glob('/tmp/sag/*'.$this->FILE_EXT);
//block ourselves so we do a partial clear
foreach($files as $file)
{
$this->assertTrue(is_file($file));
$this->assertTrue(chmod($file, 0111));
}
//attempt the clear
$this->assertFalse($this->cache->clear());
//reset for future operations
foreach($files as $file)
$this->assertTrue(chmod($file, 0777));
}
public function test_delete()
{
$this->assertNotNull($this->cache->get('/bwah'));
$this->assertTrue($this->cache->remove('/bwah'));
$this->assertNull($this->cache->get('/bwah'));
}
public function test_setSize()
{
$size = 10;
$this->cache->setSize($size);
$this->assertEquals($size, $this->cache->getSize());
}
public function test_clear()
{
$files = glob('/tmp/sag/*'.$this->FILE_EXT);
$this->assertFalse(empty($files));
$this->assertTrue($this->cache->clear());
$files = glob('/tmp/sag/*'.$this->FILE_EXT);
$this->assertTrue(empty($files));
}
public function test_defaultSizes()
{
$this->assertEquals(1000000, $this->cache->getSize());
$this->assertEquals(0, $this->cache->getUsage());
}
}
?>