/
CacheTest.php
187 lines (167 loc) · 4.8 KB
/
CacheTest.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<?php
/*
* This file is part of the ACache library.
*
* (c) Martin Rademacher <mano@radebatz.net>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Radebatz\ACache\Tests;
use PHPUnit\Framework\TestCase;
use Radebatz\ACache\CacheInterface;
/**
* Cache test base class.
*
* Tests that each cache must pass.
*/
abstract class CacheTest extends TestCase
{
/**
* Test contains.
*
* @dataProvider cacheProvider
*/
public function testContains(CacheInterface $cache)
{
$this->assertTrue($cache->save('foo', 'bar'));
$this->assertTrue($cache->save('ying', 'yang'));
$this->assertTrue($cache->contains('ying'));
$this->assertFalse($cache->contains('bar'));
}
/**
* Get protected/private property.
*
* @param mixed $obj the opject
* @param string $name the property name
*
* @return mixed the property or <code>null</code>
*/
protected function getProperty($obj, $name)
{
$rc = new \ReflectionClass($obj);
if ($property = $rc->getProperty($name)) {
$property->setAccessible(true);
return $property->getValue($obj);
}
return null;
}
/**
* Get an override value if it exists.
*
* @param string $name the override name
* @param mixed $default optional default
*
* @return mixed the value or default
*/
protected function getOverride($name, $default = null)
{
return defined($name) ? constant($name) : $default;
}
/**
* Test fetch.
*
* @dataProvider cacheProvider
*/
public function testFetch(CacheInterface $cache)
{
$this->assertTrue($cache->save('foo', 'bar'));
$this->assertTrue($cache->save('ying', 'yang'));
$this->assertEquals('yang', $cache->fetch('ying'));
$this->assertEquals(null, $cache->fetch('bar'));
}
/**
* Test fetch invalid.
*
* @dataProvider cacheProvider
*/
public function testFetchInvalid(CacheInterface $cache)
{
$this->assertNull($cache->fetch('foobar'));
}
/**
* Test save.
*
* @dataProvider cacheProvider
*/
public function testSave(CacheInterface $cache)
{
$this->assertTrue($cache->save('foo', 'bar'));
$this->assertTrue($cache->save('ying', 'yang'));
$this->assertEquals('yang', $cache->fetch('ying'));
$this->assertTrue($cache->save('ying', 'ding'));
$this->assertEquals('ding', $cache->fetch('ying'));
}
/**
* Test arrays.
*
* @dataProvider cacheProvider
*/
public function testArray(CacheInterface $cache)
{
$this->assertTrue($cache->save('arr', ['arr']));
$this->assertEquals(['arr'], $cache->fetch('arr'));
}
/**
* Test objects.
*
* @dataProvider cacheProvider
*/
public function testObjecets(CacheInterface $cache)
{
$foo = json_decode('{"foo":"bar"}');
$this->assertTrue($cache->save('foo', $foo));
$this->assertEquals($foo, $cache->fetch('foo'));
}
/**
* Test time to live.
*
* @dataProvider cacheProvider
*/
public function testTimeToLive(CacheInterface $cache)
{
$this->assertTrue($cache->save('foo', 'bar'));
$this->assertEquals(0, $cache->getTimeToLive('foo'));
$this->assertFalse($cache->getTimeToLive('bert'));
$this->assertTrue($cache->save('dough', 'nut', 20, null));
$this->assertGreaterThan(15, $cache->getTimeToLive('dough'));
$this->assertTrue($cache->save('dough', 'nut'));
$this->assertEquals(0, $cache->getTimeToLive('dough'));
$this->assertEquals(0, $cache->getDefaultTimeToLive());
}
/**
* Test delete.
*
* @dataProvider cacheProvider
*/
public function testDelete(CacheInterface $cache)
{
$this->assertTrue($cache->save('foo', 'bar'));
$this->assertEquals('bar', $cache->fetch('foo'));
$this->assertTrue($cache->delete('foo'));
$this->assertFalse($cache->contains('foo'));
$this->assertNull($cache->fetch('foo'));
}
/**
* Test flush.
*
* @dataProvider cacheProvider
*/
public function testFlush(CacheInterface $cache)
{
$this->assertTrue($cache->save('foo', 'bar'));
$this->assertEquals('bar', $cache->fetch('foo'));
$this->assertTrue($cache->flush());
$this->assertFalse($cache->contains('foo'));
$this->assertNull($cache->fetch('foo'));
}
/**
* Test stats.
*
* @dataProvider cacheProvider
*/
public function testStats(CacheInterface $cache)
{
$this->assertTrue(is_array($stats = $cache->getStats()) || null === $stats);
}
}