]> git.mxchange.org Git - friendica.git/commitdiff
Fix and adapt tests
authorPhilipp <admin@philipp.info>
Sun, 12 Feb 2023 12:09:14 +0000 (13:09 +0100)
committerPhilipp <admin@philipp.info>
Sun, 12 Feb 2023 12:09:14 +0000 (13:09 +0100)
src/Core/Config/Model/ConfigTransaction.php
tests/DatabaseTestTrait.php
tests/src/Core/Config/ConfigTest.php

index 771565af1e75b0086ad50128921fffe7d7b0869a..da5f5f6f3ece807c84a18496e18e71aa6f507382 100644 (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;
index cf03da29fbbab53ee60defc6f74b22259e21f3df..4badf85e903d0218ac8da2f273ddeb3ca3383b9d 100644 (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);
+       }
 }
index 7a198d06e9fee72115ef6e20586794e3680cdd08..643c73754892bdc151f1559bf30eb35ac21d7fa3 100644 (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'));
-       }
 }