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\DateTimeFormatMockTrait;
9 use Friendica\Test\Util\VFSTrait;
10 use Friendica\Util\PidFile;
12 abstract class CacheTest extends MockedTest
16 use DateTimeFormatMockTrait;
19 * @var int Start time of the mock (used for time operations)
21 protected $startTime = 1417011228;
24 * @var \Friendica\Core\Cache\ICacheDriver
29 * @var \Friendica\Core\Cache\IMemoryCacheDriver
34 * Dataset for test setting different types in the cache
37 public function dataTypesInCache()
40 'string' => ['data' => 'foobar'],
41 'integer' => ['data' => 1],
42 'boolTrue' => ['data' => true],
43 'boolFalse' => ['data' => false],
44 'float' => ['data' => 4.6634234],
45 'array' => ['data' => ['1', '2', '3', '4', '5']],
46 'object' => ['data' => new PidFile()],
47 'null' => ['data' => null],
52 * Dataset for simple value sets/gets
55 public function dataSimple()
60 'value2' => 'ipsum lorum',
62 'value4' => 'lasttest',
67 abstract protected function getInstance();
69 protected function setUp()
72 $this->mockApp($this->root);
74 ->shouldReceive('getHostname')
75 ->andReturn('friendica.local');
77 $this->mockUtcNow($this->startTime);
81 $this->instance = $this->getInstance();
84 $this->mockConfigGet('config', 'hostname', 'localhost');
85 $this->mockConfigGet('system', 'throttle_limit_day', 100);
86 $this->mockConfigGet('system', 'throttle_limit_week', 100);
87 $this->mockConfigGet('system', 'throttle_limit_month', 100);
88 $this->mockConfigGet('system', 'theme', 'system_theme');
90 $this->instance->clear(false);
95 * @dataProvider dataSimple
96 * @param mixed $value1 a first
97 * @param mixed $value2 a second
99 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
124 * @param mixed $value1 a first
125 * @param mixed $value2 a second
126 * @param mixed $value3 a third
127 * @param mixed $value4 a fourth
129 function testClear($value1, $value2, $value3, $value4) {
130 $value = 'ipsum lorum';
131 $this->instance->set('1_value1', $value1);
132 $this->instance->set('1_value2', $value2);
133 $this->instance->set('2_value1', $value3);
134 $this->instance->set('3_value1', $value4);
136 $this->assertEquals([
137 '1_value1' => $value1,
138 '1_value2' => $value2,
139 '2_value1' => $value3,
140 '3_value1' => $value4,
142 '1_value1' => $this->instance->get('1_value1'),
143 '1_value2' => $this->instance->get('1_value2'),
144 '2_value1' => $this->instance->get('2_value1'),
145 '3_value1' => $this->instance->get('3_value1'),
148 $this->assertTrue($this->instance->clear());
150 $this->assertEquals([
151 '1_value1' => $value1,
152 '1_value2' => $value2,
153 '2_value1' => $value3,
154 '3_value1' => $value4,
156 '1_value1' => $this->instance->get('1_value1'),
157 '1_value2' => $this->instance->get('1_value2'),
158 '2_value1' => $this->instance->get('2_value1'),
159 '3_value1' => $this->instance->get('3_value1'),
162 $this->assertTrue($this->instance->clear(false));
164 $this->assertEquals([
170 '1_value1' => $this->instance->get('1_value1'),
171 '1_value2' => $this->instance->get('1_value2'),
172 '2_value3' => $this->instance->get('2_value3'),
173 '3_value4' => $this->instance->get('3_value4'),
181 $this->markTestSkipped('taking too much time without mocking');
183 $this->assertNull($this->instance->get('value1'));
186 $this->instance->set('value1', $value, 1);
187 $received = $this->instance->get('value1');
188 $this->assertEquals($value, $received, 'Value received from cache not equal to the original');
192 $this->assertNull($this->instance->get('value1'));
197 * @param $data mixed the data to store in the cache
198 * @dataProvider dataTypesInCache
200 function testDifferentTypesInCache($data) {
201 $this->instance->set('val', $data);
202 $received = $this->instance->get('val');
203 $this->assertEquals($data, $received, 'Value type changed from ' . gettype($data) . ' to ' . gettype($received));
208 * @param mixed $value1 a first
209 * @param mixed $value2 a second
210 * @param mixed $value3 a third
211 * @dataProvider dataSimple
213 public function testGetAllKeys($value1, $value2, $value3) {
214 if ($this->cache instanceof MemcachedCacheDriver) {
215 $this->markTestSkipped('Memcached doesn\'t support getAllKeys anymore');
218 $this->assertTrue($this->instance->set('value1', $value1));
219 $this->assertTrue($this->instance->set('value2', $value2));
220 $this->assertTrue($this->instance->set('test_value3', $value3));
222 $list = $this->instance->getAllKeys();
224 $this->assertContains('value1', $list);
225 $this->assertContains('value2', $list);
226 $this->assertContains('test_value3', $list);
228 $list = $this->instance->getAllKeys('test');
230 $this->assertContains('test_value3', $list);