Skip to content

Commit 5b1b43a

Browse files
committed
Fix and add tests
1 parent f7058aa commit 5b1b43a

5 files changed

Lines changed: 67 additions & 2 deletions

File tree

system/Psr/Cache/Item.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,8 @@ public function isExpired(): bool
249249
{
250250
if (isset($this->expiration))
251251
{
252-
return $this->expiration->isBefore(Time::now());
252+
$now = Time::now();
253+
return $this->expiration->isBefore($now) || $this->expiration->sameAs($now);
253254
}
254255

255256
return false;

system/Psr/Cache/Pool.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ public function save(CacheItemInterface $item)
273273
}
274274

275275
// Deteremine TTL
276-
$ttl = ($expiration = $item->getExpiration()) ? Time::now()->difference($expiration)->getSeconds() : 0;
276+
$ttl = ($expiration = $item->getExpiration()) ? Time::now()->difference($expiration)->getSeconds() : 60;
277277

278278
return $this->adapter->save($item->getKey(), $item->get(), $ttl);
279279
}

tests/psr/CachePoolTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
<?php
22

33
use Cache\IntegrationTests\CachePoolTest as TestCase;
4+
use CodeIgniter\I18n\Time;
45
use CodeIgniter\Psr\Cache\Pool;
6+
use Config\Services;
57

68
class CachePoolTest extends TestCase
79
{
810
public function createCachePool()
911
{
12+
Services::resetSingle('cache');
13+
Time::setTestNow(null);
1014
return new Pool();
1115
}
1216
}

tests/psr/SimpleCacheTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
<?php
22

33
use Cache\IntegrationTests\SimpleCacheTest as TestCase;
4+
use CodeIgniter\I18n\Time;
45
use CodeIgniter\Psr\Cache\SimpleCache;
6+
use Config\Services;
57

68
class SimpleCacheTest extends TestCase
79
{
810
public function createSimpleCache()
911
{
12+
Services::resetSingle('cache');
13+
Time::setTestNow(null);
1014
return new SimpleCache();
1115
}
1216
}

tests/psr/SupportTraitTest.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
use CodeIgniter\Cache\Handlers\DummyHandler;
4+
use CodeIgniter\Psr\Cache\CacheArgumentException;
5+
use CodeIgniter\Psr\Cache\SimpleCache;
6+
use CodeIgniter\Test\CIUnitTestCase;
7+
use CodeIgniter\Test\Mock\MockCache;
8+
use Config\Cache;
9+
use Config\Services;
10+
11+
class SupportTraitTest extends CIUnitTestCase
12+
{
13+
public function setUp(): void
14+
{
15+
parent::setUp();
16+
17+
Services::resetSingle('cache');
18+
}
19+
20+
public function testDefaultUsesSharedInstance()
21+
{
22+
Services::injectMock('cache', new MockCache());
23+
24+
$psr = new SimpleCache();
25+
$result = $this->getPrivateProperty($psr, 'adapter');
26+
27+
$this->assertInstanceOf(MockCache::class, $result);
28+
}
29+
30+
public function testUsesConfig()
31+
{
32+
$config = new Cache();
33+
$config->handler = 'dummy';
34+
35+
$psr = new SimpleCache($config);
36+
$result = $this->getPrivateProperty($psr, 'adapter');
37+
38+
$this->assertInstanceOf(DummyHandler::class, $result);
39+
}
40+
41+
public function testUsesHandler()
42+
{
43+
$psr = new SimpleCache(new MockCache());
44+
$result = $this->getPrivateProperty($psr, 'adapter');
45+
46+
$this->assertInstanceOf(MockCache::class, $result);
47+
}
48+
49+
public function testThrowsException()
50+
{
51+
$this->expectException(CacheArgumentException::class);
52+
$this->expectExceptionMessage('CodeIgniter\Psr\Cache\SimpleCache constructor only accepts an adapter or configuration');
53+
54+
$psr = new SimpleCache(42);
55+
}
56+
}

0 commit comments

Comments
 (0)