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

View file

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