Refactored search filters

This commit is contained in:
Marcin Kurczewski 2014-09-26 19:14:34 +02:00
parent 04cce12a00
commit 2fc6a23e46
22 changed files with 239 additions and 179 deletions

View file

@ -3,19 +3,25 @@ namespace Szurubooru\Controllers;
final class PostController extends AbstractController final class PostController extends AbstractController
{ {
private $config;
private $privilegeService; private $privilegeService;
private $postService; private $postService;
private $postSearchParser;
private $inputReader; private $inputReader;
private $postViewProxy; private $postViewProxy;
public function __construct( public function __construct(
\Szurubooru\Config $config,
\Szurubooru\Services\PrivilegeService $privilegeService, \Szurubooru\Services\PrivilegeService $privilegeService,
\Szurubooru\Services\PostService $postService, \Szurubooru\Services\PostService $postService,
\Szurubooru\SearchServices\Parsers\PostSearchParser $postSearchParser,
\Szurubooru\Helpers\InputReader $inputReader, \Szurubooru\Helpers\InputReader $inputReader,
\Szurubooru\Controllers\ViewProxies\PostViewProxy $postViewProxy) \Szurubooru\Controllers\ViewProxies\PostViewProxy $postViewProxy)
{ {
$this->config = $config;
$this->privilegeService = $privilegeService; $this->privilegeService = $privilegeService;
$this->postService = $postService; $this->postService = $postService;
$this->postSearchParser = $postSearchParser;
$this->inputReader = $inputReader; $this->inputReader = $inputReader;
$this->postViewProxy = $postViewProxy; $this->postViewProxy = $postViewProxy;
} }
@ -46,13 +52,14 @@ final class PostController extends AbstractController
public function getFiltered() public function getFiltered()
{ {
$formData = new \Szurubooru\FormData\SearchFormData($this->inputReader); $filter = $this->postSearchParser->createFilterFromInputReader($this->inputReader);
$searchResult = $this->postService->getFiltered($formData); $filter->setPageSize($this->config->posts->postsPerPage);
$entities = $this->postViewProxy->fromArray($searchResult->getEntities(), $this->getLightFetchConfig()); $result = $this->postService->getFiltered($filter);
$entities = $this->postViewProxy->fromArray($result->getEntities(), $this->getLightFetchConfig());
return [ return [
'data' => $entities, 'data' => $entities,
'pageSize' => $searchResult->getPageSize(), 'pageSize' => $result->getPageSize(),
'totalRecords' => $searchResult->getTotalRecords()]; 'totalRecords' => $result->getTotalRecords()];
} }
public function createPost() public function createPost()

View file

@ -3,22 +3,28 @@ namespace Szurubooru\Controllers;
final class UserController extends AbstractController final class UserController extends AbstractController
{ {
private $config;
private $privilegeService; private $privilegeService;
private $userService; private $userService;
private $tokenService; private $tokenService;
private $userSearchParser;
private $inputReader; private $inputReader;
private $userViewProxy; private $userViewProxy;
public function __construct( public function __construct(
\Szurubooru\Config $config,
\Szurubooru\Services\PrivilegeService $privilegeService, \Szurubooru\Services\PrivilegeService $privilegeService,
\Szurubooru\Services\UserService $userService, \Szurubooru\Services\UserService $userService,
\Szurubooru\Services\TokenService $tokenService, \Szurubooru\Services\TokenService $tokenService,
\Szurubooru\SearchServices\Parsers\UserSearchParser $userSearchParser,
\Szurubooru\Helpers\InputReader $inputReader, \Szurubooru\Helpers\InputReader $inputReader,
\Szurubooru\Controllers\ViewProxies\UserViewProxy $userViewProxy) \Szurubooru\Controllers\ViewProxies\UserViewProxy $userViewProxy)
{ {
$this->config = $config;
$this->privilegeService = $privilegeService; $this->privilegeService = $privilegeService;
$this->userService = $userService; $this->userService = $userService;
$this->tokenService = $tokenService; $this->tokenService = $tokenService;
$this->userSearchParser = $userSearchParser;
$this->inputReader = $inputReader; $this->inputReader = $inputReader;
$this->userViewProxy = $userViewProxy; $this->userViewProxy = $userViewProxy;
} }
@ -46,13 +52,14 @@ final class UserController extends AbstractController
{ {
$this->privilegeService->assertPrivilege(\Szurubooru\Privilege::LIST_USERS); $this->privilegeService->assertPrivilege(\Szurubooru\Privilege::LIST_USERS);
$formData = new \Szurubooru\FormData\SearchFormData($this->inputReader); $filter = $this->userSearchParser->createFilterFromInputReader($this->inputReader);
$searchResult = $this->userService->getFiltered($formData); $filter->setPageSize($this->config->users->usersPerPage);
$entities = $this->userViewProxy->fromArray($searchResult->getEntities()); $result = $this->userService->getFiltered($filter);
$entities = $this->userViewProxy->fromArray($result->getEntities());
return [ return [
'data' => $entities, 'data' => $entities,
'pageSize' => $searchResult->getPageSize(), 'pageSize' => $result->getPageSize(),
'totalRecords' => $searchResult->getTotalRecords()]; 'totalRecords' => $result->getTotalRecords()];
} }
public function createUser() public function createUser()

View file

@ -69,30 +69,33 @@ abstract class AbstractDao implements ICrudDao
return $this->findBy($this->getIdColumn(), $entityIds); return $this->findBy($this->getIdColumn(), $entityIds);
} }
public function findFiltered(\Szurubooru\SearchServices\AbstractSearchFilter $searchFilter) public function findFiltered(\Szurubooru\SearchServices\Filters\IFilter $searchFilter)
{ {
$query = $this->prepareBaseQuery($searchFilter); $query = $this->fpdo->from($this->tableName);
return $this->arrayToEntities(iterator_to_array($query));
}
public function findFilteredAndPaged(\Szurubooru\SearchServices\AbstractSearchFilter $searchFilter, $pageNumber, $pageSize) $orderByString = self::compileOrderBy($searchFilter->getOrder());
if ($orderByString)
$query->orderBy($orderByString);
$this->decorateQueryFromFilter($query, $searchFilter);
if ($searchFilter->getPageSize() > 0)
{ {
$query = $this->prepareBaseQuery($searchFilter); $query->limit($searchFilter->getPageSize());
$query->limit($pageSize); $query->offset($searchFilter->getPageSize() * ($searchFilter->getPageNumber() - 1));
$query->offset($pageSize * ($pageNumber - 1)); }
$entities = $this->arrayToEntities(iterator_to_array($query)); $entities = $this->arrayToEntities(iterator_to_array($query));
$query = $this->prepareBaseQuery($searchFilter); $query = $this->fpdo->from($this->tableName);
$query->orderBy(null); $this->decorateQueryFromFilter($query, $searchFilter);
$totalRecords = count($query); $totalRecords = count($query);
$pagedSearchResult = new \Szurubooru\SearchServices\PagedSearchResult(); $searchResult = new \Szurubooru\SearchServices\Result();
$pagedSearchResult->setSearchFilter($searchFilter); $searchResult->setSearchFilter($searchFilter);
$pagedSearchResult->setEntities($entities); $searchResult->setEntities($entities);
$pagedSearchResult->setTotalRecords($totalRecords); $searchResult->setTotalRecords($totalRecords);
$pagedSearchResult->setPageNumber($pageNumber); $searchResult->setPageNumber($searchFilter->getPageNumber());
$pagedSearchResult->setPageSize($pageSize); $searchResult->setPageSize($searchFilter->getPageSize());
return $pagedSearchResult; return $searchResult;
} }
public function deleteAll() public function deleteAll()
@ -175,10 +178,6 @@ abstract class AbstractDao implements ICrudDao
{ {
} }
protected function decorateQueryFromFilter($query, \Szurubooru\SearchServices\AbstractSearchFilter $filter)
{
}
protected function arrayToEntities(array $arrayEntities) protected function arrayToEntities(array $arrayEntities)
{ {
$entities = []; $entities = [];
@ -190,23 +189,22 @@ abstract class AbstractDao implements ICrudDao
return $entities; return $entities;
} }
private function prepareBaseQuery(\Szurubooru\SearchServices\AbstractSearchFilter $searchFilter) private function decorateQueryFromFilter($query, \Szurubooru\SearchServices\Filters\IFilter $filter)
{ {
$query = $this->fpdo->from($this->tableName); foreach ($filter->getRequirements() as $requirement)
{
$orderByString = self::compileOrderBy($searchFilter->getOrder()); if ($requirement->isNegated())
if ($orderByString) $query->where('NOT ' . $requirement->getType(), $requirement->getValue());
$query->orderBy($orderByString); else
$query->where($requirement->getType(), $requirement->getValue());
$this->decorateQueryFromFilter($query, $searchFilter); }
return $query;
} }
private static function compileOrderBy($order) private static function compileOrderBy($order)
{ {
$orderByString = ''; $orderByString = '';
foreach ($order as $orderColumn => $orderDir) foreach ($order as $orderColumn => $orderDir)
$orderByString .= $orderColumn . ' ' . ($orderDir === \Szurubooru\SearchServices\AbstractSearchFilter::ORDER_DESC ? 'DESC' : 'ASC') . ', '; $orderByString .= $orderColumn . ' ' . ($orderDir === \Szurubooru\SearchServices\Filters\IFilter::ORDER_DESC ? 'DESC' : 'ASC') . ', ';
return substr($orderByString, 0, -2); return substr($orderByString, 0, -2);
} }
} }

View file

@ -3,6 +3,9 @@ namespace Szurubooru\Dao;
class UserDao extends AbstractDao implements ICrudDao class UserDao extends AbstractDao implements ICrudDao
{ {
const ORDER_NAME = 'name';
const ORDER_REGISTRATION_TIME = 'registrationTime';
private $fileService; private $fileService;
private $thumbnailService; private $thumbnailService;

View file

@ -1,24 +0,0 @@
<?php
namespace Szurubooru\FormData;
class SearchFormData implements \Szurubooru\IValidatable
{
public $query;
public $order;
public $pageNumber;
public function __construct($inputReader = null)
{
if ($inputReader !== null)
{
$this->query = trim($inputReader->query);
$this->order = trim($inputReader->order);
$this->pageNumber = intval($inputReader->page);
}
}
public function validate(\Szurubooru\Validator $validator = null)
{
$validator->validateNumber($this->pageNumber);
}
}

View file

@ -1,25 +0,0 @@
<?php
namespace Szurubooru\SearchServices;
abstract class AbstractSearchFilter
{
const ORDER_ASC = 1;
const ORDER_DESC = -1;
private $order;
public function getOrder()
{
return $this->order;
}
public function setOrder($order)
{
$this->order = $order;
}
public function __construct()
{
$this->setOrder(['id' => self::ORDER_DESC]);
}
}

View file

@ -0,0 +1,55 @@
<?php
namespace Szurubooru\SearchServices\Filters;
class BasicFilter implements IFilter
{
private $order;
private $requirements = [];
private $pageNumber;
private $pageSize;
public function __construct()
{
$this->setOrder(['id' => self::ORDER_DESC]);
}
public function getOrder()
{
return $this->order;
}
public function setOrder($order)
{
$this->order = $order;
}
public function addRequirement(\Szurubooru\SearchServices\Requirement $requirement)
{
$this->requirements[] = $requirement;
}
public function getRequirements()
{
return $this->requirements;
}
public function getPageSize()
{
return $this->pageSize;
}
public function setPageSize($pageSize)
{
$this->pageSize = $pageSize;
}
public function getPageNumber()
{
return $this->pageNumber;
}
public function setPageNumber($pageNumber)
{
$this->pageNumber = $pageNumber;
}
}

View file

@ -0,0 +1,24 @@
<?php
namespace Szurubooru\SearchServices\Filters;
interface IFilter
{
const ORDER_ASC = 1;
const ORDER_DESC = -1;
public function getOrder();
public function setOrder($order);
public function getRequirements();
public function addRequirement(\Szurubooru\SearchServices\Requirement $requirement);
public function getPageSize();
public function getPageNumber();
public function setPageSize($pageSize);
public function setPageNumber($pageNumber);
}

View file

@ -0,0 +1,6 @@
<?php
namespace Szurubooru\SearchServices\Filters;
class PostFilter extends BasicFilter implements IFilter
{
}

View file

@ -0,0 +1,8 @@
<?php
namespace Szurubooru\SearchServices\Filters;
class UserFilter extends BasicFilter implements IFilter
{
const ORDER_NAME = 'name';
const ORDER_REGISTRATION_TIME = 'registrationTime';
}

View file

@ -3,21 +3,27 @@ namespace Szurubooru\SearchServices\Parsers;
abstract class AbstractSearchParser abstract class AbstractSearchParser
{ {
public function createFilterFromFormData(\Szurubooru\FormData\SearchFormData $formData) public function createFilterFromInputReader(\Szurubooru\Helpers\InputReader $inputReader)
{ {
$filter = $this->createFilter(); $filter = $this->createFilter();
$filter->setOrder(array_merge($this->getOrder($formData->order), $filter->getOrder())); $filter->setOrder(array_merge($this->getOrder($inputReader->order), $filter->getOrder()));
$tokens = $this->tokenize($formData->query); if ($inputReader->page)
{
$filter->setPageNumber($inputReader->page);
$filter->setPageSize(25);
}
$tokens = $this->tokenize($inputReader->query);
foreach ($tokens as $token) foreach ($tokens as $token)
{ {
if ($token instanceof \Szurubooru\SearchServices\NamedSearchToken) if ($token instanceof \Szurubooru\SearchServices\NamedSearchToken)
$this->decorateFilterFromNamedToken($filter, $token); $this->decorateFilterFromNamedToken($filter, $token);
elseif ($token instanceof \Szurubooru\SearchService\SearchToken) elseif ($token instanceof \Szurubooru\SearchServices\SearchToken)
$this->decorateFilterFromToken($filter, $token); $this->decorateFilterFromToken($filter, $token);
else else
throw new \RuntimeException('Invalid search token type'); throw new \RuntimeException('Invalid search token type: ' . get_class($token));
} }
return $filter; return $filter;
@ -34,15 +40,15 @@ abstract class AbstractSearchParser
private function getOrder($query) private function getOrder($query)
{ {
$order = []; $order = [];
$tokens = array_filter(preg_split('/\s+/', $query)); $tokens = array_filter(preg_split('/\s+/', trim($query)));
foreach ($tokens as $token) foreach ($tokens as $token)
{ {
$token = preg_split('/,|\s+/', $token); $token = preg_split('/,|\s+/', $token);
$orderToken = $token[0]; $orderToken = $token[0];
$orderDir = (count($token) === 2 and $token[1] === 'desc') $orderDir = (count($token) === 2 and $token[1] === 'desc')
? \Szurubooru\SearchServices\AbstractSearchFilter::ORDER_DESC ? \Szurubooru\SearchServices\Filters\IFilter::ORDER_DESC
: \Szurubooru\SearchServices\AbstractSearchFilter::ORDER_ASC; : \Szurubooru\SearchServices\Filters\IFilter::ORDER_ASC;
$orderColumn = $this->getOrderColumn($orderToken); $orderColumn = $this->getOrderColumn($orderToken);
if ($orderColumn === null) if ($orderColumn === null)
@ -58,7 +64,7 @@ abstract class AbstractSearchParser
{ {
$searchTokens = []; $searchTokens = [];
foreach (array_filter(preg_split('/\s+/', $query)) as $tokenText) foreach (array_filter(preg_split('/\s+/', trim($query))) as $tokenText)
{ {
$negated = false; $negated = false;
if (substr($tokenText, 0, 1) === '-') if (substr($tokenText, 0, 1) === '-')

View file

@ -5,7 +5,7 @@ class PostSearchParser extends AbstractSearchParser
{ {
protected function createFilter() protected function createFilter()
{ {
return new \Szurubooru\SearchServices\PostSearchFilter; return new \Szurubooru\SearchServices\Filters\PostFilter;
} }
protected function decorateFilterFromToken($filter, $token) protected function decorateFilterFromToken($filter, $token)

View file

@ -5,7 +5,7 @@ class UserSearchParser extends AbstractSearchParser
{ {
protected function createFilter() protected function createFilter()
{ {
return new \Szurubooru\SearchServices\UserSearchFilter; return new \Szurubooru\SearchServices\Filters\UserFilter;
} }
protected function decorateFilterFromToken($filter, $token) protected function decorateFilterFromToken($filter, $token)
@ -21,10 +21,10 @@ class UserSearchParser extends AbstractSearchParser
protected function getOrderColumn($token) protected function getOrderColumn($token)
{ {
if ($token === 'name') if ($token === 'name')
return \Szurubooru\SearchServices\UserSearchFilter::ORDER_NAME; return \Szurubooru\SearchServices\Filters\UserFilter::ORDER_NAME;
if (in_array($token, ['registrationDate', 'registrationTime', 'registered', 'joinDate', 'joinTime', 'joined'])) if (in_array($token, ['registrationDate', 'registrationTime', 'registered', 'joinDate', 'joinTime', 'joined']))
return \Szurubooru\SearchServices\UserSearchFilter::ORDER_REGISTRATION_TIME; return \Szurubooru\SearchServices\Filters\UserFilter::ORDER_REGISTRATION_TIME;
return null; return null;
} }

View file

@ -1,6 +0,0 @@
<?php
namespace Szurubooru\SearchServices;
class PostSearchFilter extends AbstractSearchFilter
{
}

View file

@ -0,0 +1,39 @@
<?php
namespace Szurubooru\SearchServices;
class Requirement
{
private $negated = false;
private $type;
private $value;
public function isNegated()
{
return $this->negated;
}
public function setNegated($negated)
{
$this->negated = $negated;
}
public function getType()
{
return $this->type;
}
public function setType($type)
{
$this->type = $type;
}
public function getValue()
{
return $this->value;
}
public function setValue($value)
{
$this->value = $value;
}
}

View file

@ -1,7 +1,7 @@
<?php <?php
namespace Szurubooru\SearchServices; namespace Szurubooru\SearchServices;
class PagedSearchResult class Result
{ {
public $pageNumber; public $pageNumber;
public $pageSize; public $pageSize;
@ -9,7 +9,7 @@ class PagedSearchResult
public $entities; public $entities;
public $totalRecords; public $totalRecords;
public function setSearchFilter(AbstractSearchFilter $searchFilter = null) public function setSearchFilter(Filters\IFilter $searchFilter = null)
{ {
$this->searchFilter = $searchFilter; $this->searchFilter = $searchFilter;
} }

View file

@ -1,8 +0,0 @@
<?php
namespace Szurubooru\SearchServices;
class UserSearchFilter extends AbstractSearchFilter
{
const ORDER_NAME = 'name';
const ORDER_REGISTRATION_TIME = 'registrationTime';
}

View file

@ -8,7 +8,6 @@ class PostService
private $transactionManager; private $transactionManager;
private $postDao; private $postDao;
private $globalParamDao; private $globalParamDao;
private $postSearchParser;
private $timeService; private $timeService;
private $authService; private $authService;
private $fileService; private $fileService;
@ -20,7 +19,6 @@ class PostService
\Szurubooru\Dao\TransactionManager $transactionManager, \Szurubooru\Dao\TransactionManager $transactionManager,
\Szurubooru\Dao\PostDao $postDao, \Szurubooru\Dao\PostDao $postDao,
\Szurubooru\Dao\GlobalParamDao $globalParamDao, \Szurubooru\Dao\GlobalParamDao $globalParamDao,
\Szurubooru\SearchServices\Parsers\PostSearchParser $postSearchParser,
\Szurubooru\Services\AuthService $authService, \Szurubooru\Services\AuthService $authService,
\Szurubooru\Services\TimeService $timeService, \Szurubooru\Services\TimeService $timeService,
\Szurubooru\Services\FileService $fileService, \Szurubooru\Services\FileService $fileService,
@ -31,7 +29,6 @@ class PostService
$this->transactionManager = $transactionManager; $this->transactionManager = $transactionManager;
$this->postDao = $postDao; $this->postDao = $postDao;
$this->globalParamDao = $globalParamDao; $this->globalParamDao = $globalParamDao;
$this->postSearchParser = $postSearchParser;
$this->timeService = $timeService; $this->timeService = $timeService;
$this->authService = $authService; $this->authService = $authService;
$this->fileService = $fileService; $this->fileService = $fileService;
@ -64,13 +61,11 @@ class PostService
return $this->transactionManager->rollback($transactionFunc); return $this->transactionManager->rollback($transactionFunc);
} }
public function getFiltered(\Szurubooru\FormData\SearchFormData $formData) public function getFiltered(\Szurubooru\SearchServices\Filters\PostFilter $filter)
{ {
$transactionFunc = function() use ($formData) $transactionFunc = function() use ($filter)
{ {
$this->validator->validate($formData); return $this->postDao->findFiltered($filter);
$searchFilter = $this->postSearchParser->createFilterFromFormData($formData);
return $this->postDao->findFilteredAndPaged($searchFilter, $formData->pageNumber, $this->config->posts->postsPerPage);
}; };
return $this->transactionManager->rollback($transactionFunc); return $this->transactionManager->rollback($transactionFunc);
} }

View file

@ -7,7 +7,6 @@ class UserService
private $validator; private $validator;
private $transactionManager; private $transactionManager;
private $userDao; private $userDao;
private $userSearchParser;
private $passwordService; private $passwordService;
private $emailService; private $emailService;
private $fileService; private $fileService;
@ -20,7 +19,6 @@ class UserService
\Szurubooru\Validator $validator, \Szurubooru\Validator $validator,
\Szurubooru\Dao\TransactionManager $transactionManager, \Szurubooru\Dao\TransactionManager $transactionManager,
\Szurubooru\Dao\UserDao $userDao, \Szurubooru\Dao\UserDao $userDao,
\Szurubooru\SearchServices\Parsers\UserSearchParser $userSearchParser,
\Szurubooru\Services\PasswordService $passwordService, \Szurubooru\Services\PasswordService $passwordService,
\Szurubooru\Services\EmailService $emailService, \Szurubooru\Services\EmailService $emailService,
\Szurubooru\Services\FileService $fileService, \Szurubooru\Services\FileService $fileService,
@ -32,7 +30,6 @@ class UserService
$this->validator = $validator; $this->validator = $validator;
$this->transactionManager = $transactionManager; $this->transactionManager = $transactionManager;
$this->userDao = $userDao; $this->userDao = $userDao;
$this->userSearchParser = $userSearchParser;
$this->passwordService = $passwordService; $this->passwordService = $passwordService;
$this->emailService = $emailService; $this->emailService = $emailService;
$this->fileService = $fileService; $this->fileService = $fileService;
@ -82,13 +79,11 @@ class UserService
return $this->transactionManager->rollback($transactionFunc); return $this->transactionManager->rollback($transactionFunc);
} }
public function getFiltered(\Szurubooru\FormData\SearchFormData $formData) public function getFiltered(\Szurubooru\SearchServices\Filters\UserFilter $filter)
{ {
$transactionFunc = function() use ($formData) $transactionFunc = function() use ($filter)
{ {
$this->validator->validate($formData); return $this->userDao->findFiltered($filter);
$searchFilter = $this->userSearchParser->createFilterFromFormData($formData);
return $this->userDao->findFilteredAndPaged($searchFilter, $formData->pageNumber, $this->config->users->usersPerPage);
}; };
return $this->transactionManager->rollback($transactionFunc); return $this->transactionManager->rollback($transactionFunc);
} }

View file

@ -27,9 +27,11 @@ class UserDaoFilterTest extends \Szurubooru\Tests\AbstractDatabaseTestCase
public function testNothing() public function testNothing()
{ {
$searchFilter = new \Szurubooru\SearchServices\UserSearchFilter(); $searchFilter = new \Szurubooru\SearchServices\Filters\UserFilter();
$searchFilter->setPageNumber(1);
$searchFilter->setPageSize(2);
$userDao = $this->getUserDao(); $userDao = $this->getUserDao();
$result = $userDao->findFilteredAndPaged($searchFilter, 1, 2); $result = $userDao->findFiltered($searchFilter);
$this->assertEmpty($result->getEntities()); $this->assertEmpty($result->getEntities());
$this->assertEquals(0, $result->getTotalRecords()); $this->assertEquals(0, $result->getTotalRecords());
$this->assertEquals(1, $result->getPageNumber()); $this->assertEquals(1, $result->getPageNumber());
@ -51,12 +53,14 @@ class UserDaoFilterTest extends \Szurubooru\Tests\AbstractDatabaseTestCase
$expectedUsers[] = $user; $expectedUsers[] = $user;
} }
$searchFilter = new \Szurubooru\SearchServices\UserSearchFilter(); $searchFilter = new \Szurubooru\SearchServices\Filters\UserFilter();
$searchFilter->setOrder([ $searchFilter->setOrder([
\Szurubooru\SearchServices\UserSearchFilter::ORDER_NAME => \Szurubooru\SearchServices\Filters\UserFilter::ORDER_NAME =>
\Szurubooru\SearchServices\UserSearchFilter::ORDER_DESC]); \Szurubooru\SearchServices\Filters\UserFilter::ORDER_DESC]);
$searchFilter->setPageNumber($pageNumber);
$searchFilter->setPageSize($pageSize);
$result = $userDao->findFilteredAndPaged($searchFilter, $pageNumber, $pageSize); $result = $userDao->findFiltered($searchFilter);
$this->assertEquals(count($allUserNames), $result->getTotalRecords()); $this->assertEquals(count($allUserNames), $result->getTotalRecords());
$this->assertEquals($pageNumber, $result->getPageNumber()); $this->assertEquals($pageNumber, $result->getPageNumber());
$this->assertEquals($pageSize, $result->getPageSize()); $this->assertEquals($pageSize, $result->getPageSize());
@ -73,8 +77,8 @@ class UserDaoFilterTest extends \Szurubooru\Tests\AbstractDatabaseTestCase
{ {
list ($user1, $user2) = $this->prepareUsers(); list ($user1, $user2) = $this->prepareUsers();
$this->doTestSorting( $this->doTestSorting(
\Szurubooru\SearchServices\UserSearchFilter::ORDER_NAME, \Szurubooru\SearchServices\Filters\UserFilter::ORDER_NAME,
\Szurubooru\SearchServices\UserSearchFilter::ORDER_ASC, \Szurubooru\SearchServices\Filters\UserFilter::ORDER_ASC,
[$user1, $user2]); [$user1, $user2]);
} }
@ -82,8 +86,8 @@ class UserDaoFilterTest extends \Szurubooru\Tests\AbstractDatabaseTestCase
{ {
list ($user1, $user2) = $this->prepareUsers(); list ($user1, $user2) = $this->prepareUsers();
$this->doTestSorting( $this->doTestSorting(
\Szurubooru\SearchServices\UserSearchFilter::ORDER_NAME, \Szurubooru\SearchServices\Filters\UserFilter::ORDER_NAME,
\Szurubooru\SearchServices\UserSearchFilter::ORDER_DESC, \Szurubooru\SearchServices\Filters\UserFilter::ORDER_DESC,
[$user2, $user1]); [$user2, $user1]);
} }
@ -91,8 +95,8 @@ class UserDaoFilterTest extends \Szurubooru\Tests\AbstractDatabaseTestCase
{ {
list ($user1, $user2) = $this->prepareUsers(); list ($user1, $user2) = $this->prepareUsers();
$this->doTestSorting( $this->doTestSorting(
\Szurubooru\SearchServices\UserSearchFilter::ORDER_REGISTRATION_TIME, \Szurubooru\SearchServices\Filters\UserFilter::ORDER_REGISTRATION_TIME,
\Szurubooru\SearchServices\UserSearchFilter::ORDER_ASC, \Szurubooru\SearchServices\Filters\UserFilter::ORDER_ASC,
[$user2, $user1]); [$user2, $user1]);
} }
@ -100,8 +104,8 @@ class UserDaoFilterTest extends \Szurubooru\Tests\AbstractDatabaseTestCase
{ {
list ($user1, $user2) = $this->prepareUsers(); list ($user1, $user2) = $this->prepareUsers();
$this->doTestSorting( $this->doTestSorting(
\Szurubooru\SearchServices\UserSearchFilter::ORDER_REGISTRATION_TIME, \Szurubooru\SearchServices\Filters\UserFilter::ORDER_REGISTRATION_TIME,
\Szurubooru\SearchServices\UserSearchFilter::ORDER_DESC, \Szurubooru\SearchServices\Filters\UserFilter::ORDER_DESC,
[$user1, $user2]); [$user1, $user2]);
} }
@ -121,16 +125,17 @@ class UserDaoFilterTest extends \Szurubooru\Tests\AbstractDatabaseTestCase
private function doTestSorting($order, $orderDirection, $expectedUsers) private function doTestSorting($order, $orderDirection, $expectedUsers)
{ {
$userDao = $this->getUserDao(); $userDao = $this->getUserDao();
$searchFilter = new \Szurubooru\SearchServices\UserSearchFilter(); $searchFilter = new \Szurubooru\SearchServices\Filters\UserFilter();
if ($order !== null) if ($order !== null)
$searchFilter->setOrder([$order => $orderDirection]); $searchFilter->setOrder([$order => $orderDirection]);
$result = $userDao->findFilteredAndPaged($searchFilter, 1, 10); $result = $userDao->findFiltered($searchFilter, 1, 10);
$this->assertInstanceOf(\Szurubooru\SearchServices\PagedSearchResult::class, $result); $this->assertInstanceOf(\Szurubooru\SearchServices\Result::class, $result);
$this->assertEquals($searchFilter, $result->getSearchFilter()); $this->assertEquals($searchFilter, $result->getSearchFilter());
$this->assertEntitiesEqual(array_values($expectedUsers), array_values($result->getEntities())); $this->assertEntitiesEqual(array_values($expectedUsers), array_values($result->getEntities()));
$this->assertEquals(count($expectedUsers), $result->getTotalRecords()); $this->assertEquals(count($expectedUsers), $result->getTotalRecords());
$this->assertEquals(1, $result->getPageNumber()); $this->assertNull($result->getPageNumber());
$this->assertNull($result->getPageSize());
} }
private function getUserDao() private function getUserDao()

View file

@ -8,7 +8,6 @@ class PostServiceTest extends \Szurubooru\Tests\AbstractTestCase
private $transactionManagerMock; private $transactionManagerMock;
private $postDaoMock; private $postDaoMock;
private $globalParamDaoMock; private $globalParamDaoMock;
private $postSearchParserMock;
private $authServiceMock; private $authServiceMock;
private $timeServiceMock; private $timeServiceMock;
private $fileServiceMock; private $fileServiceMock;
@ -21,7 +20,6 @@ class PostServiceTest extends \Szurubooru\Tests\AbstractTestCase
$this->transactionManagerMock = $this->mockTransactionManager(); $this->transactionManagerMock = $this->mockTransactionManager();
$this->postDaoMock = $this->mock(\Szurubooru\Dao\PostDao::class); $this->postDaoMock = $this->mock(\Szurubooru\Dao\PostDao::class);
$this->globalParamDaoMock = $this->mock(\Szurubooru\Dao\GlobalParamDao::class); $this->globalParamDaoMock = $this->mock(\Szurubooru\Dao\GlobalParamDao::class);
$this->postSearchParserMock = $this->mock(\Szurubooru\SearchServices\Parsers\PostSearchParser::class);
$this->authServiceMock = $this->mock(\Szurubooru\Services\AuthService::class); $this->authServiceMock = $this->mock(\Szurubooru\Services\AuthService::class);
$this->timeServiceMock = $this->mock(\Szurubooru\Services\TimeService::class); $this->timeServiceMock = $this->mock(\Szurubooru\Services\TimeService::class);
$this->fileServiceMock = $this->mock(\Szurubooru\Services\FileService::class); $this->fileServiceMock = $this->mock(\Szurubooru\Services\FileService::class);
@ -181,7 +179,6 @@ class PostServiceTest extends \Szurubooru\Tests\AbstractTestCase
$this->transactionManagerMock, $this->transactionManagerMock,
$this->postDaoMock, $this->postDaoMock,
$this->globalParamDaoMock, $this->globalParamDaoMock,
$this->postSearchParserMock,
$this->authServiceMock, $this->authServiceMock,
$this->timeServiceMock, $this->timeServiceMock,
$this->fileServiceMock, $this->fileServiceMock,

View file

@ -7,7 +7,6 @@ final class UserServiceTest extends \Szurubooru\Tests\AbstractTestCase
private $validatorMock; private $validatorMock;
private $transactionManagerMock; private $transactionManagerMock;
private $userDaoMock; private $userDaoMock;
private $userSearchParserMock;
private $passwordServiceMock; private $passwordServiceMock;
private $emailServiceMock; private $emailServiceMock;
private $fileServiceMock; private $fileServiceMock;
@ -22,7 +21,6 @@ final class UserServiceTest extends \Szurubooru\Tests\AbstractTestCase
$this->transactionManagerMock = $this->mockTransactionManager(); $this->transactionManagerMock = $this->mockTransactionManager();
$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->userSearchParserMock = $this->mock(\Szurubooru\SearchServices\Parsers\UserSearchParser::class);
$this->passwordServiceMock = $this->mock(\Szurubooru\Services\PasswordService::class); $this->passwordServiceMock = $this->mock(\Szurubooru\Services\PasswordService::class);
$this->emailServiceMock = $this->mock(\Szurubooru\Services\EmailService::class); $this->emailServiceMock = $this->mock(\Szurubooru\Services\EmailService::class);
$this->fileServiceMock = $this->mock(\Szurubooru\Services\FileService::class); $this->fileServiceMock = $this->mock(\Szurubooru\Services\FileService::class);
@ -67,25 +65,6 @@ final class UserServiceTest extends \Szurubooru\Tests\AbstractTestCase
$userService->getById('godzilla'); $userService->getById('godzilla');
} }
public function testGettingFilteredUsers()
{
$mockUser = new \Szurubooru\Entities\User;
$mockUser->setName('user');
$expected = [$mockUser];
$this->userSearchParserMock->method('createFilterFromFormData')->willReturn(new \Szurubooru\SearchServices\UserSearchFilter());
$this->userDaoMock->method('findFilteredAndPaged')->willReturn($expected);
$this->configMock->set('users/usersPerPage', 1);
$searchFormData = new \Szurubooru\FormData\SearchFormData;
$searchFormData->query = '';
$searchFormData->order = 'joined';
$searchFormData->page = 2;
$userService = $this->getUserService();
$actual = $userService->getFiltered($searchFormData);
$this->assertEquals($expected, $actual);
}
public function testValidRegistrationWithoutMailActivation() public function testValidRegistrationWithoutMailActivation()
{ {
$formData = new \Szurubooru\FormData\RegistrationFormData; $formData = new \Szurubooru\FormData\RegistrationFormData;
@ -293,7 +272,6 @@ final class UserServiceTest extends \Szurubooru\Tests\AbstractTestCase
$this->validatorMock, $this->validatorMock,
$this->transactionManagerMock, $this->transactionManagerMock,
$this->userDaoMock, $this->userDaoMock,
$this->userSearchParserMock,
$this->passwordServiceMock, $this->passwordServiceMock,
$this->emailServiceMock, $this->emailServiceMock,
$this->fileServiceMock, $this->fileServiceMock,