Skip to content

Commit

Permalink
Optimize default condition order logic
Browse files Browse the repository at this point in the history
Add default order and sorts unit tests
  • Loading branch information
zhichao-poper committed Apr 16, 2020
1 parent 7927a19 commit 18a9949
Show file tree
Hide file tree
Showing 11 changed files with 66 additions and 10 deletions.
6 changes: 4 additions & 2 deletions src/ConditionsGeneratorTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,20 +221,22 @@ protected function handlePaginate()
Arr::forget($this->inputArgs, 'paginator.sorts');
}
$sorts = collect(array_values(array_unique($sorts)))->filter();
$sorts = collect($this->order())->merge($sorts);
$sorts = collect($sorts)->merge($this->order());
$orders = [];
foreach ($sorts as $sort) {
if (is_string($sort)) {
if (! starts_with($sort, ['+', '-'])) {
continue;
}
$orders[substr($sort, 1)] = strpos($sort, '+') === 0 ? 'asc' : 'desc';
$field = substr($sort, 1);
$orders[$field] = !array_key_exists($field, $orders) && strpos($sort, '+') === 0 ? 'asc' : 'desc';
}

if ($sort instanceof Expression) {
$orders[] = $sort;
}
}

$this->appendConditions(['order' => $orders]);

return $this;
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/AdvancedSearchTrait/GetCountTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Tests\Unit;
namespace Tests\Unit\AdvancedSearchTrait;

use Tests\DBTestCase;
use Tests\Utils\Models\User;
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/AdvancedSearchTrait/GetListQueryTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Tests\Unit;
namespace Tests\Unit\AdvancedSearchTrait;

use Tests\DBTestCase;
use Tests\Utils\Models\User;
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/AdvancedSearchTrait/GetListTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Tests\Unit;
namespace Tests\Unit\AdvancedSearchTrait;

use Tests\DBTestCase;
use Tests\Utils\Models\User;
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/AdvancedSearchTrait/MakeComboQueryTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Tests\Unit;
namespace Tests\Unit\AdvancedSearchTrait;

use MatrixLab\LaravelAdvancedSearch\ModelScope;
use Tests\DBTestCase;
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/AdvancedSearchTrait/OrderSearchTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Tests\Unit;
namespace Tests\Unit\AdvancedSearchTrait;

use MatrixLab\LaravelAdvancedSearch\ModelScope;
use Tests\DBTestCase;
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/AdvancedSearchTrait/SimpleLikeSearchTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Tests\Unit;
namespace Tests\Unit\AdvancedSearchTrait;

use Tests\DBTestCase;
use Tests\Utils\Models\User;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Tests\Unit;
namespace Tests\Unit\AdvancedSearchTrait;

use Tests\DBTestCase;
use Tests\Utils\Models\User;
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/AdvancedSearchTrait/WhereSearchTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Tests\Unit;
namespace Tests\Unit\AdvancedSearchTrait;

use MatrixLab\LaravelAdvancedSearch\ModelScope;
use Tests\DBTestCase;
Expand Down
39 changes: 39 additions & 0 deletions tests/Unit/ConditionGeneratorTrait/HandlePaginate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Tests\Unit\ConditionGeneratorTrait;

use Illuminate\Support\Arr;
use Tests\DBTestCase;
use Tests\Utils\FakeRequest;
use Tests\Utils\Models\User;

class HandlePaginate extends DBTestCase
{
public function test_sorts()
{
$fakeRequest = new FakeRequest();
$conditions = $fakeRequest->getConditions([
'paginator' => [
'sorts' => [
'+rank',
'-id',
],
],
]);

$this->assertEquals([
'rank' => 'asc',
'id' => 'desc',
], Arr::get($conditions, 'order'));
}

public function test_order()
{
$fakeRequest = new FakeRequest();
$conditions = $fakeRequest->getConditions([]);

$this->assertEquals([
'id' => 'asc',
], Arr::get($conditions, 'order'));
}
}
15 changes: 15 additions & 0 deletions tests/Utils/FakeRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Tests\Utils;

use MatrixLab\LaravelAdvancedSearch\ConditionsGeneratorTrait;

class FakeRequest
{
use ConditionsGeneratorTrait;

protected function order()
{
return '+id';
}
}

0 comments on commit 18a9949

Please sign in to comment.