Skip to content

Commit

Permalink
#3
Browse files Browse the repository at this point in the history
* add value serialization
* add test
#noticket
* improve tests
  • Loading branch information
dantodev committed Sep 28, 2016
1 parent 5770697 commit b153d71
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 36 deletions.
2 changes: 1 addition & 1 deletion src/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ private function fetchCacheElement($key)
$element = new CacheElement(
$this,
$key,
$cache["value"],
unserialize($cache["value"]),
$cache["lifetime"],
$cache["start"],
$cache["refresh"]
Expand Down
8 changes: 4 additions & 4 deletions src/CacheElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down Expand Up @@ -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,
Expand Down
78 changes: 47 additions & 31 deletions test/FileCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}

}

0 comments on commit b153d71

Please sign in to comment.