2018-07-07 18:35:42 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Friendica\Test\src\Core\Cache;
|
|
|
|
|
|
|
|
use Friendica\Core\Cache\IMemoryCacheDriver;
|
|
|
|
|
|
|
|
abstract class MemoryCacheTest extends CacheTest
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var \Friendica\Core\Cache\IMemoryCacheDriver
|
|
|
|
*/
|
|
|
|
protected $instance;
|
|
|
|
|
|
|
|
function setUp()
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
if (!($this->instance instanceof IMemoryCacheDriver)) {
|
|
|
|
throw new \Exception('MemoryCacheTest unsupported');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-18 19:04:18 +00:00
|
|
|
/**
|
|
|
|
* @small
|
|
|
|
*/
|
2018-07-07 18:35:42 +00:00
|
|
|
function testCompareSet() {
|
|
|
|
$this->assertNull($this->instance->get('value1'));
|
|
|
|
|
2018-07-07 18:51:08 +00:00
|
|
|
$value = 'foobar';
|
2018-07-07 18:35:42 +00:00
|
|
|
$this->instance->add('value1', $value);
|
2018-07-07 18:51:08 +00:00
|
|
|
$received = $this->instance->get('value1');
|
2018-07-07 18:35:42 +00:00
|
|
|
$this->assertEquals($value, $received, 'Value received from cache not equal to the original');
|
2018-07-07 18:51:08 +00:00
|
|
|
|
|
|
|
$newValue = 'ipsum lorum';
|
2018-07-07 18:35:42 +00:00
|
|
|
$this->instance->compareSet('value1', $value, $newValue);
|
2018-07-07 18:51:08 +00:00
|
|
|
$received = $this->instance->get('value1');
|
2018-07-07 18:35:42 +00:00
|
|
|
$this->assertEquals($newValue, $received, 'Value not overwritten by compareSet');
|
|
|
|
}
|
|
|
|
|
2018-07-18 19:04:18 +00:00
|
|
|
/**
|
|
|
|
* @small
|
|
|
|
*/
|
2018-07-07 18:35:42 +00:00
|
|
|
function testNegativeCompareSet() {
|
|
|
|
$this->assertNull($this->instance->get('value1'));
|
|
|
|
|
2018-07-07 18:51:08 +00:00
|
|
|
$value = 'foobar';
|
2018-07-07 18:35:42 +00:00
|
|
|
$this->instance->add('value1', $value);
|
2018-07-07 18:51:08 +00:00
|
|
|
$received = $this->instance->get('value1');
|
2018-07-07 18:35:42 +00:00
|
|
|
$this->assertEquals($value, $received, 'Value received from cache not equal to the original');
|
2018-07-07 18:51:08 +00:00
|
|
|
|
|
|
|
$newValue = 'ipsum lorum';
|
2018-07-07 18:35:42 +00:00
|
|
|
$this->instance->compareSet('value1', 'wrong', $newValue);
|
2018-07-07 18:51:08 +00:00
|
|
|
$received = $this->instance->get('value1');
|
2018-07-07 18:35:42 +00:00
|
|
|
$this->assertNotEquals($newValue, $received, 'Value was wrongly overwritten by compareSet');
|
|
|
|
$this->assertEquals($value, $received, 'Value was wrongly overwritten by any other value');
|
|
|
|
}
|
|
|
|
|
2018-07-18 19:04:18 +00:00
|
|
|
/**
|
|
|
|
* @small
|
|
|
|
*/
|
2018-07-07 18:35:42 +00:00
|
|
|
function testCompareDelete() {
|
|
|
|
$this->assertNull($this->instance->get('value1'));
|
|
|
|
|
2018-07-07 18:51:08 +00:00
|
|
|
$value = 'foobar';
|
2018-07-07 18:35:42 +00:00
|
|
|
$this->instance->add('value1', $value);
|
2018-07-07 18:51:08 +00:00
|
|
|
$received = $this->instance->get('value1');
|
2018-07-07 18:35:42 +00:00
|
|
|
$this->assertEquals($value, $received, 'Value received from cache not equal to the original');
|
|
|
|
$this->instance->compareDelete('value1', $value);
|
|
|
|
$this->assertNull($this->instance->get('value1'), 'Value was not deleted by compareDelete');
|
|
|
|
}
|
|
|
|
|
2018-07-18 19:04:18 +00:00
|
|
|
/**
|
|
|
|
* @small
|
|
|
|
*/
|
2018-07-07 18:35:42 +00:00
|
|
|
function testNegativeCompareDelete() {
|
|
|
|
$this->assertNull($this->instance->get('value1'));
|
|
|
|
|
2018-07-07 18:51:08 +00:00
|
|
|
$value = 'foobar';
|
2018-07-07 18:35:42 +00:00
|
|
|
$this->instance->add('value1', $value);
|
2018-07-07 18:51:08 +00:00
|
|
|
$received = $this->instance->get('value1');
|
2018-07-07 18:35:42 +00:00
|
|
|
$this->assertEquals($value, $received, 'Value received from cache not equal to the original');
|
|
|
|
$this->instance->compareDelete('value1', 'wrong');
|
|
|
|
$this->assertNotNull($this->instance->get('value1'), 'Value was wrongly compareDeleted');
|
|
|
|
|
|
|
|
$this->instance->compareDelete('value1', $value);
|
|
|
|
$this->assertNull($this->instance->get('value1'), 'Value was wrongly NOT deleted by compareDelete');
|
|
|
|
}
|
|
|
|
|
2018-07-18 19:04:18 +00:00
|
|
|
/**
|
|
|
|
* @small
|
|
|
|
*/
|
2018-07-07 18:35:42 +00:00
|
|
|
function testAdd() {
|
|
|
|
$this->assertNull($this->instance->get('value1'));
|
|
|
|
|
2018-07-07 18:51:08 +00:00
|
|
|
$value = 'foobar';
|
2018-07-07 18:35:42 +00:00
|
|
|
$this->instance->add('value1', $value);
|
|
|
|
|
2018-07-07 18:51:08 +00:00
|
|
|
$newValue = 'ipsum lorum';
|
2018-07-07 18:35:42 +00:00
|
|
|
$this->instance->add('value1', $newValue);
|
2018-07-07 18:51:08 +00:00
|
|
|
$received = $this->instance->get('value1');
|
2018-07-07 18:35:42 +00:00
|
|
|
$this->assertNotEquals($newValue, $received, 'Value was wrongly overwritten by add');
|
|
|
|
$this->assertEquals($value, $received, 'Value was wrongly overwritten by any other value');
|
|
|
|
|
|
|
|
$this->instance->delete('value1');
|
|
|
|
$this->instance->add('value1', $newValue);
|
2018-07-07 18:51:08 +00:00
|
|
|
$received = $this->instance->get('value1');
|
2018-07-07 18:35:42 +00:00
|
|
|
$this->assertEquals($newValue, $received, 'Value was not overwritten by add');
|
|
|
|
$this->assertNotEquals($value, $received, 'Value was not overwritten by any other value');
|
|
|
|
}
|
|
|
|
}
|