3 * @copyright Copyright (C) 2020, Friendica
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Test\src\Core\Cache;
24 use Friendica\Test\MockedTest;
25 use Friendica\Util\PidFile;
27 abstract class CacheTest extends MockedTest
30 * @var int Start time of the mock (used for time operations)
32 protected $startTime = 1417011228;
35 * @var \Friendica\Core\Cache\ICache
40 * @var \Friendica\Core\Cache\IMemoryCache
45 * Dataset for test setting different types in the cache
49 public function dataTypesInCache()
52 'string' => ['data' => 'foobar'],
53 'integer' => ['data' => 1],
54 'boolTrue' => ['data' => true],
55 'boolFalse' => ['data' => false],
56 'float' => ['data' => 4.6634234],
57 'array' => ['data' => ['1', '2', '3', '4', '5']],
58 'object' => ['data' => new PidFile()],
59 'null' => ['data' => null],
64 * Dataset for simple value sets/gets
68 public function dataSimple()
73 'value2' => 'ipsum lorum',
75 'value4' => 'lasttest',
80 abstract protected function getInstance();
82 protected function setUp()
86 $this->instance = $this->getInstance();
88 $this->instance->clear(false);
93 * @dataProvider dataSimple
95 * @param mixed $value1 a first
96 * @param mixed $value2 a second
98 function testSimple($value1, $value2)
100 $this->assertNull($this->instance->get('value1'));
102 $this->instance->set('value1', $value1);
103 $received = $this->instance->get('value1');
104 $this->assertEquals($value1, $received, 'Value received from cache not equal to the original');
106 $this->instance->set('value1', $value2);
107 $received = $this->instance->get('value1');
108 $this->assertEquals($value2, $received, 'Value not overwritten by second set');
110 $this->instance->set('value2', $value1);
111 $received2 = $this->instance->get('value2');
112 $this->assertEquals($value2, $received, 'Value changed while setting other variable');
113 $this->assertEquals($value1, $received2, 'Second value not equal to original');
115 $this->assertNull($this->instance->get('not_set'), 'Unset value not equal to null');
117 $this->assertTrue($this->instance->delete('value1'));
118 $this->assertNull($this->instance->get('value1'));
123 * @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)
132 $value = 'ipsum lorum';
133 $this->instance->set('1_value1', $value1);
134 $this->instance->set('1_value2', $value2);
135 $this->instance->set('2_value1', $value3);
136 $this->instance->set('3_value1', $value4);
138 $this->assertEquals([
139 '1_value1' => $value1,
140 '1_value2' => $value2,
141 '2_value1' => $value3,
142 '3_value1' => $value4,
144 '1_value1' => $this->instance->get('1_value1'),
145 '1_value2' => $this->instance->get('1_value2'),
146 '2_value1' => $this->instance->get('2_value1'),
147 '3_value1' => $this->instance->get('3_value1'),
150 $this->assertTrue($this->instance->clear());
152 $this->assertEquals([
153 '1_value1' => $value1,
154 '1_value2' => $value2,
155 '2_value1' => $value3,
156 '3_value1' => $value4,
158 '1_value1' => $this->instance->get('1_value1'),
159 '1_value2' => $this->instance->get('1_value2'),
160 '2_value1' => $this->instance->get('2_value1'),
161 '3_value1' => $this->instance->get('3_value1'),
164 $this->assertTrue($this->instance->clear(false));
166 $this->assertEquals([
172 '1_value1' => $this->instance->get('1_value1'),
173 '1_value2' => $this->instance->get('1_value2'),
174 '2_value3' => $this->instance->get('2_value3'),
175 '3_value4' => $this->instance->get('3_value4'),
184 $this->markTestSkipped('taking too much time without mocking');
186 $this->assertNull($this->instance->get('value1'));
189 $this->instance->set('value1', $value, 1);
190 $received = $this->instance->get('value1');
191 $this->assertEquals($value, $received, 'Value received from cache not equal to the original');
195 $this->assertNull($this->instance->get('value1'));
201 * @param $data mixed the data to store in the cache
203 * @dataProvider dataTypesInCache
205 function testDifferentTypesInCache($data)
207 $this->instance->set('val', $data);
208 $received = $this->instance->get('val');
209 $this->assertEquals($data, $received, 'Value type changed from ' . gettype($data) . ' to ' . gettype($received));
215 * @param mixed $value1 a first
216 * @param mixed $value2 a second
217 * @param mixed $value3 a third
219 * @dataProvider dataSimple
221 public function testGetAllKeys($value1, $value2, $value3)
223 $this->assertTrue($this->instance->set('value1', $value1));
224 $this->assertTrue($this->instance->set('value2', $value2));
225 $this->assertTrue($this->instance->set('test_value3', $value3));
227 $list = $this->instance->getAllKeys();
229 $this->assertContains('value1', $list);
230 $this->assertContains('value2', $list);
231 $this->assertContains('test_value3', $list);
233 $list = $this->instance->getAllKeys('test');
235 $this->assertContains('test_value3', $list);
236 $this->assertNotContains('value1', $list);
237 $this->assertNotContains('value2', $list);