2018-07-07 17:46:16 +00:00
|
|
|
<?php
|
2020-02-09 14:45:36 +00:00
|
|
|
/**
|
2022-01-02 07:27:47 +00:00
|
|
|
* @copyright Copyright (C) 2010-2022, the Friendica project
|
2020-02-09 14:45:36 +00:00
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
2018-07-07 17:46:16 +00:00
|
|
|
|
|
|
|
namespace Friendica\Test\src\Core\Cache;
|
|
|
|
|
2021-10-26 19:44:29 +00:00
|
|
|
use Friendica\Core\Cache\Capability\ICanCache;
|
|
|
|
use Friendica\Core\Cache\Capability\ICanCacheInMemory;
|
2019-01-30 19:26:17 +00:00
|
|
|
use Friendica\Test\MockedTest;
|
|
|
|
use Friendica\Util\PidFile;
|
2018-07-07 17:46:16 +00:00
|
|
|
|
2019-01-30 19:26:17 +00:00
|
|
|
abstract class CacheTest extends MockedTest
|
2018-07-07 17:46:16 +00:00
|
|
|
{
|
2019-01-30 19:26:17 +00:00
|
|
|
/**
|
|
|
|
* @var int Start time of the mock (used for time operations)
|
|
|
|
*/
|
|
|
|
protected $startTime = 1417011228;
|
|
|
|
|
2018-07-07 17:46:16 +00:00
|
|
|
/**
|
2021-10-26 19:44:29 +00:00
|
|
|
* @var ICanCache
|
2018-07-07 17:46:16 +00:00
|
|
|
*/
|
|
|
|
protected $instance;
|
|
|
|
|
2018-10-06 22:27:54 +00:00
|
|
|
/**
|
2021-10-26 19:44:29 +00:00
|
|
|
* @var \Friendica\Core\Cache\Capability\ICanCacheInMemory
|
2018-10-06 22:27:54 +00:00
|
|
|
*/
|
|
|
|
protected $cache;
|
|
|
|
|
2019-01-30 19:26:17 +00:00
|
|
|
/**
|
|
|
|
* Dataset for test setting different types in the cache
|
2019-08-03 18:48:56 +00:00
|
|
|
*
|
2019-01-30 19:26:17 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function dataTypesInCache()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'string' => ['data' => 'foobar'],
|
|
|
|
'integer' => ['data' => 1],
|
|
|
|
'boolTrue' => ['data' => true],
|
|
|
|
'boolFalse' => ['data' => false],
|
|
|
|
'float' => ['data' => 4.6634234],
|
|
|
|
'array' => ['data' => ['1', '2', '3', '4', '5']],
|
|
|
|
'object' => ['data' => new PidFile()],
|
|
|
|
'null' => ['data' => null],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Dataset for simple value sets/gets
|
2019-08-03 18:48:56 +00:00
|
|
|
*
|
2019-01-30 19:26:17 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function dataSimple()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'string' => [
|
|
|
|
'value1' => 'foobar',
|
|
|
|
'value2' => 'ipsum lorum',
|
|
|
|
'value3' => 'test',
|
|
|
|
'value4' => 'lasttest',
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
2018-10-06 22:27:54 +00:00
|
|
|
|
2018-07-07 17:46:16 +00:00
|
|
|
abstract protected function getInstance();
|
|
|
|
|
2021-04-01 21:04:30 +00:00
|
|
|
protected function setUp(): void
|
2018-07-07 17:46:16 +00:00
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
2019-01-30 19:26:17 +00:00
|
|
|
$this->instance = $this->getInstance();
|
2018-07-07 17:46:16 +00:00
|
|
|
|
2018-10-06 22:27:54 +00:00
|
|
|
$this->instance->clear(false);
|
2018-07-07 17:46:16 +00:00
|
|
|
}
|
|
|
|
|
2018-07-18 19:04:18 +00:00
|
|
|
/**
|
|
|
|
* @small
|
2019-01-30 19:26:17 +00:00
|
|
|
* @dataProvider dataSimple
|
2019-08-03 18:48:56 +00:00
|
|
|
*
|
2019-01-30 19:26:17 +00:00
|
|
|
* @param mixed $value1 a first
|
|
|
|
* @param mixed $value2 a second
|
2018-07-18 19:04:18 +00:00
|
|
|
*/
|
2020-10-18 18:31:57 +00:00
|
|
|
public function testSimple($value1, $value2)
|
2019-08-03 18:48:56 +00:00
|
|
|
{
|
2020-10-17 12:19:57 +00:00
|
|
|
self::assertNull($this->instance->get('value1'));
|
2018-07-07 17:46:16 +00:00
|
|
|
|
2019-01-30 19:26:17 +00:00
|
|
|
$this->instance->set('value1', $value1);
|
2018-07-07 18:51:08 +00:00
|
|
|
$received = $this->instance->get('value1');
|
2020-10-17 12:19:57 +00:00
|
|
|
self::assertEquals($value1, $received, 'Value received from cache not equal to the original');
|
2018-07-07 18:51:08 +00:00
|
|
|
|
2019-01-30 19:26:17 +00:00
|
|
|
$this->instance->set('value1', $value2);
|
2018-07-07 18:51:08 +00:00
|
|
|
$received = $this->instance->get('value1');
|
2020-10-17 12:19:57 +00:00
|
|
|
self::assertEquals($value2, $received, 'Value not overwritten by second set');
|
2018-07-07 17:46:16 +00:00
|
|
|
|
2019-01-30 19:26:17 +00:00
|
|
|
$this->instance->set('value2', $value1);
|
2018-07-07 18:51:08 +00:00
|
|
|
$received2 = $this->instance->get('value2');
|
2020-10-17 12:19:57 +00:00
|
|
|
self::assertEquals($value2, $received, 'Value changed while setting other variable');
|
|
|
|
self::assertEquals($value1, $received2, 'Second value not equal to original');
|
2018-07-07 17:46:16 +00:00
|
|
|
|
2020-10-17 12:19:57 +00:00
|
|
|
self::assertNull($this->instance->get('not_set'), 'Unset value not equal to null');
|
2018-07-07 17:46:16 +00:00
|
|
|
|
2020-10-17 12:19:57 +00:00
|
|
|
self::assertTrue($this->instance->delete('value1'));
|
|
|
|
self::assertNull($this->instance->get('value1'));
|
2018-07-07 17:46:16 +00:00
|
|
|
}
|
|
|
|
|
2018-07-18 19:04:18 +00:00
|
|
|
/**
|
|
|
|
* @small
|
2019-01-30 19:26:17 +00:00
|
|
|
* @dataProvider dataSimple
|
2019-08-03 18:48:56 +00:00
|
|
|
*
|
2019-01-30 19:26:17 +00:00
|
|
|
* @param mixed $value1 a first
|
|
|
|
* @param mixed $value2 a second
|
|
|
|
* @param mixed $value3 a third
|
|
|
|
* @param mixed $value4 a fourth
|
2018-07-18 19:04:18 +00:00
|
|
|
*/
|
2020-10-18 18:31:57 +00:00
|
|
|
public function testClear($value1, $value2, $value3, $value4)
|
2019-08-03 18:48:56 +00:00
|
|
|
{
|
2019-01-30 19:26:17 +00:00
|
|
|
$this->instance->set('1_value1', $value1);
|
|
|
|
$this->instance->set('1_value2', $value2);
|
|
|
|
$this->instance->set('2_value1', $value3);
|
|
|
|
$this->instance->set('3_value1', $value4);
|
2018-07-07 17:46:16 +00:00
|
|
|
|
2020-10-17 12:19:57 +00:00
|
|
|
self::assertEquals([
|
2019-01-30 19:26:17 +00:00
|
|
|
'1_value1' => $value1,
|
|
|
|
'1_value2' => $value2,
|
|
|
|
'2_value1' => $value3,
|
|
|
|
'3_value1' => $value4,
|
2018-07-07 17:46:16 +00:00
|
|
|
], [
|
|
|
|
'1_value1' => $this->instance->get('1_value1'),
|
|
|
|
'1_value2' => $this->instance->get('1_value2'),
|
|
|
|
'2_value1' => $this->instance->get('2_value1'),
|
|
|
|
'3_value1' => $this->instance->get('3_value1'),
|
|
|
|
]);
|
|
|
|
|
2020-10-17 12:19:57 +00:00
|
|
|
self::assertTrue($this->instance->clear());
|
2018-09-06 06:11:18 +00:00
|
|
|
|
2020-10-17 12:19:57 +00:00
|
|
|
self::assertEquals([
|
2019-01-30 19:26:17 +00:00
|
|
|
'1_value1' => $value1,
|
|
|
|
'1_value2' => $value2,
|
|
|
|
'2_value1' => $value3,
|
|
|
|
'3_value1' => $value4,
|
2018-09-06 06:11:18 +00:00
|
|
|
], [
|
|
|
|
'1_value1' => $this->instance->get('1_value1'),
|
|
|
|
'1_value2' => $this->instance->get('1_value2'),
|
|
|
|
'2_value1' => $this->instance->get('2_value1'),
|
|
|
|
'3_value1' => $this->instance->get('3_value1'),
|
|
|
|
]);
|
|
|
|
|
2020-10-17 12:19:57 +00:00
|
|
|
self::assertTrue($this->instance->clear(false));
|
2018-07-07 17:46:16 +00:00
|
|
|
|
2020-10-17 12:19:57 +00:00
|
|
|
self::assertEquals([
|
2018-07-07 17:46:16 +00:00
|
|
|
'1_value1' => null,
|
|
|
|
'1_value2' => null,
|
2018-09-06 06:11:18 +00:00
|
|
|
'2_value3' => null,
|
|
|
|
'3_value4' => null,
|
2018-07-07 17:46:16 +00:00
|
|
|
], [
|
|
|
|
'1_value1' => $this->instance->get('1_value1'),
|
|
|
|
'1_value2' => $this->instance->get('1_value2'),
|
2018-09-06 06:11:18 +00:00
|
|
|
'2_value3' => $this->instance->get('2_value3'),
|
|
|
|
'3_value4' => $this->instance->get('3_value4'),
|
2018-07-07 17:46:16 +00:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2018-07-18 19:04:18 +00:00
|
|
|
/**
|
|
|
|
* @medium
|
|
|
|
*/
|
2020-10-18 18:31:57 +00:00
|
|
|
public function testTTL()
|
2019-08-03 18:48:56 +00:00
|
|
|
{
|
2020-10-18 18:31:57 +00:00
|
|
|
static::markTestSkipped('taking too much time without mocking');
|
2018-11-09 19:39:47 +00:00
|
|
|
|
2020-10-17 12:19:57 +00:00
|
|
|
self::assertNull($this->instance->get('value1'));
|
2018-07-07 17:46:16 +00:00
|
|
|
|
2018-07-07 18:51:08 +00:00
|
|
|
$value = 'foobar';
|
2018-07-07 17:46:16 +00:00
|
|
|
$this->instance->set('value1', $value, 1);
|
2018-07-07 18:51:08 +00:00
|
|
|
$received = $this->instance->get('value1');
|
2020-10-17 12:19:57 +00:00
|
|
|
self::assertEquals($value, $received, 'Value received from cache not equal to the original');
|
2018-07-07 17:46:16 +00:00
|
|
|
|
|
|
|
sleep(2);
|
|
|
|
|
2020-10-17 12:19:57 +00:00
|
|
|
self::assertNull($this->instance->get('value1'));
|
2018-07-07 17:46:16 +00:00
|
|
|
}
|
2018-07-08 11:35:28 +00:00
|
|
|
|
2018-07-18 19:04:18 +00:00
|
|
|
/**
|
|
|
|
* @small
|
2019-08-03 18:48:56 +00:00
|
|
|
*
|
2020-10-18 18:31:57 +00:00
|
|
|
* @param mixed $data the data to store in the cache
|
2019-08-03 18:48:56 +00:00
|
|
|
*
|
2019-01-30 19:26:17 +00:00
|
|
|
* @dataProvider dataTypesInCache
|
2018-07-18 19:04:18 +00:00
|
|
|
*/
|
2020-10-18 18:31:57 +00:00
|
|
|
public function testDifferentTypesInCache($data)
|
2019-08-03 18:48:56 +00:00
|
|
|
{
|
2019-01-30 19:26:17 +00:00
|
|
|
$this->instance->set('val', $data);
|
|
|
|
$received = $this->instance->get('val');
|
2020-10-17 12:19:57 +00:00
|
|
|
self::assertEquals($data, $received, 'Value type changed from ' . gettype($data) . ' to ' . gettype($received));
|
2018-07-08 11:35:28 +00:00
|
|
|
}
|
2018-10-06 22:27:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @small
|
2019-08-03 18:48:56 +00:00
|
|
|
*
|
2019-01-30 19:26:17 +00:00
|
|
|
* @param mixed $value1 a first
|
|
|
|
* @param mixed $value2 a second
|
|
|
|
* @param mixed $value3 a third
|
2019-08-03 18:48:56 +00:00
|
|
|
*
|
2019-01-30 19:26:17 +00:00
|
|
|
* @dataProvider dataSimple
|
2018-10-06 22:27:54 +00:00
|
|
|
*/
|
2019-08-03 18:48:56 +00:00
|
|
|
public function testGetAllKeys($value1, $value2, $value3)
|
|
|
|
{
|
2020-10-17 12:19:57 +00:00
|
|
|
self::assertTrue($this->instance->set('value1', $value1));
|
|
|
|
self::assertTrue($this->instance->set('value2', $value2));
|
|
|
|
self::assertTrue($this->instance->set('test_value3', $value3));
|
2018-10-06 22:27:54 +00:00
|
|
|
|
|
|
|
$list = $this->instance->getAllKeys();
|
|
|
|
|
2020-10-17 12:19:57 +00:00
|
|
|
self::assertContains('value1', $list);
|
|
|
|
self::assertContains('value2', $list);
|
|
|
|
self::assertContains('test_value3', $list);
|
2018-10-06 22:27:54 +00:00
|
|
|
|
|
|
|
$list = $this->instance->getAllKeys('test');
|
|
|
|
|
2020-10-17 12:19:57 +00:00
|
|
|
self::assertContains('test_value3', $list);
|
|
|
|
self::assertNotContains('value1', $list);
|
|
|
|
self::assertNotContains('value2', $list);
|
2018-10-06 22:27:54 +00:00
|
|
|
}
|
2022-06-26 09:56:13 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @small
|
|
|
|
*/
|
|
|
|
public function testSpaceInKey()
|
|
|
|
{
|
|
|
|
self::assertTrue($this->instance->set('key space', 'value'));
|
|
|
|
self::assertEquals('value', $this->instance->get('key space'));
|
|
|
|
}
|
2018-07-07 17:46:16 +00:00
|
|
|
}
|