1
1
Fork 0
mirror of https://code.mensbeam.com/MensBeam/Arsse.git synced 2024-12-23 05:54:55 +00:00

Use smarter coverage executer; properly suppress stderr during CLI tests

This commit is contained in:
J. King 2018-12-07 20:03:04 -05:00
parent 0513b606c2
commit f6966659a9
3 changed files with 31 additions and 24 deletions

View file

@ -81,13 +81,16 @@ class RoboFile extends \Robo\Tasks {
} }
protected function findCoverageEngine(): string { protected function findCoverageEngine(): string {
$null = null; if ($this->isWindows()) {
$code = 0; $dbg = dirname(\PHP_BINARY)."\\phpdbg.exe";
exec("phpdbg --version", $null, $code); $dbg = file_exists($dbg) ? $dbg : "";
if (!$code) {
return "phpdbg -qrr";
} else { } else {
return "php"; $dbg = `which phpdbg`;
}
if ($dbg) {
return escapeshellarg($dbg)." -qrr";
} else {
return escapeshellarg(\PHP_BINARY);
} }
} }
@ -114,9 +117,8 @@ class RoboFile extends \Robo\Tasks {
} }
$execpath = realpath(self::BASE."vendor-bin/phpunit/vendor/phpunit/phpunit/phpunit"); $execpath = realpath(self::BASE."vendor-bin/phpunit/vendor/phpunit/phpunit/phpunit");
$confpath = realpath(self::BASE_TEST."phpunit.xml"); $confpath = realpath(self::BASE_TEST."phpunit.xml");
$blackhole = $this->isWindows() ? "nul" : "/dev/null";
$this->taskServer(8000)->host("localhost")->dir(self::BASE_TEST."docroot")->rawArg("-n")->arg(self::BASE_TEST."server.php")->background()->run(); $this->taskServer(8000)->host("localhost")->dir(self::BASE_TEST."docroot")->rawArg("-n")->arg(self::BASE_TEST."server.php")->background()->run();
return $this->taskExec($executor)->arg($execpath)->option("-c", $confpath)->args(array_merge($set, $args))->rawArg("2>$blackhole")->run(); return $this->taskExec($executor)->arg($execpath)->option("-c", $confpath)->args(array_merge($set, $args))->run();
} }
/** Packages a given commit of the software into a release tarball /** Packages a given commit of the software into a release tarball

View file

@ -81,11 +81,16 @@ USAGE_TEXT;
return $this->userManage($args); return $this->userManage($args);
} }
} catch (AbstractException $e) { } catch (AbstractException $e) {
fwrite(STDERR, $e->getMessage().\PHP_EOL); $this->logError($e->getMessage());
return $e->getCode(); return $e->getCode();
} }
} }
/** @codeCoverageIgnore */
protected function logError(string $msg) {
fwrite(STDERR,$msg.\PHP_EOL);
}
/** @codeCoverageIgnore */ /** @codeCoverageIgnore */
protected function getService(): Service { protected function getService(): Service {
return new Service; return new Service;

View file

@ -18,6 +18,8 @@ use Phake;
class TestCLI extends \JKingWeb\Arsse\Test\AbstractTest { class TestCLI extends \JKingWeb\Arsse\Test\AbstractTest {
public function setUp() { public function setUp() {
self::clearData(false); self::clearData(false);
$this->cli = Phake::partialMock(CLI::class);
Phake::when($this->cli)->logError->thenReturn(null);
} }
public function assertConsole(CLI $cli, string $command, int $exitStatus, string $output = "", bool $pattern = false) { public function assertConsole(CLI $cli, string $command, int $exitStatus, string $output = "", bool $pattern = false) {
@ -44,13 +46,13 @@ class TestCLI extends \JKingWeb\Arsse\Test\AbstractTest {
} }
public function testPrintVersion() { public function testPrintVersion() {
$this->assertConsole(new CLI, "arsse.php --version", 0, Arsse::VERSION); $this->assertConsole($this->cli, "arsse.php --version", 0, Arsse::VERSION);
$this->assertLoaded(false); $this->assertLoaded(false);
} }
/** @dataProvider provideHelpText */ /** @dataProvider provideHelpText */
public function testPrintHelp(string $cmd, string $name) { public function testPrintHelp(string $cmd, string $name) {
$this->assertConsole(new CLI, $cmd, 0, str_replace("arsse.php", $name, CLI::USAGE)); $this->assertConsole($this->cli, $cmd, 0, str_replace("arsse.php", $name, CLI::USAGE));
$this->assertLoaded(false); $this->assertLoaded(false);
} }
@ -64,13 +66,12 @@ class TestCLI extends \JKingWeb\Arsse\Test\AbstractTest {
public function testStartTheDaemon() { public function testStartTheDaemon() {
$srv = Phake::mock(Service::class); $srv = Phake::mock(Service::class);
$cli = Phake::partialMock(CLI::class);
Phake::when($srv)->watch->thenReturn(new \DateTimeImmutable); Phake::when($srv)->watch->thenReturn(new \DateTimeImmutable);
Phake::when($cli)->getService->thenReturn($srv); Phake::when($this->cli)->getService->thenReturn($srv);
$this->assertConsole($cli, "arsse.php daemon", 0); $this->assertConsole($this->cli, "arsse.php daemon", 0);
$this->assertLoaded(true); $this->assertLoaded(true);
Phake::verify($srv)->watch(true); Phake::verify($srv)->watch(true);
Phake::verify($cli)->getService; Phake::verify($this->cli)->getService;
} }
/** @dataProvider provideFeedUpdates */ /** @dataProvider provideFeedUpdates */
@ -78,7 +79,7 @@ class TestCLI extends \JKingWeb\Arsse\Test\AbstractTest {
Arsse::$db = Phake::mock(Database::class); Arsse::$db = Phake::mock(Database::class);
Phake::when(Arsse::$db)->feedUpdate(1, true)->thenReturn(true); 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/", new \PicoFeed\Client\InvalidUrlException)); Phake::when(Arsse::$db)->feedUpdate(2, true)->thenThrow(new \JKingWeb\Arsse\Feed\Exception("http://example.com/", new \PicoFeed\Client\InvalidUrlException));
$this->assertConsole(new CLI, $cmd, $exitStatus, $output); $this->assertConsole($this->cli, $cmd, $exitStatus, $output);
$this->assertLoaded(true); $this->assertLoaded(true);
Phake::verify(Arsse::$db)->feedUpdate; Phake::verify(Arsse::$db)->feedUpdate;
} }
@ -93,12 +94,11 @@ class TestCLI extends \JKingWeb\Arsse\Test\AbstractTest {
/** @dataProvider provideDefaultConfigurationSaves */ /** @dataProvider provideDefaultConfigurationSaves */
public function testSaveTheDefaultConfiguration(string $cmd, int $exitStatus, string $file) { public function testSaveTheDefaultConfiguration(string $cmd, int $exitStatus, string $file) {
$conf = Phake::mock(Conf::class); $conf = Phake::mock(Conf::class);
$cli = Phake::partialMock(CLI::class);
Phake::when($conf)->exportFile("php://output", true)->thenReturn(true); Phake::when($conf)->exportFile("php://output", true)->thenReturn(true);
Phake::when($conf)->exportFile("good.conf", 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($conf)->exportFile("bad.conf", true)->thenThrow(new \JKingWeb\Arsse\Conf\Exception("fileUnwritable"));
Phake::when($cli)->getConf->thenReturn($conf); Phake::when($this->cli)->getConf->thenReturn($conf);
$this->assertConsole($cli, $cmd, $exitStatus); $this->assertConsole($this->cli, $cmd, $exitStatus);
$this->assertLoaded(false); $this->assertLoaded(false);
Phake::verify($conf)->exportFile($file, true); Phake::verify($conf)->exportFile($file, true);
} }
@ -117,7 +117,7 @@ class TestCLI extends \JKingWeb\Arsse\Test\AbstractTest {
// FIXME: Phake is somehow unable to mock the User class correctly, so we use PHPUnit's mocks instead // 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 = $this->createMock(User::class);
Arsse::$user->method("list")->willReturn($list); Arsse::$user->method("list")->willReturn($list);
$this->assertConsole(new CLI, $cmd, $exitStatus, $output); $this->assertConsole($this->cli, $cmd, $exitStatus, $output);
} }
public function provideUserList() { public function provideUserList() {
@ -143,7 +143,7 @@ class TestCLI extends \JKingWeb\Arsse\Test\AbstractTest {
return is_null($pass) ? "random password" : $pass; return is_null($pass) ? "random password" : $pass;
} }
})); }));
$this->assertConsole(new CLI, $cmd, $exitStatus, $output); $this->assertConsole($this->cli, $cmd, $exitStatus, $output);
} }
public function provideUserAdditions() { public function provideUserAdditions() {
@ -164,7 +164,7 @@ class TestCLI extends \JKingWeb\Arsse\Test\AbstractTest {
($user == "jane.doe@example.com" && $pass == "superman") ($user == "jane.doe@example.com" && $pass == "superman")
); );
})); }));
$this->assertConsole(new CLI, $cmd, $exitStatus, $output); $this->assertConsole($this->cli, $cmd, $exitStatus, $output);
} }
public function provideUserAuthentication() { public function provideUserAuthentication() {
@ -187,7 +187,7 @@ class TestCLI extends \JKingWeb\Arsse\Test\AbstractTest {
} }
throw new \JKingWeb\Arsse\User\Exception("doesNotExist"); throw new \JKingWeb\Arsse\User\Exception("doesNotExist");
})); }));
$this->assertConsole(new CLI, $cmd, $exitStatus, $output); $this->assertConsole($this->cli, $cmd, $exitStatus, $output);
} }
public function provideUserRemovals() { public function provideUserRemovals() {
@ -209,7 +209,7 @@ class TestCLI extends \JKingWeb\Arsse\Test\AbstractTest {
return is_null($pass) ? "random password" : $pass; return is_null($pass) ? "random password" : $pass;
} }
})); }));
$this->assertConsole(new CLI, $cmd, $exitStatus, $output); $this->assertConsole($this->cli, $cmd, $exitStatus, $output);
} }
public function provideUserPasswordChanges() { public function provideUserPasswordChanges() {