From fea4b202c15b8890da671c9983a07b344c884ca2 Mon Sep 17 00:00:00 2001 From: Philipp Date: Sun, 1 Jan 2023 18:50:02 +0100 Subject: [PATCH] Introduce ConfigFileTransformer for Config files --- .../Config/Util/ConfigFileTransformer.php | 85 +++++++++++++++++++ tests/datasets/config/A.node.config.php | 22 +++++ tests/datasets/config/B.node.config.php | 38 +++++++++ .../Config/Util/ConfigFileTransformerTest.php | 54 ++++++++++++ 4 files changed, 199 insertions(+) create mode 100644 src/Core/Config/Util/ConfigFileTransformer.php create mode 100644 tests/datasets/config/A.node.config.php create mode 100644 tests/datasets/config/B.node.config.php create mode 100644 tests/src/Core/Config/Util/ConfigFileTransformerTest.php diff --git a/src/Core/Config/Util/ConfigFileTransformer.php b/src/Core/Config/Util/ConfigFileTransformer.php new file mode 100644 index 0000000000..9b80991af6 --- /dev/null +++ b/src/Core/Config/Util/ConfigFileTransformer.php @@ -0,0 +1,85 @@ +. + * + */ + +namespace Friendica\Core\Config\Util; + +/** + * Util to transform back the config array into a string + */ +class ConfigFileTransformer +{ + public static function encode(array $data): string + { + $dataString = ' [" . PHP_EOL; + + if (is_array($data[$category])) { + $keys = array_keys($data[$category]); + + foreach ($keys as $key) { + $dataString .= static::mapConfigValue($key, $data[$category][$key]); + } + } + $dataString .= "\t]," . PHP_EOL; + } + + $dataString .= "];" . PHP_EOL; + + return $dataString; + } + + protected static function extractArray(array $config, int $level = 0): string + { + $string = ''; + + foreach ($config as $configKey => $configValue) { + $string .= static::mapConfigValue($configKey, $configValue, $level); + } + + return $string; + } + + protected static function mapConfigValue(string $key, $value, $level = 0): string + { + $string = str_repeat("\t", $level + 2) . "'$key' => "; + + if (is_array($value)) { + $string .= "[" . PHP_EOL; + $string .= static::extractArray($value, ++$level); + $string .= str_repeat("\t", $level + 1) . '],'; + } elseif (is_bool($value)) { + $string .= ($value ? 'true' : 'false') . ","; + } elseif (is_numeric($value)) { + $string .= $value . ","; + } else { + $string .= sprintf('\'%s\',', $value); + } + + $string .= PHP_EOL; + + return $string; + } +} diff --git a/tests/datasets/config/A.node.config.php b/tests/datasets/config/A.node.config.php new file mode 100644 index 0000000000..b81e01737e --- /dev/null +++ b/tests/datasets/config/A.node.config.php @@ -0,0 +1,22 @@ + [ + 'hostname' => 'testhost', + 'username' => 'testuser', + 'password' => 'testpw', + 'database' => 'testdb', + 'charset' => 'utf8mb4', + ], + 'config' => [ + 'admin_email' => 'admin@test.it', + 'sitename' => 'Friendica Social Network', + 'register_policy' => 2, + 'register_text' => '', + ], + 'system' => [ + 'default_timezone' => 'UTC', + 'language' => 'en', + 'theme' => 'frio', + ], +]; diff --git a/tests/datasets/config/B.node.config.php b/tests/datasets/config/B.node.config.php new file mode 100644 index 0000000000..94b2e3f12e --- /dev/null +++ b/tests/datasets/config/B.node.config.php @@ -0,0 +1,38 @@ + [ + 'hostname' => 'testhost', + 'username' => 'testuser', + 'password' => 'testpw', + 'database' => 'testdb', + 'charset' => 'utf8mb4', + ], + 'config' => [ + 'admin_email' => 'admin@test.it', + 'sitename' => 'Friendica Social Network', + 'register_policy' => 2, + 'register_text' => '', + 'test' => [ + 'a' => [ + 'next' => 'value', + 'bool' => false, + 'innerArray' => [ + 'a' => 4.55, + 'b' => false, + 'string2' => 'false', + ], + ], + 'v' => true, + 'v3' => 1, + 'v4' => 5.6443, + ], + ], + 'system' => [ + 'default_timezone' => 'UTC', + 'language' => 'en', + 'theme' => 'frio', + 'int' => 23, + 'float' => 2.5, + ], +]; diff --git a/tests/src/Core/Config/Util/ConfigFileTransformerTest.php b/tests/src/Core/Config/Util/ConfigFileTransformerTest.php new file mode 100644 index 0000000000..6cd5bc7064 --- /dev/null +++ b/tests/src/Core/Config/Util/ConfigFileTransformerTest.php @@ -0,0 +1,54 @@ +. + * + */ + +namespace Friendica\Test\src\Core\Config\Util; + +use Friendica\Core\Config\Util\ConfigFileTransformer; +use Friendica\Test\MockedTest; + +class ConfigFileTransformerTest extends MockedTest +{ + public function dataTests() + { + return [ + 'default' => [ + 'configFile' => (dirname(__DIR__, 4) . '/datasets/config/A.node.config.php'), + ], + 'extended' => [ + 'configFile' => (dirname(__DIR__, 4) . '/datasets/config/B.node.config.php'), + ], + ]; + } + + /** + * Tests if the given config will be decoded into an array and encoded into the same string again + * + * @dataProvider dataTests + */ + public function testConfigFile(string $configFile) + { + $dataArray = include $configFile; + + $newConfig = ConfigFileTransformer::encode($dataArray); + + self::assertEquals(file_get_contents($configFile), $newConfig); + } +}