From 514cb0a3514e978b50f920eac45723add27c7e48 Mon Sep 17 00:00:00 2001 From: "J. King" Date: Fri, 25 Jun 2021 11:08:56 -0400 Subject: [PATCH] Ow --- tests/cases/CLI/TestCLI.php | 40 ++++++++++++++++++++++++++++++++++++- tests/lib/AbstractTest.php | 9 +++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/tests/cases/CLI/TestCLI.php b/tests/cases/CLI/TestCLI.php index c3367f24..258d020e 100644 --- a/tests/cases/CLI/TestCLI.php +++ b/tests/cases/CLI/TestCLI.php @@ -6,6 +6,7 @@ declare(strict_types=1); namespace JKingWeb\Arsse\TestCase\CLI; +use Eloquent\Phony\Phpunit\Phony; use GuzzleHttp\Exception\ClientException; use JKingWeb\Arsse\Arsse; use JKingWeb\Arsse\Conf; @@ -13,10 +14,10 @@ use JKingWeb\Arsse\User; use JKingWeb\Arsse\Database; use JKingWeb\Arsse\Service; use JKingWeb\Arsse\CLI; -use JKingWeb\Arsse\CLI\Exception; use JKingWeb\Arsse\REST\Fever\User as FeverUser; use JKingWeb\Arsse\REST\Miniflux\Token as MinifluxToken; use JKingWeb\Arsse\ImportExport\OPML; +use JKingWeb\Arsse\Service\Daemon; /** @covers \JKingWeb\Arsse\CLI */ class TestCLI extends \JKingWeb\Arsse\Test\AbstractTest { @@ -72,6 +73,43 @@ class TestCLI extends \JKingWeb\Arsse\Test\AbstractTest { $srv->watch->calledWith(true); } + public function testStartTheForkingDaemon(): void { + $f = tempnam(sys_get_temp_dir(), "arsse"); + $srv = $this->mock(Service::class); + $srv->watch->returns(new \DateTimeImmutable); + $daemon = $this->mock(Daemon::class); + $daemon->checkPIDFilePath->returns($f); + $daemon->fork->returns(null); + $this->objMock->get->with(Service::class)->returns($srv->get()); + $this->objMock->get->with(Daemon::class)->returns($daemon->get()); + $this->assertConsole("arsse.php daemon --fork=arsse.pid", 0); + $this->assertFileDoesNotExist($f); + Phony::inOrder( + $daemon->checkPIDFilePath->calledWith("arsse.pid"), + $daemon->fork->calledWith($f), + $this->cli->loadConf->called(), + $srv->watch->calledWith(true) + ); + } + + public function testFailToStartTheForkingDaemon(): void { + $srv = $this->mock(Service::class); + $srv->watch->returns(new \DateTimeImmutable); + $daemon = $this->mock(Daemon::class); + $daemon->checkPIDFilePath->throws(new Service\Exception("pidDuplicate", ['pid' =>2112])); + $daemon->fork->returns(null); + $this->objMock->get->with(Service::class)->returns($srv->get()); + $this->objMock->get->with(Daemon::class)->returns($daemon->get()); + $this->assertConsole("arsse.php daemon --fork=arsse.pid", 0); + $this->assertFileDoesNotExist($f); + Phony::inOrder( + $daemon->checkPIDFilePath->calledWith("arsse.pid"), + $daemon->fork->calledWith($f), + $this->cli->loadConf->called(), + $srv->watch->calledWith(true) + ); + } + public function testRefreshAllFeeds(): void { $srv = $this->mock(Service::class); $srv->watch->returns(new \DateTimeImmutable); diff --git a/tests/lib/AbstractTest.php b/tests/lib/AbstractTest.php index 5bfd0353..d41b7d37 100644 --- a/tests/lib/AbstractTest.php +++ b/tests/lib/AbstractTest.php @@ -149,6 +149,15 @@ abstract class AbstractTest extends \PHPUnit\Framework\TestCase { } } + public static function assertFileDoesNotExist(string $filename, string $message = ''): void { + if (method_exists(parent::class, "assertFileDoesNotExist")) { + parent::assertFileDoesNotExist($filename, $message); + } else { + parent::assertFileNotExists($filename, $message); + } + + } + public function assertException($msg = "", string $prefix = "", string $type = "Exception"): void { if (func_num_args()) { if ($msg instanceof \JKingWeb\Arsse\AbstractException) {