Skip to content

Commit da6f19d

Browse files
committed
Check expiresAt and expiresAfter methods.
1 parent 34086e4 commit da6f19d

File tree

2 files changed

+52
-45
lines changed

2 files changed

+52
-45
lines changed

src/Psr6/CacheItem.php

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
namespace ByJG\Cache\Psr6;
44

5+
use DateInterval;
6+
use DateTime;
7+
use DateTimeInterface;
58
use Psr\Cache\CacheItemInterface;
69

710
class CacheItem implements CacheItemInterface
@@ -22,7 +25,7 @@ class CacheItem implements CacheItemInterface
2225
protected $hit;
2326

2427
/**
25-
* @var \DateTime
28+
* @var DateTime
2629
*/
2730
protected $expiration;
2831

@@ -76,10 +79,8 @@ public function isHit()
7679
*/
7780
public function expiresAt($expiration)
7881
{
79-
if (is_null($expiration)) {
80-
$this->expiration = new \DateTime('now +1 year');
81-
} else {
82-
assert('$expiration instanceof \DateTimeInterface');
82+
$this->expiration = new DateTime('now +1 year');
83+
if ($expiration instanceof DateTimeInterface) {
8384
$this->expiration = $expiration;
8485
}
8586
return $this;
@@ -89,21 +90,19 @@ public function expiresAt($expiration)
8990
*/
9091
public function expiresAfter($time)
9192
{
92-
if (is_null($time)) {
93-
$this->expiration = new \DateTime('now +1 year');
94-
} elseif (is_numeric($time)) {
95-
$this->expiration = new \DateTime('now +' . $time . ' seconds');
96-
} else {
97-
assert('$time instanceof DateInterval');
98-
$expiration = new \DateTime();
93+
$this->expiration = new DateTime('now +1 year');
94+
if (is_numeric($time)) {
95+
$this->expiration = new DateTime('now +' . $time . ' seconds');
96+
} else if ($time instanceof DateInterval) {
97+
$expiration = new DateTime();
9998
$expiration->add($time);
10099
$this->expiration = $expiration;
101100
}
102101
return $this;
103102
}
104103

105104
/**
106-
* @return \DateTime
105+
* @return DateTime
107106
*/
108107
public function getExpiresAt()
109108
{
@@ -112,6 +111,6 @@ public function getExpiresAt()
112111

113112
public function getExpiresInSecs()
114113
{
115-
return $this->getExpiresAt()->getTimestamp() - (new \DateTime('now'))->getTimestamp();
114+
return $this->getExpiresAt()->getTimestamp() - (new DateTime('now'))->getTimestamp();
116115
}
117116
}

tests/CachePSR6Test.php

Lines changed: 39 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use ByJG\Cache\Psr16\BaseCacheEngine;
66
use ByJG\Cache\Psr6\CachePool;
7+
use DateInterval;
78

89
require_once 'BaseCacheTest.php';
910

@@ -96,37 +97,44 @@ public function testGetMultipleItems(BaseCacheEngine $cacheEngine)
9697
*/
9798
public function testTtl(BaseCacheEngine $cacheEngine)
9899
{
99-
$this->cacheEngine = $cacheEngine;
100-
101-
$object = new CachePool($cacheEngine);
102-
if ($object->isAvailable()) {
103-
// First time
104-
$item = $object->getItem('chave');
105-
$this->assertFalse($item->isHit());
106-
107-
// Set object
108-
$item->set('valor');
109-
$item->expiresAfter(2);
110-
$object->save($item);
111-
$this->assertTrue($item->isHit());
112-
113-
// Get Object
114-
$item2 = $object->getItem('chave');
115-
$this->assertTrue($item2->isHit());
116-
$this->assertEquals('valor', $item2->get());
117-
sleep(3);
118-
$item3 = $object->getItem('chave');
119-
$this->assertFalse($item3->isHit());
120-
$this->assertEquals(null, $item3->get());
121-
122-
// Remove
123-
$object->deleteItem('chave');
124-
125-
// Check Removed
126-
$item = $object->getItem('chave');
127-
$this->assertFalse($item->isHit());
128-
} else {
129-
$this->markTestIncomplete('Object is not fully functional');
100+
$timeList = [
101+
2,
102+
DateInterval::createFromDateString("2 seconds")
103+
];
104+
105+
foreach ($timeList as $time) {
106+
$this->cacheEngine = $cacheEngine;
107+
108+
$object = new CachePool($cacheEngine);
109+
if ($object->isAvailable()) {
110+
// First time
111+
$item = $object->getItem('chave');
112+
$this->assertFalse($item->isHit());
113+
114+
// Set object
115+
$item->set('valor');
116+
$item->expiresAfter($time);
117+
$object->save($item);
118+
$this->assertTrue($item->isHit());
119+
120+
// Get Object
121+
$item2 = $object->getItem('chave');
122+
$this->assertTrue($item2->isHit());
123+
$this->assertEquals('valor', $item2->get());
124+
sleep(3);
125+
$item3 = $object->getItem('chave');
126+
$this->assertFalse($item3->isHit());
127+
$this->assertEquals(null, $item3->get());
128+
129+
// Remove
130+
$object->deleteItem('chave');
131+
132+
// Check Removed
133+
$item = $object->getItem('chave');
134+
$this->assertFalse($item->isHit());
135+
} else {
136+
$this->markTestIncomplete('Object is not fully functional');
137+
}
130138
}
131139
}
132140

0 commit comments

Comments
 (0)