Skip to content

Commit

Permalink
Add test for Overriding QueryOrderBuilder()
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyslik committed Sep 10, 2016
1 parent ef153e9 commit 2cf8e44
Showing 1 changed file with 22 additions and 17 deletions.
39 changes: 22 additions & 17 deletions tests/ColumnSortableTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ public function testSortableWithDefaultAndWithoutRequestParameters()
$this->assertEquals($expected, head($resultArray));
}

public function testSortableQueryJoinBuilder() {
public function testSortableQueryJoinBuilder()
{
$query = $this->user->newQuery()->with(['profile']);
$relation = $query->getRelation('profile');
$resultQuery = $this->invokeMethod($this->user, 'queryJoinBuilder', [$query, $relation]);
Expand All @@ -104,11 +105,21 @@ public function testSortableQueryJoinBuilder() {
$this->assertEquals($expectedQuery->toSql(), $resultQuery->toSql());
}

public function testSortableOverridingQueryOrderBuilder()
{
$sortParameters = ['sort' => 'address', 'order' => 'desc'];
$query = $this->user->newQuery();
$resultQuery = $this->invokeMethod($this->user, 'queryOrderBuilder', [$query, $sortParameters]);
$expectedQuery = $this->user->newQuery()->join('profiles', 'users.id', '=', 'profiles.user_id')->orderBy('address', 'desc')->select('users.*');
$this->assertEquals($expectedQuery, $resultQuery);
}

/**
* @expectedException \Exception
* @expectedExceptionCode 0
*/
public function testSortableQueryJoinBuilderThrowsExeption() {
public function testSortableQueryJoinBuilderThrowsExeption()
{
$query = $this->user->hasMany(Profile::class)->newQuery();
$relation = $query->getRelation('profile');
$this->invokeMethod($this->user, 'queryJoinBuilder', [$query, $relation]);
Expand Down Expand Up @@ -224,20 +235,6 @@ protected function invokeMethod(&$object, $methodName, array $parameters = array

return $method->invokeArgs($object, $parameters);
}

/**
* @ignore
*
*/
public function dummy()
{
$this->markTestSkipped();
$query = $this->user->newQuery()->with(['profile']); //query with relation
Input::replace(['sort' => 'profile.phone', 'order' => 'asc']); //replace GET data in request

$d = $this->user->scopeSortable($query); //
dd($query->relation); // get orders that scopeSortable produced or ->toSql() to get raw SQL
}
}

/**
Expand All @@ -263,6 +260,13 @@ public function profile()
{
return $this->hasOne(Profile::class, 'user_id', 'id');
}

public function addressSortable($query, $direction)
{
return $query->join('profiles', 'users.id', '=', 'profiles.user_id')
->orderBy('address', $direction)
->select('users.*');
}
}

/**
Expand All @@ -274,7 +278,8 @@ class Profile extends Model
* @var array
*/
public $sortable = [
'phone'
'phone',
'address'
];

/**
Expand Down

0 comments on commit 2cf8e44

Please sign in to comment.