From 0049d5935488f83702894e776f3e973bb6fd1110 Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Fri, 3 Oct 2014 19:47:55 +0200 Subject: [PATCH] Fixed managing default search order --- .../Parsers/AbstractSearchParser.php | 4 +-- tests/SearchServices/UserSearchParserTest.php | 32 +++++++++++++++---- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/src/SearchServices/Parsers/AbstractSearchParser.php b/src/SearchServices/Parsers/AbstractSearchParser.php index d13c4549..f3490a93 100644 --- a/src/SearchServices/Parsers/AbstractSearchParser.php +++ b/src/SearchServices/Parsers/AbstractSearchParser.php @@ -9,7 +9,7 @@ abstract class AbstractSearchParser public function createFilterFromInputReader(\Szurubooru\Helpers\InputReader $inputReader) { $filter = $this->createFilter(); - $filter->setOrder(array_merge($filter->getOrder(), $this->getOrder($inputReader->order))); + $filter->setOrder($this->getOrder($inputReader->order) + $filter->getOrder()); if ($inputReader->page) { @@ -24,7 +24,7 @@ abstract class AbstractSearchParser if ($token instanceof \Szurubooru\SearchServices\Tokens\NamedSearchToken) { if ($token->getKey() === 'order') - $filter->setOrder(array_merge($filter->getOrder(), $this->getOrder($token->getValue()))); + $filter->setOrder($this->getOrder($token->getValue()) + $filter->getOrder()); else $this->decorateFilterFromNamedToken($filter, $token); } diff --git a/tests/SearchServices/UserSearchParserTest.php b/tests/SearchServices/UserSearchParserTest.php index fa5c744b..e6b16bac 100644 --- a/tests/SearchServices/UserSearchParserTest.php +++ b/tests/SearchServices/UserSearchParserTest.php @@ -21,7 +21,7 @@ class UserSearchParserTest extends AbstractTestCase public function testDefaultOrder() { $filter = $this->userSearchParser->createFilterFromInputReader($this->inputReader); - $this->assertEquals([UserFilter::ORDER_NAME => UserFilter::ORDER_ASC], $filter->getOrder()); + $this->assertOrderEquals([UserFilter::ORDER_NAME => UserFilter::ORDER_ASC], $filter->getOrder()); } public function testInvalidOrder() @@ -42,24 +42,42 @@ class UserSearchParserTest extends AbstractTestCase { $this->inputReader->order = 'name,desc'; $filter = $this->userSearchParser->createFilterFromInputReader($this->inputReader); - $this->assertEquals([UserFilter::ORDER_NAME => UserFilter::ORDER_DESC], $filter->getOrder()); + $this->assertOrderEquals([UserFilter::ORDER_NAME => UserFilter::ORDER_DESC], $filter->getOrder()); + } + + public function testTokenOverwriteDefaultOrder() + { + $this->inputReader->query = 'order:name,desc'; + $filter = $this->userSearchParser->createFilterFromInputReader($this->inputReader); + $this->assertOrderEquals([UserFilter::ORDER_NAME => UserFilter::ORDER_DESC], $filter->getOrder()); } public function testTokenOrder() { - $this->inputReader->query = 'order:name,desc'; + $this->inputReader->query = 'order:registration_time,desc'; $filter = $this->userSearchParser->createFilterFromInputReader($this->inputReader); - $this->assertEquals([UserFilter::ORDER_NAME => UserFilter::ORDER_DESC], $filter->getOrder()); + $this->assertOrderEquals([ + UserFilter::ORDER_REGISTRATION_TIME => UserFilter::ORDER_DESC, + UserFilter::ORDER_NAME => UserFilter::ORDER_ASC], + $filter->getOrder()); } public function testParamAndTokenOrder() { - $this->inputReader->order = 'registration_time,desc'; - $this->inputReader->query = 'order:name,desc'; + $this->inputReader->order = 'name,desc'; + $this->inputReader->query = 'order:registration_time,desc'; $filter = $this->userSearchParser->createFilterFromInputReader($this->inputReader); - $this->assertEquals([ + $this->assertOrderEquals([ UserFilter::ORDER_REGISTRATION_TIME => UserFilter::ORDER_DESC, UserFilter::ORDER_NAME => UserFilter::ORDER_DESC], $filter->getOrder()); } + + 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)); + } }