Skip to content

Commit b153d71

Browse files
committed
* add value serialization * add test #noticket * improve tests
1 parent 5770697 commit b153d71

File tree

3 files changed

+52
-36
lines changed

3 files changed

+52
-36
lines changed

src/Cache.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ private function fetchCacheElement($key)
177177
$element = new CacheElement(
178178
$this,
179179
$key,
180-
$cache["value"],
180+
unserialize($cache["value"]),
181181
$cache["lifetime"],
182182
$cache["start"],
183183
$cache["refresh"]

src/CacheElement.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ class CacheElement
2424
public function __construct(Cache $cache_instance, $key, $value, $lifetime = 60, $start = null, $refresh = false)
2525
{
2626
$this->_cache_instance = $cache_instance;
27-
$this->_key = (string)$key;
28-
$this->_value = (string)$value;
27+
$this->_key = (string) $key;
28+
$this->_value = $value;
2929
$this->_lifetime = $lifetime;
3030
$this->_start = $start ?: time();
3131
$this->_refresh = $refresh;
@@ -40,7 +40,7 @@ public function __construct(Cache $cache_instance, $key, $value, $lifetime = 60,
4040
*/
4141
public function update($value, $lifetime = null, $refresh = null)
4242
{
43-
$this->_value = (string)$value;
43+
$this->_value = $value;
4444
if (!is_null($lifetime)) {
4545
$this->_lifetime = $lifetime;
4646
}
@@ -82,7 +82,7 @@ public function writeToFs()
8282
{
8383
if ($this->_modified) {
8484
file_put_contents($this->path(), json_encode([
85-
"value" => $this->_value,
85+
"value" => serialize($this->_value),
8686
"lifetime" => $this->_lifetime,
8787
"start" => $this->_start,
8888
"refresh" => $this->_refresh,

test/FileCacheTest.php

Lines changed: 47 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,56 +3,72 @@
33
class FileCacheTest extends \PHPUnit_Framework_TestCase
44
{
55

6-
public function test()
6+
/** @var Cache */
7+
private $cache;
8+
9+
public function setUp()
710
{
8-
$cache = new Cache(__DIR__ . "/test_path/");
9-
10-
// get if not set
11-
$this->assertNull($cache->get("foo"));
12-
$this->assertEquals("default", $cache->get("foo", "default"));
13-
14-
// test has = false
15-
$this->assertFalse($cache->has("foo"));
16-
17-
// set & get if set
18-
$cache->set("foo", "bar");
19-
$this->assertEquals("bar", $cache->get("foo"));
11+
$this->cache = new Cache(__DIR__ . "/test_path/");
12+
parent::setUp();
13+
}
14+
15+
public function testGetHasEmpty()
16+
{
17+
$this->assertNull($this->cache->get("foo"));
18+
$this->assertEquals("default", $this->cache->get("foo", "default"));
19+
$this->assertFalse($this->cache->has("foo"));
20+
}
2021

21-
// test has = true
22-
$this->assertTrue($cache->has("foo"));
22+
public function testGetHasExisting()
23+
{
24+
$this->cache->set("foo", "bar");
25+
$this->assertEquals("bar", $this->cache->get("foo"));
26+
$this->assertTrue($this->cache->has("foo"));
2327

2428
//test if cache has been written
25-
$cache->writeCache()->flush(true);
26-
$this->assertEquals("bar", $cache->get("foo"));
29+
$this->cache->writeCache()->flush(true);
30+
$this->assertEquals("bar", $this->cache->get("foo"));
31+
}
2732

28-
// test remove
29-
$cache->forget("foo");
30-
$this->assertNull($cache->get("foo"));
33+
public function testRemove()
34+
{
35+
$this->cache->set("foo", "bar");
36+
$this->assertEquals("bar", $this->cache->get("foo"));
37+
$this->cache->forget("foo");
38+
$this->assertNull($this->cache->get("foo"));
39+
}
3140

32-
// test remember
33-
$this->assertEquals("bar2", $cache->remember("foo2", function () {
41+
public function testRemember()
42+
{
43+
$this->assertEquals("bar2", $this->cache->remember("foo2", function () {
3444
return "bar2";
3545
}));
36-
$this->assertEquals("bar2", $cache->remember("foo2", function () {
46+
$this->assertEquals("bar2", $this->cache->remember("foo2", function () {
3747
return "this will never be set";
3848
}));
49+
}
3950

51+
public function testTimeout()
52+
{
4053
// test timeout
41-
$cache->set("foo3", "bar3", 1);
54+
$this->cache->set("foo3", "bar3", 1);
4255
sleep(2);
43-
$this->assertNull($cache->get("foo3"));
56+
$this->assertNull($this->cache->get("foo3"));
4457

4558
// test refresh
46-
$cache->set("foo4", "bar4", 4);
59+
$this->cache->set("foo4", "bar4", 4);
4760
sleep(2);
48-
$this->assertEquals("bar4", $cache->get("foo4"));
61+
$this->assertEquals("bar4", $this->cache->get("foo4"));
4962
sleep(2);
50-
$this->assertEquals("bar4", $cache->get("foo4"));
63+
$this->assertEquals("bar4", $this->cache->get("foo4"));
5164
sleep(5);
52-
$this->assertNull($cache->get("foo4"));
65+
$this->assertNull($this->cache->get("foo4"));
66+
}
5367

54-
// test flush
55-
$cache->flush();
68+
public function testSerialization()
69+
{
70+
$this->cache->set('foo4', ['item1', 'item2']);
71+
$this->assertEquals(['item1', 'item2'], $this->cache->get('foo4'));
5672
}
5773

5874
}

0 commit comments

Comments
 (0)