Skip to content

Commit

Permalink
Merge pull request #9 from ghonijee/development
Browse files Browse the repository at this point in the history
Update sorted by relation field & Pagination test
  • Loading branch information
ghonijee authored Nov 4, 2021
2 parents 5936c15 + 0c68e75 commit c95d0c9
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 8 deletions.
2 changes: 2 additions & 0 deletions src/Builders/SortByQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ protected function buildSortQuery()
$this->query->orderBy(
$relationModel::select($item->field)
->whereColumn($tableRelation . '.' . $relation->getOwnerKeyName(), $tableNameFrom . '.' . $relation->getForeignKeyName()) // targetTable , FromTable
,
$item->type
);
} else {
$this->query->orderBy($item->field, $item->type);
Expand Down
22 changes: 17 additions & 5 deletions tests/Feature/PaginationTest.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,32 @@
<?php

use GhoniJee\DxAdapter\DxAdapter;
use GhoniJee\DxAdapter\Models\TestComment;
use GhoniJee\DxAdapter\Models\TestModel;
use Illuminate\Http\Request;

beforeEach(function () {
$this->request = Request::create('/test');
});

test('can build query get data with pagination', function () {
$sort = ['desc' => false, 'selector' => 'name'];
test('can build query get data with pagination with sortBy', function () {
$sort = ['desc' => false, 'selector' => 'comment'];

$this->request->replace(['sort' => $sort, 'skip' => 2, "take" => 2]);

$this->request->replace(['sort' => $sort]);
$query = DxAdapter::for(TestComment::class, $this->request)->paginate(2);
$queryExpectation = TestComment::orderBy('comment')->paginate(2);

expect($this->request->all())->toHaveKey('page', 2);
expect($query)->toEqual($queryExpectation);
});

test('can build query get data with pagination', function () {
$this->request->replace(['skip' => 2, "take" => 2]);

$query = DxAdapter::for(TestModel::class, $this->request)->paginate();
$queryExpectation = TestModel::orderBy('name')->paginate();
$query = DxAdapter::for(TestComment::class, $this->request)->paginate(2);
$queryExpectation = TestComment::paginate(2);

expect($this->request->all())->toHaveKey('page', 2);
expect($query)->toEqual($queryExpectation);
});
41 changes: 38 additions & 3 deletions tests/Feature/SortByTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,24 +66,59 @@
});

test('can build query for order by relation fieldname with select', function () {
$sort = ['desc' => true, 'selector' => 'test.name'];
$sort = ['desc' => false, 'selector' => 'test.name'];
$select = ['test_model_id', 'comment'];

$this->request->replace(['sort' => $sort, 'select' => $select]);

$result = DxAdapter::load(TestComment::query()->with('test'), $this->request)->get();

$expected = TestComment::select(['comment', 'test_model_id'])->with('test')->orderBy(
TestModel::select('name')->whereColumn('test_models.id', 'test_comments.test_model_id')
)->get();

expect($result)->toEqual($expected);
});

test('can build query for order by relation fieldname without select', function () {
$sort = ['desc' => false, 'selector' => 'test.name'];

$this->request->replace(['sort' => $sort]);

$result = DxAdapter::for(TestComment::class, $this->request)->get();
$expected = TestComment::orderBy(
TestModel::select('name')->whereColumn('test_models.id', 'test_comments.test_model_id')
)->get();
expect($result)->toEqual($expected);
});

test('can build query for order by relation fieldname with select DESC', function () {
$sort = ['desc' => true, 'selector' => 'test.name'];
$select = ['test_model_id', 'comment'];

$this->request->replace(['sort' => $sort, 'select' => $select]);

$result = DxAdapter::load(TestComment::query()->with('test'), $this->request)->get();

$expected = TestComment::select(['comment', 'test_model_id'])->with('test')->orderBy(
TestModel::select('name')->whereColumn('test_models.id', 'test_comments.test_model_id'),
'DESC'
)->get();

expect($result)->toEqual($expected);
});

test('can build query for order by relation fieldname without select DESC', function () {
$sort = ['desc' => true, 'selector' => 'test.name'];

$this->request->replace(['sort' => $sort]);

$result = DxAdapter::for(TestComment::class, $this->request)->first();
$expected = TestComment::where('id', 4)->first();
$result = DxAdapter::for(TestComment::class, $this->request)->get();

$expected = TestComment::orderBy(
TestModel::select('name')->whereColumn('test_models.id', 'test_comments.test_model_id'),
'DESC'
)->get();

expect($result)->toEqual($expected);
});

0 comments on commit c95d0c9

Please sign in to comment.