diff --git a/tests/AbstractDatabaseTest.php b/tests/AbstractDatabaseTestCase.php similarity index 88% rename from tests/AbstractDatabaseTest.php rename to tests/AbstractDatabaseTestCase.php index 12dfcb2d..2a98958f 100644 --- a/tests/AbstractDatabaseTest.php +++ b/tests/AbstractDatabaseTestCase.php @@ -1,7 +1,7 @@ getMockBuilder($className)->disableOriginalConstructor()->getMock(); + } +} diff --git a/tests/ConfigTest.php b/tests/ConfigTest.php index 1806bbd0..c29759d8 100644 --- a/tests/ConfigTest.php +++ b/tests/ConfigTest.php @@ -1,7 +1,7 @@ databaseConnection); + + $user = new \Szurubooru\Entities\User(); + $user->name = 'test'; + + $userDao->save($user); + $expected = $user; + $actual = $userDao->getByName($user->name); + + $this->assertEquals($actual, $expected); + } + + public function testRetrievingByInvalidName() + { + $userDao = new \Szurubooru\Dao\UserDao($this->databaseConnection); + + $actual = $userDao->getByName('rubbish'); + + $this->assertNull($actual); + } + + public function testCheckingUserPresence() + { + $userDao = new \Szurubooru\Dao\UserDao($this->databaseConnection); + + $this->assertFalse($userDao->hasAnyUsers()); + + $user = new \Szurubooru\Entities\User(); + $user->name = 'test'; + $userDao->save($user); + + $this->assertTrue($userDao->hasAnyUsers()); + } +} diff --git a/tests/DispatcherTest.php b/tests/DispatcherTest.php index 1f0015d8..26405c6b 100644 --- a/tests/DispatcherTest.php +++ b/tests/DispatcherTest.php @@ -1,7 +1,7 @@ passwordServiceMock = $this->mock(\Szurubooru\Services\PasswordService::class); + $this->timeServiceMock = $this->mock(\Szurubooru\Services\TimeService::class); + $this->tokenDaoMock = $this->mock(\Szurubooru\Dao\TokenDao::class); + $this->userDaoMock = $this->mock(\Szurubooru\Dao\UserDao::class); + } + public function testInvalidUser() { - $passwordServiceMock = $this->getPasswordServiceMock(); - $timeServiceMock = $this->getTimeServiceMock(); - $tokenDaoMock = $this->getTokenDaoMock(); - $userDaoMock = $this->getUserDaoMock(); - $this->setExpectedException(\InvalidArgumentException::class, 'User not found'); - $authService = new \Szurubooru\Services\AuthService($passwordServiceMock, $timeServiceMock, $tokenDaoMock, $userDaoMock); + $authService = $this->getAuthService(); $authService->loginFromCredentials('dummy', 'godzilla'); } public function testInvalidPassword() { - $passwordServiceMock = $this->getPasswordServiceMock(); - $timeServiceMock = $this->getTimeServiceMock(); - $tokenDaoMock = $this->getTokenDaoMock(); - $userDaoMock = $this->getUserDaoMock(); - - $passwordServiceMock->method('getHash')->willReturn('unmatchingHash'); + $this->passwordServiceMock->method('getHash')->willReturn('unmatchingHash'); $testUser = new \Szurubooru\Entities\User(); $testUser->name = 'dummy'; $testUser->passwordHash = 'hash'; - $userDaoMock->expects($this->once())->method('getByName')->willReturn($testUser); + $this->userDaoMock->expects($this->once())->method('getByName')->willReturn($testUser); - $authService = new \Szurubooru\Services\AuthService($passwordServiceMock, $timeServiceMock, $tokenDaoMock, $userDaoMock); + $authService = $this->getAuthService(); $this->setExpectedException(\InvalidArgumentException::class, 'Specified password is invalid'); $authService->loginFromCredentials('dummy', 'godzilla'); } public function testValidCredentials() { - $passwordServiceMock = $this->getPasswordServiceMock(); - $timeServiceMock = $this->getTimeServiceMock(); - $tokenDaoMock = $this->getTokenDaoMock(); - $userDaoMock = $this->getUserDaoMock(); - - $tokenDaoMock->expects($this->once())->method('save'); - $passwordServiceMock->method('getHash')->willReturn('hash'); + $this->tokenDaoMock->expects($this->once())->method('save'); + $this->passwordServiceMock->method('getHash')->willReturn('hash'); $testUser = new \Szurubooru\Entities\User(); $testUser->name = 'dummy'; $testUser->passwordHash = 'hash'; - $userDaoMock->expects($this->once())->method('getByName')->willReturn($testUser); + $this->userDaoMock->expects($this->once())->method('getByName')->willReturn($testUser); - $authService = new \Szurubooru\Services\AuthService($passwordServiceMock, $timeServiceMock, $tokenDaoMock, $userDaoMock); + $authService = $this->getAuthService(); $authService->loginFromCredentials('dummy', 'godzilla'); $this->assertTrue($authService->isLoggedIn()); @@ -61,37 +59,27 @@ class AuthServiceTest extends \PHPUnit_Framework_TestCase public function testInvalidToken() { - $passwordServiceMock = $this->getPasswordServiceMock(); - $timeServiceMock = $this->getTimeServiceMock(); - $tokenDaoMock = $this->getTokenDaoMock(); - $userDaoMock = $this->getUserDaoMock(); - - $tokenDaoMock->expects($this->once())->method('getByName')->willReturn(null); + $this->tokenDaoMock->expects($this->once())->method('getByName')->willReturn(null); $this->setExpectedException(\Exception::class); - $authService = new \Szurubooru\Services\AuthService($passwordServiceMock, $timeServiceMock, $tokenDaoMock, $userDaoMock); + $authService = $this->getAuthService(); $authService->loginFromToken(''); } public function testValidToken() { - $passwordServiceMock = $this->getPasswordServiceMock(); - $timeServiceMock = $this->getTimeServiceMock(); - $tokenDaoMock = $this->getTokenDaoMock(); - $userDaoMock = $this->getUserDaoMock(); - $testUser = new \Szurubooru\Entities\User(); $testUser->id = 5; $testUser->name = 'dummy'; $testUser->passwordHash = 'hash'; - $userDaoMock->expects($this->once())->method('getById')->willReturn($testUser); + $this->userDaoMock->expects($this->once())->method('getById')->willReturn($testUser); $testToken = new \Szurubooru\Entities\Token(); $testToken->name = 'dummy_token'; $testToken->additionalData = $testUser->id; - $tokenDaoMock->expects($this->once())->method('getByName')->willReturn($testToken); + $this->tokenDaoMock->expects($this->once())->method('getByName')->willReturn($testToken); - $authService = new \Szurubooru\Services\AuthService($passwordServiceMock, $timeServiceMock, $tokenDaoMock, $userDaoMock); + $authService = $this->getAuthService(); $authService->loginFromToken($testToken->name); $this->assertTrue($authService->isLoggedIn()); @@ -100,23 +88,12 @@ class AuthServiceTest extends \PHPUnit_Framework_TestCase $this->assertNotNull($authService->getLoginToken()->name); } - private function getTokenDaoMock() + private function getAuthService() { - return $this->getMockBuilder(\Szurubooru\Dao\TokenDao::class)->disableOriginalConstructor()->getMock(); - } - - private function getUserDaoMock() - { - return $this->getMockBuilder(\Szurubooru\Dao\UserDao::class)->disableOriginalConstructor()->getMock(); - } - - private function getPasswordServiceMock() - { - return $this->getMockBuilder(\Szurubooru\Services\PasswordService::class)->disableOriginalConstructor()->getMock(); - } - - private function getTimeServiceMock() - { - return $this->getMockBuilder(\Szurubooru\Services\TimeService::class)->disableOriginalConstructor()->getMock(); + return new \Szurubooru\Services\AuthService( + $this->passwordServiceMock, + $this->timeServiceMock, + $this->tokenDaoMock, + $this->userDaoMock); } } diff --git a/tests/Services/EmailServiceTest.php b/tests/Services/EmailServiceTest.php index 18865f4a..2ba5c067 100644 --- a/tests/Services/EmailServiceTest.php +++ b/tests/Services/EmailServiceTest.php @@ -1,7 +1,7 @@