3 * @copyright Copyright (C) 2010-2023, the Friendica project
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\Core\Cache\Capability\ICanCache;
25 use Friendica\Core\Cache\Capability\ICanCacheInMemory;
26 use Friendica\Core\Cache\Type\AbstractCache;
27 use Friendica\Test\MockedTest;
28 use Friendica\Util\PidFile;
30 abstract class CacheTest extends MockedTest
33 * @var int Start time of the mock (used for time operations)
35 protected $startTime = 1417011228;
43 * @var \Friendica\Core\Cache\Capability\ICanCacheInMemory
48 * Dataset for test setting different types in the cache
52 public function dataTypesInCache()
55 'string' => ['data' => 'foobar'],
56 'integer' => ['data' => 1],
57 'boolTrue' => ['data' => true],
58 'boolFalse' => ['data' => false],
59 'float' => ['data' => 4.6634234],
60 'array' => ['data' => ['1', '2', '3', '4', '5']],
61 'object' => ['data' => new PidFile()],
62 'null' => ['data' => null],
67 * Dataset for simple value sets/gets
71 public function dataSimple()
76 'value2' => 'ipsum lorum',
78 'value4' => 'lasttest',
83 abstract protected function getInstance();
85 protected function setUp(): void
89 $this->instance = $this->getInstance();
91 $this->instance->clear(false);
96 * @dataProvider dataSimple
98 * @param mixed $value1 a first
99 * @param mixed $value2 a second
101 public function testSimple($value1, $value2)
103 self::assertNull($this->instance->get('value1'));
105 $this->instance->set('value1', $value1);
106 $received = $this->instance->get('value1');
107 self::assertEquals($value1, $received, 'Value received from cache not equal to the original');
109 $this->instance->set('value1', $value2);
110 $received = $this->instance->get('value1');
111 self::assertEquals($value2, $received, 'Value not overwritten by second set');
113 $this->instance->set('value2', $value1);
114 $received2 = $this->instance->get('value2');
115 self::assertEquals($value2, $received, 'Value changed while setting other variable');
116 self::assertEquals($value1, $received2, 'Second value not equal to original');
118 self::assertNull($this->instance->get('not_set'), 'Unset value not equal to null');
120 self::assertTrue($this->instance->delete('value1'));
121 self::assertNull($this->instance->get('value1'));
126 * @dataProvider dataSimple
128 * @param mixed $value1 a first
129 * @param mixed $value2 a second
130 * @param mixed $value3 a third
131 * @param mixed $value4 a fourth
133 public function testClear($value1, $value2, $value3, $value4)
135 $this->instance->set('1_value1', $value1);
136 $this->instance->set('1_value2', $value2);
137 $this->instance->set('2_value1', $value3);
138 $this->instance->set('3_value1', $value4);
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 self::assertTrue($this->instance->clear());
155 '1_value1' => $value1,
156 '1_value2' => $value2,
157 '2_value1' => $value3,
158 '3_value1' => $value4,
160 '1_value1' => $this->instance->get('1_value1'),
161 '1_value2' => $this->instance->get('1_value2'),
162 '2_value1' => $this->instance->get('2_value1'),
163 '3_value1' => $this->instance->get('3_value1'),
166 self::assertTrue($this->instance->clear(false));
174 '1_value1' => $this->instance->get('1_value1'),
175 '1_value2' => $this->instance->get('1_value2'),
176 '2_value3' => $this->instance->get('2_value3'),
177 '3_value4' => $this->instance->get('3_value4'),
184 public function testTTL()
186 static::markTestSkipped('taking too much time without mocking');
188 self::assertNull($this->instance->get('value1'));
191 $this->instance->set('value1', $value, 1);
192 $received = $this->instance->get('value1');
193 self::assertEquals($value, $received, 'Value received from cache not equal to the original');
197 self::assertNull($this->instance->get('value1'));
203 * @param mixed $data the data to store in the cache
205 * @dataProvider dataTypesInCache
207 public function testDifferentTypesInCache($data)
209 $this->instance->set('val', $data);
210 $received = $this->instance->get('val');
211 self::assertEquals($data, $received, 'Value type changed from ' . gettype($data) . ' to ' . gettype($received));
217 * @param mixed $value1 a first
218 * @param mixed $value2 a second
219 * @param mixed $value3 a third
221 * @dataProvider dataSimple
223 public function testGetAllKeys($value1, $value2, $value3)
225 self::assertTrue($this->instance->set('value1', $value1));
226 self::assertTrue($this->instance->set('value2', $value2));
227 self::assertTrue($this->instance->set('test_value3', $value3));
229 $list = $this->instance->getAllKeys();
231 self::assertContains('value1', $list);
232 self::assertContains('value2', $list);
233 self::assertContains('test_value3', $list);
235 $list = $this->instance->getAllKeys('test');
237 self::assertContains('test_value3', $list);
238 self::assertNotContains('value1', $list);
239 self::assertNotContains('value2', $list);
245 public function testSpaceInKey()
247 self::assertTrue($this->instance->set('key space', 'value'));
248 self::assertEquals('value', $this->instance->get('key space'));
251 public function testGetName()
253 if (defined(get_class($this->instance) . '::NAME')) {
254 self::assertEquals($this->instance::NAME, $this->instance->getName());
256 self::expectNotToPerformAssertions();