|
8 | 8 | use Illuminate\Http\JsonResponse;
|
9 | 9 | use Illuminate\Support\Facades\DB;
|
10 | 10 | use PHPUnit\Framework\Attributes\Test;
|
| 11 | +use PHPUnit\Framework\Attributes\TestWith; |
11 | 12 | use Yajra\DataTables\DataTables;
|
12 | 13 | use Yajra\DataTables\Facades\DataTables as DatatablesFacade;
|
13 | 14 | use Yajra\DataTables\QueryDataTable;
|
@@ -406,6 +407,42 @@ public function it_can_return_added_column_with_dependency_injection()
|
406 | 407 | $this->assertEquals($user->name.'_di', $data['name_di']);
|
407 | 408 | }
|
408 | 409 |
|
| 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 | + |
409 | 446 | protected function setUp(): void
|
410 | 447 | {
|
411 | 448 | parent::setUp();
|
@@ -497,5 +534,23 @@ protected function setUp(): void
|
497 | 534 | $router->get('/closure-di', fn (DataTables $dataTable) => $dataTable->query(DB::table('users'))
|
498 | 535 | ->addColumn('name_di', fn ($user, User $u) => $u->newQuery()->find($user->id)->name.'_di')
|
499 | 536 | ->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()); |
500 | 555 | }
|
501 | 556 | }
|
0 commit comments