1
0
Fork 0

Fix and adapt tests

This commit is contained in:
Philipp Holzer 2023-02-12 13:09:14 +01:00
parent ad284208ee
commit a153c3f594
Signed by: nupplaPhil
GPG key ID: 24A7501396EB5432
3 changed files with 43 additions and 44 deletions

View file

@ -60,7 +60,7 @@ class ConfigTransaction implements ISetConfigValuesTransactionally
/** {@inheritDoc} */
public function delete(string $cat, string $key): ISetConfigValuesTransactionally
{
$this->delCache->delete($cat, $key);
$this->delCache->set($cat, $key, true, Cache::SOURCE_DATA);
$this->changedConfig = true;
return $this;

View file

@ -53,16 +53,14 @@ trait DatabaseTestTrait
/**
* Loads a given DB fixture for this DB test
*
* @param string $fixture The path to the fixture
* @param string[][] $fixture The fixture array
* @param Database $dba The DB connection
*
* @throws \Exception
*/
protected function loadFixture(string $fixture, Database $dba)
protected function loadDirectFixture(array $fixture, Database $dba)
{
$data = include $fixture;
foreach ($data as $tableName => $rows) {
foreach ($fixture as $tableName => $rows) {
if (is_numeric($tableName)) {
continue;
}
@ -77,4 +75,19 @@ trait DatabaseTestTrait
}
}
}
/**
* Loads a given DB fixture-file for this DB test
*
* @param string $fixture The path to the fixture
* @param Database $dba The DB connection
*
* @throws \Exception
*/
protected function loadFixture(string $fixture, Database $dba)
{
$data = include $fixture;
$this->loadDirectFixture($data, $dba);
}
}

View file

@ -26,7 +26,6 @@ use Friendica\Core\Config\Capability\IManageConfigValues;
use Friendica\Core\Config\Model\DatabaseConfig;
use Friendica\Core\Config\Model\ReadOnlyFileConfig;
use Friendica\Core\Config\Util\ConfigFileManager;
use Friendica\Core\Config\Util\ConfigFileTransformer;
use Friendica\Core\Config\ValueObject\Cache;
use Friendica\Test\DatabaseTest;
use Friendica\Test\Util\CreateDatabaseTrait;
@ -165,15 +164,30 @@ class ConfigTest extends DatabaseTest
];
}
public function configToDbArray(array $config): array
{
$dbarray = [];
foreach ($config as $category => $data) {
foreach ($data as $key => $value) {
$dbarray[] = [
'cat' => $category,
'k' => $key,
'v' => $value,
];
}
}
return ['config' => $dbarray];
}
/**
* Test the configuration initialization
* @dataProvider dataConfigLoad
*/
public function testSetUp(array $data)
{
vfsStream::newFile(ConfigFileManager::CONFIG_DATA_FILE)
->at($this->root->getChild('config'))
->setContent(print_r($data, true));
$this->loadDirectFixture($this->configToDbArray($data) , $this->getDbInstance());
$this->testedConfig = $this->getInstance();
self::assertInstanceOf(Cache::class, $this->testedConfig->getCache());
@ -192,9 +206,7 @@ class ConfigTest extends DatabaseTest
*/
public function testReload(array $data, array $load)
{
vfsStream::newFile(ConfigFileManager::CONFIG_DATA_FILE)
->at($this->root->getChild('config'))
->setContent(print_r($data, true));
$this->loadDirectFixture($this->configToDbArray($data), $this->getDbInstance());
$this->testedConfig = $this->getInstance();
self::assertInstanceOf(Cache::class, $this->testedConfig->getCache());
@ -277,9 +289,7 @@ class ConfigTest extends DatabaseTest
*/
public function testCacheLoadDouble(array $data1, array $data2, array $expect = [])
{
vfsStream::newFile(ConfigFileManager::CONFIG_DATA_FILE)
->at($this->root->getChild('config'))
->setContent(print_r($data1, true));
$this->loadDirectFixture($this->configToDbArray($data1), $this->getDbInstance());
$this->testedConfig = $this->getInstance();
self::assertInstanceOf(Cache::class, $this->testedConfig->getCache());
@ -289,9 +299,7 @@ class ConfigTest extends DatabaseTest
self::assertConfig($cat, $data);
}
vfsStream::newFile(ConfigFileManager::CONFIG_DATA_FILE)
->at($this->root->getChild('config'))
->setContent(print_r($data2, true));
$this->loadDirectFixture($this->configToDbArray($data2), $this->getDbInstance());
$this->testedConfig->reload();
@ -389,19 +397,15 @@ class ConfigTest extends DatabaseTest
*/
public function testSetGetLowPrio()
{
vfsStream::newFile(ConfigFileManager::CONFIG_DATA_FILE)
->at($this->root->getChild('config'))
->setContent(print_r([
'config' => ['test' => 'it'],
], true));
$this->loadDirectFixture(['config' => [['cat' => 'config', 'k' => 'test', 'v' => 'it']]], $this->getDbInstance());
$this->testedConfig = $this->getInstance();
self::assertInstanceOf(Cache::class, $this->testedConfig->getCache());
self::assertEquals('it', $this->testedConfig->get('config', 'test'));
$this->testedConfig->getCache()->set('config', 'test', 'prio', Cache::SOURCE_ENV);
// now you have to get the env variable entry as output, even with a new set (which failed) and a get refresh
self::assertFalse($this->testedConfig->set('config', 'test', '123'));
// You can set a config value, but if there's a value with a higher priority (environment), this value will persist when retrieving
self::assertTrue($this->testedConfig->set('config', 'test', '123'));
self::assertEquals('prio', $this->testedConfig->get('config', 'test', '', true));
}
@ -533,22 +537,4 @@ class ConfigTest extends DatabaseTest
self::assertEquals($assertion, $config->get($category));
}
/**
* Tests, if an overwritten value of an existing key will reset to it's default after deletion
*/
public function testDeleteReturnsDefault()
{
vfsStream::newFile(ConfigFileManager::CONFIG_DATA_FILE)
->at($this->root->getChild('config'))
->setContent(print_r([
'config' => ['sitename' => 'overritten'],
], true));
$config = $this->getInstance();
self::assertEquals('overritten', $config->get('config', 'sitename'));
$config->delete('config', 'sitename');
self::assertEquals('Friendica Social Network', $config->get('config', 'sitename'));
}
}