1
1
Fork 0
mirror of https://code.mensbeam.com/MensBeam/Arsse.git synced 2024-12-22 13:12:41 +00:00

CLI test for import

Fixes #35
This commit is contained in:
J. King 2019-07-25 15:45:18 -04:00
parent 13b76dea0c
commit faf524c54f
3 changed files with 62 additions and 4 deletions

View file

@ -196,11 +196,11 @@ USAGE_TEXT;
case "export":
$u = $args['<username>'];
$file = $this->resolveFile($args['<file>'], "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['<username>'];
$file = $this->resolveFile($args['<file>'], "w");
return (int) !$this->getInstance(OPML::class)->importFile($file, $u, $args['--flat'], $args['--replace']);
$file = $this->resolveFile($args['<file>'], "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;
}

View file

@ -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

View file

@ -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],
];
}
}