Skip to content

Commit f99390e

Browse files
authored
Merge pull request #3239 from Seb33300/alias-tests
tests: Add tests to cover prefix detection
2 parents bcf7040 + 0a2a795 commit f99390e

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

tests/Integration/QueryDataTableTest.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Illuminate\Http\JsonResponse;
99
use Illuminate\Support\Facades\DB;
1010
use PHPUnit\Framework\Attributes\Test;
11+
use PHPUnit\Framework\Attributes\TestWith;
1112
use Yajra\DataTables\DataTables;
1213
use Yajra\DataTables\Facades\DataTables as DatatablesFacade;
1314
use Yajra\DataTables\QueryDataTable;
@@ -406,6 +407,42 @@ public function it_can_return_added_column_with_dependency_injection()
406407
$this->assertEquals($user->name.'_di', $data['name_di']);
407408
}
408409

410+
#[Test]
411+
#[TestWith(['title', '"posts"."title"'])] // column from base table wildcard posts.*
412+
#[TestWith(['email', '"users"."email"'])] // column from join table added to select without alias
413+
#[TestWith(['alias_field_with_as', '"alias_field_with_as"'])]
414+
#[TestWith(['alias_field_without_as', '"alias_field_without_as"'])]
415+
#[TestWith(['alias_expression_with_as', '"alias_expression_with_as"'])]
416+
#[TestWith(['alias_expression_without_as', '"alias_expression_without_as"'])]
417+
#[TestWith(['alias_case_with_as', '"alias_case_with_as"'])]
418+
#[TestWith(['alias_case_without_as', '"alias_case_without_as"'])]
419+
#[TestWith(['sub_query', '"sub_query"'])]
420+
public function it_can_detect_column_alias(string $column, string $expected)
421+
{
422+
DB::enableQueryLog();
423+
424+
$crawler = $this->call('GET', '/query/aliases', [
425+
'columns' => [
426+
['data' => $column, 'name' => $column, 'orderable' => 'true'],
427+
],
428+
'order' => [['column' => 0, 'dir' => 'asc']],
429+
]);
430+
431+
$crawler->assertJsonStructure([
432+
'draw',
433+
'recordsTotal',
434+
'recordsFiltered',
435+
]);
436+
437+
DB::disableQueryLog();
438+
$queryLog = DB::getQueryLog();
439+
440+
$this->assertCount(2, $queryLog);
441+
442+
$sql = end($queryLog)['query'];
443+
$this->assertStringContainsString("order by $expected asc", $sql);
444+
}
445+
409446
protected function setUp(): void
410447
{
411448
parent::setUp();
@@ -497,5 +534,23 @@ protected function setUp(): void
497534
$router->get('/closure-di', fn (DataTables $dataTable) => $dataTable->query(DB::table('users'))
498535
->addColumn('name_di', fn ($user, User $u) => $u->newQuery()->find($user->id)->name.'_di')
499536
->toJson());
537+
538+
$router->get('/query/aliases', fn (DataTables $dataTable) => $dataTable->query(
539+
DB::table('posts')
540+
->join('users', 'users.id', '=', 'posts.user_id')
541+
->select([
542+
'posts.*',
543+
'users.email', // From join table, without alias
544+
'posts.id as alias_field_with_as',
545+
DB::raw('posts.id alias_field_without_as'),
546+
DB::raw('(1 + 1) as alias_expression_with_as'),
547+
DB::raw('(SELECT 1) alias_expression_without_as'),
548+
DB::raw('CASE WHEN 1 THEN 1 ELSE 2 END as alias_case_with_as'),
549+
DB::raw('CASE WHEN 0 THEN 1 ELSE 2 END alias_case_without_as'),
550+
'sub_query' => DB::table('users')
551+
->whereColumn('posts.user_id', 'users.id')
552+
->select('name'),
553+
])
554+
)->toJson());
500555
}
501556
}

0 commit comments

Comments
 (0)