Skip to content

Commit

Permalink
Check expiresAt and expiresAfter methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
byjg committed Nov 18, 2018
1 parent 34086e4 commit da6f19d
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 45 deletions.
27 changes: 13 additions & 14 deletions src/Psr6/CacheItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace ByJG\Cache\Psr6;

use DateInterval;
use DateTime;
use DateTimeInterface;
use Psr\Cache\CacheItemInterface;

class CacheItem implements CacheItemInterface
Expand All @@ -22,7 +25,7 @@ class CacheItem implements CacheItemInterface
protected $hit;

/**
* @var \DateTime
* @var DateTime
*/
protected $expiration;

Expand Down Expand Up @@ -76,10 +79,8 @@ public function isHit()
*/
public function expiresAt($expiration)
{
if (is_null($expiration)) {
$this->expiration = new \DateTime('now +1 year');
} else {
assert('$expiration instanceof \DateTimeInterface');
$this->expiration = new DateTime('now +1 year');
if ($expiration instanceof DateTimeInterface) {
$this->expiration = $expiration;
}
return $this;
Expand All @@ -89,21 +90,19 @@ public function expiresAt($expiration)
*/
public function expiresAfter($time)
{
if (is_null($time)) {
$this->expiration = new \DateTime('now +1 year');
} elseif (is_numeric($time)) {
$this->expiration = new \DateTime('now +' . $time . ' seconds');
} else {
assert('$time instanceof DateInterval');
$expiration = new \DateTime();
$this->expiration = new DateTime('now +1 year');
if (is_numeric($time)) {
$this->expiration = new DateTime('now +' . $time . ' seconds');
} else if ($time instanceof DateInterval) {
$expiration = new DateTime();
$expiration->add($time);
$this->expiration = $expiration;
}
return $this;
}

/**
* @return \DateTime
* @return DateTime
*/
public function getExpiresAt()
{
Expand All @@ -112,6 +111,6 @@ public function getExpiresAt()

public function getExpiresInSecs()
{
return $this->getExpiresAt()->getTimestamp() - (new \DateTime('now'))->getTimestamp();
return $this->getExpiresAt()->getTimestamp() - (new DateTime('now'))->getTimestamp();
}
}
70 changes: 39 additions & 31 deletions tests/CachePSR6Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use ByJG\Cache\Psr16\BaseCacheEngine;
use ByJG\Cache\Psr6\CachePool;
use DateInterval;

require_once 'BaseCacheTest.php';

Expand Down Expand Up @@ -96,37 +97,44 @@ public function testGetMultipleItems(BaseCacheEngine $cacheEngine)
*/
public function testTtl(BaseCacheEngine $cacheEngine)
{
$this->cacheEngine = $cacheEngine;

$object = new CachePool($cacheEngine);
if ($object->isAvailable()) {
// First time
$item = $object->getItem('chave');
$this->assertFalse($item->isHit());

// Set object
$item->set('valor');
$item->expiresAfter(2);
$object->save($item);
$this->assertTrue($item->isHit());

// Get Object
$item2 = $object->getItem('chave');
$this->assertTrue($item2->isHit());
$this->assertEquals('valor', $item2->get());
sleep(3);
$item3 = $object->getItem('chave');
$this->assertFalse($item3->isHit());
$this->assertEquals(null, $item3->get());

// Remove
$object->deleteItem('chave');

// Check Removed
$item = $object->getItem('chave');
$this->assertFalse($item->isHit());
} else {
$this->markTestIncomplete('Object is not fully functional');
$timeList = [
2,
DateInterval::createFromDateString("2 seconds")
];

foreach ($timeList as $time) {
$this->cacheEngine = $cacheEngine;

$object = new CachePool($cacheEngine);
if ($object->isAvailable()) {
// First time
$item = $object->getItem('chave');
$this->assertFalse($item->isHit());

// Set object
$item->set('valor');
$item->expiresAfter($time);
$object->save($item);
$this->assertTrue($item->isHit());

// Get Object
$item2 = $object->getItem('chave');
$this->assertTrue($item2->isHit());
$this->assertEquals('valor', $item2->get());
sleep(3);
$item3 = $object->getItem('chave');
$this->assertFalse($item3->isHit());
$this->assertEquals(null, $item3->get());

// Remove
$object->deleteItem('chave');

// Check Removed
$item = $object->getItem('chave');
$this->assertFalse($item->isHit());
} else {
$this->markTestIncomplete('Object is not fully functional');
}
}
}

Expand Down

0 comments on commit da6f19d

Please sign in to comment.