1
1
Fork 0
mirror of https://code.mensbeam.com/MensBeam/Arsse.git synced 2024-12-23 17:12:41 +00:00
Arsse/tests/cases/CLI/TestCLI.php

363 lines
22 KiB
PHP
Raw Normal View History

2018-11-06 17:32:28 +00:00
<?php
/** @license MIT
* Copyright 2017 J. King, Dustin Wilson et al.
* See LICENSE and AUTHORS files for details */
declare(strict_types=1);
namespace JKingWeb\Arsse\TestCase\CLI;
use GuzzleHttp\Exception\ClientException;
2018-11-06 17:32:28 +00:00
use JKingWeb\Arsse\Arsse;
use JKingWeb\Arsse\Conf;
use JKingWeb\Arsse\User;
use JKingWeb\Arsse\Database;
use JKingWeb\Arsse\Service;
use JKingWeb\Arsse\CLI;
2019-03-25 14:45:05 +00:00
use JKingWeb\Arsse\REST\Fever\User as FeverUser;
use JKingWeb\Arsse\ImportExport\OPML;
2018-11-06 17:32:28 +00:00
/** @covers \JKingWeb\Arsse\CLI */
class TestCLI extends \JKingWeb\Arsse\Test\AbstractTest {
2019-10-16 18:42:43 +00:00
public function setUp(): void {
self::clearData();
2019-09-05 14:03:32 +00:00
$this->cli = \Phake::partialMock(CLI::class);
\Phake::when($this->cli)->logError->thenReturn(null);
\Phake::when($this->cli)->loadConf->thenReturn(true);
2018-11-06 17:32:28 +00:00
}
public function assertConsole(CLI $cli, string $command, int $exitStatus, string $output = "", bool $pattern = false): void {
2018-11-06 17:32:28 +00:00
$argv = \Clue\Arguments\split($command);
$output = strlen($output) ? $output.\PHP_EOL : "";
if ($pattern) {
2018-12-05 22:28:11 +00:00
$this->expectOutputRegex($output);
2018-11-06 17:32:28 +00:00
} else {
$this->expectOutputString($output);
}
$this->assertSame($exitStatus, $cli->dispatch($argv));
}
2020-01-20 18:52:48 +00:00
public function testPrintVersion(): void {
$this->assertConsole($this->cli, "arsse.php --version", 0, Arsse::VERSION);
\Phake::verify($this->cli, \Phake::times(0))->loadConf;
2018-11-06 17:32:28 +00:00
}
/** @dataProvider provideHelpText */
2020-01-20 18:52:48 +00:00
public function testPrintHelp(string $cmd, string $name): void {
$this->assertConsole($this->cli, $cmd, 0, str_replace("arsse.php", $name, CLI::USAGE));
\Phake::verify($this->cli, \Phake::times(0))->loadConf;
2018-11-06 17:32:28 +00:00
}
2019-10-16 18:42:43 +00:00
public function provideHelpText(): iterable {
2018-11-06 17:32:28 +00:00
return [
["arsse.php --help", "arsse.php"],
["arsse --help", "arsse"],
["thearsse --help", "thearsse"],
2019-05-06 04:02:59 +00:00
["arsse.php -h", "arsse.php"],
["arsse -h", "arsse"],
["thearsse -h", "thearsse"],
2018-11-06 17:32:28 +00:00
];
}
2020-01-20 18:52:48 +00:00
public function testStartTheDaemon(): void {
2019-09-05 14:03:32 +00:00
$srv = \Phake::mock(Service::class);
\Phake::when($srv)->watch->thenReturn(new \DateTimeImmutable);
\Phake::when($this->cli)->getInstance(Service::class)->thenReturn($srv);
$this->assertConsole($this->cli, "arsse.php daemon", 0);
\Phake::verify($this->cli)->loadConf;
2019-09-05 14:03:32 +00:00
\Phake::verify($srv)->watch(true);
\Phake::verify($this->cli)->getInstance(Service::class);
2018-11-06 17:32:28 +00:00
}
2020-01-20 18:52:48 +00:00
public function testRefreshAllFeeds(): void {
2019-09-05 14:03:32 +00:00
$srv = \Phake::mock(Service::class);
\Phake::when($srv)->watch->thenReturn(new \DateTimeImmutable);
\Phake::when($this->cli)->getInstance(Service::class)->thenReturn($srv);
$this->assertConsole($this->cli, "arsse.php feed refresh-all", 0);
\Phake::verify($this->cli)->loadConf;
2019-09-05 14:03:32 +00:00
\Phake::verify($srv)->watch(false);
\Phake::verify($this->cli)->getInstance(Service::class);
}
2018-11-06 17:32:28 +00:00
/** @dataProvider provideFeedUpdates */
2020-01-20 18:52:48 +00:00
public function testRefreshAFeed(string $cmd, int $exitStatus, string $output): void {
2019-09-05 14:03:32 +00:00
Arsse::$db = \Phake::mock(Database::class);
\Phake::when(Arsse::$db)->feedUpdate(1, true)->thenReturn(true);
\Phake::when(Arsse::$db)->feedUpdate(2, true)->thenThrow(new \JKingWeb\Arsse\Feed\Exception("http://example.com/", $this->mockGuzzleException(ClientException::class, "", 404)));
$this->assertConsole($this->cli, $cmd, $exitStatus, $output);
\Phake::verify($this->cli)->loadConf;
2019-09-05 14:03:32 +00:00
\Phake::verify(Arsse::$db)->feedUpdate;
2018-11-06 17:32:28 +00:00
}
2019-10-16 18:42:43 +00:00
public function provideFeedUpdates(): iterable {
2018-11-06 17:32:28 +00:00
return [
["arsse.php feed refresh 1", 0, ""],
["arsse.php feed refresh 2", 10502, ""],
];
}
/** @dataProvider provideDefaultConfigurationSaves */
2020-01-20 18:52:48 +00:00
public function testSaveTheDefaultConfiguration(string $cmd, int $exitStatus, string $file): void {
2019-09-05 14:03:32 +00:00
$conf = \Phake::mock(Conf::class);
\Phake::when($conf)->exportFile("php://output", true)->thenReturn(true);
\Phake::when($conf)->exportFile("good.conf", true)->thenReturn(true);
\Phake::when($conf)->exportFile("bad.conf", true)->thenThrow(new \JKingWeb\Arsse\Conf\Exception("fileUnwritable"));
\Phake::when($this->cli)->getInstance(Conf::class)->thenReturn($conf);
$this->assertConsole($this->cli, $cmd, $exitStatus);
\Phake::verify($this->cli, \Phake::times(0))->loadConf;
2019-09-05 14:03:32 +00:00
\Phake::verify($conf)->exportFile($file, true);
2018-11-06 17:32:28 +00:00
}
2019-10-16 18:42:43 +00:00
public function provideDefaultConfigurationSaves(): iterable {
2018-11-06 17:32:28 +00:00
return [
["arsse.php conf save-defaults", 0, "php://output"],
["arsse.php conf save-defaults -", 0, "php://output"],
["arsse.php conf save-defaults good.conf", 0, "good.conf"],
["arsse.php conf save-defaults bad.conf", 10304, "bad.conf"],
];
}
/** @dataProvider provideUserList */
2020-01-20 18:52:48 +00:00
public function testListUsers(string $cmd, array $list, int $exitStatus, string $output): void {
2018-12-08 00:21:44 +00:00
// FIXME: Phake is somehow unable to mock the User class correctly, so we use PHPUnit's mocks instead
2018-11-06 21:35:33 +00:00
Arsse::$user = $this->createMock(User::class);
Arsse::$user->method("list")->willReturn($list);
$this->assertConsole($this->cli, $cmd, $exitStatus, $output);
2018-11-06 17:32:28 +00:00
}
2019-10-16 18:42:43 +00:00
public function provideUserList(): iterable {
2018-11-06 17:32:28 +00:00
$list = ["john.doe@example.com", "jane.doe@example.com"];
$str = implode(PHP_EOL, $list);
return [
["arsse.php user list", $list, 0, $str],
["arsse.php user", $list, 0, $str],
["arsse.php user list", [], 0, ""],
["arsse.php user", [], 0, ""],
];
}
2018-11-06 21:35:33 +00:00
/** @dataProvider provideUserAdditions */
2020-01-20 18:52:48 +00:00
public function testAddAUser(string $cmd, int $exitStatus, string $output): void {
2018-12-08 00:21:44 +00:00
// FIXME: Phake is somehow unable to mock the User class correctly, so we use PHPUnit's mocks instead
2018-11-06 21:35:33 +00:00
Arsse::$user = $this->createMock(User::class);
Arsse::$user->method("add")->will($this->returnCallback(function($user, $pass = null) {
switch ($user) {
case "john.doe@example.com":
throw new \JKingWeb\Arsse\User\Exception("alreadyExists");
case "jane.doe@example.com":
return is_null($pass) ? "random password" : $pass;
}
}));
$this->assertConsole($this->cli, $cmd, $exitStatus, $output);
2018-11-06 21:35:33 +00:00
}
2019-10-16 18:42:43 +00:00
public function provideUserAdditions(): iterable {
2018-11-06 21:35:33 +00:00
return [
["arsse.php user add john.doe@example.com", 10403, ""],
["arsse.php user add jane.doe@example.com", 0, "random password"],
["arsse.php user add jane.doe@example.com superman", 0, ""],
];
}
/** @dataProvider provideUserAuthentication */
2020-01-20 18:52:48 +00:00
public function testAuthenticateAUser(string $cmd, int $exitStatus, string $output): void {
2018-12-08 00:21:44 +00:00
// FIXME: Phake is somehow unable to mock the User class correctly, so we use PHPUnit's mocks instead
2018-11-06 21:35:33 +00:00
Arsse::$user = $this->createMock(User::class);
Arsse::$user->method("auth")->will($this->returnCallback(function($user, $pass) {
2020-03-01 20:16:50 +00:00
return
($user === "john.doe@example.com" && $pass === "secret") ||
($user === "jane.doe@example.com" && $pass === "superman")
2020-03-01 20:16:50 +00:00
;
2018-11-06 21:35:33 +00:00
}));
2019-03-25 14:45:05 +00:00
$fever = \Phake::mock(FeverUser::class);
\Phake::when($fever)->authenticate->thenReturn(false);
\Phake::when($fever)->authenticate("john.doe@example.com", "ashalla")->thenReturn(true);
2019-03-25 18:24:58 +00:00
\Phake::when($fever)->authenticate("jane.doe@example.com", "thx1138")->thenReturn(true);
\Phake::when($this->cli)->getInstance(FeverUser::class)->thenReturn($fever);
$this->assertConsole($this->cli, $cmd, $exitStatus, $output);
2018-11-06 21:35:33 +00:00
}
2019-10-16 18:42:43 +00:00
public function provideUserAuthentication(): iterable {
2018-11-06 21:35:33 +00:00
$l = new \JKingWeb\Arsse\Lang;
2019-03-25 14:45:05 +00:00
$success = $l("CLI.Auth.Success");
$failure = $l("CLI.Auth.Failure");
2018-11-06 21:35:33 +00:00
return [
2019-03-25 14:45:05 +00:00
["arsse.php user auth john.doe@example.com secret", 0, $success],
["arsse.php user auth john.doe@example.com superman", 1, $failure],
["arsse.php user auth jane.doe@example.com secret", 1, $failure],
["arsse.php user auth jane.doe@example.com superman", 0, $success],
["arsse.php user auth john.doe@example.com ashalla --fever", 0, $success],
["arsse.php user auth john.doe@example.com thx1138 --fever", 1, $failure],
["arsse.php user auth --fever jane.doe@example.com ashalla", 1, $failure],
["arsse.php user auth --fever jane.doe@example.com thx1138", 0, $success],
2018-11-06 21:35:33 +00:00
];
}
/** @dataProvider provideUserRemovals */
2020-01-20 18:52:48 +00:00
public function testRemoveAUser(string $cmd, int $exitStatus, string $output): void {
2018-12-08 00:21:44 +00:00
// FIXME: Phake is somehow unable to mock the User class correctly, so we use PHPUnit's mocks instead
2018-11-06 21:35:33 +00:00
Arsse::$user = $this->createMock(User::class);
Arsse::$user->method("remove")->will($this->returnCallback(function($user) {
if ($user === "john.doe@example.com") {
2018-11-06 21:35:33 +00:00
return true;
}
throw new \JKingWeb\Arsse\User\Exception("doesNotExist");
}));
$this->assertConsole($this->cli, $cmd, $exitStatus, $output);
2018-11-06 21:35:33 +00:00
}
2019-10-16 18:42:43 +00:00
public function provideUserRemovals(): iterable {
2018-11-06 21:35:33 +00:00
return [
["arsse.php user remove john.doe@example.com", 0, ""],
["arsse.php user remove jane.doe@example.com", 10402, ""],
];
}
/** @dataProvider provideUserPasswordChanges */
2020-01-20 18:52:48 +00:00
public function testChangeAUserPassword(string $cmd, int $exitStatus, string $output): void {
$passwordChange = function($user, $pass = null) {
2018-11-06 21:35:33 +00:00
switch ($user) {
case "jane.doe@example.com":
throw new \JKingWeb\Arsse\User\Exception("doesNotExist");
case "john.doe@example.com":
return is_null($pass) ? "random password" : $pass;
}
};
// FIXME: Phake is somehow unable to mock the User class correctly, so we use PHPUnit's mocks instead
Arsse::$user = $this->createMock(User::class);
Arsse::$user->method("passwordSet")->will($this->returnCallback($passwordChange));
$fever = \Phake::mock(FeverUser::class);
\Phake::when($fever)->register->thenReturnCallback($passwordChange);
\Phake::when($this->cli)->getInstance(FeverUser::class)->thenReturn($fever);
$this->assertConsole($this->cli, $cmd, $exitStatus, $output);
2018-11-06 21:35:33 +00:00
}
2019-10-16 18:42:43 +00:00
public function provideUserPasswordChanges(): iterable {
2018-11-06 21:35:33 +00:00
return [
["arsse.php user set-pass john.doe@example.com", 0, "random password"],
["arsse.php user set-pass john.doe@example.com superman", 0, ""],
["arsse.php user set-pass jane.doe@example.com", 10402, ""],
["arsse.php user set-pass john.doe@example.com --fever", 0, "random password"],
["arsse.php user set-pass --fever john.doe@example.com superman", 0, ""],
["arsse.php user set-pass jane.doe@example.com --fever", 10402, ""],
2018-11-06 21:35:33 +00:00
];
}
2019-03-25 14:45:05 +00:00
/** @dataProvider provideUserPasswordClearings */
2020-01-20 18:52:48 +00:00
public function testClearAUserPassword(string $cmd, int $exitStatus, string $output): void {
$passwordClear = function($user) {
switch ($user) {
case "jane.doe@example.com":
throw new \JKingWeb\Arsse\User\Exception("doesNotExist");
case "john.doe@example.com":
return true;
}
};
// FIXME: Phake is somehow unable to mock the User class correctly, so we use PHPUnit's mocks instead
Arsse::$user = $this->createMock(User::class);
2019-03-25 21:07:28 +00:00
Arsse::$user->method("passwordUnset")->will($this->returnCallback($passwordClear));
$fever = \Phake::mock(FeverUser::class);
2019-03-25 21:07:28 +00:00
\Phake::when($fever)->unregister->thenReturnCallback($passwordClear);
\Phake::when($this->cli)->getInstance(FeverUser::class)->thenReturn($fever);
$this->assertConsole($this->cli, $cmd, $exitStatus, $output);
}
2019-10-16 18:42:43 +00:00
public function provideUserPasswordClearings(): iterable {
return [
["arsse.php user unset-pass john.doe@example.com", 0, ""],
["arsse.php user unset-pass jane.doe@example.com", 10402, ""],
["arsse.php user unset-pass john.doe@example.com --fever", 0, ""],
["arsse.php user unset-pass jane.doe@example.com --fever", 10402, ""],
];
2019-03-25 14:45:05 +00:00
}
/** @dataProvider provideOpmlExports */
2020-01-20 18:52:48 +00:00
public function testExportToOpml(string $cmd, int $exitStatus, string $file, string $user, bool $flat): void {
2019-09-05 14:03:32 +00:00
$opml = \Phake::mock(OPML::class);
\Phake::when($opml)->exportFile("php://output", $user, $flat)->thenReturn(true);
\Phake::when($opml)->exportFile("good.opml", $user, $flat)->thenReturn(true);
\Phake::when($opml)->exportFile("bad.opml", $user, $flat)->thenThrow(new \JKingWeb\Arsse\ImportExport\Exception("fileUnwritable"));
\Phake::when($this->cli)->getInstance(OPML::class)->thenReturn($opml);
$this->assertConsole($this->cli, $cmd, $exitStatus);
\Phake::verify($this->cli)->loadConf;
2019-09-05 14:03:32 +00:00
\Phake::verify($opml)->exportFile($file, $user, $flat);
}
2019-10-16 18:42:43 +00:00
public function provideOpmlExports(): iterable {
return [
["arsse.php export john.doe@example.com", 0, "php://output", "john.doe@example.com", false],
["arsse.php export john.doe@example.com -", 0, "php://output", "john.doe@example.com", false],
["arsse.php export john.doe@example.com good.opml", 0, "good.opml", "john.doe@example.com", false],
["arsse.php export john.doe@example.com bad.opml", 10604, "bad.opml", "john.doe@example.com", false],
["arsse.php export john.doe@example.com --flat", 0, "php://output", "john.doe@example.com", true],
["arsse.php export john.doe@example.com - --flat", 0, "php://output", "john.doe@example.com", true],
["arsse.php export --flat john.doe@example.com good.opml", 0, "good.opml", "john.doe@example.com", true],
["arsse.php export john.doe@example.com bad.opml --flat", 10604, "bad.opml", "john.doe@example.com", true],
["arsse.php export jane.doe@example.com", 0, "php://output", "jane.doe@example.com", false],
["arsse.php export jane.doe@example.com -", 0, "php://output", "jane.doe@example.com", false],
["arsse.php export jane.doe@example.com good.opml", 0, "good.opml", "jane.doe@example.com", false],
["arsse.php export jane.doe@example.com bad.opml", 10604, "bad.opml", "jane.doe@example.com", false],
["arsse.php export jane.doe@example.com --flat", 0, "php://output", "jane.doe@example.com", true],
["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],
2019-07-25 19:45:18 +00:00
["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 */
2020-01-20 18:52:48 +00:00
public function testImportFromOpml(string $cmd, int $exitStatus, string $file, string $user, bool $flat, bool $replace): void {
2019-09-05 14:03:32 +00:00
$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);
2019-07-25 19:45:18 +00:00
$this->assertConsole($this->cli, $cmd, $exitStatus);
\Phake::verify($this->cli)->loadConf;
2019-09-05 14:03:32 +00:00
\Phake::verify($opml)->importFile($file, $user, $flat, $replace);
2019-07-25 19:45:18 +00:00
}
2019-10-16 18:42:43 +00:00
public function provideOpmlImports(): iterable {
2019-07-25 19:45:18 +00:00
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],
];
}
2018-11-06 17:32:28 +00:00
}