From 9d92c1661d158f4362f6006e8977e744f1462470 Mon Sep 17 00:00:00 2001 From: "J. King" Date: Sat, 11 Nov 2023 17:22:13 -0500 Subject: [PATCH] More Phake conversion --- tests/cases/CLI/TestCLI.php | 190 ++++++++++++------------ tests/cases/Database/AbstractTest.php | 3 +- tests/cases/Exception/TestException.php | 5 +- tests/cases/TestArsse.php | 20 +-- tests/cases/User/TestUser.php | 100 ++++++------- tests/lib/AbstractTest.php | 15 -- 6 files changed, 151 insertions(+), 182 deletions(-) diff --git a/tests/cases/CLI/TestCLI.php b/tests/cases/CLI/TestCLI.php index f3481398..1d3d92c7 100644 --- a/tests/cases/CLI/TestCLI.php +++ b/tests/cases/CLI/TestCLI.php @@ -15,6 +15,7 @@ use JKingWeb\Arsse\User; use JKingWeb\Arsse\Database; use JKingWeb\Arsse\Service; use JKingWeb\Arsse\CLI; +use JKingWeb\Arsse\Factory; use JKingWeb\Arsse\REST\Fever\User as FeverUser; use JKingWeb\Arsse\REST\Miniflux\Token as MinifluxToken; use JKingWeb\Arsse\ImportExport\OPML; @@ -25,16 +26,17 @@ class TestCLI extends \JKingWeb\Arsse\Test\AbstractTest { protected $cli; public function setUp(): void { - parent::setUp(); - $this->cli = $this->partialMock(CLI::class); - $this->cli->logError->returns(null); - $this->cli->loadConf->returns(true); - $this->dbMock = $this->mock(Database::class); + Arsse::$db = \Phake::mock(Database::class); + Arsse::$obj = \Phake::mock(Factory::class); + \Phake::when(Arsse::$obj)->get->thenReturnCallback(function($c) { + return new $c; + }); + $this->cli = \Phake::partialMock(CLI::class); + \Phake::when($this->cli)->logError->thenReturn(null); + \Phake::when($this->cli)->loadConf->thenReturn(true); } public function assertConsole(string $command, int $exitStatus, string $output = "", bool $pattern = false): void { - Arsse::$obj = $this->objMock->get(); - Arsse::$db = $this->dbMock->get(); $argv = \Clue\Arguments\split($command); $output = strlen($output) ? $output.\PHP_EOL : ""; if ($pattern) { @@ -42,18 +44,18 @@ class TestCLI extends \JKingWeb\Arsse\Test\AbstractTest { } else { $this->expectOutputString($output); } - $this->assertSame($exitStatus, $this->cli->get()->dispatch($argv)); + $this->assertSame($exitStatus, $this->cli->dispatch($argv)); } public function testPrintVersion(): void { $this->assertConsole("arsse.php --version", 0, Arsse::VERSION); - $this->cli->loadConf->never()->called(); + \Phake::verify($this->cli, \Phake::never())->loadConf(); } /** @dataProvider provideHelpText */ public function testPrintHelp(string $cmd, string $name): void { $this->assertConsole($cmd, 0, str_replace("arsse.php", $name, CLI::USAGE)); - $this->cli->loadConf->never()->called(); + \Phake::verify($this->cli, \Phake::never())->loadConf(); } public function provideHelpText(): iterable { @@ -68,64 +70,64 @@ class TestCLI extends \JKingWeb\Arsse\Test\AbstractTest { } public function testStartTheDaemon(): void { - $srv = $this->mock(Service::class); - $srv->watch->returns(new \DateTimeImmutable); - $this->objMock->get->with(Service::class)->returns($srv->get()); + $srv = \Phake::mock(Service::class); + \Phake::when($srv)->watch->thenReturn(new \DateTimeImmutable); + \Phake::when(Arsse::$obj)->get(Service::class)->thenReturn($srv); $this->assertConsole("arsse.php daemon", 0); - $this->cli->loadConf->called(); - $srv->watch->calledWith(true); + \Phake::verify($this->cli)->loadConf(); + \Phake::verify($srv)->watch(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()); + $srv = \Phake::mock(Service::class); + $daemon = \Phake::mock(Daemon::class); + \Phake::when($srv)->watch->thenReturn(new \DateTimeImmutable); + \Phake::when($daemon)->checkPIDFilePath->thenReturn($f); + \Phake::when($daemon)->fork->thenReturn(null); + \Phake::when(Arsse::$obj)->get(Service::class)->thenReturn($srv); + \Phake::when(Arsse::$obj)->get(Daemon::class)->thenReturn($daemon); $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) + \Phake::inOrder( + \Phake::verify($daemon)->checkPIDFilePath("arsse.pid"), + \Phake::verify($daemon)->fork($f), + \Phake::verify($this->cli)->loadConf(), + \Phake::verify($srv)->watch(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()); + $srv = \Phake::mock(Service::class); + $daemon = \Phake::mock(Daemon::class); + \Phake::when($srv)->watch->thenReturns(new \DateTimeImmutable); + \Phake::when($daemon)->checkPIDFilePath->throws(new Service\Exception("pidDuplicate", ['pid' => 2112])); + \Phake::when($daemon)->fork->returns(null); + \Phake::when(Arsse::$obj)->get(Service::class)->returns($srv); + \Phake::when(Arsse::$obj)->get(Daemon::class)->returns($daemon); $this->assertConsole("arsse.php daemon --fork=arsse.pid", 10809); - $daemon->checkPIDFilePath->calledWith("arsse.pid"); - $daemon->fork->never()->called(); - $this->cli->loadConf->never()->called(); - $srv->watch->never()->called(); + \Phake::verify($daemon)->checkPIDFilePath("arsse.pid"); + \Phake::verify($daemon, \Phake::never())->fork($this->anything()); + \Phake::verify($this->cli, \Phake::never())->loadConf(); + \Phake::verify($srv, \Phake::never())->watch($this->anything()); } public function testRefreshAllFeeds(): void { - $srv = $this->mock(Service::class); - $srv->watch->returns(new \DateTimeImmutable); - $this->objMock->get->with(Service::class)->returns($srv->get()); + $srv = \Phake::mock(Service::class); + \Phake::when($srv)->watch->thenReturn(new \DateTimeImmutable); + \Phake::when(Arsse::$obj)->get(Service::class)->thenReturn($srv); $this->assertConsole("arsse.php feed refresh-all", 0); - $this->cli->loadConf->called(); - $srv->watch->calledWith(false); + \Phake::verify($this->cli)->loadConf(); + \Phake::verify($srv)->watch(false); } /** @dataProvider provideFeedUpdates */ public function testRefreshAFeed(string $cmd, int $exitStatus, string $output): void { - $this->dbMock->feedUpdate->with(1, true)->returns(true); - $this->dbMock->feedUpdate->with(2, true)->throws(new \JKingWeb\Arsse\Feed\Exception("", ['url' => "http://example.com/"], $this->mockGuzzleException(ClientException::class, "", 404))); + \Phake::when(Arsse::$db)->feedUpdate(1, true)->thenReturn(true); + \Phake::when(Arsse::$db)->feedUpdate(2, true)->thenThrow(new \JKingWeb\Arsse\Feed\Exception("", ['url' => "http://example.com/"], $this->mockGuzzleException(ClientException::class, "", 404))); $this->assertConsole($cmd, $exitStatus, $output); - $this->cli->loadConf->called(); - $this->dbMock->feedUpdate->called(); + \Phake::verify($this->cli)->loadConf(); + \Phake::verify($this->dbMock)->feedUpdate(); } public function provideFeedUpdates(): iterable { @@ -137,14 +139,14 @@ class TestCLI extends \JKingWeb\Arsse\Test\AbstractTest { /** @dataProvider provideDefaultConfigurationSaves */ public function testSaveTheDefaultConfiguration(string $cmd, int $exitStatus, string $file): void { - $conf = $this->mock(Conf::class); - $conf->exportFile->with("php://output", true)->returns(true); - $conf->exportFile->with("good.conf", true)->returns(true); - $conf->exportFile->with("bad.conf", true)->throws(new \JKingWeb\Arsse\Conf\Exception("fileUnwritable")); - $this->objMock->get->with(Conf::class)->returns($conf->get()); + $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(Arsse::$obj)->get(Conf::class)->thenReturn($conf); $this->assertConsole($cmd, $exitStatus); - $this->cli->loadConf->never()->called(); - $conf->exportFile->calledWith($file, true); + \Phake::verify($this->cli)->loadConf(); + \Phake::verify($conf)->exportFile($file, true); } public function provideDefaultConfigurationSaves(): iterable { @@ -209,10 +211,10 @@ class TestCLI extends \JKingWeb\Arsse\Test\AbstractTest { \Phake::when(Arsse::$user)->auth("john.doe@example.com", "secret")->thenReturn(true); \Phake::when(Arsse::$user)->auth("jane.doe@example.com", "superman")->thenReturn(true); $fever = $this->mock(FeverUser::class); - $fever->authenticate->returns(false); - $fever->authenticate->with("john.doe@example.com", "ashalla")->returns(true); - $fever->authenticate->with("jane.doe@example.com", "thx1138")->returns(true); - $this->objMock->get->with(FeverUser::class)->returns($fever->get()); + \Phake::when($fever)->authenticate->thenReturn(false); + \Phake::when($fever)->authenticate("john.doe@example.com", "ashalla")->thenReturn(true); + \Phake::when($fever)->authenticate("jane.doe@example.com", "thx1138")->thenReturn(true); + \Phake::when(Arsse::$obj)->get(FeverUser::class)->thenReturn($fever); $this->assertConsole($cmd, $exitStatus, $output); } @@ -259,9 +261,9 @@ class TestCLI extends \JKingWeb\Arsse\Test\AbstractTest { }; Arsse::$user = \Phake::mock(User::class); \Phake::when(Arsse::$user)->passwordSet->thenReturnCallback($passwordChange); - $fever = $this->mock(FeverUser::class); - $fever->register->does($passwordChange); - $this->objMock->get->with(FeverUser::class)->returns($fever->get()); + $fever = \Phake::mock(FeverUser::class); + \Phake::when($fever)->register->thenReturnCallback($passwordChange); + \Phake::when(Arsse::$obj)->get(FeverUser::class)->returns($fever); $this->assertConsole($cmd, $exitStatus, $output); } @@ -288,9 +290,9 @@ class TestCLI extends \JKingWeb\Arsse\Test\AbstractTest { }; Arsse::$user = \Phake::mock(User::class); \Phake::when(Arsse::$user)->passwordUnset->thenReturnCallback($passwordClear); - $fever = $this->mock(FeverUser::class); - $fever->unregister->does($passwordClear); - $this->objMock->get->with(FeverUser::class)->returns($fever->get()); + $fever = \Phake::mock(FeverUser::class); + \Phake::when($fever)->unregister->thenReturnCallback($passwordClear); + \Phake::when(Arsse::$obj)->get(FeverUser::class)->thenReturn($fever); $this->assertConsole($cmd, $exitStatus, $output); } @@ -305,14 +307,14 @@ class TestCLI extends \JKingWeb\Arsse\Test\AbstractTest { /** @dataProvider provideOpmlExports */ public function testExportToOpml(string $cmd, int $exitStatus, string $file, string $user, bool $flat): void { - $opml = $this->mock(OPML::class); - $opml->exportFile->with("php://output", $user, $flat)->returns(true); - $opml->exportFile->with("good.opml", $user, $flat)->returns(true); - $opml->exportFile->with("bad.opml", $user, $flat)->throws(new \JKingWeb\Arsse\ImportExport\Exception("fileUnwritable")); - $this->objMock->get->with(OPML::class)->returns($opml->get()); + $opml = \Phake::mock(OPML::class); + \Phake::when($opml)->exportFile("php://output", $user, $flat)->thenReturn(true); + \Phake::when($opml)->exportFile("good.opml", $user, $flat)->thenRreturn(true); + \Phake::when($opml)->exportFile("bad.opml", $user, $flat)->thenThrow(new \JKingWeb\Arsse\ImportExport\Exception("fileUnwritable")); + \Phake::when(Arsse::$obj)->get(OPML::class)->returns($opml); $this->assertConsole($cmd, $exitStatus); - $this->cli->loadConf->called(); - $opml->exportFile->calledWith($file, $user, $flat); + \Phake::verify($this->cli)->loadConf(); + \Phake::verify($opml)->exportFile($file, $user, $flat); } public function provideOpmlExports(): iterable { @@ -346,14 +348,14 @@ class TestCLI extends \JKingWeb\Arsse\Test\AbstractTest { /** @dataProvider provideOpmlImports */ public function testImportFromOpml(string $cmd, int $exitStatus, string $file, string $user, bool $flat, bool $replace): void { - $opml = $this->mock(OPML::class); - $opml->importFile->with("php://input", $user, $flat, $replace)->returns(true); - $opml->importFile->with("good.opml", $user, $flat, $replace)->returns(true); - $opml->importFile->with("bad.opml", $user, $flat, $replace)->throws(new \JKingWeb\Arsse\ImportExport\Exception("fileUnreadable")); - $this->objMock->get->with(OPML::class)->returns($opml->get()); + $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(Arsse::$obj)->get(OPML::class)->thenReturn($opml); $this->assertConsole($cmd, $exitStatus); - $this->cli->loadConf->called(); - $opml->importFile->calledWith($file, $user, $flat, $replace); + \Phake::verify($this->cli)->loadConf(); + \Phake::verify($opml)->importFile($file, $user, $flat, $replace); } public function provideOpmlImports(): iterable { @@ -458,38 +460,38 @@ class TestCLI extends \JKingWeb\Arsse\Test\AbstractTest { "TOKEN 2 Eek", "TOKEN 1 Ook", ]); - $t = $this->mock(MinifluxToken::class); - $t->tokenList->returns($data); - $this->objMock->get->with(MinifluxToken::class)->returns($t->get()); + $t = \Phake::mock(MinifluxToken::class); + \Phake::when($t)->tokenList->thenReturn($data); + \Phake::when(Arsse::$obj)->get(MinifluxToken::class)->thenReturn($t); $this->assertConsole("arsse.php token list john", 0, $exp); - $t->tokenList->calledWith("john"); + \Phake::verify($t)->tokenList("john"); } public function testCreateToken(): void { - $t = $this->mock(MinifluxToken::class); - $t->tokenGenerate->returns("RANDOM TOKEN"); - $this->objMock->get->with(MinifluxToken::class)->returns($t->get()); + $t = \Phake::mock(MinifluxToken::class); + \Phake::when($t)->tokenGenerate->thenReturn("RANDOM TOKEN"); + \Phake::when(Arsse::$obj)->get(MinifluxToken::class)->thenReturn($t); $this->assertConsole("arse.php token create jane", 0, "RANDOM TOKEN"); - $t->tokenGenerate->calledWith("jane", null); + \Phake::verify($t)->tokenGenerate("jane", null); } public function testCreateTokenWithLabel(): void { - $t = $this->mock(MinifluxToken::class); - $t->tokenGenerate->returns("RANDOM TOKEN"); - $this->objMock->get->with(MinifluxToken::class)->returns($t->get()); + $t = \Phake::mock(MinifluxToken::class); + \Phake::when($t)->tokenGenerate->thenReturn("RANDOM TOKEN"); + \Phake::when(Arsse::$obj)->get(MinifluxToken::class)->thenReturn($t); $this->assertConsole("arse.php token create jane Ook", 0, "RANDOM TOKEN"); - $t->tokenGenerate->calledWith("jane", "Ook"); + \Phake::verify($t)->tokenGenerate("jane", "Ook"); } public function testRevokeAToken(): void { - $this->dbMock->tokenRevoke->returns(true); + \Phake::when(Arsse::$db)->tokenRevoke->thenReturn(true); $this->assertConsole("arse.php token revoke jane TOKEN_ID", 0); - $this->dbMock->tokenRevoke->calledWith("jane", "miniflux.login", "TOKEN_ID"); + \Phake::verify(Arsse::$db)->tokenRevoke("jane", "miniflux.login", "TOKEN_ID"); } public function testRevokeAllTokens(): void { - $this->dbMock->tokenRevoke->returns(true); + \Phake::when(Arsse::$db)->tokenRevoke->thenReturn(true); $this->assertConsole("arse.php token revoke jane", 0); - $this->dbMock->tokenRevoke->calledWith("jane", "miniflux.login", null); + \Phake::verify(Arsse::$db)->tokenRevoke("jane", "miniflux.login", null); } } diff --git a/tests/cases/Database/AbstractTest.php b/tests/cases/Database/AbstractTest.php index 53303f4f..dc133740 100644 --- a/tests/cases/Database/AbstractTest.php +++ b/tests/cases/Database/AbstractTest.php @@ -78,8 +78,7 @@ abstract class AbstractTest extends \JKingWeb\Arsse\Test\AbstractTest { Arsse::$db = new Database(static::$drv); Arsse::$db->driverSchemaUpdate(); // create a mock user manager - $this->userMock = $this->mock(User::class); - Arsse::$user = $this->userMock->get(); + Arsse::$user = \Phake::mock(User::class); // call the series-specific setup method $setUp = "setUp".$this->series; $this->$setUp(); diff --git a/tests/cases/Exception/TestException.php b/tests/cases/Exception/TestException.php index 850be7ae..d8e9a08d 100644 --- a/tests/cases/Exception/TestException.php +++ b/tests/cases/Exception/TestException.php @@ -17,9 +17,8 @@ class TestException extends \JKingWeb\Arsse\Test\AbstractTest { public function setUp(): void { self::clearData(false); // create a mock Lang object so as not to create a dependency loop - $this->langMock = $this->mock(Lang::class); - $this->langMock->msg->returns(""); - Arsse::$lang = $this->langMock->get(); + Arsse::$lang = \Phake::mock(Lang::class); + \Phake::when(Arsse::$lang)->msg->thenReturn(""); } public function testBaseClass(): void { diff --git a/tests/cases/TestArsse.php b/tests/cases/TestArsse.php index 141d51b6..151b26ff 100644 --- a/tests/cases/TestArsse.php +++ b/tests/cases/TestArsse.php @@ -21,21 +21,17 @@ class TestArsse extends \JKingWeb\Arsse\Test\AbstractTest { } public function testLoadExistingData(): void { - $lang = $this->mock(Lang::class); - $db = $this->mock(Database::class); - $user = $this->mock(User::class); - $conf1 = $this->mock(Conf::class); - Arsse::$lang = $lang->get(); - Arsse::$db = $db->get(); - Arsse::$user = $user->get(); - Arsse::$conf = $conf1->get(); + Arsse::$conf = \Phake::mock(Conf::class); + $lang = Arsse::$lang = \Phake::mock(Lang::class); + $db = Arsse::$db = \Phake::mock(Database::class); + $user = Arsse::$user = \Phake::mock(User::class); $conf2 = (new Conf)->import(['lang' => "test"]); Arsse::load($conf2); $this->assertSame($conf2, Arsse::$conf); - $this->assertSame($lang->get(), Arsse::$lang); - $this->assertSame($db->get(), Arsse::$db); - $this->assertSame($user->get(), Arsse::$user); - $lang->set->calledWith("test"); + $this->assertSame($lang, Arsse::$lang); + $this->assertSame($db, Arsse::$db); + $this->assertSame($user, Arsse::$user); + \Phake::verify($lang)->set("test"); } public function testLoadNewData(): void { diff --git a/tests/cases/User/TestUser.php b/tests/cases/User/TestUser.php index 888699e0..bb0b5f76 100644 --- a/tests/cases/User/TestUser.php +++ b/tests/cases/User/TestUser.php @@ -21,24 +21,12 @@ class TestUser extends \JKingWeb\Arsse\Test\AbstractTest { protected $drv; public function setUp(): void { - parent::setUp(); self::setConf(); // create a mock database interface - $this->dbMock = $this->mock(Database::class); - $this->dbMock->begin->returns($this->mock(\JKingWeb\Arsse\Db\Transaction::class)); + Arsse::$db = \Phake::mock(Database::class); + \Phake::when(Arsse::$db)->begin->thenReturn(\Phake::mock(\JKingWeb\Arsse\Db\Transaction::class)); // create a mock user driver - $this->drv = $this->mock(Driver::class); - } - - protected function prepTest(?\Closure $partialMockDef = null): User { - Arsse::$db = $this->dbMock->get(); - if ($partialMockDef) { - $this->userMock = $this->partialMock(User::class, $this->drv->get()); - $partialMockDef($this->userMock); - return $this->userMock->get(); - } else { - return new User($this->drv->get()); - } + $this->drv = \Phake::mock(Driver::class); } public function testConstruct(): void { @@ -55,13 +43,13 @@ class TestUser extends \JKingWeb\Arsse\Test\AbstractTest { } public function testStartATransaction(): void { - $u = $this->prepTest(); + $u = new User($this->drv); $this->assertInstanceOf(Transaction::class, $u->begin()); - $this->dbMock->begin->calledWith(); + \Phake::verify(Arsse::$db)->begin(); } public function testGeneratePasswords(): void { - $u = $this->prepTest(); + $u = new User($this->drv); $pass1 = $u->generatePassword(); $pass2 = $u->generatePassword(); $this->assertNotEquals($pass1, $pass2); @@ -70,18 +58,18 @@ class TestUser extends \JKingWeb\Arsse\Test\AbstractTest { /** @dataProvider provideAuthentication */ public function testAuthenticateAUser(bool $preAuth, string $user, string $password, bool $exp): void { Arsse::$conf->userPreAuth = $preAuth; - $this->drv->auth->returns(false); - $this->drv->auth->with("john.doe@example.com", "secret")->returns(true); - $this->drv->auth->with("jane.doe@example.com", "superman")->returns(true); - $this->dbMock->userExists->with("john.doe@example.com")->returns(true); - $this->dbMock->userExists->with("jane.doe@example.com")->returns(false); - $this->dbMock->userAdd->returns(""); - $u = $this->prepTest(); + \Phake::when($this->drv)->auth->thenReturn(false); + \Phake::when($this->drv)->auth("john.doe@example.com", "secret")->thenReturn(true); + \Phake::when($this->drv)->auth("jane.doe@example.com", "superman")->thenReturn(true); + \Phake::when(Arsse::$db)->userExists("john.doe@example.com")->thenReturn(true); + \Phake::when(Arsse::$db)->userExists("jane.doe@example.com")->thenReturn(false); + \Phake::when(Arsse::$db)->userAdd->thenReturn(""); + $u = new User($this->drv); $this->assertSame($exp, $u->auth($user, $password)); $this->assertNull($u->id); - $this->drv->auth->times((int) !$preAuth)->called(); - $this->dbMock->userExists->times($exp ? 1 : 0)->calledWith($user); - $this->dbMock->userAdd->times($exp && $user === "jane.doe@example.com" ? 1 : 0)->calledWith($user, $password); + \Phake::verify($this->drv, \Phake::times((int) !$preAuth))->auth($this->anything(), $this->anything()); + \Phake::verify(Arsse::$db, \Phake::times($exp ? 1 : 0))->userExists($user); + \Phake::verify(Arsse::$db, \Phake::times($exp && $user === "jane.doe@example.com" ? 1 : 0))->userAdd($user, $password); } public function provideAuthentication(): iterable { @@ -102,7 +90,7 @@ class TestUser extends \JKingWeb\Arsse\Test\AbstractTest { public function testListUsers(): void { $exp = ["john.doe@example.com", "jane.doe@example.com"]; $this->drv->userList->returns(["john.doe@example.com", "jane.doe@example.com"]); - $u = $this->prepTest(); + $u = new User($this->drv); $this->assertSame($exp, $u->list()); $this->drv->userList->calledWith(); } @@ -110,7 +98,7 @@ class TestUser extends \JKingWeb\Arsse\Test\AbstractTest { public function testLookUpAUserByNumber(): void { $exp = "john.doe@example.com"; $this->dbMock->userLookup->returns($exp); - $u = $this->prepTest(); + $u = new User($this->drv); $this->assertSame($exp, $u->lookup(2112)); $this->dbMock->userLookup->calledWith(2112); } @@ -120,7 +108,7 @@ class TestUser extends \JKingWeb\Arsse\Test\AbstractTest { $pass = "secret"; $this->drv->userAdd->returns($pass); $this->dbMock->userExists->returns(true); - $u = $this->prepTest(); + $u = new User($this->drv); $this->assertSame($pass, $u->add($user, $pass)); $this->drv->userAdd->calledWith($user, $pass); $this->dbMock->userExists->calledWith($user); @@ -131,7 +119,7 @@ class TestUser extends \JKingWeb\Arsse\Test\AbstractTest { $pass = "secret"; $this->drv->userAdd->returns($pass); $this->dbMock->userExists->returns(false); - $u = $this->prepTest(); + $u = new User($this->drv); $this->assertSame($pass, $u->add($user, $pass)); $this->drv->userAdd->calledWith($user, $pass); $this->dbMock->userExists->calledWith($user); @@ -143,7 +131,7 @@ class TestUser extends \JKingWeb\Arsse\Test\AbstractTest { $pass = "secret"; $this->drv->userAdd->throws(new ExceptionConflict("alreadyExists")); $this->dbMock->userExists->returns(true); - $u = $this->prepTest(); + $u = new User($this->drv); $this->assertException("alreadyExists", "User", "ExceptionConflict"); try { $u->add($user, $pass); @@ -158,7 +146,7 @@ class TestUser extends \JKingWeb\Arsse\Test\AbstractTest { $pass = "secret"; $this->drv->userAdd->throws(new ExceptionConflict("alreadyExists")); $this->dbMock->userExists->returns(false); - $u = $this->prepTest(); + $u = new User($this->drv); $this->assertException("alreadyExists", "User", "ExceptionConflict"); try { $u->add($user, $pass); @@ -171,7 +159,7 @@ class TestUser extends \JKingWeb\Arsse\Test\AbstractTest { /** @dataProvider provideInvalidUserNames */ public function testAddAnInvalidUser(string $user): void { - $u = $this->prepTest(); + $u = new User($this->drv); $this->assertException("invalidUsername", "User", "ExceptionInput"); $u->add($user, "secret"); } @@ -208,7 +196,7 @@ class TestUser extends \JKingWeb\Arsse\Test\AbstractTest { $this->dbMock->userAdd->returns(true); $this->dbMock->userRename->returns(true); $this->drv->userRename->returns(true); - $u = $this->prepTest(); + $u = new User($this->drv); $old = "john.doe@example.com"; $new = "jane.doe@example.com"; $this->assertTrue($u->rename($old, $new)); @@ -230,7 +218,7 @@ class TestUser extends \JKingWeb\Arsse\Test\AbstractTest { $this->dbMock->userAdd->returns(true); $this->dbMock->userRename->returns(true); $this->drv->userRename->returns(true); - $u = $this->prepTest(); + $u = new User($this->drv); $old = "john.doe@example.com"; $new = "jane.doe@example.com"; $this->assertTrue($u->rename($old, $new)); @@ -248,7 +236,7 @@ class TestUser extends \JKingWeb\Arsse\Test\AbstractTest { $this->dbMock->userAdd->returns(true); $this->dbMock->userRename->returns(true); $this->drv->userRename->returns(false); - $u = $this->prepTest(); + $u = new User($this->drv); $old = "john.doe@example.com"; $this->assertFalse($u->rename($old, $old)); $this->drv->userRename->calledWith($old, $old); @@ -256,7 +244,7 @@ class TestUser extends \JKingWeb\Arsse\Test\AbstractTest { /** @dataProvider provideInvalidUserNames */ public function testRenameAUserToAnInvalidName(string $new): void { - $u = $this->prepTest(); + $u = new User($this->drv); $this->assertException("invalidUsername", "User", "ExceptionInput"); $u->rename("john.doe@example.com", $new); } @@ -265,7 +253,7 @@ class TestUser extends \JKingWeb\Arsse\Test\AbstractTest { $user = "john.doe@example.com"; $this->drv->userRemove->returns(true); $this->dbMock->userExists->returns(true); - $u = $this->prepTest(); + $u = new User($this->drv); $this->assertTrue($u->remove($user)); $this->dbMock->userExists->calledWith($user); $this->dbMock->userRemove->calledWith($user); @@ -276,7 +264,7 @@ class TestUser extends \JKingWeb\Arsse\Test\AbstractTest { $user = "john.doe@example.com"; $this->drv->userRemove->returns(true); $this->dbMock->userExists->returns(false); - $u = $this->prepTest(); + $u = new User($this->drv); $this->assertTrue($u->remove($user)); $this->dbMock->userExists->calledWith($user); $this->drv->userRemove->calledWith($user); @@ -287,7 +275,7 @@ class TestUser extends \JKingWeb\Arsse\Test\AbstractTest { $pass = "secret"; $this->drv->userRemove->throws(new ExceptionConflict("doesNotExist")); $this->dbMock->userExists->returns(true); - $u = $this->prepTest(); + $u = new User($this->drv); $this->assertException("doesNotExist", "User", "ExceptionConflict"); try { $u->remove($user); @@ -303,7 +291,7 @@ class TestUser extends \JKingWeb\Arsse\Test\AbstractTest { $pass = "secret"; $this->drv->userRemove->throws(new ExceptionConflict("doesNotExist")); $this->dbMock->userExists->returns(false); - $u = $this->prepTest(); + $u = new User($this->drv); $this->assertException("doesNotExist", "User", "ExceptionConflict"); try { $u->remove($user); @@ -319,7 +307,7 @@ class TestUser extends \JKingWeb\Arsse\Test\AbstractTest { $this->drv->userPasswordSet->returns($pass); $this->dbMock->userPasswordSet->returns($pass); $this->dbMock->userExists->returns(true); - $u = $this->prepTest(); + $u = new User($this->drv); $this->assertSame($pass, $u->passwordSet($user, $pass)); $this->drv->userPasswordSet->calledWith($user, $pass, null); $this->dbMock->userPasswordSet->calledWith($user, $pass); @@ -350,7 +338,7 @@ class TestUser extends \JKingWeb\Arsse\Test\AbstractTest { $this->drv->userPasswordSet->returns($pass); $this->dbMock->userPasswordSet->returns($pass); $this->dbMock->userExists->returns(false); - $u = $this->prepTest(); + $u = new User($this->drv); $this->assertSame($pass, $u->passwordSet($user, $pass)); $this->drv->userPasswordSet->calledWith($user, $pass, null); $this->dbMock->userAdd->calledWith($user, $pass); @@ -393,7 +381,7 @@ class TestUser extends \JKingWeb\Arsse\Test\AbstractTest { $this->drv->userPasswordUnset->returns(true); $this->dbMock->userPasswordSet->returns(true); $this->dbMock->userExists->returns(true); - $u = $this->prepTest(); + $u = new User($this->drv); $this->assertTrue($u->passwordUnset($user)); $this->drv->userPasswordUnset->calledWith($user, null); $this->dbMock->userPasswordSet->calledWith($user, null); @@ -406,7 +394,7 @@ class TestUser extends \JKingWeb\Arsse\Test\AbstractTest { $this->drv->userPasswordUnset->returns(true); $this->dbMock->userPasswordSet->returns(true); $this->dbMock->userExists->returns(false); - $u = $this->prepTest(); + $u = new User($this->drv); $this->assertTrue($u->passwordUnset($user)); $this->drv->userPasswordUnset->calledWith($user, null); $this->dbMock->userExists->calledWith($user); @@ -415,7 +403,7 @@ class TestUser extends \JKingWeb\Arsse\Test\AbstractTest { public function testUnsetAPasswordForAMissingUser(): void { $user = "john.doe@example.com"; $this->drv->userPasswordUnset->throws(new ExceptionConflict("doesNotExist")); - $u = $this->prepTest(); + $u = new User($this->drv); $this->assertException("doesNotExist", "User", "ExceptionConflict"); try { $u->passwordUnset($user); @@ -431,7 +419,7 @@ class TestUser extends \JKingWeb\Arsse\Test\AbstractTest { $this->drv->userPropertiesGet->returns($extra); $this->dbMock->userPropertiesGet->returns($base); $this->dbMock->userExists->returns(true); - $u = $this->prepTest(); + $u = new User($this->drv); $this->assertSame($exp, $u->propertiesGet($user)); $this->drv->userPropertiesGet->calledWith($user, true); $this->dbMock->userPropertiesGet->calledWith($user, true); @@ -458,7 +446,7 @@ class TestUser extends \JKingWeb\Arsse\Test\AbstractTest { $this->dbMock->userPropertiesGet->returns($base); $this->dbMock->userAdd->returns(true); $this->dbMock->userExists->returns(false); - $u = $this->prepTest(); + $u = new User($this->drv); $this->assertSame($exp, $u->propertiesGet($user)); $this->drv->userPropertiesGet->calledWith($user, true); $this->dbMock->userPropertiesGet->calledWith($user, true); @@ -470,7 +458,7 @@ class TestUser extends \JKingWeb\Arsse\Test\AbstractTest { public function testGetThePropertiesOfAMissingUser(): void { $user = "john.doe@example.com"; $this->drv->userPropertiesGet->throws(new ExceptionConflict("doesNotExist")); - $u = $this->prepTest(); + $u = new User($this->drv); $this->assertException("doesNotExist", "User", "ExceptionConflict"); try { $u->propertiesGet($user); @@ -483,14 +471,14 @@ class TestUser extends \JKingWeb\Arsse\Test\AbstractTest { public function testSetThePropertiesOfAUser(array $in, $out): void { $user = "john.doe@example.com"; if ($out instanceof \Exception) { - $u = $this->prepTest(); + $u = new User($this->drv); $this->assertException($out); $u->propertiesSet($user, $in); } else { $this->dbMock->userExists->returns(true); $this->drv->userPropertiesSet->returns($out); $this->dbMock->userPropertiesSet->returns(true); - $u = $this->prepTest(); + $u = new User($this->drv); $this->assertSame($out, $u->propertiesSet($user, $in)); $this->drv->userPropertiesSet->calledWith($user, $in); $this->dbMock->userPropertiesSet->calledWith($user, $out); @@ -502,14 +490,14 @@ class TestUser extends \JKingWeb\Arsse\Test\AbstractTest { public function testSetThePropertiesOfAUserWeDoNotKnow(array $in, $out): void { $user = "john.doe@example.com"; if ($out instanceof \Exception) { - $u = $this->prepTest(); + $u = new User($this->drv); $this->assertException($out); $u->propertiesSet($user, $in); } else { $this->dbMock->userExists->returns(false); $this->drv->userPropertiesSet->returns($out); $this->dbMock->userPropertiesSet->returns(true); - $u = $this->prepTest(); + $u = new User($this->drv); $this->assertSame($out, $u->propertiesSet($user, $in)); $this->drv->userPropertiesSet->calledWith($user, $in); $this->dbMock->userPropertiesSet->calledWith($user, $out); @@ -536,7 +524,7 @@ class TestUser extends \JKingWeb\Arsse\Test\AbstractTest { $user = "john.doe@example.com"; $in = ['admin' => true]; $this->drv->userPropertiesSet->throws(new ExceptionConflict("doesNotExist")); - $u = $this->prepTest(); + $u = new User($this->drv); $this->assertException("doesNotExist", "User", "ExceptionConflict"); try { $u->propertiesSet($user, $in); diff --git a/tests/lib/AbstractTest.php b/tests/lib/AbstractTest.php index 67d4da0c..9fad7637 100644 --- a/tests/lib/AbstractTest.php +++ b/tests/lib/AbstractTest.php @@ -31,21 +31,6 @@ use GuzzleHttp\Psr7\ServerRequest; abstract class AbstractTest extends \PHPUnit\Framework\TestCase { use \DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts; - protected $objMock; - protected $confMock; - protected $langMock; - protected $dbMock; - protected $userMock; - - public function setUp(): void { - self::clearData(); - // create the object factory as a mock - $this->objMock = Arsse::$obj = $this->mock(Factory::class); - $this->objMock->get->does(function(string $class) { - return new $class; - }); - } - public static function clearData(bool $loadLang = true): void { date_default_timezone_set("America/Toronto"); $r = new \ReflectionClass(\JKingWeb\Arsse\Arsse::class);