friendica/tests/MemoryCacheTestCase.php

119 wiersze
3.6 KiB
PHP

2018-07-07 18:35:42 +00:00
<?php
2024-08-24 12:31:41 +00:00
// Copyright (C) 2010-2024, the Friendica project
// SPDX-FileCopyrightText: 2010-2024 the Friendica project
//
// SPDX-License-Identifier: AGPL-3.0-or-later
2018-07-07 18:35:42 +00:00
namespace Friendica\Test;
2018-07-07 18:35:42 +00:00
use Exception;
2021-10-26 19:44:29 +00:00
use Friendica\Core\Cache\Capability\ICanCacheInMemory;
2018-07-07 18:35:42 +00:00
abstract class MemoryCacheTestCase extends CacheTestCase
2018-07-07 18:35:42 +00:00
{
/**
2021-10-26 19:44:29 +00:00
* @var \Friendica\Core\Cache\Capability\ICanCacheInMemory
2018-07-07 18:35:42 +00:00
*/
protected $instance;
protected function setUp(): void
2018-07-07 18:35:42 +00:00
{
parent::setUp();
2019-07-26 13:54:14 +00:00
2021-10-26 19:44:29 +00:00
if (!($this->instance instanceof ICanCacheInMemory)) {
throw new Exception('MemoryCacheTest unsupported');
2018-07-07 18:35:42 +00:00
}
}
/**
* @small
* @dataProvider dataSimple
*/
public function testCompareSet($value1, $value2)
{
self::assertNull($this->instance->get('value1'));
2018-07-07 18:35:42 +00:00
$this->instance->add('value1', $value1);
$received = $this->instance->get('value1');
self::assertEquals($value1, $received, 'Value received from cache not equal to the original');
$this->instance->compareSet('value1', $value1, $value2);
$received = $this->instance->get('value1');
self::assertEquals($value2, $received, 'Value not overwritten by compareSet');
2018-07-07 18:35:42 +00:00
}
/**
* @small
* @dataProvider dataSimple
*/
public function testNegativeCompareSet($value1, $value2)
{
self::assertNull($this->instance->get('value1'));
2018-07-07 18:35:42 +00:00
$this->instance->add('value1', $value1);
$received = $this->instance->get('value1');
self::assertEquals($value1, $received, 'Value received from cache not equal to the original');
$this->instance->compareSet('value1', 'wrong', $value2);
$received = $this->instance->get('value1');
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
}
/**
* @small
* @dataProvider dataSimple
*/
public function testCompareDelete($data)
{
self::assertNull($this->instance->get('value1'));
2018-07-07 18:35:42 +00:00
$this->instance->add('value1', $data);
$received = $this->instance->get('value1');
self::assertEquals($data, $received, 'Value received from cache not equal to the original');
$this->instance->compareDelete('value1', $data);
self::assertNull($this->instance->get('value1'), 'Value was not deleted by compareDelete');
2018-07-07 18:35:42 +00:00
}
/**
* @small
* @dataProvider dataSimple
*/
public function testNegativeCompareDelete($data)
{
self::assertNull($this->instance->get('value1'));
2018-07-07 18:35:42 +00:00
$this->instance->add('value1', $data);
$received = $this->instance->get('value1');
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');
self::assertNotNull($this->instance->get('value1'), 'Value was wrongly compareDeleted');
2018-07-07 18:35:42 +00:00
$this->instance->compareDelete('value1', $data);
self::assertNull($this->instance->get('value1'), 'Value was wrongly NOT deleted by compareDelete');
2018-07-07 18:35:42 +00:00
}
/**
* @small
* @dataProvider dataSimple
*/
public function testAdd($value1, $value2)
{
self::assertNull($this->instance->get('value1'));
2018-07-07 18:35:42 +00:00
$this->instance->add('value1', $value1);
2018-07-07 18:35:42 +00:00
$this->instance->add('value1', $value2);
$received = $this->instance->get('value1');
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');
$this->instance->add('value1', $value2);
$received = $this->instance->get('value1');
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
}
}