diff --git a/lib/CLI.php b/lib/CLI.php index 0b1f3b97..7c0d30b5 100644 --- a/lib/CLI.php +++ b/lib/CLI.php @@ -196,11 +196,11 @@ USAGE_TEXT; case "export": $u = $args['']; $file = $this->resolveFile($args[''], "w"); - return (int) !$this->getInstance(OPML::class)->exportFile($file, $u, $args['--flat']); + return (int) !$this->getInstance(OPML::class)->exportFile($file, $u, ($args['--flat'] || $args['-f'])); case "import": $u = $args['']; - $file = $this->resolveFile($args[''], "w"); - return (int) !$this->getInstance(OPML::class)->importFile($file, $u, $args['--flat'], $args['--replace']); + $file = $this->resolveFile($args[''], "r"); + return (int) !$this->getInstance(OPML::class)->importFile($file, $u, ($args['--flat'] || $args['-f']), ($args['--replace'] || $args['-r'])); } } catch (AbstractException $e) { $this->logError($e->getMessage()); @@ -213,6 +213,7 @@ USAGE_TEXT; fwrite(STDERR, $msg.\PHP_EOL); } + /** @codeCoverageIgnore */ protected function getInstance(string $class) { return new $class; } diff --git a/lib/ImportExport/AbstractImportExport.php b/lib/ImportExport/AbstractImportExport.php index 66cc9b23..f882ea16 100644 --- a/lib/ImportExport/AbstractImportExport.php +++ b/lib/ImportExport/AbstractImportExport.php @@ -155,7 +155,7 @@ abstract class AbstractImportExport { return true; } - public function importFile(string $file, string $user, bool $flat = false, bool $replace): bool { + public function importFile(string $file, string $user, bool $flat = false, bool $replace = false): bool { $data = @file_get_contents($file); if ($data === false) { // if it fails throw an exception diff --git a/tests/cases/CLI/TestCLI.php b/tests/cases/CLI/TestCLI.php index 46290fc6..825bc381 100644 --- a/tests/cases/CLI/TestCLI.php +++ b/tests/cases/CLI/TestCLI.php @@ -311,6 +311,63 @@ class TestCLI extends \JKingWeb\Arsse\Test\AbstractTest { ["arsse.php export jane.doe@example.com - --flat", 0, "php://output", "jane.doe@example.com", true], ["arsse.php export --flat jane.doe@example.com good.opml", 0, "good.opml", "jane.doe@example.com", true], ["arsse.php export jane.doe@example.com bad.opml --flat", 10604, "bad.opml", "jane.doe@example.com", true], + ["arsse.php export john.doe@example.com -f", 0, "php://output", "john.doe@example.com", true], + ["arsse.php export john.doe@example.com - -f", 0, "php://output", "john.doe@example.com", true], + ["arsse.php export -f john.doe@example.com good.opml", 0, "good.opml", "john.doe@example.com", true], + ["arsse.php export john.doe@example.com bad.opml -f", 10604, "bad.opml", "john.doe@example.com", true], + ["arsse.php export jane.doe@example.com -f", 0, "php://output", "jane.doe@example.com", true], + ["arsse.php export jane.doe@example.com - -f", 0, "php://output", "jane.doe@example.com", true], + ["arsse.php export -f jane.doe@example.com good.opml", 0, "good.opml", "jane.doe@example.com", true], + ["arsse.php export jane.doe@example.com bad.opml -f", 10604, "bad.opml", "jane.doe@example.com", true], + ]; + } + + /** @dataProvider provideOpmlImports */ + public function testImportFromOpml(string $cmd, int $exitStatus, string $file, string $user, bool $flat, bool $replace) { + $opml = Phake::mock(OPML::class); + Phake::when($opml)->importFile("php://input", $user, $flat, $replace)->thenReturn(true); + Phake::when($opml)->importFile("good.opml", $user, $flat, $replace)->thenReturn(true); + Phake::when($opml)->importFile("bad.opml", $user, $flat, $replace)->thenThrow(new \JKingWeb\Arsse\ImportExport\Exception("fileUnreadable")); + Phake::when($this->cli)->getInstance(OPML::class)->thenReturn($opml); + $this->assertConsole($this->cli, $cmd, $exitStatus); + $this->assertLoaded(true); + Phake::verify($opml)->importFile($file, $user, $flat, $replace); + } + + public function provideOpmlImports() { + return [ + ["arsse.php import john.doe@example.com", 0, "php://input", "john.doe@example.com", false, false], + ["arsse.php import john.doe@example.com -", 0, "php://input", "john.doe@example.com", false, false], + ["arsse.php import john.doe@example.com good.opml", 0, "good.opml", "john.doe@example.com", false, false], + ["arsse.php import john.doe@example.com bad.opml", 10603, "bad.opml", "john.doe@example.com", false, false], + ["arsse.php import john.doe@example.com --flat", 0, "php://input", "john.doe@example.com", true, false], + ["arsse.php import john.doe@example.com - --flat", 0, "php://input", "john.doe@example.com", true, false], + ["arsse.php import --flat john.doe@example.com good.opml", 0, "good.opml", "john.doe@example.com", true, false], + ["arsse.php import john.doe@example.com bad.opml --flat", 10603, "bad.opml", "john.doe@example.com", true, false], + ["arsse.php import jane.doe@example.com", 0, "php://input", "jane.doe@example.com", false, false], + ["arsse.php import jane.doe@example.com -", 0, "php://input", "jane.doe@example.com", false, false], + ["arsse.php import jane.doe@example.com good.opml", 0, "good.opml", "jane.doe@example.com", false, false], + ["arsse.php import jane.doe@example.com bad.opml", 10603, "bad.opml", "jane.doe@example.com", false, false], + ["arsse.php import jane.doe@example.com --flat", 0, "php://input", "jane.doe@example.com", true, false], + ["arsse.php import jane.doe@example.com - --flat", 0, "php://input", "jane.doe@example.com", true, false], + ["arsse.php import --flat jane.doe@example.com good.opml", 0, "good.opml", "jane.doe@example.com", true, false], + ["arsse.php import jane.doe@example.com bad.opml --flat", 10603, "bad.opml", "jane.doe@example.com", true, false], + ["arsse.php import john.doe@example.com --replace", 0, "php://input", "john.doe@example.com", false, true], + ["arsse.php import john.doe@example.com - -r", 0, "php://input", "john.doe@example.com", false, true], + ["arsse.php import --replace john.doe@example.com good.opml", 0, "good.opml", "john.doe@example.com", false, true], + ["arsse.php import -r john.doe@example.com bad.opml", 10603, "bad.opml", "john.doe@example.com", false, true], + ["arsse.php import --replace john.doe@example.com --flat", 0, "php://input", "john.doe@example.com", true, true], + ["arsse.php import -r john.doe@example.com - --flat", 0, "php://input", "john.doe@example.com", true, true], + ["arsse.php import --flat john.doe@example.com good.opml -r", 0, "good.opml", "john.doe@example.com", true, true], + ["arsse.php import --replace john.doe@example.com bad.opml --flat", 10603, "bad.opml", "john.doe@example.com", true, true], + ["arsse.php import jane.doe@example.com -r ", 0, "php://input", "jane.doe@example.com", false, true], + ["arsse.php import jane.doe@example.com - --replace", 0, "php://input", "jane.doe@example.com", false, true], + ["arsse.php import -r jane.doe@example.com good.opml", 0, "good.opml", "jane.doe@example.com", false, true], + ["arsse.php import --replace jane.doe@example.com bad.opml", 10603, "bad.opml", "jane.doe@example.com", false, true], + ["arsse.php import jane.doe@example.com --flat -r", 0, "php://input", "jane.doe@example.com", true, true], + ["arsse.php import jane.doe@example.com - --flat --replace", 0, "php://input", "jane.doe@example.com", true, true], + ["arsse.php import --flat jane.doe@example.com good.opml -r", 0, "good.opml", "jane.doe@example.com", true, true], + ["arsse.php import jane.doe@example.com bad.opml --replace --flat", 10603, "bad.opml", "jane.doe@example.com", true, true], ]; } }