Skip to content

Commit

Permalink
Update QueryDataTableTest.php
Browse files Browse the repository at this point in the history
  • Loading branch information
Arkhas committed Apr 12, 2024
1 parent f814ae9 commit b3b1414
Showing 1 changed file with 17 additions and 21 deletions.
38 changes: 17 additions & 21 deletions tests/Unit/QueryDataTableTest.php
Expand Up @@ -74,52 +74,48 @@ public function test_complex_query_can_ignore_select_in_count()
$this->assertQueryHasNoSelect(true, $dataTable->prepareCountQuery());
$this->assertEquals(20, $dataTable->count());
}

public function test_complexe_queries_with_complexe_select_are_wrapped_without_selects()
public function test_simple_queries_with_complexe_select_are_not_wrapped()
{
/** @var \Yajra\DataTables\QueryDataTable $dataTable */
$dataTable = app('datatables')->of(
DB::table('users')
->select(['users.*', DB::raw('count(*) as posts_count')])
DB::table('users')
->select('users.*')
->addSelect([
'last_post_id' => DB::table('posts')
->whereColumn('posts.user_id', 'users.id')
->orderBy('created_at')
->select('id'),
])
->join('posts', 'posts.user_id', 'users.id')
->groupBy('users.id')
);


$this->assertQueryWrapped(true, $dataTable->prepareCountQuery());
$this->assertQueryHasNoSelect(false, $dataTable->prepareCountQuery());
$this->assertQueryWrapped(false, $dataTable->prepareCountQuery());
$this->assertEquals(20, $dataTable->count());
}

public function test_simple_queries_with_complexe_select_are_not_wrapped()
public function test_simple_queries_with_complexe_where_are_not_wrapped()
{
/** @var \Yajra\DataTables\QueryDataTable $dataTable */
$dataTable = app('datatables')->of(
DB::table('users')
->select('users.*')
->addSelect([
'last_post_id' => DB::table('posts')
->where(
DB::table('posts')
->whereColumn('posts.user_id', 'users.id')
->orderBy('created_at')
->select('id'),
])
->select('title'), 'User-1 Post-1'
)
);

$this->assertQueryWrapped(false, $dataTable->prepareCountQuery());
$this->assertEquals(20, $dataTable->count());
$this->assertEquals(1, $dataTable->prepareCountQuery()->count());
}

public function test_simple_queries_with_complexe_where_are_not_wrapped()
public function test_simple_eloquent_queries_with_complexe_where_are_not_wrapped()
{
/** @var \Yajra\DataTables\QueryDataTable $dataTable */
$dataTable = app('datatables')->of(
DB::table('users')
User::query()
->select('users.*')
->where(
DB::table('posts')
Expand Down Expand Up @@ -157,10 +153,10 @@ public function test_complexe_queries_can_be_wrapped_and_countable()

/**
* @param $expected bool
* @param $query \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder
* @param $query \Illuminate\Contracts\Database\Query\Builder
* @return void
*/
protected function assertQueryWrapped($expected, $query)
protected function assertQueryWrapped($expected, $query): void
{
$sql = $query->toSql();

Expand All @@ -169,10 +165,10 @@ protected function assertQueryWrapped($expected, $query)

/**
* @param $expected bool
* @param $query \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder
* @param $query \Illuminate\Contracts\Database\Query\Builder
* @return void
*/
public function assertQueryHasNoSelect($expected, $query)
public function assertQueryHasNoSelect($expected, $query): void
{
$sql = $query->select(DB::raw('count(*)'))->toSql();

Expand Down

0 comments on commit b3b1414

Please sign in to comment.