Skip to content

Commit

Permalink
Support for multi orderBy
Browse files Browse the repository at this point in the history
  • Loading branch information
imanghafoori1 committed Dec 2, 2022
1 parent 351d790 commit 4922886
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 21 deletions.
30 changes: 12 additions & 18 deletions src/FakeDB.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,19 +130,9 @@ public static function performJoins($base, $joins)
return collect($base);
}

public static function sort($column, $collection, $sortBy, $isDate)
public static function sort($column, $collection)
{
if (! $isDate) {
$sortBy = ($sortBy === 'desc' ? 'sortByDesc' : 'sortBy');

return $collection->$sortBy($column);
}

return $collection->sort(function ($item1, $item2) use ($column, $sortBy) {
$direction = ($sortBy === 'desc' ? 1 : -1);

return (strtotime($item2[$column]) <=> strtotime($item1[$column])) * $direction;
});
return $collection->sortBy($column);
}

private static function parseSelects($columns, $selects)
Expand Down Expand Up @@ -399,7 +389,7 @@ private static function applyBasicWheres($collection, $table, array $orWheres)
});
}

public static function filter($query, string $from, $joins, $columns, $selects, $offset, $limit, $orderBy, $shuffle, $dates)
public static function filter($query, string $from, $joins, $columns, $selects, $offset, $limit, $orderBy, $shuffle)
{
$base = FakeDB::$fakeRows[$from] ?? [];
$collection = FakeDB::performJoins($base, $joins);
Expand All @@ -408,7 +398,7 @@ public static function filter($query, string $from, $joins, $columns, $selects,
$orderBy[$i]['column'] = FakeDB::prefixColumn($_order['column'], $from, $joins);
}

$orderBy && ($collection = self::sortRows($collection, $orderBy, $dates, $shuffle));
$orderBy && ($collection = self::sortRows($collection, $orderBy, $shuffle));

if (! FakeDB::$ignoreWheres) {
$collection = FakeDB::applyWheres($query, $collection);
Expand All @@ -423,12 +413,16 @@ public static function filter($query, string $from, $joins, $columns, $selects,
return $collection;
}

public static function sortRows($collection, $orderBy, $dates, $shuffle)
public static function sortRows($collection, $orderBy, $shuffle)
{
if ($orderBy) {
$column = $orderBy[0]['column'];
$isDates = in_array($column, $dates);
$collection = FakeDB::sort($column, $collection, $orderBy[0]['direction'], $isDates);
foreach ($orderBy as $ord) {
$column = $ord['column'];
$orderBy = $ord['direction'];
$order[] = [$column, $orderBy];
}

$collection = FakeDB::sort($order, $collection);
} elseif ($shuffle !== false) {
$collection->shuffle($shuffle[1]);
}
Expand Down
3 changes: 1 addition & 2 deletions src/FakeQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ public function filterRows($sort = true, $columns = ['*'])
$this->offset,
$this->limit,
$sort ? $this->orders : null,
$this->shuffle,
$this->dates
$this->shuffle
);
}

Expand Down
40 changes: 39 additions & 1 deletion tests/OrderByTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Imanghafoori\EloquentMockery\Tests;

use Illuminate\Database\Eloquent\Model;
use Imanghafoori\EloquentMockery\FakeDB;
use Imanghafoori\EloquentMockery\MockableModel;
use PHPUnit\Framework\TestCase;

Expand All @@ -13,9 +14,15 @@ class OrderUser extends Model

class OrderByTest extends TestCase
{
public function setUp(): void
{
FakeDB::mockQueryBuilder();
}

public function tearDown(): void
{
OrderUser::stopFaking();
FakeDB::dontMockQueryBuilder();
}

/**
Expand All @@ -41,8 +48,39 @@ public function orderBy()
/**
* @test
*/
public function reorderBy()
public function multiOrderBy()
{
OrderUser::addFakeRow(['id' => 1, 'name' => 'a', 'age' => 30,]);
OrderUser::addFakeRow(['id' => 2, 'name' => 'a', 'age' => 20,]);
OrderUser::addFakeRow(['id' => 3, 'name' => 'b', 'age' => 31,]);
OrderUser::addFakeRow(['id' => 4, 'name' => 'b', 'age' => 21,]);

$users = OrderUser::query()
->orderBy('name', 'desc')
->orderBy('age', 'asc')
->get();
$user = $users[0];
$this->assertEquals(4, $user->id);
$this->assertEquals('b', $user->name);
$user = $users[1];
$this->assertEquals(3, $user->id);
$this->assertEquals('b', $user->name);
$user = $users[2];
$this->assertEquals(2, $user->id);
$this->assertEquals('a', $user->name);
$user = $users[3];
$this->assertEquals(1, $user->id);
$this->assertEquals('a', $user->name);
}

/**
* @test
*/
public function reorder()
{
if (! method_exists(OrderUser::query()->getQuery(), 'reorder')) {
$this->markTestSkipped('reorder does not exist in this laravel version.');
}
OrderUser::addFakeRow(['id' => 1, 'name' => 'Hello', 'age' => 40,]);
OrderUser::addFakeRow(['id' => 2, 'name' => 'Iman 2', 'age' => 30,]);
OrderUser::addFakeRow(['id' => 3, 'name' => 'a Iman 3', 'age' => 34,]);
Expand Down

0 comments on commit 4922886

Please sign in to comment.