1
0
Fork 0

Introduce ISetConfigValuesTransactional for transactional config behaviour

This commit is contained in:
Philipp Holzer 2023-01-03 14:18:53 +01:00
commit 65d79d4c93
Signed by: nupplaPhil
GPG key ID: 24A7501396EB5432
14 changed files with 588 additions and 150 deletions

View file

@ -358,4 +358,52 @@ class CacheTest extends MockedTest
$this->assertEquals(['system' => ['test_2' => 'with_data']], $configCache->getDataBySource(Cache::SOURCE_DATA));
$this->assertEquals($data, $configCache->getDataBySource(Cache::SOURCE_FILE));
}
/**
* @dataProvider dataTests
*/
public function testMerge($data)
{
$configCache = new Cache();
$configCache->load($data, Cache::SOURCE_FILE);
$configCache->set('system', 'test_2','with_data', Cache::SOURCE_DATA);
$configCache->set('config', 'test_override','with_another_data', Cache::SOURCE_DATA);
$newCache = new Cache();
$newCache->set('config', 'test_override','override it again', Cache::SOURCE_DATA);
$newCache->set('system', 'test_3','new value', Cache::SOURCE_DATA);
$mergedCache = $configCache->merge($newCache);
self::assertEquals('with_data', $mergedCache->get('system', 'test_2'));
self::assertEquals('override it again', $mergedCache->get('config', 'test_override'));
self::assertEquals('new value', $mergedCache->get('system', 'test_3'));
}
/**
* @dataProvider dataTests
*/
public function testDiff($data)
{
$configCache = new Cache();
$configCache->load($data, Cache::SOURCE_FILE);
$configCache->set('system', 'test_2','with_data', Cache::SOURCE_DATA);
$configCache->set('config', 'test_override','with_another_data', Cache::SOURCE_DATA);
$newCache = new Cache();
$newCache->set('config', 'test_override','override it again', Cache::SOURCE_DATA);
$newCache->set('system', 'test_3','new value', Cache::SOURCE_DATA);
$mergedCache = $configCache->diff($newCache);
print_r($mergedCache);
self::assertEquals('with_data', $mergedCache->get('system', 'test_2'));
// existing entry was dropped
self::assertNull($mergedCache->get('config', 'test_override'));
// the newCache entry wasn't set, because we Diff
self::assertNull($mergedCache->get('system', 'test_3'));
}
}

View file

@ -21,8 +21,8 @@
namespace Friendica\Test\src\Core\Config\Cache;
use Friendica\Core\Config\Cache;
use Friendica\Core\Config\Factory\Config;
use Friendica\Core\Config\ValueObject\Cache;
use Friendica\Test\MockedTest;
use Friendica\Test\Util\VFSTrait;
use Friendica\Core\Config\Util\ConfigFileManager;
@ -51,7 +51,7 @@ class ConfigFileLoaderTest extends MockedTest
$this->root->url() . DIRECTORY_SEPARATOR . Config::CONFIG_DIR,
$this->root->url() . DIRECTORY_SEPARATOR . Config::STATIC_DIR
);
$configCache = new \Friendica\Core\Config\ValueObject\Cache();
$configCache = new Cache();
$configFileLoader->setupCache($configCache);
@ -77,7 +77,7 @@ class ConfigFileLoaderTest extends MockedTest
$this->root->url() . DIRECTORY_SEPARATOR . Config::CONFIG_DIR,
$this->root->url() . DIRECTORY_SEPARATOR . Config::STATIC_DIR
);
$configCache = new \Friendica\Core\Config\ValueObject\Cache();
$configCache = new Cache();
$configFileLoader->setupCache($configCache);
}
@ -106,7 +106,7 @@ class ConfigFileLoaderTest extends MockedTest
$this->root->url() . DIRECTORY_SEPARATOR . Config::CONFIG_DIR,
$this->root->url() . DIRECTORY_SEPARATOR . Config::STATIC_DIR
);
$configCache = new \Friendica\Core\Config\ValueObject\Cache();
$configCache = new Cache();
$configFileLoader->setupCache($configCache);
@ -143,7 +143,7 @@ class ConfigFileLoaderTest extends MockedTest
$this->root->url() . DIRECTORY_SEPARATOR . Config::CONFIG_DIR,
$this->root->url() . DIRECTORY_SEPARATOR . Config::STATIC_DIR
);
$configCache = new \Friendica\Core\Config\ValueObject\Cache();
$configCache = new Cache();
$configFileLoader->setupCache($configCache);
@ -179,7 +179,7 @@ class ConfigFileLoaderTest extends MockedTest
$this->root->url() . DIRECTORY_SEPARATOR . Config::CONFIG_DIR,
$this->root->url() . DIRECTORY_SEPARATOR . Config::STATIC_DIR
);
$configCache = new \Friendica\Core\Config\ValueObject\Cache();
$configCache = new Cache();
$configFileLoader->setupCache($configCache);
@ -270,7 +270,7 @@ class ConfigFileLoaderTest extends MockedTest
$this->root->url() . DIRECTORY_SEPARATOR . Config::CONFIG_DIR,
$this->root->url() . DIRECTORY_SEPARATOR . Config::STATIC_DIR
);
$configCache = new \Friendica\Core\Config\ValueObject\Cache();
$configCache = new Cache();
$configFileLoader->setupCache($configCache);
@ -304,7 +304,7 @@ class ConfigFileLoaderTest extends MockedTest
$this->root->url() . DIRECTORY_SEPARATOR . Config::CONFIG_DIR,
$this->root->url() . DIRECTORY_SEPARATOR . Config::STATIC_DIR
);
$configCache = new \Friendica\Core\Config\ValueObject\Cache();
$configCache = new Cache();
$configFileLoader->setupCache($configCache);
@ -338,7 +338,7 @@ class ConfigFileLoaderTest extends MockedTest
$this->root->url() . DIRECTORY_SEPARATOR . Config::CONFIG_DIR,
$this->root->url() . DIRECTORY_SEPARATOR . Config::STATIC_DIR
);
$configCache = new \Friendica\Core\Config\ValueObject\Cache();
$configCache = new Cache();
$configFileLoader->setupCache($configCache);
@ -354,7 +354,7 @@ class ConfigFileLoaderTest extends MockedTest
$this->delConfigFile('local.config.php');
$configFileLoader = (new Config())->createConfigFileLoader($this->root->url(), ['FRIENDICA_CONFIG_DIR' => '/a/wrong/dir/']);
$configCache = new \Friendica\Core\Config\ValueObject\Cache();
$configCache = new Cache();
$configFileLoader->setupCache($configCache);
@ -380,7 +380,7 @@ class ConfigFileLoaderTest extends MockedTest
->setContent(file_get_contents($fileDir . 'B.config.php'));
$configFileLoader = (new Config())->createConfigFileLoader($this->root->url(), ['FRIENDICA_CONFIG_DIR' => $this->root->getChild('config2')->url()]);
$configCache = new \Friendica\Core\Config\ValueObject\Cache();
$configCache = new Cache();
$configFileLoader->setupCache($configCache);
@ -403,18 +403,18 @@ class ConfigFileLoaderTest extends MockedTest
->setContent(file_get_contents($fileDir . 'B.config.php'));
$configFileLoader = (new Config())->createConfigFileLoader($this->root->url(), ['FRIENDICA_CONFIG_DIR' => $this->root->getChild('config2')->url()]);
$configCache = new \Friendica\Core\Config\ValueObject\Cache();
$configCache = new Cache();
$configFileLoader->setupCache($configCache);
// overwrite some data and save it back to the config file
$configCache->set('system', 'test', 'it', \Friendica\Core\Config\ValueObject\Cache::SOURCE_DATA);
$configCache->set('config', 'test', 'it', \Friendica\Core\Config\ValueObject\Cache::SOURCE_DATA);
$configCache->set('system', 'test_2', 2, \Friendica\Core\Config\ValueObject\Cache::SOURCE_DATA);
$configCache->set('system', 'test', 'it', Cache::SOURCE_DATA);
$configCache->set('config', 'test', 'it', Cache::SOURCE_DATA);
$configCache->set('system', 'test_2', 2, Cache::SOURCE_DATA);
$configFileLoader->saveData($configCache);
// Reload the configCache with the new values
$configCache2 = new \Friendica\Core\Config\ValueObject\Cache();
$configCache2 = new Cache();
$configFileLoader->setupCache($configCache2);
self::assertEquals($configCache, $configCache2);
@ -425,6 +425,6 @@ class ConfigFileLoaderTest extends MockedTest
],
'config' => [
'test' => 'it'
]], $configCache2->getDataBySource(\Friendica\Core\Config\ValueObject\Cache::SOURCE_DATA));
]], $configCache2->getDataBySource(Cache::SOURCE_DATA));
}
}

View file

@ -0,0 +1,110 @@
<?php
namespace Friendica\Test\src\Core\Config;
use Friendica\Core\Config\Capability\ISetConfigValuesTransactional;
use Friendica\Core\Config\Model\Config;
use Friendica\Core\Config\Model\TransactionalConfig;
use Friendica\Core\Config\Util\ConfigFileManager;
use Friendica\Core\Config\ValueObject\Cache;
use Friendica\Test\MockedTest;
use Friendica\Test\Util\VFSTrait;
class TransactionalConfigTest extends MockedTest
{
use VFSTrait;
/** @var ConfigFileManager */
protected $configFileManager;
protected function setUp(): void
{
parent::setUp();
$this->setUpVfsDir();
$this->configFileManager = new ConfigFileManager($this->root->url(), $this->root->url() . '/config/', $this->root->url() . '/static/');
}
public function dataTests(): array
{
return [
'default' => [
'data' => include dirname(__FILE__, 4) . '/datasets/B.node.config.php',
]
];
}
public function testInstance()
{
$config = new Config($this->configFileManager, new Cache());
$transactionalConfig = new TransactionalConfig($config);
self::assertInstanceOf(ISetConfigValuesTransactional::class, $transactionalConfig);
self::assertInstanceOf(TransactionalConfig::class, $transactionalConfig);
}
public function testTransactionalConfig()
{
$config = new Config($this->configFileManager, new Cache());
$config->set('config', 'key1', 'value1');
$config->set('system', 'key2', 'value2');
$config->set('system', 'keyDel', 'valueDel');
$config->set('delete', 'keyDel', 'catDel');
$transactionalConfig = new TransactionalConfig($config);
self::assertEquals('value1', $transactionalConfig->get('config', 'key1'));
self::assertEquals('value2', $transactionalConfig->get('system', 'key2'));
self::assertEquals('valueDel', $transactionalConfig->get('system', 'keyDel'));
self::assertEquals('catDel', $transactionalConfig->get('delete', 'keyDel'));
// the config file knows it as well immediately
$tempData = include $this->root->url() . '/config/' . ConfigFileManager::CONFIG_DATA_FILE;
self::assertEquals('value1', $tempData['config']['key1'] ?? null);
self::assertEquals('value2', $tempData['system']['key2'] ?? null);
// new key-value
$transactionalConfig->set('transaction', 'key3', 'value3');
// overwrite key-value
$transactionalConfig->set('config', 'key1', 'changedValue1');
// delete key-value
$transactionalConfig->delete('system', 'keyDel');
// delete last key of category - so the category is gone
$transactionalConfig->delete('delete', 'keyDel');
// The main config still doesn't know about the change
self::assertNull($config->get('transaction', 'key3'));
self::assertEquals('value1', $config->get('config', 'key1'));
self::assertEquals('valueDel', $config->get('system', 'keyDel'));
self::assertEquals('catDel', $config->get('delete', 'keyDel'));
// but the transaction config of course knows it
self::assertEquals('value3', $transactionalConfig->get('transaction', 'key3'));
self::assertEquals('changedValue1', $transactionalConfig->get('config', 'key1'));
self::assertNull($transactionalConfig->get('system', 'keyDel'));
self::assertNull($transactionalConfig->get('delete', 'keyDel'));
// The config file still doesn't know it either
$tempData = include $this->root->url() . '/config/' . ConfigFileManager::CONFIG_DATA_FILE;
self::assertEquals('value1', $tempData['config']['key1'] ?? null);
self::assertEquals('value2', $tempData['system']['key2'] ?? null);
self::assertEquals('catDel', $tempData['delete']['keyDel'] ?? null);
self::assertNull($tempData['transaction']['key3'] ?? null);
// save it back!
$transactionalConfig->save();
// Now every config and file knows the change
self::assertEquals('changedValue1', $config->get('config', 'key1'));
self::assertEquals('value3', $config->get('transaction', 'key3'));
self::assertNull($config->get('system', 'keyDel'));
self::assertNull($config->get('delete', 'keyDel'));
self::assertEquals('value3', $transactionalConfig->get('transaction', 'key3'));
self::assertEquals('changedValue1', $transactionalConfig->get('config', 'key1'));
self::assertNull($transactionalConfig->get('system', 'keyDel'));
$tempData = include $this->root->url() . '/config/' . ConfigFileManager::CONFIG_DATA_FILE;
self::assertEquals('changedValue1', $tempData['config']['key1'] ?? null);
self::assertEquals('value2', $tempData['system']['key2'] ?? null);
self::assertEquals('value3', $tempData['transaction']['key3'] ?? null);
self::assertNull($tempData['system']['keyDel'] ?? null);
self::assertNull($tempData['delete']['keyDel'] ?? null);
// the whole category should be gone
self::assertNull($tempData['delete'] ?? null);
}
}