From f6966659a9b7560a6d46a22892e9dad28c10c531 Mon Sep 17 00:00:00 2001 From: "J. King" Date: Fri, 7 Dec 2018 20:03:04 -0500 Subject: [PATCH] Use smarter coverage executer; properly suppress stderr during CLI tests --- RoboFile.php | 18 ++++++++++-------- lib/CLI.php | 7 ++++++- tests/cases/CLI/TestCLI.php | 30 +++++++++++++++--------------- 3 files changed, 31 insertions(+), 24 deletions(-) diff --git a/RoboFile.php b/RoboFile.php index 2a5dfda..e957860 100644 --- a/RoboFile.php +++ b/RoboFile.php @@ -81,13 +81,16 @@ class RoboFile extends \Robo\Tasks { } protected function findCoverageEngine(): string { - $null = null; - $code = 0; - exec("phpdbg --version", $null, $code); - if (!$code) { - return "phpdbg -qrr"; + if ($this->isWindows()) { + $dbg = dirname(\PHP_BINARY)."\\phpdbg.exe"; + $dbg = file_exists($dbg) ? $dbg : ""; } 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"); $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(); - 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 diff --git a/lib/CLI.php b/lib/CLI.php index 0ad8e53..7f9deac 100644 --- a/lib/CLI.php +++ b/lib/CLI.php @@ -81,11 +81,16 @@ USAGE_TEXT; return $this->userManage($args); } } catch (AbstractException $e) { - fwrite(STDERR, $e->getMessage().\PHP_EOL); + $this->logError($e->getMessage()); return $e->getCode(); } } + /** @codeCoverageIgnore */ + protected function logError(string $msg) { + fwrite(STDERR,$msg.\PHP_EOL); + } + /** @codeCoverageIgnore */ protected function getService(): Service { return new Service; diff --git a/tests/cases/CLI/TestCLI.php b/tests/cases/CLI/TestCLI.php index ba4d1d0..789767a 100644 --- a/tests/cases/CLI/TestCLI.php +++ b/tests/cases/CLI/TestCLI.php @@ -18,6 +18,8 @@ use Phake; class TestCLI extends \JKingWeb\Arsse\Test\AbstractTest { public function setUp() { 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) { @@ -44,13 +46,13 @@ class TestCLI extends \JKingWeb\Arsse\Test\AbstractTest { } 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); } /** @dataProvider provideHelpText */ 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); } @@ -64,13 +66,12 @@ class TestCLI extends \JKingWeb\Arsse\Test\AbstractTest { public function testStartTheDaemon() { $srv = Phake::mock(Service::class); - $cli = Phake::partialMock(CLI::class); Phake::when($srv)->watch->thenReturn(new \DateTimeImmutable); - Phake::when($cli)->getService->thenReturn($srv); - $this->assertConsole($cli, "arsse.php daemon", 0); + Phake::when($this->cli)->getService->thenReturn($srv); + $this->assertConsole($this->cli, "arsse.php daemon", 0); $this->assertLoaded(true); Phake::verify($srv)->watch(true); - Phake::verify($cli)->getService; + Phake::verify($this->cli)->getService; } /** @dataProvider provideFeedUpdates */ @@ -78,7 +79,7 @@ class TestCLI extends \JKingWeb\Arsse\Test\AbstractTest { 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/", new \PicoFeed\Client\InvalidUrlException)); - $this->assertConsole(new CLI, $cmd, $exitStatus, $output); + $this->assertConsole($this->cli, $cmd, $exitStatus, $output); $this->assertLoaded(true); Phake::verify(Arsse::$db)->feedUpdate; } @@ -93,12 +94,11 @@ class TestCLI extends \JKingWeb\Arsse\Test\AbstractTest { /** @dataProvider provideDefaultConfigurationSaves */ public function testSaveTheDefaultConfiguration(string $cmd, int $exitStatus, string $file) { $conf = Phake::mock(Conf::class); - $cli = Phake::partialMock(CLI::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($cli)->getConf->thenReturn($conf); - $this->assertConsole($cli, $cmd, $exitStatus); + Phake::when($this->cli)->getConf->thenReturn($conf); + $this->assertConsole($this->cli, $cmd, $exitStatus); $this->assertLoaded(false); 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 Arsse::$user = $this->createMock(User::class); Arsse::$user->method("list")->willReturn($list); - $this->assertConsole(new CLI, $cmd, $exitStatus, $output); + $this->assertConsole($this->cli, $cmd, $exitStatus, $output); } public function provideUserList() { @@ -143,7 +143,7 @@ class TestCLI extends \JKingWeb\Arsse\Test\AbstractTest { return is_null($pass) ? "random password" : $pass; } })); - $this->assertConsole(new CLI, $cmd, $exitStatus, $output); + $this->assertConsole($this->cli, $cmd, $exitStatus, $output); } public function provideUserAdditions() { @@ -164,7 +164,7 @@ class TestCLI extends \JKingWeb\Arsse\Test\AbstractTest { ($user == "jane.doe@example.com" && $pass == "superman") ); })); - $this->assertConsole(new CLI, $cmd, $exitStatus, $output); + $this->assertConsole($this->cli, $cmd, $exitStatus, $output); } public function provideUserAuthentication() { @@ -187,7 +187,7 @@ class TestCLI extends \JKingWeb\Arsse\Test\AbstractTest { } throw new \JKingWeb\Arsse\User\Exception("doesNotExist"); })); - $this->assertConsole(new CLI, $cmd, $exitStatus, $output); + $this->assertConsole($this->cli, $cmd, $exitStatus, $output); } public function provideUserRemovals() { @@ -209,7 +209,7 @@ class TestCLI extends \JKingWeb\Arsse\Test\AbstractTest { return is_null($pass) ? "random password" : $pass; } })); - $this->assertConsole(new CLI, $cmd, $exitStatus, $output); + $this->assertConsole($this->cli, $cmd, $exitStatus, $output); } public function provideUserPasswordChanges() {