Changed tests structure

This commit is contained in:
Marcin Kurczewski 2014-09-01 20:51:59 +02:00
parent f59968a062
commit 57fd080e52
10 changed files with 91 additions and 63 deletions

View file

@ -1,7 +1,7 @@
<?php <?php
namespace Szurubooru\Tests; namespace Szurubooru\Tests;
abstract class AbstractDatabaseTest extends \PHPUnit_Framework_TestCase abstract class AbstractDatabaseTestCase extends \Szurubooru\Tests\AbstractTestCase
{ {
protected $databaseConnection; protected $databaseConnection;
protected $upgradeService; protected $upgradeService;

View file

@ -0,0 +1,10 @@
<?php
namespace Szurubooru\Tests;
abstract class AbstractTestCase extends \PHPUnit_Framework_TestCase
{
public function mock($className)
{
return $this->getMockBuilder($className)->disableOriginalConstructor()->getMock();
}
}

View file

@ -1,7 +1,7 @@
<?php <?php
namespace Szurubooru\Tests; namespace Szurubooru\Tests;
final class ConfigTest extends \PHPUnit_Framework_TestCase final class ConfigTest extends \Szurubooru\Tests\AbstractTestCase
{ {
private static $testFileName1; private static $testFileName1;
private static $testFileName2; private static $testFileName2;

View file

@ -1,7 +1,7 @@
<?php <?php
namespace Szurubooru\Tests\Dao; namespace Szurubooru\Tests\Dao;
final class PostDaoTest extends \Szurubooru\Tests\AbstractDatabaseTest final class PostDaoTest extends \Szurubooru\Tests\AbstractDatabaseTestCase
{ {
public function testSaving() public function testSaving()
{ {

View file

@ -1,7 +1,7 @@
<?php <?php
namespace Szurubooru\Tests\Dao; namespace Szurubooru\Tests\Dao;
final class TokenDaoTest extends \Szurubooru\Tests\AbstractDatabaseTest final class TokenDaoTest extends \Szurubooru\Tests\AbstractDatabaseTestCase
{ {
public function testRetrievingByValidName() public function testRetrievingByValidName()
{ {

41
tests/Dao/UserDaoTest.php Normal file
View file

@ -0,0 +1,41 @@
<?php
namespace Szurubooru\Tests\Dao;
final class UserDaoTest extends \Szurubooru\Tests\AbstractDatabaseTestCase
{
public function testRetrievingByValidName()
{
$userDao = new \Szurubooru\Dao\UserDao($this->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());
}
}

View file

@ -1,7 +1,7 @@
<?php <?php
namespace Szurubooru\Tests; namespace Szurubooru\Tests;
final class DispatcherTest extends \PHPUnit_Framework_TestCase final class DispatcherTest extends \Szurubooru\Tests\AbstractTestCase
{ {
public function testDispatchingArrays() public function testDispatchingArrays()
{ {

View file

@ -1,7 +1,7 @@
<?php <?php
namespace Szurubooru\Tests; namespace Szurubooru\Tests;
final class PostDaoTest extends \PHPUnit_Framework_TestCase final class PostDaoTest extends \Szurubooru\Tests\AbstractTestCase
{ {
public function testParameterlessHandling() public function testParameterlessHandling()
{ {

View file

@ -1,56 +1,54 @@
<?php <?php
namespace Szurubooru\Tests\Services; namespace Szurubooru\Tests\Services;
class AuthServiceTest extends \PHPUnit_Framework_TestCase class AuthServiceTest extends \Szurubooru\Tests\AbstractTestCase
{ {
private $passwordServiceMock;
private $timeServiceMock;
private $tokenDaoMock;
private $userDaoMock;
public function setUp()
{
$this->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() public function testInvalidUser()
{ {
$passwordServiceMock = $this->getPasswordServiceMock();
$timeServiceMock = $this->getTimeServiceMock();
$tokenDaoMock = $this->getTokenDaoMock();
$userDaoMock = $this->getUserDaoMock();
$this->setExpectedException(\InvalidArgumentException::class, 'User not found'); $this->setExpectedException(\InvalidArgumentException::class, 'User not found');
$authService = new \Szurubooru\Services\AuthService($passwordServiceMock, $timeServiceMock, $tokenDaoMock, $userDaoMock); $authService = $this->getAuthService();
$authService->loginFromCredentials('dummy', 'godzilla'); $authService->loginFromCredentials('dummy', 'godzilla');
} }
public function testInvalidPassword() public function testInvalidPassword()
{ {
$passwordServiceMock = $this->getPasswordServiceMock(); $this->passwordServiceMock->method('getHash')->willReturn('unmatchingHash');
$timeServiceMock = $this->getTimeServiceMock();
$tokenDaoMock = $this->getTokenDaoMock();
$userDaoMock = $this->getUserDaoMock();
$passwordServiceMock->method('getHash')->willReturn('unmatchingHash');
$testUser = new \Szurubooru\Entities\User(); $testUser = new \Szurubooru\Entities\User();
$testUser->name = 'dummy'; $testUser->name = 'dummy';
$testUser->passwordHash = 'hash'; $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'); $this->setExpectedException(\InvalidArgumentException::class, 'Specified password is invalid');
$authService->loginFromCredentials('dummy', 'godzilla'); $authService->loginFromCredentials('dummy', 'godzilla');
} }
public function testValidCredentials() public function testValidCredentials()
{ {
$passwordServiceMock = $this->getPasswordServiceMock(); $this->tokenDaoMock->expects($this->once())->method('save');
$timeServiceMock = $this->getTimeServiceMock(); $this->passwordServiceMock->method('getHash')->willReturn('hash');
$tokenDaoMock = $this->getTokenDaoMock();
$userDaoMock = $this->getUserDaoMock();
$tokenDaoMock->expects($this->once())->method('save');
$passwordServiceMock->method('getHash')->willReturn('hash');
$testUser = new \Szurubooru\Entities\User(); $testUser = new \Szurubooru\Entities\User();
$testUser->name = 'dummy'; $testUser->name = 'dummy';
$testUser->passwordHash = 'hash'; $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'); $authService->loginFromCredentials('dummy', 'godzilla');
$this->assertTrue($authService->isLoggedIn()); $this->assertTrue($authService->isLoggedIn());
@ -61,37 +59,27 @@ class AuthServiceTest extends \PHPUnit_Framework_TestCase
public function testInvalidToken() public function testInvalidToken()
{ {
$passwordServiceMock = $this->getPasswordServiceMock(); $this->tokenDaoMock->expects($this->once())->method('getByName')->willReturn(null);
$timeServiceMock = $this->getTimeServiceMock();
$tokenDaoMock = $this->getTokenDaoMock();
$userDaoMock = $this->getUserDaoMock();
$tokenDaoMock->expects($this->once())->method('getByName')->willReturn(null);
$this->setExpectedException(\Exception::class); $this->setExpectedException(\Exception::class);
$authService = new \Szurubooru\Services\AuthService($passwordServiceMock, $timeServiceMock, $tokenDaoMock, $userDaoMock); $authService = $this->getAuthService();
$authService->loginFromToken(''); $authService->loginFromToken('');
} }
public function testValidToken() public function testValidToken()
{ {
$passwordServiceMock = $this->getPasswordServiceMock();
$timeServiceMock = $this->getTimeServiceMock();
$tokenDaoMock = $this->getTokenDaoMock();
$userDaoMock = $this->getUserDaoMock();
$testUser = new \Szurubooru\Entities\User(); $testUser = new \Szurubooru\Entities\User();
$testUser->id = 5; $testUser->id = 5;
$testUser->name = 'dummy'; $testUser->name = 'dummy';
$testUser->passwordHash = 'hash'; $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 = new \Szurubooru\Entities\Token();
$testToken->name = 'dummy_token'; $testToken->name = 'dummy_token';
$testToken->additionalData = $testUser->id; $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); $authService->loginFromToken($testToken->name);
$this->assertTrue($authService->isLoggedIn()); $this->assertTrue($authService->isLoggedIn());
@ -100,23 +88,12 @@ class AuthServiceTest extends \PHPUnit_Framework_TestCase
$this->assertNotNull($authService->getLoginToken()->name); $this->assertNotNull($authService->getLoginToken()->name);
} }
private function getTokenDaoMock() private function getAuthService()
{ {
return $this->getMockBuilder(\Szurubooru\Dao\TokenDao::class)->disableOriginalConstructor()->getMock(); return new \Szurubooru\Services\AuthService(
} $this->passwordServiceMock,
$this->timeServiceMock,
private function getUserDaoMock() $this->tokenDaoMock,
{ $this->userDaoMock);
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();
} }
} }

View file

@ -1,7 +1,7 @@
<?php <?php
namespace Szurubooru\Tests\Services; namespace Szurubooru\Tests\Services;
class EmailServiceTest extends \PHPUnit_Framework_TestCase class EmailServiceTest extends \Szurubooru\Tests\AbstractTestCase
{ {
public function testEmailWithoutAt() public function testEmailWithoutAt()
{ {