/
ApcuCacheTest.php
132 lines (112 loc) · 3.79 KB
/
ApcuCacheTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
namespace Kirby\Cache;
use PHPUnit\Framework\TestCase;
require_once __DIR__ . '/mocks.php';
/**
* @coversDefaultClass \Kirby\Cache\ApcuCache
*/
class ApcuCacheTest extends TestCase
{
public function setUp(): void
{
if (
function_exists('apcu_enabled') === false ||
apcu_enabled() === false
) {
$this->markTestSkipped('APCu is not available.');
return;
}
}
/**
* @covers ::set
* @covers ::exists
* @covers ::retrieve
* @covers ::remove
*/
public function testOperations()
{
$cache = new ApcuCache([]);
$time = time();
$this->assertTrue($cache->set('foo', 'A basic value', 10));
$this->assertTrue($cache->exists('foo'));
$this->assertSame('A basic value', $cache->retrieve('foo')->value());
$this->assertSame($time, $cache->created('foo'));
$this->assertSame($time + 600, $cache->expires('foo'));
$this->assertTrue($cache->remove('foo'));
$this->assertFalse($cache->exists('foo'));
$this->assertNull($cache->retrieve('foo'));
$this->assertFalse($cache->remove('doesnotexist'));
}
/**
* @covers ::set
* @covers ::exists
* @covers ::retrieve
* @covers ::remove
*/
public function testOperationsWithPrefix()
{
$cache1 = new ApcuCache([
'prefix' => 'test1'
]);
$cache2 = new ApcuCache([
'prefix' => 'test2'
]);
$time = time();
$this->assertTrue($cache1->set('foo', 'A basic value', 10));
$this->assertTrue($cache1->exists('foo'));
$this->assertFalse($cache2->exists('foo'));
$this->assertSame('A basic value', $cache1->retrieve('foo')->value());
$this->assertSame($time, $cache1->created('foo'));
$this->assertSame($time + 600, $cache1->expires('foo'));
$this->assertTrue($cache2->set('foo', 'Another basic value'));
$this->assertTrue($cache2->exists('foo'));
$this->assertSame('A basic value', $cache1->retrieve('foo')->value());
$this->assertTrue($cache1->remove('foo'));
$this->assertFalse($cache1->exists('foo'));
$this->assertNull($cache1->retrieve('foo'));
$this->assertTrue($cache2->exists('foo'));
$this->assertSame('Another basic value', $cache2->retrieve('foo')->value());
}
/**
* @covers ::flush
*/
public function testFlush()
{
$cache = new ApcuCache([]);
$cache->set('a', 'A basic value');
$cache->set('b', 'A basic value');
$cache->set('c', 'A basic value');
$this->assertTrue($cache->exists('a'));
$this->assertTrue($cache->exists('b'));
$this->assertTrue($cache->exists('c'));
$this->assertTrue($cache->flush());
$this->assertFalse($cache->exists('a'));
$this->assertFalse($cache->exists('b'));
$this->assertFalse($cache->exists('c'));
}
/**
* @covers ::flush
*/
public function testFlushWithPrefix()
{
$cache1 = new ApcuCache([
'prefix' => 'test1'
]);
$cache2 = new ApcuCache([
'prefix' => 'test2'
]);
$cache1->set('a', 'A basic value');
$cache1->set('b', 'A basic value');
$cache2->set('a', 'A basic value');
$cache2->set('b', 'A basic value');
$this->assertTrue($cache1->exists('a'));
$this->assertTrue($cache1->exists('b'));
$this->assertTrue($cache2->exists('a'));
$this->assertTrue($cache2->exists('b'));
$this->assertTrue($cache1->flush());
$this->assertFalse($cache1->exists('a'));
$this->assertFalse($cache1->exists('b'));
$this->assertTrue($cache2->exists('a'));
$this->assertTrue($cache2->exists('b'));
}
}