3 namespace Friendica\Test\src\Core\Cache;
5 use Friendica\Test\MockedTest;
6 use Friendica\Util\PidFile;
8 abstract class CacheTest extends MockedTest
11 * @var int Start time of the mock (used for time operations)
13 protected $startTime = 1417011228;
16 * @var \Friendica\Core\Cache\ICache
21 * @var \Friendica\Core\Cache\IMemoryCache
26 * Dataset for test setting different types in the cache
30 public function dataTypesInCache()
33 'string' => ['data' => 'foobar'],
34 'integer' => ['data' => 1],
35 'boolTrue' => ['data' => true],
36 'boolFalse' => ['data' => false],
37 'float' => ['data' => 4.6634234],
38 'array' => ['data' => ['1', '2', '3', '4', '5']],
39 'object' => ['data' => new PidFile()],
40 'null' => ['data' => null],
45 * Dataset for simple value sets/gets
49 public function dataSimple()
54 'value2' => 'ipsum lorum',
56 'value4' => 'lasttest',
61 abstract protected function getInstance();
63 protected function setUp()
67 $this->instance = $this->getInstance();
69 $this->instance->clear(false);
74 * @dataProvider dataSimple
76 * @param mixed $value1 a first
77 * @param mixed $value2 a second
79 function testSimple($value1, $value2)
81 $this->assertNull($this->instance->get('value1'));
83 $this->instance->set('value1', $value1);
84 $received = $this->instance->get('value1');
85 $this->assertEquals($value1, $received, 'Value received from cache not equal to the original');
87 $this->instance->set('value1', $value2);
88 $received = $this->instance->get('value1');
89 $this->assertEquals($value2, $received, 'Value not overwritten by second set');
91 $this->instance->set('value2', $value1);
92 $received2 = $this->instance->get('value2');
93 $this->assertEquals($value2, $received, 'Value changed while setting other variable');
94 $this->assertEquals($value1, $received2, 'Second value not equal to original');
96 $this->assertNull($this->instance->get('not_set'), 'Unset value not equal to null');
98 $this->assertTrue($this->instance->delete('value1'));
99 $this->assertNull($this->instance->get('value1'));
104 * @dataProvider dataSimple
106 * @param mixed $value1 a first
107 * @param mixed $value2 a second
108 * @param mixed $value3 a third
109 * @param mixed $value4 a fourth
111 function testClear($value1, $value2, $value3, $value4)
113 $value = 'ipsum lorum';
114 $this->instance->set('1_value1', $value1);
115 $this->instance->set('1_value2', $value2);
116 $this->instance->set('2_value1', $value3);
117 $this->instance->set('3_value1', $value4);
119 $this->assertEquals([
120 '1_value1' => $value1,
121 '1_value2' => $value2,
122 '2_value1' => $value3,
123 '3_value1' => $value4,
125 '1_value1' => $this->instance->get('1_value1'),
126 '1_value2' => $this->instance->get('1_value2'),
127 '2_value1' => $this->instance->get('2_value1'),
128 '3_value1' => $this->instance->get('3_value1'),
131 $this->assertTrue($this->instance->clear());
133 $this->assertEquals([
134 '1_value1' => $value1,
135 '1_value2' => $value2,
136 '2_value1' => $value3,
137 '3_value1' => $value4,
139 '1_value1' => $this->instance->get('1_value1'),
140 '1_value2' => $this->instance->get('1_value2'),
141 '2_value1' => $this->instance->get('2_value1'),
142 '3_value1' => $this->instance->get('3_value1'),
145 $this->assertTrue($this->instance->clear(false));
147 $this->assertEquals([
153 '1_value1' => $this->instance->get('1_value1'),
154 '1_value2' => $this->instance->get('1_value2'),
155 '2_value3' => $this->instance->get('2_value3'),
156 '3_value4' => $this->instance->get('3_value4'),
165 $this->markTestSkipped('taking too much time without mocking');
167 $this->assertNull($this->instance->get('value1'));
170 $this->instance->set('value1', $value, 1);
171 $received = $this->instance->get('value1');
172 $this->assertEquals($value, $received, 'Value received from cache not equal to the original');
176 $this->assertNull($this->instance->get('value1'));
182 * @param $data mixed the data to store in the cache
184 * @dataProvider dataTypesInCache
186 function testDifferentTypesInCache($data)
188 $this->instance->set('val', $data);
189 $received = $this->instance->get('val');
190 $this->assertEquals($data, $received, 'Value type changed from ' . gettype($data) . ' to ' . gettype($received));
196 * @param mixed $value1 a first
197 * @param mixed $value2 a second
198 * @param mixed $value3 a third
200 * @dataProvider dataSimple
202 public function testGetAllKeys($value1, $value2, $value3)
204 $this->assertTrue($this->instance->set('value1', $value1));
205 $this->assertTrue($this->instance->set('value2', $value2));
206 $this->assertTrue($this->instance->set('test_value3', $value3));
208 $list = $this->instance->getAllKeys();
210 $this->assertContains('value1', $list);
211 $this->assertContains('value2', $list);
212 $this->assertContains('test_value3', $list);
214 $list = $this->instance->getAllKeys('test');
216 $this->assertContains('test_value3', $list);
217 $this->assertNotContains('value1', $list);
218 $this->assertNotContains('value2', $list);