3 namespace Friendica\Test\src\Core\Cache;
5 use Friendica\Core\Cache\MemcachedCacheDriver;
6 use Friendica\Test\MockedTest;
7 use Friendica\Test\Util\AppMockTrait;
8 use Friendica\Test\Util\DateTimeFormatMockTrait;
9 use Friendica\Test\Util\VFSTrait;
10 use Friendica\Util\PidFile;
12 abstract class CacheTest extends MockedTest
16 use DateTimeFormatMockTrait;
19 * @var int Start time of the mock (used for time operations)
21 protected $startTime = 1417011228;
24 * @var \Friendica\Core\Cache\ICacheDriver
29 * @var \Friendica\Core\Cache\IMemoryCacheDriver
34 * Dataset for test setting different types in the cache
37 public function dataTypesInCache()
40 'string' => ['data' => 'foobar'],
41 'integer' => ['data' => 1],
42 'boolTrue' => ['data' => true],
43 'boolFalse' => ['data' => false],
44 'float' => ['data' => 4.6634234],
45 'array' => ['data' => ['1', '2', '3', '4', '5']],
46 'object' => ['data' => new PidFile()],
47 'null' => ['data' => null],
52 * Dataset for simple value sets/gets
55 public function dataSimple()
60 'value2' => 'ipsum lorum',
62 'value4' => 'lasttest',
67 abstract protected function getInstance();
69 protected function setUp()
72 $configMock = \Mockery::mock('Friendica\Core\Config\ConfigCache');
73 $this->mockApp($this->root, $configMock);
75 ->shouldReceive('getHostname')
76 ->andReturn('friendica.local');
78 $this->mockUtcNow($this->startTime);
82 $this->instance = $this->getInstance();
85 $this->mockConfigGet('config', 'hostname', 'localhost');
86 $this->mockConfigGet('system', 'throttle_limit_day', 100);
87 $this->mockConfigGet('system', 'throttle_limit_week', 100);
88 $this->mockConfigGet('system', 'throttle_limit_month', 100);
89 $this->mockConfigGet('system', 'theme', 'system_theme');
91 $this->instance->clear(false);
96 * @dataProvider dataSimple
97 * @param mixed $value1 a first
98 * @param mixed $value2 a second
100 function testSimple($value1, $value2) {
101 $this->assertNull($this->instance->get('value1'));
103 $this->instance->set('value1', $value1);
104 $received = $this->instance->get('value1');
105 $this->assertEquals($value1, $received, 'Value received from cache not equal to the original');
107 $this->instance->set('value1', $value2);
108 $received = $this->instance->get('value1');
109 $this->assertEquals($value2, $received, 'Value not overwritten by second set');
111 $this->instance->set('value2', $value1);
112 $received2 = $this->instance->get('value2');
113 $this->assertEquals($value2, $received, 'Value changed while setting other variable');
114 $this->assertEquals($value1, $received2, 'Second value not equal to original');
116 $this->assertNull($this->instance->get('not_set'), 'Unset value not equal to null');
118 $this->assertTrue($this->instance->delete('value1'));
119 $this->assertNull($this->instance->get('value1'));
124 * @dataProvider dataSimple
125 * @param mixed $value1 a first
126 * @param mixed $value2 a second
127 * @param mixed $value3 a third
128 * @param mixed $value4 a fourth
130 function testClear($value1, $value2, $value3, $value4) {
131 $value = 'ipsum lorum';
132 $this->instance->set('1_value1', $value1);
133 $this->instance->set('1_value2', $value2);
134 $this->instance->set('2_value1', $value3);
135 $this->instance->set('3_value1', $value4);
137 $this->assertEquals([
138 '1_value1' => $value1,
139 '1_value2' => $value2,
140 '2_value1' => $value3,
141 '3_value1' => $value4,
143 '1_value1' => $this->instance->get('1_value1'),
144 '1_value2' => $this->instance->get('1_value2'),
145 '2_value1' => $this->instance->get('2_value1'),
146 '3_value1' => $this->instance->get('3_value1'),
149 $this->assertTrue($this->instance->clear());
151 $this->assertEquals([
152 '1_value1' => $value1,
153 '1_value2' => $value2,
154 '2_value1' => $value3,
155 '3_value1' => $value4,
157 '1_value1' => $this->instance->get('1_value1'),
158 '1_value2' => $this->instance->get('1_value2'),
159 '2_value1' => $this->instance->get('2_value1'),
160 '3_value1' => $this->instance->get('3_value1'),
163 $this->assertTrue($this->instance->clear(false));
165 $this->assertEquals([
171 '1_value1' => $this->instance->get('1_value1'),
172 '1_value2' => $this->instance->get('1_value2'),
173 '2_value3' => $this->instance->get('2_value3'),
174 '3_value4' => $this->instance->get('3_value4'),
182 $this->markTestSkipped('taking too much time without mocking');
184 $this->assertNull($this->instance->get('value1'));
187 $this->instance->set('value1', $value, 1);
188 $received = $this->instance->get('value1');
189 $this->assertEquals($value, $received, 'Value received from cache not equal to the original');
193 $this->assertNull($this->instance->get('value1'));
198 * @param $data mixed the data to store in the cache
199 * @dataProvider dataTypesInCache
201 function testDifferentTypesInCache($data) {
202 $this->instance->set('val', $data);
203 $received = $this->instance->get('val');
204 $this->assertEquals($data, $received, 'Value type changed from ' . gettype($data) . ' to ' . gettype($received));
209 * @param mixed $value1 a first
210 * @param mixed $value2 a second
211 * @param mixed $value3 a third
212 * @dataProvider dataSimple
214 public function testGetAllKeys($value1, $value2, $value3) {
215 if ($this->cache instanceof MemcachedCacheDriver) {
216 $this->markTestSkipped('Memcached doesn\'t support getAllKeys anymore');
219 $this->assertTrue($this->instance->set('value1', $value1));
220 $this->assertTrue($this->instance->set('value2', $value2));
221 $this->assertTrue($this->instance->set('test_value3', $value3));
223 $list = $this->instance->getAllKeys();
225 $this->assertContains('value1', $list);
226 $this->assertContains('value2', $list);
227 $this->assertContains('test_value3', $list);
229 $list = $this->instance->getAllKeys('test');
231 $this->assertContains('test_value3', $list);