Added config mock

This commit is contained in:
Marcin Kurczewski 2014-09-05 20:08:54 +02:00
parent 926c5af0d6
commit 8edf81e75e
5 changed files with 44 additions and 30 deletions

View file

@ -7,6 +7,11 @@ abstract class AbstractTestCase extends \PHPUnit_Framework_TestCase
{ {
return $this->getMockBuilder($className)->disableOriginalConstructor()->getMock(); return $this->getMockBuilder($className)->disableOriginalConstructor()->getMock();
} }
public function mockConfig()
{
return new ConfigMock();
}
} }
date_default_timezone_set('UTC'); date_default_timezone_set('UTC');

19
tests/ConfigMock.php Normal file
View file

@ -0,0 +1,19 @@
<?php
namespace Szurubooru\Tests;
class ConfigMock extends \Szurubooru\Config
{
public function set($key, $value)
{
$keys = preg_split('/[\\/]/', $key);
$current = $this;
$lastKey = array_pop($keys);
foreach ($keys as $key)
{
if (!isset($current->$key))
$current->$key = new \ArrayObject([], \ArrayObject::ARRAY_AS_PROPS);
$current = $current->$key;
}
$current->$lastKey = $value;
}
}

View file

@ -13,7 +13,7 @@ class AuthServiceTest extends \Szurubooru\Tests\AbstractTestCase
public function setUp() public function setUp()
{ {
$this->validatorMock = $this->mock(\Szurubooru\Validator::class); $this->validatorMock = $this->mock(\Szurubooru\Validator::class);
$this->configMock = $this->mock(\Szurubooru\Config::class); $this->configMock = $this->mockConfig();
$this->passwordServiceMock = $this->mock(\Szurubooru\Services\PasswordService::class); $this->passwordServiceMock = $this->mock(\Szurubooru\Services\PasswordService::class);
$this->timeServiceMock = $this->mock(\Szurubooru\Services\TimeService::class); $this->timeServiceMock = $this->mock(\Szurubooru\Services\TimeService::class);
$this->tokenDaoMock = $this->mock(\Szurubooru\Dao\TokenDao::class); $this->tokenDaoMock = $this->mock(\Szurubooru\Dao\TokenDao::class);

View file

@ -13,7 +13,7 @@ final class UserServiceTest extends \Szurubooru\Tests\AbstractTestCase
public function setUp() public function setUp()
{ {
$this->configMock = $this->mock(\Szurubooru\Config::class); $this->configMock = $this->mockConfig();
$this->validatorMock = $this->mock(\Szurubooru\Validator::class); $this->validatorMock = $this->mock(\Szurubooru\Validator::class);
$this->userDaoMock = $this->mock(\Szurubooru\Dao\UserDao::class); $this->userDaoMock = $this->mock(\Szurubooru\Dao\UserDao::class);
$this->userSearchService = $this->mock(\Szurubooru\Dao\Services\UserSearchService::class); $this->userSearchService = $this->mock(\Szurubooru\Dao\Services\UserSearchService::class);
@ -29,8 +29,7 @@ final class UserServiceTest extends \Szurubooru\Tests\AbstractTestCase
$expected = [$mockUser]; $expected = [$mockUser];
$this->userSearchService->method('getFiltered')->willReturn($expected); $this->userSearchService->method('getFiltered')->willReturn($expected);
$this->configMock->users = new \StdClass; $this->configMock->set('users/usersPerPage', 1);
$this->configMock->users->usersPerPage = 1;
$searchFormData = new \Szurubooru\FormData\SearchFormData; $searchFormData = new \Szurubooru\FormData\SearchFormData;
$searchFormData->query = ''; $searchFormData->query = '';
$searchFormData->order = 'joined'; $searchFormData->order = 'joined';

View file

@ -3,11 +3,11 @@ namespace Szurubooru\Tests;
final class ValidatorTest extends \Szurubooru\Tests\AbstractTestCase final class ValidatorTest extends \Szurubooru\Tests\AbstractTestCase
{ {
private $config; private $configMock;
public function setUp() public function setUp()
{ {
$this->config = new \Szurubooru\Config(); $this->configMock = $this->mockConfig();
} }
public function testMinLengthName() public function testMinLengthName()
@ -34,9 +34,8 @@ final class ValidatorTest extends \Szurubooru\Tests\AbstractTestCase
public function testEmptyUserName() public function testEmptyUserName()
{ {
$this->config->users = new \StdClass; $this->configMock->set('users/minUserNameLength', 0);
$this->config->users->minUserNameLength = 0; $this->configMock->set('users/maxUserNameLength', 1);
$this->config->users->maxUserNameLength = 1;
$this->setExpectedException(\Exception::class, 'User name cannot be empty'); $this->setExpectedException(\Exception::class, 'User name cannot be empty');
$userName = ''; $userName = '';
$validator = $this->getValidator(); $validator = $this->getValidator();
@ -45,9 +44,8 @@ final class ValidatorTest extends \Szurubooru\Tests\AbstractTestCase
public function testTooShortUserName() public function testTooShortUserName()
{ {
$this->config->users = new \StdClass; $this->configMock->set('users/minUserNameLength', 30);
$this->config->users->minUserNameLength = 30; $this->configMock->set('users/maxUserNameLength', 50);
$this->config->users->maxUserNameLength = 50;
$this->setExpectedException(\Exception::class, 'User name must have at least 30 character(s)'); $this->setExpectedException(\Exception::class, 'User name must have at least 30 character(s)');
$userName = 'godzilla'; $userName = 'godzilla';
$validator = $this->getValidator(); $validator = $this->getValidator();
@ -56,9 +54,8 @@ final class ValidatorTest extends \Szurubooru\Tests\AbstractTestCase
public function testTooLongUserName() public function testTooLongUserName()
{ {
$this->config->users = new \StdClass; $this->configMock->set('users/minUserNameLength', 30);
$this->config->users->minUserNameLength = 30; $this->configMock->set('users/maxUserNameLength', 50);
$this->config->users->maxUserNameLength = 50;
$this->setExpectedException(\Exception::class, 'User name must have at most 50 character(s)'); $this->setExpectedException(\Exception::class, 'User name must have at most 50 character(s)');
$userName = 'godzilla' . str_repeat('a', 50); $userName = 'godzilla' . str_repeat('a', 50);
$validator = $this->getValidator(); $validator = $this->getValidator();
@ -67,9 +64,8 @@ final class ValidatorTest extends \Szurubooru\Tests\AbstractTestCase
public function testUserNameWithSpaces() public function testUserNameWithSpaces()
{ {
$this->config->users = new \StdClass; $this->configMock->set('users/minUserNameLength', 0);
$this->config->users->minUserNameLength = 0; $this->configMock->set('users/maxUserNameLength', 100);
$this->config->users->maxUserNameLength = 100;
$userName = ' godzilla '; $userName = ' godzilla ';
$validator = $this->getValidator(); $validator = $this->getValidator();
$validator->validateUserName($userName); $validator->validateUserName($userName);
@ -78,9 +74,8 @@ final class ValidatorTest extends \Szurubooru\Tests\AbstractTestCase
public function testUserNameWithInvalidCharacters() public function testUserNameWithInvalidCharacters()
{ {
$this->config->users = new \StdClass; $this->configMock->set('users/minUserNameLength', 0);
$this->config->users->minUserNameLength = 0; $this->configMock->set('users/maxUserNameLength', 100);
$this->config->users->maxUserNameLength = 100;
$userName = '..:xXx:godzilla:xXx:..'; $userName = '..:xXx:godzilla:xXx:..';
$this->setExpectedException(\Exception::class, 'User name may contain only'); $this->setExpectedException(\Exception::class, 'User name may contain only');
$validator = $this->getValidator(); $validator = $this->getValidator();
@ -109,8 +104,7 @@ final class ValidatorTest extends \Szurubooru\Tests\AbstractTestCase
public function testEmptyPassword() public function testEmptyPassword()
{ {
$this->config->security = new \StdClass; $this->configMock->set('security/minPasswordLength', 0);
$this->config->security->minPasswordLength = 0;
$this->setExpectedException(\Exception::class, 'Password cannot be empty'); $this->setExpectedException(\Exception::class, 'Password cannot be empty');
$validator = $this->getValidator(); $validator = $this->getValidator();
$validator->validatePassword(''); $validator->validatePassword('');
@ -118,8 +112,7 @@ final class ValidatorTest extends \Szurubooru\Tests\AbstractTestCase
public function testTooShortPassword() public function testTooShortPassword()
{ {
$this->config->security = new \StdClass; $this->configMock->set('security/minPasswordLength', 10000);
$this->config->security->minPasswordLength = 10000;
$this->setExpectedException(\Exception::class, 'Password must have at least 10000 character(s)'); $this->setExpectedException(\Exception::class, 'Password must have at least 10000 character(s)');
$validator = $this->getValidator(); $validator = $this->getValidator();
$validator->validatePassword('password123'); $validator->validatePassword('password123');
@ -127,8 +120,7 @@ final class ValidatorTest extends \Szurubooru\Tests\AbstractTestCase
public function testNonAsciiPassword() public function testNonAsciiPassword()
{ {
$this->config->security = new \StdClass; $this->configMock->set('security/minPasswordLength', 0);
$this->config->security->minPasswordLength = 0;
$this->setExpectedException(\Exception::class, 'Password may contain only'); $this->setExpectedException(\Exception::class, 'Password may contain only');
$validator = $this->getValidator(); $validator = $this->getValidator();
$validator->validatePassword('良いパスワード'); $validator->validatePassword('良いパスワード');
@ -136,14 +128,13 @@ final class ValidatorTest extends \Szurubooru\Tests\AbstractTestCase
public function testValidPassword() public function testValidPassword()
{ {
$this->config->security = new \StdClass; $this->configMock->set('security/minPasswordLength', 0);
$this->config->security->minPasswordLength = 0;
$validator = $this->getValidator(); $validator = $this->getValidator();
$this->assertNull($validator->validatePassword('password')); $this->assertNull($validator->validatePassword('password'));
} }
private function getValidator() private function getValidator()
{ {
return new \Szurubooru\Validator($this->config); return new \Szurubooru\Validator($this->configMock);
} }
} }