From 4eaeea7889d6024f2bb837c796fb708bab09be86 Mon Sep 17 00:00:00 2001 From: Philipp Date: Fri, 17 Aug 2018 21:41:46 +0200 Subject: [PATCH] Config Console Tests (#5621) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Config Console Tests - `MultiUseConsole` is now a testable console - `ConsoleTest` is a abstract TestClass for console tests - `ConfigConsoleTest` tests the config console commands * disable preserve global state because of smarty * fixed requires & basepath für Console Test --- tests/Util/Intercept.php | 45 +++++++++++ tests/src/Core/Console/ConfigConsoleTest.php | 85 ++++++++++++++++++++ tests/src/Core/Console/ConsoleTest.php | 48 +++++++++++ tests/src/Core/Console/MultiUseConsole.php | 23 ++++++ 4 files changed, 201 insertions(+) create mode 100644 tests/Util/Intercept.php create mode 100644 tests/src/Core/Console/ConfigConsoleTest.php create mode 100644 tests/src/Core/Console/ConsoleTest.php create mode 100644 tests/src/Core/Console/MultiUseConsole.php diff --git a/tests/Util/Intercept.php b/tests/Util/Intercept.php new file mode 100644 index 0000000000..9435a5950b --- /dev/null +++ b/tests/Util/Intercept.php @@ -0,0 +1,45 @@ +data; + $consumed += $bucket->datalen; + stream_bucket_append($out, $bucket); + } + return PSFS_FEED_ME; + } + + /** + * Registers the interceptor and prevents therefore the output to STDOUT + */ + public static function setUp() { + stream_filter_register("intercept", Intercept::class); + stream_filter_append(STDOUT, "intercept"); + stream_filter_append(STDERR, "intercept"); + } + + /** + * Resets the cache + */ + public static function reset() { + self::$cache = ''; + } +} diff --git a/tests/src/Core/Console/ConfigConsoleTest.php b/tests/src/Core/Console/ConfigConsoleTest.php new file mode 100644 index 0000000000..5512528dff --- /dev/null +++ b/tests/src/Core/Console/ConfigConsoleTest.php @@ -0,0 +1,85 @@ + 'test']); + + parent::tearDown(); + } + + private function assertGet($family, $key, $value) { + $config = $this->execute([__FILE__, 'config', $family, $key]); + $this->assertEquals($family . "." . $key . " => " . $value . "\n", $config); + } + + private function assertSet($family, $key, $value) { + $config = $this->execute([__FILE__, 'config', $family, $key, $value]); + $this->assertEquals($family . "." . $key . " <= " . $value . "\n", $config); + } + + function testSetGetKeyValue() { + $this->assertSet( 'config', 'test', 'now'); + $this->assertGet('config', 'test', 'now'); + $this->assertSet('config', 'test', ''); + $this->assertGet('config', 'test', ''); + DBA::delete('config', ['k' => 'test']); + $this->assertGet('config', 'test', null); + } + + function testSetArrayValue() { + $testArray = [1, 2, 3]; + DBA::insert('config', ['cat' => 'config', 'k' => 'test', 'v' => serialize($testArray)]); + + $txt = $this->execute([__FILE__, 'config', 'config', 'test', 'now']); + + $this->assertEquals("[Error] config.test is an array and can't be set using this command.\n", $txt); + } + + function testTooManyArguments() { + $txt = $this->execute([__FILE__, 'config', 'config', 'test', 'it', 'now']); + $assertion = '[Warning] Too many arguments'; + $firstline = substr($txt, 0, strlen($assertion)); + + $this->assertEquals($assertion, $firstline); + } + + function testVerbose() { + $this->assertSet('test', 'it', 'now'); + $assertion = <<app->basepath}/tests/src/Core/Console/ConfigConsoleTest.php +Arguments: array ( + 0 => 'config', + 1 => 'test', +) +Options: array ( + 'v' => 1, +) +Command: config +Executable: {$this->app->basepath}/tests/src/Core/Console/ConfigConsoleTest.php +Class: Friendica\Core\Console\Config +Arguments: array ( + 0 => 'test', +) +Options: array ( + 'v' => 1, +) +[test] +it => now + +CONF; + $txt = $this->execute([__FILE__, 'config', 'test', '-v']); + + $this->assertEquals($assertion, $txt); + } +} diff --git a/tests/src/Core/Console/ConsoleTest.php b/tests/src/Core/Console/ConsoleTest.php new file mode 100644 index 0000000000..fd09a79452 --- /dev/null +++ b/tests/src/Core/Console/ConsoleTest.php @@ -0,0 +1,48 @@ +markTestSkipped('Please set the MYSQL_* environment variables to your test database credentials.'); + } + + // Reusable App object + $this->app = BaseObject::getApp(); + $this->console = new MultiUseConsole(); + } + + public function execute($args) { + Intercept::reset(); + $this->console->reset(); + $this->console->parseTestArgv($args); + $this->console->execute(); + + $returnStr = Intercept::$cache; + Intercept::reset(); + return $returnStr; + } +} diff --git a/tests/src/Core/Console/MultiUseConsole.php b/tests/src/Core/Console/MultiUseConsole.php new file mode 100644 index 0000000000..ddcbfebc34 --- /dev/null +++ b/tests/src/Core/Console/MultiUseConsole.php @@ -0,0 +1,23 @@ +args = []; + $this->options = []; + } + + public function parseTestArgv($argv) + { + $this->parseArgv($argv); + } +}