Refactored search filters
This commit is contained in:
parent
04cce12a00
commit
2fc6a23e46
22 changed files with 239 additions and 179 deletions
|
@ -3,19 +3,25 @@ namespace Szurubooru\Controllers;
|
|||
|
||||
final class PostController extends AbstractController
|
||||
{
|
||||
private $config;
|
||||
private $privilegeService;
|
||||
private $postService;
|
||||
private $postSearchParser;
|
||||
private $inputReader;
|
||||
private $postViewProxy;
|
||||
|
||||
public function __construct(
|
||||
\Szurubooru\Config $config,
|
||||
\Szurubooru\Services\PrivilegeService $privilegeService,
|
||||
\Szurubooru\Services\PostService $postService,
|
||||
\Szurubooru\SearchServices\Parsers\PostSearchParser $postSearchParser,
|
||||
\Szurubooru\Helpers\InputReader $inputReader,
|
||||
\Szurubooru\Controllers\ViewProxies\PostViewProxy $postViewProxy)
|
||||
{
|
||||
$this->config = $config;
|
||||
$this->privilegeService = $privilegeService;
|
||||
$this->postService = $postService;
|
||||
$this->postSearchParser = $postSearchParser;
|
||||
$this->inputReader = $inputReader;
|
||||
$this->postViewProxy = $postViewProxy;
|
||||
}
|
||||
|
@ -46,13 +52,14 @@ final class PostController extends AbstractController
|
|||
|
||||
public function getFiltered()
|
||||
{
|
||||
$formData = new \Szurubooru\FormData\SearchFormData($this->inputReader);
|
||||
$searchResult = $this->postService->getFiltered($formData);
|
||||
$entities = $this->postViewProxy->fromArray($searchResult->getEntities(), $this->getLightFetchConfig());
|
||||
$filter = $this->postSearchParser->createFilterFromInputReader($this->inputReader);
|
||||
$filter->setPageSize($this->config->posts->postsPerPage);
|
||||
$result = $this->postService->getFiltered($filter);
|
||||
$entities = $this->postViewProxy->fromArray($result->getEntities(), $this->getLightFetchConfig());
|
||||
return [
|
||||
'data' => $entities,
|
||||
'pageSize' => $searchResult->getPageSize(),
|
||||
'totalRecords' => $searchResult->getTotalRecords()];
|
||||
'pageSize' => $result->getPageSize(),
|
||||
'totalRecords' => $result->getTotalRecords()];
|
||||
}
|
||||
|
||||
public function createPost()
|
||||
|
|
|
@ -3,22 +3,28 @@ namespace Szurubooru\Controllers;
|
|||
|
||||
final class UserController extends AbstractController
|
||||
{
|
||||
private $config;
|
||||
private $privilegeService;
|
||||
private $userService;
|
||||
private $tokenService;
|
||||
private $userSearchParser;
|
||||
private $inputReader;
|
||||
private $userViewProxy;
|
||||
|
||||
public function __construct(
|
||||
\Szurubooru\Config $config,
|
||||
\Szurubooru\Services\PrivilegeService $privilegeService,
|
||||
\Szurubooru\Services\UserService $userService,
|
||||
\Szurubooru\Services\TokenService $tokenService,
|
||||
\Szurubooru\SearchServices\Parsers\UserSearchParser $userSearchParser,
|
||||
\Szurubooru\Helpers\InputReader $inputReader,
|
||||
\Szurubooru\Controllers\ViewProxies\UserViewProxy $userViewProxy)
|
||||
{
|
||||
$this->config = $config;
|
||||
$this->privilegeService = $privilegeService;
|
||||
$this->userService = $userService;
|
||||
$this->tokenService = $tokenService;
|
||||
$this->userSearchParser = $userSearchParser;
|
||||
$this->inputReader = $inputReader;
|
||||
$this->userViewProxy = $userViewProxy;
|
||||
}
|
||||
|
@ -46,13 +52,14 @@ final class UserController extends AbstractController
|
|||
{
|
||||
$this->privilegeService->assertPrivilege(\Szurubooru\Privilege::LIST_USERS);
|
||||
|
||||
$formData = new \Szurubooru\FormData\SearchFormData($this->inputReader);
|
||||
$searchResult = $this->userService->getFiltered($formData);
|
||||
$entities = $this->userViewProxy->fromArray($searchResult->getEntities());
|
||||
$filter = $this->userSearchParser->createFilterFromInputReader($this->inputReader);
|
||||
$filter->setPageSize($this->config->users->usersPerPage);
|
||||
$result = $this->userService->getFiltered($filter);
|
||||
$entities = $this->userViewProxy->fromArray($result->getEntities());
|
||||
return [
|
||||
'data' => $entities,
|
||||
'pageSize' => $searchResult->getPageSize(),
|
||||
'totalRecords' => $searchResult->getTotalRecords()];
|
||||
'pageSize' => $result->getPageSize(),
|
||||
'totalRecords' => $result->getTotalRecords()];
|
||||
}
|
||||
|
||||
public function createUser()
|
||||
|
|
|
@ -69,30 +69,33 @@ abstract class AbstractDao implements ICrudDao
|
|||
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);
|
||||
return $this->arrayToEntities(iterator_to_array($query));
|
||||
}
|
||||
$query = $this->fpdo->from($this->tableName);
|
||||
|
||||
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($pageSize);
|
||||
$query->offset($pageSize * ($pageNumber - 1));
|
||||
$query->limit($searchFilter->getPageSize());
|
||||
$query->offset($searchFilter->getPageSize() * ($searchFilter->getPageNumber() - 1));
|
||||
}
|
||||
$entities = $this->arrayToEntities(iterator_to_array($query));
|
||||
|
||||
$query = $this->prepareBaseQuery($searchFilter);
|
||||
$query->orderBy(null);
|
||||
$query = $this->fpdo->from($this->tableName);
|
||||
$this->decorateQueryFromFilter($query, $searchFilter);
|
||||
$totalRecords = count($query);
|
||||
|
||||
$pagedSearchResult = new \Szurubooru\SearchServices\PagedSearchResult();
|
||||
$pagedSearchResult->setSearchFilter($searchFilter);
|
||||
$pagedSearchResult->setEntities($entities);
|
||||
$pagedSearchResult->setTotalRecords($totalRecords);
|
||||
$pagedSearchResult->setPageNumber($pageNumber);
|
||||
$pagedSearchResult->setPageSize($pageSize);
|
||||
return $pagedSearchResult;
|
||||
$searchResult = new \Szurubooru\SearchServices\Result();
|
||||
$searchResult->setSearchFilter($searchFilter);
|
||||
$searchResult->setEntities($entities);
|
||||
$searchResult->setTotalRecords($totalRecords);
|
||||
$searchResult->setPageNumber($searchFilter->getPageNumber());
|
||||
$searchResult->setPageSize($searchFilter->getPageSize());
|
||||
return $searchResult;
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
$entities = [];
|
||||
|
@ -190,23 +189,22 @@ abstract class AbstractDao implements ICrudDao
|
|||
return $entities;
|
||||
}
|
||||
|
||||
private function prepareBaseQuery(\Szurubooru\SearchServices\AbstractSearchFilter $searchFilter)
|
||||
private function decorateQueryFromFilter($query, \Szurubooru\SearchServices\Filters\IFilter $filter)
|
||||
{
|
||||
$query = $this->fpdo->from($this->tableName);
|
||||
|
||||
$orderByString = self::compileOrderBy($searchFilter->getOrder());
|
||||
if ($orderByString)
|
||||
$query->orderBy($orderByString);
|
||||
|
||||
$this->decorateQueryFromFilter($query, $searchFilter);
|
||||
return $query;
|
||||
foreach ($filter->getRequirements() as $requirement)
|
||||
{
|
||||
if ($requirement->isNegated())
|
||||
$query->where('NOT ' . $requirement->getType(), $requirement->getValue());
|
||||
else
|
||||
$query->where($requirement->getType(), $requirement->getValue());
|
||||
}
|
||||
}
|
||||
|
||||
private static function compileOrderBy($order)
|
||||
{
|
||||
$orderByString = '';
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,6 +3,9 @@ namespace Szurubooru\Dao;
|
|||
|
||||
class UserDao extends AbstractDao implements ICrudDao
|
||||
{
|
||||
const ORDER_NAME = 'name';
|
||||
const ORDER_REGISTRATION_TIME = 'registrationTime';
|
||||
|
||||
private $fileService;
|
||||
private $thumbnailService;
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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]);
|
||||
}
|
||||
}
|
55
src/SearchServices/Filters/BasicFilter.php
Normal file
55
src/SearchServices/Filters/BasicFilter.php
Normal 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;
|
||||
}
|
||||
}
|
24
src/SearchServices/Filters/IFilter.php
Normal file
24
src/SearchServices/Filters/IFilter.php
Normal 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);
|
||||
}
|
6
src/SearchServices/Filters/PostFilter.php
Normal file
6
src/SearchServices/Filters/PostFilter.php
Normal file
|
@ -0,0 +1,6 @@
|
|||
<?php
|
||||
namespace Szurubooru\SearchServices\Filters;
|
||||
|
||||
class PostFilter extends BasicFilter implements IFilter
|
||||
{
|
||||
}
|
8
src/SearchServices/Filters/UserFilter.php
Normal file
8
src/SearchServices/Filters/UserFilter.php
Normal 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';
|
||||
}
|
|
@ -3,21 +3,27 @@ namespace Szurubooru\SearchServices\Parsers;
|
|||
|
||||
abstract class AbstractSearchParser
|
||||
{
|
||||
public function createFilterFromFormData(\Szurubooru\FormData\SearchFormData $formData)
|
||||
public function createFilterFromInputReader(\Szurubooru\Helpers\InputReader $inputReader)
|
||||
{
|
||||
$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)
|
||||
{
|
||||
if ($token instanceof \Szurubooru\SearchServices\NamedSearchToken)
|
||||
$this->decorateFilterFromNamedToken($filter, $token);
|
||||
elseif ($token instanceof \Szurubooru\SearchService\SearchToken)
|
||||
elseif ($token instanceof \Szurubooru\SearchServices\SearchToken)
|
||||
$this->decorateFilterFromToken($filter, $token);
|
||||
else
|
||||
throw new \RuntimeException('Invalid search token type');
|
||||
throw new \RuntimeException('Invalid search token type: ' . get_class($token));
|
||||
}
|
||||
|
||||
return $filter;
|
||||
|
@ -34,15 +40,15 @@ abstract class AbstractSearchParser
|
|||
private function getOrder($query)
|
||||
{
|
||||
$order = [];
|
||||
$tokens = array_filter(preg_split('/\s+/', $query));
|
||||
$tokens = array_filter(preg_split('/\s+/', trim($query)));
|
||||
|
||||
foreach ($tokens as $token)
|
||||
{
|
||||
$token = preg_split('/,|\s+/', $token);
|
||||
$orderToken = $token[0];
|
||||
$orderDir = (count($token) === 2 and $token[1] === 'desc')
|
||||
? \Szurubooru\SearchServices\AbstractSearchFilter::ORDER_DESC
|
||||
: \Szurubooru\SearchServices\AbstractSearchFilter::ORDER_ASC;
|
||||
? \Szurubooru\SearchServices\Filters\IFilter::ORDER_DESC
|
||||
: \Szurubooru\SearchServices\Filters\IFilter::ORDER_ASC;
|
||||
|
||||
$orderColumn = $this->getOrderColumn($orderToken);
|
||||
if ($orderColumn === null)
|
||||
|
@ -58,7 +64,7 @@ abstract class AbstractSearchParser
|
|||
{
|
||||
$searchTokens = [];
|
||||
|
||||
foreach (array_filter(preg_split('/\s+/', $query)) as $tokenText)
|
||||
foreach (array_filter(preg_split('/\s+/', trim($query))) as $tokenText)
|
||||
{
|
||||
$negated = false;
|
||||
if (substr($tokenText, 0, 1) === '-')
|
||||
|
|
|
@ -5,7 +5,7 @@ class PostSearchParser extends AbstractSearchParser
|
|||
{
|
||||
protected function createFilter()
|
||||
{
|
||||
return new \Szurubooru\SearchServices\PostSearchFilter;
|
||||
return new \Szurubooru\SearchServices\Filters\PostFilter;
|
||||
}
|
||||
|
||||
protected function decorateFilterFromToken($filter, $token)
|
||||
|
|
|
@ -5,7 +5,7 @@ class UserSearchParser extends AbstractSearchParser
|
|||
{
|
||||
protected function createFilter()
|
||||
{
|
||||
return new \Szurubooru\SearchServices\UserSearchFilter;
|
||||
return new \Szurubooru\SearchServices\Filters\UserFilter;
|
||||
}
|
||||
|
||||
protected function decorateFilterFromToken($filter, $token)
|
||||
|
@ -21,10 +21,10 @@ class UserSearchParser extends AbstractSearchParser
|
|||
protected function getOrderColumn($token)
|
||||
{
|
||||
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']))
|
||||
return \Szurubooru\SearchServices\UserSearchFilter::ORDER_REGISTRATION_TIME;
|
||||
return \Szurubooru\SearchServices\Filters\UserFilter::ORDER_REGISTRATION_TIME;
|
||||
|
||||
return null;
|
||||
}
|
||||
|
|
|
@ -1,6 +0,0 @@
|
|||
<?php
|
||||
namespace Szurubooru\SearchServices;
|
||||
|
||||
class PostSearchFilter extends AbstractSearchFilter
|
||||
{
|
||||
}
|
39
src/SearchServices/Requirement.php
Normal file
39
src/SearchServices/Requirement.php
Normal 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;
|
||||
}
|
||||
}
|
|
@ -1,7 +1,7 @@
|
|||
<?php
|
||||
namespace Szurubooru\SearchServices;
|
||||
|
||||
class PagedSearchResult
|
||||
class Result
|
||||
{
|
||||
public $pageNumber;
|
||||
public $pageSize;
|
||||
|
@ -9,7 +9,7 @@ class PagedSearchResult
|
|||
public $entities;
|
||||
public $totalRecords;
|
||||
|
||||
public function setSearchFilter(AbstractSearchFilter $searchFilter = null)
|
||||
public function setSearchFilter(Filters\IFilter $searchFilter = null)
|
||||
{
|
||||
$this->searchFilter = $searchFilter;
|
||||
}
|
|
@ -1,8 +0,0 @@
|
|||
<?php
|
||||
namespace Szurubooru\SearchServices;
|
||||
|
||||
class UserSearchFilter extends AbstractSearchFilter
|
||||
{
|
||||
const ORDER_NAME = 'name';
|
||||
const ORDER_REGISTRATION_TIME = 'registrationTime';
|
||||
}
|
|
@ -8,7 +8,6 @@ class PostService
|
|||
private $transactionManager;
|
||||
private $postDao;
|
||||
private $globalParamDao;
|
||||
private $postSearchParser;
|
||||
private $timeService;
|
||||
private $authService;
|
||||
private $fileService;
|
||||
|
@ -20,7 +19,6 @@ class PostService
|
|||
\Szurubooru\Dao\TransactionManager $transactionManager,
|
||||
\Szurubooru\Dao\PostDao $postDao,
|
||||
\Szurubooru\Dao\GlobalParamDao $globalParamDao,
|
||||
\Szurubooru\SearchServices\Parsers\PostSearchParser $postSearchParser,
|
||||
\Szurubooru\Services\AuthService $authService,
|
||||
\Szurubooru\Services\TimeService $timeService,
|
||||
\Szurubooru\Services\FileService $fileService,
|
||||
|
@ -31,7 +29,6 @@ class PostService
|
|||
$this->transactionManager = $transactionManager;
|
||||
$this->postDao = $postDao;
|
||||
$this->globalParamDao = $globalParamDao;
|
||||
$this->postSearchParser = $postSearchParser;
|
||||
$this->timeService = $timeService;
|
||||
$this->authService = $authService;
|
||||
$this->fileService = $fileService;
|
||||
|
@ -64,13 +61,11 @@ class PostService
|
|||
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);
|
||||
$searchFilter = $this->postSearchParser->createFilterFromFormData($formData);
|
||||
return $this->postDao->findFilteredAndPaged($searchFilter, $formData->pageNumber, $this->config->posts->postsPerPage);
|
||||
return $this->postDao->findFiltered($filter);
|
||||
};
|
||||
return $this->transactionManager->rollback($transactionFunc);
|
||||
}
|
||||
|
|
|
@ -7,7 +7,6 @@ class UserService
|
|||
private $validator;
|
||||
private $transactionManager;
|
||||
private $userDao;
|
||||
private $userSearchParser;
|
||||
private $passwordService;
|
||||
private $emailService;
|
||||
private $fileService;
|
||||
|
@ -20,7 +19,6 @@ class UserService
|
|||
\Szurubooru\Validator $validator,
|
||||
\Szurubooru\Dao\TransactionManager $transactionManager,
|
||||
\Szurubooru\Dao\UserDao $userDao,
|
||||
\Szurubooru\SearchServices\Parsers\UserSearchParser $userSearchParser,
|
||||
\Szurubooru\Services\PasswordService $passwordService,
|
||||
\Szurubooru\Services\EmailService $emailService,
|
||||
\Szurubooru\Services\FileService $fileService,
|
||||
|
@ -32,7 +30,6 @@ class UserService
|
|||
$this->validator = $validator;
|
||||
$this->transactionManager = $transactionManager;
|
||||
$this->userDao = $userDao;
|
||||
$this->userSearchParser = $userSearchParser;
|
||||
$this->passwordService = $passwordService;
|
||||
$this->emailService = $emailService;
|
||||
$this->fileService = $fileService;
|
||||
|
@ -82,13 +79,11 @@ class UserService
|
|||
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);
|
||||
$searchFilter = $this->userSearchParser->createFilterFromFormData($formData);
|
||||
return $this->userDao->findFilteredAndPaged($searchFilter, $formData->pageNumber, $this->config->users->usersPerPage);
|
||||
return $this->userDao->findFiltered($filter);
|
||||
};
|
||||
return $this->transactionManager->rollback($transactionFunc);
|
||||
}
|
||||
|
|
|
@ -27,9 +27,11 @@ class UserDaoFilterTest extends \Szurubooru\Tests\AbstractDatabaseTestCase
|
|||
|
||||
public function testNothing()
|
||||
{
|
||||
$searchFilter = new \Szurubooru\SearchServices\UserSearchFilter();
|
||||
$searchFilter = new \Szurubooru\SearchServices\Filters\UserFilter();
|
||||
$searchFilter->setPageNumber(1);
|
||||
$searchFilter->setPageSize(2);
|
||||
$userDao = $this->getUserDao();
|
||||
$result = $userDao->findFilteredAndPaged($searchFilter, 1, 2);
|
||||
$result = $userDao->findFiltered($searchFilter);
|
||||
$this->assertEmpty($result->getEntities());
|
||||
$this->assertEquals(0, $result->getTotalRecords());
|
||||
$this->assertEquals(1, $result->getPageNumber());
|
||||
|
@ -51,12 +53,14 @@ class UserDaoFilterTest extends \Szurubooru\Tests\AbstractDatabaseTestCase
|
|||
$expectedUsers[] = $user;
|
||||
}
|
||||
|
||||
$searchFilter = new \Szurubooru\SearchServices\UserSearchFilter();
|
||||
$searchFilter = new \Szurubooru\SearchServices\Filters\UserFilter();
|
||||
$searchFilter->setOrder([
|
||||
\Szurubooru\SearchServices\UserSearchFilter::ORDER_NAME =>
|
||||
\Szurubooru\SearchServices\UserSearchFilter::ORDER_DESC]);
|
||||
\Szurubooru\SearchServices\Filters\UserFilter::ORDER_NAME =>
|
||||
\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($pageNumber, $result->getPageNumber());
|
||||
$this->assertEquals($pageSize, $result->getPageSize());
|
||||
|
@ -73,8 +77,8 @@ class UserDaoFilterTest extends \Szurubooru\Tests\AbstractDatabaseTestCase
|
|||
{
|
||||
list ($user1, $user2) = $this->prepareUsers();
|
||||
$this->doTestSorting(
|
||||
\Szurubooru\SearchServices\UserSearchFilter::ORDER_NAME,
|
||||
\Szurubooru\SearchServices\UserSearchFilter::ORDER_ASC,
|
||||
\Szurubooru\SearchServices\Filters\UserFilter::ORDER_NAME,
|
||||
\Szurubooru\SearchServices\Filters\UserFilter::ORDER_ASC,
|
||||
[$user1, $user2]);
|
||||
}
|
||||
|
||||
|
@ -82,8 +86,8 @@ class UserDaoFilterTest extends \Szurubooru\Tests\AbstractDatabaseTestCase
|
|||
{
|
||||
list ($user1, $user2) = $this->prepareUsers();
|
||||
$this->doTestSorting(
|
||||
\Szurubooru\SearchServices\UserSearchFilter::ORDER_NAME,
|
||||
\Szurubooru\SearchServices\UserSearchFilter::ORDER_DESC,
|
||||
\Szurubooru\SearchServices\Filters\UserFilter::ORDER_NAME,
|
||||
\Szurubooru\SearchServices\Filters\UserFilter::ORDER_DESC,
|
||||
[$user2, $user1]);
|
||||
}
|
||||
|
||||
|
@ -91,8 +95,8 @@ class UserDaoFilterTest extends \Szurubooru\Tests\AbstractDatabaseTestCase
|
|||
{
|
||||
list ($user1, $user2) = $this->prepareUsers();
|
||||
$this->doTestSorting(
|
||||
\Szurubooru\SearchServices\UserSearchFilter::ORDER_REGISTRATION_TIME,
|
||||
\Szurubooru\SearchServices\UserSearchFilter::ORDER_ASC,
|
||||
\Szurubooru\SearchServices\Filters\UserFilter::ORDER_REGISTRATION_TIME,
|
||||
\Szurubooru\SearchServices\Filters\UserFilter::ORDER_ASC,
|
||||
[$user2, $user1]);
|
||||
}
|
||||
|
||||
|
@ -100,8 +104,8 @@ class UserDaoFilterTest extends \Szurubooru\Tests\AbstractDatabaseTestCase
|
|||
{
|
||||
list ($user1, $user2) = $this->prepareUsers();
|
||||
$this->doTestSorting(
|
||||
\Szurubooru\SearchServices\UserSearchFilter::ORDER_REGISTRATION_TIME,
|
||||
\Szurubooru\SearchServices\UserSearchFilter::ORDER_DESC,
|
||||
\Szurubooru\SearchServices\Filters\UserFilter::ORDER_REGISTRATION_TIME,
|
||||
\Szurubooru\SearchServices\Filters\UserFilter::ORDER_DESC,
|
||||
[$user1, $user2]);
|
||||
}
|
||||
|
||||
|
@ -121,16 +125,17 @@ class UserDaoFilterTest extends \Szurubooru\Tests\AbstractDatabaseTestCase
|
|||
private function doTestSorting($order, $orderDirection, $expectedUsers)
|
||||
{
|
||||
$userDao = $this->getUserDao();
|
||||
$searchFilter = new \Szurubooru\SearchServices\UserSearchFilter();
|
||||
$searchFilter = new \Szurubooru\SearchServices\Filters\UserFilter();
|
||||
if ($order !== null)
|
||||
$searchFilter->setOrder([$order => $orderDirection]);
|
||||
|
||||
$result = $userDao->findFilteredAndPaged($searchFilter, 1, 10);
|
||||
$this->assertInstanceOf(\Szurubooru\SearchServices\PagedSearchResult::class, $result);
|
||||
$result = $userDao->findFiltered($searchFilter, 1, 10);
|
||||
$this->assertInstanceOf(\Szurubooru\SearchServices\Result::class, $result);
|
||||
$this->assertEquals($searchFilter, $result->getSearchFilter());
|
||||
$this->assertEntitiesEqual(array_values($expectedUsers), array_values($result->getEntities()));
|
||||
$this->assertEquals(count($expectedUsers), $result->getTotalRecords());
|
||||
$this->assertEquals(1, $result->getPageNumber());
|
||||
$this->assertNull($result->getPageNumber());
|
||||
$this->assertNull($result->getPageSize());
|
||||
}
|
||||
|
||||
private function getUserDao()
|
||||
|
|
|
@ -8,7 +8,6 @@ class PostServiceTest extends \Szurubooru\Tests\AbstractTestCase
|
|||
private $transactionManagerMock;
|
||||
private $postDaoMock;
|
||||
private $globalParamDaoMock;
|
||||
private $postSearchParserMock;
|
||||
private $authServiceMock;
|
||||
private $timeServiceMock;
|
||||
private $fileServiceMock;
|
||||
|
@ -21,7 +20,6 @@ class PostServiceTest extends \Szurubooru\Tests\AbstractTestCase
|
|||
$this->transactionManagerMock = $this->mockTransactionManager();
|
||||
$this->postDaoMock = $this->mock(\Szurubooru\Dao\PostDao::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->timeServiceMock = $this->mock(\Szurubooru\Services\TimeService::class);
|
||||
$this->fileServiceMock = $this->mock(\Szurubooru\Services\FileService::class);
|
||||
|
@ -181,7 +179,6 @@ class PostServiceTest extends \Szurubooru\Tests\AbstractTestCase
|
|||
$this->transactionManagerMock,
|
||||
$this->postDaoMock,
|
||||
$this->globalParamDaoMock,
|
||||
$this->postSearchParserMock,
|
||||
$this->authServiceMock,
|
||||
$this->timeServiceMock,
|
||||
$this->fileServiceMock,
|
||||
|
|
|
@ -7,7 +7,6 @@ final class UserServiceTest extends \Szurubooru\Tests\AbstractTestCase
|
|||
private $validatorMock;
|
||||
private $transactionManagerMock;
|
||||
private $userDaoMock;
|
||||
private $userSearchParserMock;
|
||||
private $passwordServiceMock;
|
||||
private $emailServiceMock;
|
||||
private $fileServiceMock;
|
||||
|
@ -22,7 +21,6 @@ final class UserServiceTest extends \Szurubooru\Tests\AbstractTestCase
|
|||
$this->transactionManagerMock = $this->mockTransactionManager();
|
||||
$this->validatorMock = $this->mock(\Szurubooru\Validator::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->emailServiceMock = $this->mock(\Szurubooru\Services\EmailService::class);
|
||||
$this->fileServiceMock = $this->mock(\Szurubooru\Services\FileService::class);
|
||||
|
@ -67,25 +65,6 @@ final class UserServiceTest extends \Szurubooru\Tests\AbstractTestCase
|
|||
$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()
|
||||
{
|
||||
$formData = new \Szurubooru\FormData\RegistrationFormData;
|
||||
|
@ -293,7 +272,6 @@ final class UserServiceTest extends \Szurubooru\Tests\AbstractTestCase
|
|||
$this->validatorMock,
|
||||
$this->transactionManagerMock,
|
||||
$this->userDaoMock,
|
||||
$this->userSearchParserMock,
|
||||
$this->passwordServiceMock,
|
||||
$this->emailServiceMock,
|
||||
$this->fileServiceMock,
|
||||
|
|
Loading…
Reference in a new issue