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\VFSTrait;
9 use Friendica\Util\PidFile;
11 abstract class CacheTest extends MockedTest
17 * @var int Start time of the mock (used for time operations)
19 protected $startTime = 1417011228;
22 * @var \Friendica\Core\Cache\ICacheDriver
27 * @var \Friendica\Core\Cache\IMemoryCacheDriver
32 * Dataset for test setting different types in the cache
35 public function dataTypesInCache()
38 'string' => ['data' => 'foobar'],
39 'integer' => ['data' => 1],
40 'boolTrue' => ['data' => true],
41 'boolFalse' => ['data' => false],
42 'float' => ['data' => 4.6634234],
43 'array' => ['data' => ['1', '2', '3', '4', '5']],
44 'object' => ['data' => new PidFile()],
45 'null' => ['data' => null],
50 * Dataset for simple value sets/gets
53 public function dataSimple()
58 'value2' => 'ipsum lorum',
60 'value4' => 'lasttest',
65 abstract protected function getInstance();
67 protected function setUp()
70 $configMock = \Mockery::mock('Friendica\Core\Config\ConfigCache');
71 $this->mockApp($this->root, $configMock);
73 ->shouldReceive('getHostname')
74 ->andReturn('friendica.local');
78 $this->instance = $this->getInstance();
80 $this->instance->clear(false);
85 * @dataProvider dataSimple
86 * @param mixed $value1 a first
87 * @param mixed $value2 a second
89 function testSimple($value1, $value2) {
90 $this->assertNull($this->instance->get('value1'));
92 $this->instance->set('value1', $value1);
93 $received = $this->instance->get('value1');
94 $this->assertEquals($value1, $received, 'Value received from cache not equal to the original');
96 $this->instance->set('value1', $value2);
97 $received = $this->instance->get('value1');
98 $this->assertEquals($value2, $received, 'Value not overwritten by second set');
100 $this->instance->set('value2', $value1);
101 $received2 = $this->instance->get('value2');
102 $this->assertEquals($value2, $received, 'Value changed while setting other variable');
103 $this->assertEquals($value1, $received2, 'Second value not equal to original');
105 $this->assertNull($this->instance->get('not_set'), 'Unset value not equal to null');
107 $this->assertTrue($this->instance->delete('value1'));
108 $this->assertNull($this->instance->get('value1'));
113 * @dataProvider dataSimple
114 * @param mixed $value1 a first
115 * @param mixed $value2 a second
116 * @param mixed $value3 a third
117 * @param mixed $value4 a fourth
119 function testClear($value1, $value2, $value3, $value4) {
120 $value = 'ipsum lorum';
121 $this->instance->set('1_value1', $value1);
122 $this->instance->set('1_value2', $value2);
123 $this->instance->set('2_value1', $value3);
124 $this->instance->set('3_value1', $value4);
126 $this->assertEquals([
127 '1_value1' => $value1,
128 '1_value2' => $value2,
129 '2_value1' => $value3,
130 '3_value1' => $value4,
132 '1_value1' => $this->instance->get('1_value1'),
133 '1_value2' => $this->instance->get('1_value2'),
134 '2_value1' => $this->instance->get('2_value1'),
135 '3_value1' => $this->instance->get('3_value1'),
138 $this->assertTrue($this->instance->clear());
140 $this->assertEquals([
141 '1_value1' => $value1,
142 '1_value2' => $value2,
143 '2_value1' => $value3,
144 '3_value1' => $value4,
146 '1_value1' => $this->instance->get('1_value1'),
147 '1_value2' => $this->instance->get('1_value2'),
148 '2_value1' => $this->instance->get('2_value1'),
149 '3_value1' => $this->instance->get('3_value1'),
152 $this->assertTrue($this->instance->clear(false));
154 $this->assertEquals([
160 '1_value1' => $this->instance->get('1_value1'),
161 '1_value2' => $this->instance->get('1_value2'),
162 '2_value3' => $this->instance->get('2_value3'),
163 '3_value4' => $this->instance->get('3_value4'),
171 $this->markTestSkipped('taking too much time without mocking');
173 $this->assertNull($this->instance->get('value1'));
176 $this->instance->set('value1', $value, 1);
177 $received = $this->instance->get('value1');
178 $this->assertEquals($value, $received, 'Value received from cache not equal to the original');
182 $this->assertNull($this->instance->get('value1'));
187 * @param $data mixed the data to store in the cache
188 * @dataProvider dataTypesInCache
190 function testDifferentTypesInCache($data) {
191 $this->instance->set('val', $data);
192 $received = $this->instance->get('val');
193 $this->assertEquals($data, $received, 'Value type changed from ' . gettype($data) . ' to ' . gettype($received));
198 * @param mixed $value1 a first
199 * @param mixed $value2 a second
200 * @param mixed $value3 a third
201 * @dataProvider dataSimple
203 public function testGetAllKeys($value1, $value2, $value3) {
204 if ($this->cache instanceof MemcachedCacheDriver) {
205 $this->markTestSkipped('Memcached doesn\'t support getAllKeys anymore');
208 $this->assertTrue($this->instance->set('value1', $value1));
209 $this->assertTrue($this->instance->set('value2', $value2));
210 $this->assertTrue($this->instance->set('test_value3', $value3));
212 $list = $this->instance->getAllKeys();
214 $this->assertContains('value1', $list);
215 $this->assertContains('value2', $list);
216 $this->assertContains('test_value3', $list);
218 $list = $this->instance->getAllKeys('test');
220 $this->assertContains('test_value3', $list);