diff --git a/tests/Util/Intercept.php b/tests/Util/Intercept.php new file mode 100644 index 000000000..9435a5950 --- /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 000000000..5512528df --- /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 000000000..fd09a7945 --- /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 000000000..ddcbfebc3 --- /dev/null +++ b/tests/src/Core/Console/MultiUseConsole.php @@ -0,0 +1,23 @@ +args = []; + $this->options = []; + } + + public function parseTestArgv($argv) + { + $this->parseArgv($argv); + } +}