diff --git a/lib/CLI.php b/lib/CLI.php index 1a17cc52..27335778 100644 --- a/lib/CLI.php +++ b/lib/CLI.php @@ -352,8 +352,12 @@ USAGE_TEXT; throw new CLI\Exception("pidUncreatable", ['pidfile' => $out]); } } else { - throw new \Exception("pidDirNotFound", ['piddir' => $dir]); + throw new CLI\Exception("pidDirNotFound", ['piddir' => $dir]); } return $out; } + + protected function realpath(string $path) { + return @realpath($path); + } } diff --git a/tests/cases/CLI/TestCLI.php b/tests/cases/CLI/TestCLI.php index ff62c75e..7c542144 100644 --- a/tests/cases/CLI/TestCLI.php +++ b/tests/cases/CLI/TestCLI.php @@ -13,12 +13,27 @@ 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 org\bovigo\vfs\vfsStream; /** @covers \JKingWeb\Arsse\CLI */ class TestCLI extends \JKingWeb\Arsse\Test\AbstractTest { + protected $pidfiles = [ + 'errors' => [ + 'create' => [], + 'read' => "", + 'write' => "", + 'readwrite' => "", + ], + 'ok' => [ + 'dir' => [], + 'file' => "", + ], + ]; + public function setUp(): void { parent::setUp(); $this->cli = $this->partialMock(CLI::class); @@ -27,6 +42,33 @@ class TestCLI extends \JKingWeb\Arsse\Test\AbstractTest { $this->dbMock = $this->mock(Database::class); } + /** @dataProvider providePidResolutions */ + public function testResolvePidFiles(string $file, bool $realpath, $exp): void { + $vfs = vfsStream::setup("pidtest", 0777, $this->pidfiles); + $path = $vfs->url()."/"; + // set up access blocks + chmod($path."errors/create", 0555); + chmod($path."errors/read", 0333); + chmod($path."errors/write", 0555); + chmod($path."errors/readwrite", 0111); + // set up mock CLI + $this->cli->realPath->returns($realpath ? $path.$file : false); + $cli = $this->cli->get(); + // perform the test + if ($exp instanceof \Exception) { + $this->assertException($exp); + $cli->resolvePID($file); + } else { + $this->assertSame($exp, $cli->resolvePID($file)); + } + } + + public function providePidResolutions(): iterable { + return [ + ["errors/create", true, new Exception("pidUncreatable")], + ]; + } + public function assertConsole(string $command, int $exitStatus, string $output = "", bool $pattern = false): void { Arsse::$obj = $this->objMock->get(); Arsse::$db = $this->dbMock->get();