From: Philipp Date: Sun, 12 Feb 2023 12:09:14 +0000 (+0100) Subject: Fix and adapt tests X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=a153c3f5940464b294f9b61139394a6239744f49;p=friendica.git Fix and adapt tests --- diff --git a/src/Core/Config/Model/ConfigTransaction.php b/src/Core/Config/Model/ConfigTransaction.php index 771565af1e..da5f5f6f3e 100644 --- a/src/Core/Config/Model/ConfigTransaction.php +++ b/src/Core/Config/Model/ConfigTransaction.php @@ -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; diff --git a/tests/DatabaseTestTrait.php b/tests/DatabaseTestTrait.php index cf03da29fb..4badf85e90 100644 --- a/tests/DatabaseTestTrait.php +++ b/tests/DatabaseTestTrait.php @@ -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); + } } diff --git a/tests/src/Core/Config/ConfigTest.php b/tests/src/Core/Config/ConfigTest.php index 7a198d06e9..643c737548 100644 --- a/tests/src/Core/Config/ConfigTest.php +++ b/tests/src/Core/Config/ConfigTest.php @@ -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')); - } }