2018-07-07 18:35:42 +00:00
|
|
|
<?php
|
2020-02-09 14:45:36 +00:00
|
|
|
/**
|
2021-03-29 06:40:20 +00:00
|
|
|
* @copyright Copyright (C) 2010-2021, 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 18:35:42 +00:00
|
|
|
|
|
|
|
namespace Friendica\Test\src\Core\Cache;
|
|
|
|
|
2020-10-18 18:31:57 +00:00
|
|
|
use Exception;
|
2019-08-04 08:26:53 +00:00
|
|
|
use Friendica\Core\Cache\IMemoryCache;
|
2018-07-07 18:35:42 +00:00
|
|
|
|
|
|
|
abstract class MemoryCacheTest extends CacheTest
|
|
|
|
{
|
|
|
|
/**
|
2020-10-18 18:31:57 +00:00
|
|
|
* @var IMemoryCache
|
2018-07-07 18:35:42 +00:00
|
|
|
*/
|
|
|
|
protected $instance;
|
|
|
|
|
2021-04-01 21:04:30 +00:00
|
|
|
protected function setUp(): void
|
2018-07-07 18:35:42 +00:00
|
|
|
{
|
|
|
|
parent::setUp();
|
2019-07-26 13:54:14 +00:00
|
|
|
|
2019-08-04 08:26:53 +00:00
|
|
|
if (!($this->instance instanceof IMemoryCache)) {
|
2020-10-18 18:31:57 +00:00
|
|
|
throw new Exception('MemoryCacheTest unsupported');
|
2018-07-07 18:35:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-18 19:04:18 +00:00
|
|
|
/**
|
|
|
|
* @small
|
2019-01-30 19:26:17 +00:00
|
|
|
* @dataProvider dataSimple
|
2018-07-18 19:04:18 +00:00
|
|
|
*/
|
2020-10-18 18:31:57 +00:00
|
|
|
public function testCompareSet($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 18:35:42 +00:00
|
|
|
|
2019-01-30 19:26:17 +00:00
|
|
|
$this->instance->add('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->compareSet('value1', $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 compareSet');
|
2018-07-07 18:35:42 +00:00
|
|
|
}
|
|
|
|
|
2018-07-18 19:04:18 +00:00
|
|
|
/**
|
|
|
|
* @small
|
2019-01-30 19:26:17 +00:00
|
|
|
* @dataProvider dataSimple
|
2018-07-18 19:04:18 +00:00
|
|
|
*/
|
2020-10-18 18:31:57 +00:00
|
|
|
public function testNegativeCompareSet($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 18:35:42 +00:00
|
|
|
|
2019-01-30 19:26:17 +00:00
|
|
|
$this->instance->add('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->compareSet('value1', 'wrong', $value2);
|
2018-07-07 18:51:08 +00:00
|
|
|
$received = $this->instance->get('value1');
|
2020-10-17 12:19:57 +00:00
|
|
|
self::assertNotEquals($value2, $received, 'Value was wrongly overwritten by compareSet');
|
|
|
|
self::assertEquals($value1, $received, 'Value was wrongly overwritten by any other value');
|
2018-07-07 18:35:42 +00:00
|
|
|
}
|
|
|
|
|
2018-07-18 19:04:18 +00:00
|
|
|
/**
|
|
|
|
* @small
|
2019-01-30 19:26:17 +00:00
|
|
|
* @dataProvider dataSimple
|
2018-07-18 19:04:18 +00:00
|
|
|
*/
|
2020-10-18 18:31:57 +00:00
|
|
|
public function testCompareDelete($data)
|
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 18:35:42 +00:00
|
|
|
|
2019-01-30 19:26:17 +00:00
|
|
|
$this->instance->add('value1', $data);
|
2018-07-07 18:51:08 +00:00
|
|
|
$received = $this->instance->get('value1');
|
2020-10-17 12:19:57 +00:00
|
|
|
self::assertEquals($data, $received, 'Value received from cache not equal to the original');
|
2019-01-30 19:26:17 +00:00
|
|
|
$this->instance->compareDelete('value1', $data);
|
2020-10-17 12:19:57 +00:00
|
|
|
self::assertNull($this->instance->get('value1'), 'Value was not deleted by compareDelete');
|
2018-07-07 18:35:42 +00:00
|
|
|
}
|
|
|
|
|
2018-07-18 19:04:18 +00:00
|
|
|
/**
|
|
|
|
* @small
|
2019-01-30 19:26:17 +00:00
|
|
|
* @dataProvider dataSimple
|
2018-07-18 19:04:18 +00:00
|
|
|
*/
|
2020-10-18 18:31:57 +00:00
|
|
|
public function testNegativeCompareDelete($data)
|
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 18:35:42 +00:00
|
|
|
|
2019-01-30 19:26:17 +00:00
|
|
|
$this->instance->add('value1', $data);
|
2018-07-07 18:51:08 +00:00
|
|
|
$received = $this->instance->get('value1');
|
2020-10-17 12:19:57 +00:00
|
|
|
self::assertEquals($data, $received, 'Value received from cache not equal to the original');
|
2018-07-07 18:35:42 +00:00
|
|
|
$this->instance->compareDelete('value1', 'wrong');
|
2020-10-17 12:19:57 +00:00
|
|
|
self::assertNotNull($this->instance->get('value1'), 'Value was wrongly compareDeleted');
|
2018-07-07 18:35:42 +00:00
|
|
|
|
2019-01-30 19:26:17 +00:00
|
|
|
$this->instance->compareDelete('value1', $data);
|
2020-10-17 12:19:57 +00:00
|
|
|
self::assertNull($this->instance->get('value1'), 'Value was wrongly NOT deleted by compareDelete');
|
2018-07-07 18:35:42 +00:00
|
|
|
}
|
|
|
|
|
2018-07-18 19:04:18 +00:00
|
|
|
/**
|
|
|
|
* @small
|
2019-01-30 19:26:17 +00:00
|
|
|
* @dataProvider dataSimple
|
2018-07-18 19:04:18 +00:00
|
|
|
*/
|
2020-10-18 18:31:57 +00:00
|
|
|
public function testAdd($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 18:35:42 +00:00
|
|
|
|
2019-01-30 19:26:17 +00:00
|
|
|
$this->instance->add('value1', $value1);
|
2018-07-07 18:35:42 +00:00
|
|
|
|
2019-01-30 19:26:17 +00:00
|
|
|
$this->instance->add('value1', $value2);
|
2018-07-07 18:51:08 +00:00
|
|
|
$received = $this->instance->get('value1');
|
2020-10-17 12:19:57 +00:00
|
|
|
self::assertNotEquals($value2, $received, 'Value was wrongly overwritten by add');
|
|
|
|
self::assertEquals($value1, $received, 'Value was wrongly overwritten by any other value');
|
2018-07-07 18:35:42 +00:00
|
|
|
|
|
|
|
$this->instance->delete('value1');
|
2019-01-30 19:26:17 +00:00
|
|
|
$this->instance->add('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 was not overwritten by add');
|
|
|
|
self::assertNotEquals($value1, $received, 'Value was not overwritten by any other value');
|
2018-07-07 18:35:42 +00:00
|
|
|
}
|
2019-08-03 18:48:56 +00:00
|
|
|
}
|