1
1
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} */ /** {@inheritDoc} */
public function delete(string $cat, string $key): ISetConfigValuesTransactionally 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; $this->changedConfig = true;
return $this; return $this;

View file

@ -53,16 +53,14 @@ trait DatabaseTestTrait
/** /**
* Loads a given DB fixture for this DB test * 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 * @param Database $dba The DB connection
* *
* @throws \Exception * @throws \Exception
*/ */
protected function loadFixture(string $fixture, Database $dba) protected function loadDirectFixture(array $fixture, Database $dba)
{ {
$data = include $fixture; foreach ($fixture as $tableName => $rows) {
foreach ($data as $tableName => $rows) {
if (is_numeric($tableName)) { if (is_numeric($tableName)) {
continue; 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\DatabaseConfig;
use Friendica\Core\Config\Model\ReadOnlyFileConfig; use Friendica\Core\Config\Model\ReadOnlyFileConfig;
use Friendica\Core\Config\Util\ConfigFileManager; use Friendica\Core\Config\Util\ConfigFileManager;
use Friendica\Core\Config\Util\ConfigFileTransformer;
use Friendica\Core\Config\ValueObject\Cache; use Friendica\Core\Config\ValueObject\Cache;
use Friendica\Test\DatabaseTest; use Friendica\Test\DatabaseTest;
use Friendica\Test\Util\CreateDatabaseTrait; 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 * Test the configuration initialization
* @dataProvider dataConfigLoad * @dataProvider dataConfigLoad
*/ */
public function testSetUp(array $data) public function testSetUp(array $data)
{ {
vfsStream::newFile(ConfigFileManager::CONFIG_DATA_FILE) $this->loadDirectFixture($this->configToDbArray($data) , $this->getDbInstance());
->at($this->root->getChild('config'))
->setContent(print_r($data, true));
$this->testedConfig = $this->getInstance(); $this->testedConfig = $this->getInstance();
self::assertInstanceOf(Cache::class, $this->testedConfig->getCache()); self::assertInstanceOf(Cache::class, $this->testedConfig->getCache());
@ -192,9 +206,7 @@ class ConfigTest extends DatabaseTest
*/ */
public function testReload(array $data, array $load) public function testReload(array $data, array $load)
{ {
vfsStream::newFile(ConfigFileManager::CONFIG_DATA_FILE) $this->loadDirectFixture($this->configToDbArray($data), $this->getDbInstance());
->at($this->root->getChild('config'))
->setContent(print_r($data, true));
$this->testedConfig = $this->getInstance(); $this->testedConfig = $this->getInstance();
self::assertInstanceOf(Cache::class, $this->testedConfig->getCache()); self::assertInstanceOf(Cache::class, $this->testedConfig->getCache());
@ -277,9 +289,7 @@ class ConfigTest extends DatabaseTest
*/ */
public function testCacheLoadDouble(array $data1, array $data2, array $expect = []) public function testCacheLoadDouble(array $data1, array $data2, array $expect = [])
{ {
vfsStream::newFile(ConfigFileManager::CONFIG_DATA_FILE) $this->loadDirectFixture($this->configToDbArray($data1), $this->getDbInstance());
->at($this->root->getChild('config'))
->setContent(print_r($data1, true));
$this->testedConfig = $this->getInstance(); $this->testedConfig = $this->getInstance();
self::assertInstanceOf(Cache::class, $this->testedConfig->getCache()); self::assertInstanceOf(Cache::class, $this->testedConfig->getCache());
@ -289,9 +299,7 @@ class ConfigTest extends DatabaseTest
self::assertConfig($cat, $data); self::assertConfig($cat, $data);
} }
vfsStream::newFile(ConfigFileManager::CONFIG_DATA_FILE) $this->loadDirectFixture($this->configToDbArray($data2), $this->getDbInstance());
->at($this->root->getChild('config'))
->setContent(print_r($data2, true));
$this->testedConfig->reload(); $this->testedConfig->reload();
@ -389,19 +397,15 @@ class ConfigTest extends DatabaseTest
*/ */
public function testSetGetLowPrio() public function testSetGetLowPrio()
{ {
vfsStream::newFile(ConfigFileManager::CONFIG_DATA_FILE) $this->loadDirectFixture(['config' => [['cat' => 'config', 'k' => 'test', 'v' => 'it']]], $this->getDbInstance());
->at($this->root->getChild('config'))
->setContent(print_r([
'config' => ['test' => 'it'],
], true));
$this->testedConfig = $this->getInstance(); $this->testedConfig = $this->getInstance();
self::assertInstanceOf(Cache::class, $this->testedConfig->getCache()); self::assertInstanceOf(Cache::class, $this->testedConfig->getCache());
self::assertEquals('it', $this->testedConfig->get('config', 'test')); self::assertEquals('it', $this->testedConfig->get('config', 'test'));
$this->testedConfig->getCache()->set('config', 'test', 'prio', Cache::SOURCE_ENV); $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 // You can set a config value, but if there's a value with a higher priority (environment), this value will persist when retrieving
self::assertFalse($this->testedConfig->set('config', 'test', '123')); self::assertTrue($this->testedConfig->set('config', 'test', '123'));
self::assertEquals('prio', $this->testedConfig->get('config', 'test', '', true)); self::assertEquals('prio', $this->testedConfig->get('config', 'test', '', true));
} }
@ -533,22 +537,4 @@ class ConfigTest extends DatabaseTest
self::assertEquals($assertion, $config->get($category)); 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'));
}
} }