2018-07-07 20:35:42 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Friendica\Test\src\Core\Cache;
|
|
|
|
|
|
|
|
use Friendica\Core\Cache\IMemoryCacheDriver;
|
2019-07-26 15:54:14 +02:00
|
|
|
use Psr\Log\LoggerInterface;
|
2019-07-12 22:38:50 +02:00
|
|
|
use Psr\Log\NullLogger;
|
2018-07-07 20:35:42 +02:00
|
|
|
|
|
|
|
abstract class MemoryCacheTest extends CacheTest
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var \Friendica\Core\Cache\IMemoryCacheDriver
|
|
|
|
*/
|
|
|
|
protected $instance;
|
|
|
|
|
2018-10-09 19:58:58 +02:00
|
|
|
protected function setUp()
|
2018-07-07 20:35:42 +02:00
|
|
|
{
|
|
|
|
parent::setUp();
|
2019-07-26 15:54:14 +02:00
|
|
|
|
|
|
|
$logger = new NullLogger();
|
|
|
|
$this->dice->shouldReceive('create')
|
|
|
|
->with(LoggerInterface::class)
|
|
|
|
->andReturn($logger);
|
|
|
|
|
2018-07-07 20:35:42 +02:00
|
|
|
if (!($this->instance instanceof IMemoryCacheDriver)) {
|
|
|
|
throw new \Exception('MemoryCacheTest unsupported');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-18 21:04:18 +02:00
|
|
|
/**
|
|
|
|
* @small
|
2019-01-30 20:26:17 +01:00
|
|
|
* @dataProvider dataSimple
|
2018-07-18 21:04:18 +02:00
|
|
|
*/
|
2019-01-30 20:26:17 +01:00
|
|
|
function testCompareSet($value1, $value2) {
|
2018-07-07 20:35:42 +02:00
|
|
|
$this->assertNull($this->instance->get('value1'));
|
|
|
|
|
2019-01-30 20:26:17 +01:00
|
|
|
$this->instance->add('value1', $value1);
|
2018-07-07 20:51:08 +02:00
|
|
|
$received = $this->instance->get('value1');
|
2019-01-30 20:26:17 +01:00
|
|
|
$this->assertEquals($value1, $received, 'Value received from cache not equal to the original');
|
2018-07-07 20:51:08 +02:00
|
|
|
|
2019-01-30 20:26:17 +01:00
|
|
|
$this->instance->compareSet('value1', $value1, $value2);
|
2018-07-07 20:51:08 +02:00
|
|
|
$received = $this->instance->get('value1');
|
2019-01-30 20:26:17 +01:00
|
|
|
$this->assertEquals($value2, $received, 'Value not overwritten by compareSet');
|
2018-07-07 20:35:42 +02:00
|
|
|
}
|
|
|
|
|
2018-07-18 21:04:18 +02:00
|
|
|
/**
|
|
|
|
* @small
|
2019-01-30 20:26:17 +01:00
|
|
|
* @dataProvider dataSimple
|
2018-07-18 21:04:18 +02:00
|
|
|
*/
|
2019-01-30 20:26:17 +01:00
|
|
|
function testNegativeCompareSet($value1, $value2) {
|
2018-07-07 20:35:42 +02:00
|
|
|
$this->assertNull($this->instance->get('value1'));
|
|
|
|
|
2019-01-30 20:26:17 +01:00
|
|
|
$this->instance->add('value1', $value1);
|
2018-07-07 20:51:08 +02:00
|
|
|
$received = $this->instance->get('value1');
|
2019-01-30 20:26:17 +01:00
|
|
|
$this->assertEquals($value1, $received, 'Value received from cache not equal to the original');
|
2018-07-07 20:51:08 +02:00
|
|
|
|
2019-01-30 20:26:17 +01:00
|
|
|
$this->instance->compareSet('value1', 'wrong', $value2);
|
2018-07-07 20:51:08 +02:00
|
|
|
$received = $this->instance->get('value1');
|
2019-01-30 20:26:17 +01:00
|
|
|
$this->assertNotEquals($value2, $received, 'Value was wrongly overwritten by compareSet');
|
|
|
|
$this->assertEquals($value1, $received, 'Value was wrongly overwritten by any other value');
|
2018-07-07 20:35:42 +02:00
|
|
|
}
|
|
|
|
|
2018-07-18 21:04:18 +02:00
|
|
|
/**
|
|
|
|
* @small
|
2019-01-30 20:26:17 +01:00
|
|
|
* @dataProvider dataSimple
|
2018-07-18 21:04:18 +02:00
|
|
|
*/
|
2019-01-30 20:26:17 +01:00
|
|
|
function testCompareDelete($data) {
|
2018-07-07 20:35:42 +02:00
|
|
|
$this->assertNull($this->instance->get('value1'));
|
|
|
|
|
2019-01-30 20:26:17 +01:00
|
|
|
$this->instance->add('value1', $data);
|
2018-07-07 20:51:08 +02:00
|
|
|
$received = $this->instance->get('value1');
|
2019-01-30 20:26:17 +01:00
|
|
|
$this->assertEquals($data, $received, 'Value received from cache not equal to the original');
|
|
|
|
$this->instance->compareDelete('value1', $data);
|
2018-07-07 20:35:42 +02:00
|
|
|
$this->assertNull($this->instance->get('value1'), 'Value was not deleted by compareDelete');
|
|
|
|
}
|
|
|
|
|
2018-07-18 21:04:18 +02:00
|
|
|
/**
|
|
|
|
* @small
|
2019-01-30 20:26:17 +01:00
|
|
|
* @dataProvider dataSimple
|
2018-07-18 21:04:18 +02:00
|
|
|
*/
|
2019-01-30 20:26:17 +01:00
|
|
|
function testNegativeCompareDelete($data) {
|
2018-07-07 20:35:42 +02:00
|
|
|
$this->assertNull($this->instance->get('value1'));
|
|
|
|
|
2019-01-30 20:26:17 +01:00
|
|
|
$this->instance->add('value1', $data);
|
2018-07-07 20:51:08 +02:00
|
|
|
$received = $this->instance->get('value1');
|
2019-01-30 20:26:17 +01:00
|
|
|
$this->assertEquals($data, $received, 'Value received from cache not equal to the original');
|
2018-07-07 20:35:42 +02:00
|
|
|
$this->instance->compareDelete('value1', 'wrong');
|
|
|
|
$this->assertNotNull($this->instance->get('value1'), 'Value was wrongly compareDeleted');
|
|
|
|
|
2019-01-30 20:26:17 +01:00
|
|
|
$this->instance->compareDelete('value1', $data);
|
2018-07-07 20:35:42 +02:00
|
|
|
$this->assertNull($this->instance->get('value1'), 'Value was wrongly NOT deleted by compareDelete');
|
|
|
|
}
|
|
|
|
|
2018-07-18 21:04:18 +02:00
|
|
|
/**
|
|
|
|
* @small
|
2019-01-30 20:26:17 +01:00
|
|
|
* @dataProvider dataSimple
|
2018-07-18 21:04:18 +02:00
|
|
|
*/
|
2019-01-30 20:26:17 +01:00
|
|
|
function testAdd($value1, $value2) {
|
2018-07-07 20:35:42 +02:00
|
|
|
$this->assertNull($this->instance->get('value1'));
|
|
|
|
|
2019-01-30 20:26:17 +01:00
|
|
|
$this->instance->add('value1', $value1);
|
2018-07-07 20:35:42 +02:00
|
|
|
|
2019-01-30 20:26:17 +01:00
|
|
|
$this->instance->add('value1', $value2);
|
2018-07-07 20:51:08 +02:00
|
|
|
$received = $this->instance->get('value1');
|
2019-01-30 20:26:17 +01:00
|
|
|
$this->assertNotEquals($value2, $received, 'Value was wrongly overwritten by add');
|
|
|
|
$this->assertEquals($value1, $received, 'Value was wrongly overwritten by any other value');
|
2018-07-07 20:35:42 +02:00
|
|
|
|
|
|
|
$this->instance->delete('value1');
|
2019-01-30 20:26:17 +01:00
|
|
|
$this->instance->add('value1', $value2);
|
2018-07-07 20:51:08 +02:00
|
|
|
$received = $this->instance->get('value1');
|
2019-01-30 20:26:17 +01:00
|
|
|
$this->assertEquals($value2, $received, 'Value was not overwritten by add');
|
|
|
|
$this->assertNotEquals($value1, $received, 'Value was not overwritten by any other value');
|
2018-07-07 20:35:42 +02:00
|
|
|
}
|
|
|
|
}
|