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\Core\Cache\ICache;
25 use Friendica\Core\Cache\IMemoryCache;
26 use Friendica\Test\MockedTest;
27 use Friendica\Util\PidFile;
29 abstract class CacheTest extends MockedTest
32 * @var int Start time of the mock (used for time operations)
34 protected $startTime = 1417011228;
47 * Dataset for test setting different types in the cache
51 public function dataTypesInCache()
54 'string' => ['data' => 'foobar'],
55 'integer' => ['data' => 1],
56 'boolTrue' => ['data' => true],
57 'boolFalse' => ['data' => false],
58 'float' => ['data' => 4.6634234],
59 'array' => ['data' => ['1', '2', '3', '4', '5']],
60 'object' => ['data' => new PidFile()],
61 'null' => ['data' => null],
66 * Dataset for simple value sets/gets
70 public function dataSimple()
75 'value2' => 'ipsum lorum',
77 'value4' => 'lasttest',
82 abstract protected function getInstance();
84 protected function setUp()
88 $this->instance = $this->getInstance();
90 $this->instance->clear(false);
95 * @dataProvider dataSimple
97 * @param mixed $value1 a first
98 * @param mixed $value2 a second
100 public function testSimple($value1, $value2)
102 self::assertNull($this->instance->get('value1'));
104 $this->instance->set('value1', $value1);
105 $received = $this->instance->get('value1');
106 self::assertEquals($value1, $received, 'Value received from cache not equal to the original');
108 $this->instance->set('value1', $value2);
109 $received = $this->instance->get('value1');
110 self::assertEquals($value2, $received, 'Value not overwritten by second set');
112 $this->instance->set('value2', $value1);
113 $received2 = $this->instance->get('value2');
114 self::assertEquals($value2, $received, 'Value changed while setting other variable');
115 self::assertEquals($value1, $received2, 'Second value not equal to original');
117 self::assertNull($this->instance->get('not_set'), 'Unset value not equal to null');
119 self::assertTrue($this->instance->delete('value1'));
120 self::assertNull($this->instance->get('value1'));
125 * @dataProvider dataSimple
127 * @param mixed $value1 a first
128 * @param mixed $value2 a second
129 * @param mixed $value3 a third
130 * @param mixed $value4 a fourth
132 public function testClear($value1, $value2, $value3, $value4)
134 $this->instance->set('1_value1', $value1);
135 $this->instance->set('1_value2', $value2);
136 $this->instance->set('2_value1', $value3);
137 $this->instance->set('3_value1', $value4);
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 self::assertTrue($this->instance->clear());
154 '1_value1' => $value1,
155 '1_value2' => $value2,
156 '2_value1' => $value3,
157 '3_value1' => $value4,
159 '1_value1' => $this->instance->get('1_value1'),
160 '1_value2' => $this->instance->get('1_value2'),
161 '2_value1' => $this->instance->get('2_value1'),
162 '3_value1' => $this->instance->get('3_value1'),
165 self::assertTrue($this->instance->clear(false));
173 '1_value1' => $this->instance->get('1_value1'),
174 '1_value2' => $this->instance->get('1_value2'),
175 '2_value3' => $this->instance->get('2_value3'),
176 '3_value4' => $this->instance->get('3_value4'),
183 public function testTTL()
185 static::markTestSkipped('taking too much time without mocking');
187 self::assertNull($this->instance->get('value1'));
190 $this->instance->set('value1', $value, 1);
191 $received = $this->instance->get('value1');
192 self::assertEquals($value, $received, 'Value received from cache not equal to the original');
196 self::assertNull($this->instance->get('value1'));
202 * @param mixed $data the data to store in the cache
204 * @dataProvider dataTypesInCache
206 public function testDifferentTypesInCache($data)
208 $this->instance->set('val', $data);
209 $received = $this->instance->get('val');
210 self::assertEquals($data, $received, 'Value type changed from ' . gettype($data) . ' to ' . gettype($received));
216 * @param mixed $value1 a first
217 * @param mixed $value2 a second
218 * @param mixed $value3 a third
220 * @dataProvider dataSimple
222 public function testGetAllKeys($value1, $value2, $value3)
224 self::assertTrue($this->instance->set('value1', $value1));
225 self::assertTrue($this->instance->set('value2', $value2));
226 self::assertTrue($this->instance->set('test_value3', $value3));
228 $list = $this->instance->getAllKeys();
230 self::assertContains('value1', $list);
231 self::assertContains('value2', $list);
232 self::assertContains('test_value3', $list);
234 $list = $this->instance->getAllKeys('test');
236 self::assertContains('test_value3', $list);
237 self::assertNotContains('value1', $list);
238 self::assertNotContains('value2', $list);