diff --git a/src/Cache.php b/src/Cache.php index 42bb0b9..dac3256 100644 --- a/src/Cache.php +++ b/src/Cache.php @@ -177,7 +177,7 @@ private function fetchCacheElement($key) $element = new CacheElement( $this, $key, - $cache["value"], + unserialize($cache["value"]), $cache["lifetime"], $cache["start"], $cache["refresh"] diff --git a/src/CacheElement.php b/src/CacheElement.php index 777f07f..6d272bf 100644 --- a/src/CacheElement.php +++ b/src/CacheElement.php @@ -24,8 +24,8 @@ class CacheElement public function __construct(Cache $cache_instance, $key, $value, $lifetime = 60, $start = null, $refresh = false) { $this->_cache_instance = $cache_instance; - $this->_key = (string)$key; - $this->_value = (string)$value; + $this->_key = (string) $key; + $this->_value = $value; $this->_lifetime = $lifetime; $this->_start = $start ?: time(); $this->_refresh = $refresh; @@ -40,7 +40,7 @@ public function __construct(Cache $cache_instance, $key, $value, $lifetime = 60, */ public function update($value, $lifetime = null, $refresh = null) { - $this->_value = (string)$value; + $this->_value = $value; if (!is_null($lifetime)) { $this->_lifetime = $lifetime; } @@ -82,7 +82,7 @@ public function writeToFs() { if ($this->_modified) { file_put_contents($this->path(), json_encode([ - "value" => $this->_value, + "value" => serialize($this->_value), "lifetime" => $this->_lifetime, "start" => $this->_start, "refresh" => $this->_refresh, diff --git a/test/FileCacheTest.php b/test/FileCacheTest.php index af7443e..9ddfc8b 100644 --- a/test/FileCacheTest.php +++ b/test/FileCacheTest.php @@ -3,56 +3,72 @@ class FileCacheTest extends \PHPUnit_Framework_TestCase { - public function test() + /** @var Cache */ + private $cache; + + public function setUp() { - $cache = new Cache(__DIR__ . "/test_path/"); - - // get if not set - $this->assertNull($cache->get("foo")); - $this->assertEquals("default", $cache->get("foo", "default")); - - // test has = false - $this->assertFalse($cache->has("foo")); - - // set & get if set - $cache->set("foo", "bar"); - $this->assertEquals("bar", $cache->get("foo")); + $this->cache = new Cache(__DIR__ . "/test_path/"); + parent::setUp(); + } + + public function testGetHasEmpty() + { + $this->assertNull($this->cache->get("foo")); + $this->assertEquals("default", $this->cache->get("foo", "default")); + $this->assertFalse($this->cache->has("foo")); + } - // test has = true - $this->assertTrue($cache->has("foo")); + public function testGetHasExisting() + { + $this->cache->set("foo", "bar"); + $this->assertEquals("bar", $this->cache->get("foo")); + $this->assertTrue($this->cache->has("foo")); //test if cache has been written - $cache->writeCache()->flush(true); - $this->assertEquals("bar", $cache->get("foo")); + $this->cache->writeCache()->flush(true); + $this->assertEquals("bar", $this->cache->get("foo")); + } - // test remove - $cache->forget("foo"); - $this->assertNull($cache->get("foo")); + public function testRemove() + { + $this->cache->set("foo", "bar"); + $this->assertEquals("bar", $this->cache->get("foo")); + $this->cache->forget("foo"); + $this->assertNull($this->cache->get("foo")); + } - // test remember - $this->assertEquals("bar2", $cache->remember("foo2", function () { + public function testRemember() + { + $this->assertEquals("bar2", $this->cache->remember("foo2", function () { return "bar2"; })); - $this->assertEquals("bar2", $cache->remember("foo2", function () { + $this->assertEquals("bar2", $this->cache->remember("foo2", function () { return "this will never be set"; })); + } + public function testTimeout() + { // test timeout - $cache->set("foo3", "bar3", 1); + $this->cache->set("foo3", "bar3", 1); sleep(2); - $this->assertNull($cache->get("foo3")); + $this->assertNull($this->cache->get("foo3")); // test refresh - $cache->set("foo4", "bar4", 4); + $this->cache->set("foo4", "bar4", 4); sleep(2); - $this->assertEquals("bar4", $cache->get("foo4")); + $this->assertEquals("bar4", $this->cache->get("foo4")); sleep(2); - $this->assertEquals("bar4", $cache->get("foo4")); + $this->assertEquals("bar4", $this->cache->get("foo4")); sleep(5); - $this->assertNull($cache->get("foo4")); + $this->assertNull($this->cache->get("foo4")); + } - // test flush - $cache->flush(); + public function testSerialization() + { + $this->cache->set('foo4', ['item1', 'item2']); + $this->assertEquals(['item1', 'item2'], $this->cache->get('foo4')); } } \ No newline at end of file