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 $this->mockApp($this->root);
72 ->shouldReceive('getHostname')
73 ->andReturn('friendica.local');
77 $this->instance = $this->getInstance();
79 $this->instance->clear(false);
84 * @dataProvider dataSimple
85 * @param mixed $value1 a first
86 * @param mixed $value2 a second
88 function testSimple($value1, $value2) {
89 $this->assertNull($this->instance->get('value1'));
91 $this->instance->set('value1', $value1);
92 $received = $this->instance->get('value1');
93 $this->assertEquals($value1, $received, 'Value received from cache not equal to the original');
95 $this->instance->set('value1', $value2);
96 $received = $this->instance->get('value1');
97 $this->assertEquals($value2, $received, 'Value not overwritten by second set');
99 $this->instance->set('value2', $value1);
100 $received2 = $this->instance->get('value2');
101 $this->assertEquals($value2, $received, 'Value changed while setting other variable');
102 $this->assertEquals($value1, $received2, 'Second value not equal to original');
104 $this->assertNull($this->instance->get('not_set'), 'Unset value not equal to null');
106 $this->assertTrue($this->instance->delete('value1'));
107 $this->assertNull($this->instance->get('value1'));
112 * @dataProvider dataSimple
113 * @param mixed $value1 a first
114 * @param mixed $value2 a second
115 * @param mixed $value3 a third
116 * @param mixed $value4 a fourth
118 function testClear($value1, $value2, $value3, $value4) {
119 $value = 'ipsum lorum';
120 $this->instance->set('1_value1', $value1);
121 $this->instance->set('1_value2', $value2);
122 $this->instance->set('2_value1', $value3);
123 $this->instance->set('3_value1', $value4);
125 $this->assertEquals([
126 '1_value1' => $value1,
127 '1_value2' => $value2,
128 '2_value1' => $value3,
129 '3_value1' => $value4,
131 '1_value1' => $this->instance->get('1_value1'),
132 '1_value2' => $this->instance->get('1_value2'),
133 '2_value1' => $this->instance->get('2_value1'),
134 '3_value1' => $this->instance->get('3_value1'),
137 $this->assertTrue($this->instance->clear());
139 $this->assertEquals([
140 '1_value1' => $value1,
141 '1_value2' => $value2,
142 '2_value1' => $value3,
143 '3_value1' => $value4,
145 '1_value1' => $this->instance->get('1_value1'),
146 '1_value2' => $this->instance->get('1_value2'),
147 '2_value1' => $this->instance->get('2_value1'),
148 '3_value1' => $this->instance->get('3_value1'),
151 $this->assertTrue($this->instance->clear(false));
153 $this->assertEquals([
159 '1_value1' => $this->instance->get('1_value1'),
160 '1_value2' => $this->instance->get('1_value2'),
161 '2_value3' => $this->instance->get('2_value3'),
162 '3_value4' => $this->instance->get('3_value4'),
170 $this->markTestSkipped('taking too much time without mocking');
172 $this->assertNull($this->instance->get('value1'));
175 $this->instance->set('value1', $value, 1);
176 $received = $this->instance->get('value1');
177 $this->assertEquals($value, $received, 'Value received from cache not equal to the original');
181 $this->assertNull($this->instance->get('value1'));
186 * @param $data mixed the data to store in the cache
187 * @dataProvider dataTypesInCache
189 function testDifferentTypesInCache($data) {
190 $this->instance->set('val', $data);
191 $received = $this->instance->get('val');
192 $this->assertEquals($data, $received, 'Value type changed from ' . gettype($data) . ' to ' . gettype($received));
197 * @param mixed $value1 a first
198 * @param mixed $value2 a second
199 * @param mixed $value3 a third
200 * @dataProvider dataSimple
202 public function testGetAllKeys($value1, $value2, $value3) {
203 if ($this->cache instanceof MemcachedCacheDriver) {
204 $this->markTestSkipped('Memcached doesn\'t support getAllKeys anymore');
207 $this->assertTrue($this->instance->set('value1', $value1));
208 $this->assertTrue($this->instance->set('value2', $value2));
209 $this->assertTrue($this->instance->set('test_value3', $value3));
211 $list = $this->instance->getAllKeys();
213 $this->assertContains('value1', $list);
214 $this->assertContains('value2', $list);
215 $this->assertContains('test_value3', $list);
217 $list = $this->instance->getAllKeys('test');
219 $this->assertContains('test_value3', $list);