1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Koded\Caching\Client; |
4
|
|
|
|
5
|
|
|
use Koded\Caching\Configuration\ConfigFactory; |
6
|
|
|
use Koded\Caching\SimpleCacheTestCaseTrait; |
7
|
|
|
use PHPUnit\Framework\TestCase; |
8
|
|
|
use function Koded\Caching\simple_cache_factory; |
9
|
|
|
use function Koded\Stdlib\now; |
10
|
|
|
|
11
|
|
|
class MemoryClientTest extends TestCase |
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
use SimpleCacheTestCaseTrait; |
15
|
|
|
|
16
|
|
|
public function test_global_ttl_when_null() |
17
|
|
|
{ |
18
|
|
|
/** @var MemoryClient $cache */ |
19
|
|
|
$cache = simple_cache_factory('memory'); |
20
|
|
|
|
21
|
|
|
$this->assertNull($cache->getTtl(), 'Default TTL is NULL'); |
22
|
|
|
$this->assertAttributeEquals([], 'expiration', $cache); |
23
|
|
|
|
24
|
|
|
$cache->set('key', 'value', 10); |
25
|
|
|
$this->assertGreaterThanOrEqual(10 + now()->getTimestamp(), $cache->getExpirationFor('key'), |
26
|
|
|
'Exp. time is calculated internally'); |
27
|
|
|
|
28
|
|
|
$this->assertNull($cache->getTtl(), 'Global TTL is not changed on explicit item TTL'); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function test_global_ttl_when_set() |
32
|
|
|
{ |
33
|
|
|
/** @var MemoryClient $cache */ |
34
|
|
|
$cache = simple_cache_factory('memory', ['ttl' => 60]); |
35
|
|
|
|
36
|
|
|
$this->assertEquals(60, $cache->getTtl()); |
37
|
|
|
$this->assertAttributeEquals([], 'expiration', $cache, 'Ex. time is empty if global TTL is set'); |
38
|
|
|
|
39
|
|
|
$cache->set('key', 'value'); |
40
|
|
|
$this->assertGreaterThanOrEqual(60 + now()->getTimestamp(), $cache->getExpirationFor('key'), |
41
|
|
|
'Global TTL is applied if explicit is NULL'); |
42
|
|
|
|
43
|
|
|
$cache->set('key', 'value', 120); |
44
|
|
|
$this->assertGreaterThanOrEqual(120 + now()->getTimestamp(), $cache->getExpirationFor('key'), |
45
|
|
|
'Global TTL is ignored if explicit is not NULL'); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
protected function setUp(): void |
49
|
|
|
{ |
50
|
|
|
putenv('CACHE_CLIENT=memory'); |
51
|
|
|
$this->cache = (new CacheClientFactory(new ConfigFactory))->new(); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|