szurubooru/tests/SearchServices/UserSearchParserTest.php

84 lines
3.1 KiB
PHP
Raw Normal View History

2014-10-01 23:04:32 +02:00
<?php
namespace Szurubooru\Tests\SearchService;
use \Szurubooru\Helpers\InputReader;
use \Szurubooru\Search\Filters\UserFilter;
2015-11-25 11:39:32 +01:00
use \Szurubooru\Search\ParserConfigs\UserSearchParserConfig;
use \Szurubooru\Search\SearchParser;
use \Szurubooru\Tests\AbstractTestCase;
2014-10-01 23:04:32 +02:00
final class UserSearchParserTest extends AbstractTestCase
2014-10-01 23:04:32 +02:00
{
2015-11-25 09:48:03 +01:00
private $inputReader;
2015-11-25 11:39:32 +01:00
private $searchParser;
2014-10-01 23:04:32 +02:00
2015-11-25 09:48:03 +01:00
public function setUp()
{
parent::setUp();
$this->inputReader = new InputReader;
2015-11-25 11:39:32 +01:00
$this->searchParser = new SearchParser(new UserSearchParserConfig);
2015-11-25 09:48:03 +01:00
}
2014-10-01 23:04:32 +02:00
2015-11-25 09:48:03 +01:00
public function testDefaultOrder()
{
2015-11-25 11:39:32 +01:00
$filter = $this->searchParser->createFilterFromInputReader($this->inputReader);
2015-11-25 09:48:03 +01:00
$this->assertOrderEquals([UserFilter::ORDER_NAME => UserFilter::ORDER_ASC], $filter->getOrder());
}
2014-10-01 23:04:32 +02:00
2015-11-25 09:48:03 +01:00
public function testInvalidOrder()
{
$this->inputReader->order = 'invalid,desc';
$this->setExpectedException(\Exception::class);
2015-11-25 11:39:32 +01:00
$filter = $this->searchParser->createFilterFromInputReader($this->inputReader);
2015-11-25 09:48:03 +01:00
}
2015-11-25 09:48:03 +01:00
public function testInvalidOrderDirection()
{
$this->inputReader->order = 'name,invalid';
$this->setExpectedException(\Exception::class);
2015-11-25 11:39:32 +01:00
$filter = $this->searchParser->createFilterFromInputReader($this->inputReader);
2015-11-25 09:48:03 +01:00
}
2015-11-25 09:48:03 +01:00
public function testParamOrder()
{
$this->inputReader->order = 'name,desc';
2015-11-25 11:39:32 +01:00
$filter = $this->searchParser->createFilterFromInputReader($this->inputReader);
2015-11-25 09:48:03 +01:00
$this->assertOrderEquals([UserFilter::ORDER_NAME => UserFilter::ORDER_DESC], $filter->getOrder());
}
2014-10-01 22:23:18 +02:00
2015-11-25 09:48:03 +01:00
public function testTokenOverwriteDefaultOrder()
{
$this->inputReader->query = 'order:name,desc';
2015-11-25 11:39:32 +01:00
$filter = $this->searchParser->createFilterFromInputReader($this->inputReader);
2015-11-25 09:48:03 +01:00
$this->assertOrderEquals([UserFilter::ORDER_NAME => UserFilter::ORDER_DESC], $filter->getOrder());
}
2014-10-03 19:47:55 +02:00
2015-11-25 09:48:03 +01:00
public function testTokenOrder()
{
2015-11-25 11:39:32 +01:00
$this->inputReader->query = 'order:creation_time,desc';
$filter = $this->searchParser->createFilterFromInputReader($this->inputReader);
2015-11-25 09:48:03 +01:00
$this->assertOrderEquals([
2015-11-25 11:39:32 +01:00
UserFilter::ORDER_CREATION_TIME => UserFilter::ORDER_DESC,
2015-11-25 09:48:03 +01:00
UserFilter::ORDER_NAME => UserFilter::ORDER_ASC],
$filter->getOrder());
}
2014-10-01 22:23:18 +02:00
2015-11-25 09:48:03 +01:00
public function testParamAndTokenOrder()
{
$this->inputReader->order = 'name,desc';
2015-11-25 11:39:32 +01:00
$this->inputReader->query = 'order:creation_time,desc';
$filter = $this->searchParser->createFilterFromInputReader($this->inputReader);
2015-11-25 09:48:03 +01:00
$this->assertOrderEquals([
2015-11-25 11:39:32 +01:00
UserFilter::ORDER_CREATION_TIME => UserFilter::ORDER_DESC,
2015-11-25 09:48:03 +01:00
UserFilter::ORDER_NAME => UserFilter::ORDER_DESC],
$filter->getOrder());
}
2014-10-03 19:47:55 +02:00
2015-11-25 09:48:03 +01:00
private function assertOrderEquals($expected, $actual)
{
$this->assertEquals($expected, $actual);
//also test associative array's key order - something that PHPUnit doesn't seem to do
$this->assertEquals(array_values($expected), array_values($actual));
$this->assertEquals(array_keys($expected), array_keys($actual));
}
2014-10-01 23:04:32 +02:00
}