mirror of
https://github.com/friendica/friendica
synced 2025-04-26 05:10:11 +00:00
Introduce Key-Value Pair storage provider
This commit is contained in:
parent
9a10bb4295
commit
47764387b3
8 changed files with 436 additions and 0 deletions
64
tests/src/Core/KeyValueStorage/DBKeyValueStorageTest.php
Normal file
64
tests/src/Core/KeyValueStorage/DBKeyValueStorageTest.php
Normal file
|
@ -0,0 +1,64 @@
|
|||
<?php
|
||||
/**
|
||||
* @copyright Copyright (C) 2010-2022, the Friendica project
|
||||
*
|
||||
* @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/>.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace Friendica\Test\src\Core\KeyValueStorage;
|
||||
|
||||
use Friendica\Core\Config\ValueObject\Cache;
|
||||
use Friendica\Core\KeyValueStorage\Capabilities\ICanManageKeyValuePairs;
|
||||
use Friendica\Core\KeyValueStorage\Type\DBKeyValueStorage;
|
||||
use Friendica\Database\Definition\DbaDefinition;
|
||||
use Friendica\Database\Definition\ViewDefinition;
|
||||
use Friendica\Test\DatabaseTestTrait;
|
||||
use Friendica\Test\Util\Database\StaticDatabase;
|
||||
use Friendica\Util\BasePath;
|
||||
use Friendica\Util\Profiler;
|
||||
|
||||
class DBKeyValueStorageTest extends KeyValueStorageTest
|
||||
{
|
||||
use DatabaseTestTrait;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->setUpDb();
|
||||
}
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
parent::tearDown();
|
||||
|
||||
$this->tearDownDb();
|
||||
}
|
||||
|
||||
public function getInstance(): ICanManageKeyValuePairs
|
||||
{
|
||||
$cache = new Cache();
|
||||
$cache->set('database', 'disable_pdo', true);
|
||||
|
||||
$basePath = new BasePath(dirname(__FILE__, 5), $_SERVER);
|
||||
|
||||
$database = new StaticDatabase($cache, new Profiler($cache), (new DbaDefinition($basePath->getPath()))->load(), (new ViewDefinition($basePath->getPath()))->load());
|
||||
$database->setTestmode(true);
|
||||
|
||||
return new DBKeyValueStorage($database);
|
||||
}
|
||||
}
|
105
tests/src/Core/KeyValueStorage/KeyValueStorageTest.php
Normal file
105
tests/src/Core/KeyValueStorage/KeyValueStorageTest.php
Normal file
|
@ -0,0 +1,105 @@
|
|||
<?php
|
||||
/**
|
||||
* @copyright Copyright (C) 2010-2022, the Friendica project
|
||||
*
|
||||
* @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/>.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace Friendica\Test\src\Core\KeyValueStorage;
|
||||
|
||||
use Friendica\Core\KeyValueStorage\Capabilities\ICanManageKeyValuePairs;
|
||||
use Friendica\Test\MockedTest;
|
||||
|
||||
abstract class KeyValueStorageTest extends MockedTest
|
||||
{
|
||||
abstract public function getInstance(): ICanManageKeyValuePairs;
|
||||
|
||||
public function testInstance()
|
||||
{
|
||||
$instance = $this->getInstance();
|
||||
|
||||
self::assertInstanceOf(ICanManageKeyValuePairs::class, $instance);
|
||||
}
|
||||
|
||||
public function dataTests(): array
|
||||
{
|
||||
return [
|
||||
'string' => ['k' => 'data', 'v' => 'it'],
|
||||
'boolTrue' => ['k' => 'data', 'v' => true],
|
||||
'boolFalse' => ['k' => 'data', 'v' => false],
|
||||
'integer' => ['k' => 'data', 'v' => 235],
|
||||
'decimal' => ['k' => 'data', 'v' => 2.456],
|
||||
'array' => ['k' => 'data', 'v' => ['1', 2, '3', true, false]],
|
||||
'boolIntTrue' => ['k' => 'data', 'v' => 1],
|
||||
'boolIntFalse' => ['k' => 'data', 'v' => 0],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider dataTests
|
||||
*/
|
||||
public function testGetSetDelete($k, $v)
|
||||
{
|
||||
$instance = $this->getInstance();
|
||||
|
||||
$instance->set($k, $v);
|
||||
|
||||
self::assertEquals($v, $instance->get($k));
|
||||
self::assertEquals($v, $instance[$k]);
|
||||
|
||||
$instance->delete($k);
|
||||
|
||||
self::assertNull($instance->get($k));
|
||||
self::assertNull($instance[$k]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider dataTests
|
||||
*/
|
||||
public function testSetOverride($k, $v)
|
||||
{
|
||||
$instance = $this->getInstance();
|
||||
|
||||
$instance->set($k, $v);
|
||||
|
||||
self::assertEquals($v, $instance->get($k));
|
||||
self::assertEquals($v, $instance[$k]);
|
||||
|
||||
$instance->set($k, 'another_value');
|
||||
|
||||
self::assertEquals('another_value', $instance->get($k));
|
||||
self::assertEquals('another_value', $instance[$k]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider dataTests
|
||||
*/
|
||||
public function testOffsetSetDelete($k, $v)
|
||||
{
|
||||
$instance = $this->getInstance();
|
||||
|
||||
$instance[$k] = $v;
|
||||
|
||||
self::assertEquals($v, $instance->get($k));
|
||||
self::assertEquals($v, $instance[$k]);
|
||||
|
||||
unset($instance[$k]);
|
||||
|
||||
self::assertNull($instance->get($k));
|
||||
self::assertNull($instance[$k]);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue