Skip to content

Commit 469a4bc

Browse files
authored
Create DeepRelationTest.php
1 parent 6662926 commit 469a4bc

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
namespace Yajra\DataTables\Tests\Integration;
4+
5+
use Yajra\DataTables\DataTables;
6+
use Yajra\DataTables\Tests\TestCase;
7+
use Yajra\DataTables\Tests\Models\Post;
8+
use Yajra\DataTables\Tests\Models\User;
9+
use Illuminate\Foundation\Testing\DatabaseTransactions;
10+
11+
class DeepRelationTest extends TestCase
12+
{
13+
use DatabaseTransactions;
14+
15+
/** @test */
16+
public function it_returns_all_records_with_the_relation_when_called_without_parameters()
17+
{
18+
$response = $this->getJsonResponse();
19+
$response->assertJson([
20+
'draw' => 0,
21+
'recordsTotal' => 60,
22+
'recordsFiltered' => 60,
23+
]);
24+
25+
$this->assertCount(60, $response->json()['data']);
26+
}
27+
28+
/** @test */
29+
public function it_can_perform_global_search_on_the_relation()
30+
{
31+
$response = $this->getJsonResponse([
32+
'search' => ['value' => '[email protected]'],
33+
]);
34+
35+
$response->assertJson([
36+
'draw' => 0,
37+
'recordsTotal' => 60,
38+
'recordsFiltered' => 3,
39+
]);
40+
41+
$this->assertCount(3, $response->json()['data']);
42+
}
43+
44+
protected function setUp(): void
45+
{
46+
parent::setUp();
47+
48+
$this->app['router']->get('/relations/deep', function (DataTables $datatables) {
49+
$query = Post::with('user.roles')->select('posts.*');
50+
51+
return $datatables
52+
->eloquent($query)
53+
->toJson();
54+
});
55+
}
56+
57+
protected function getJsonResponse(array $params = [])
58+
{
59+
$data = [
60+
'columns' => [
61+
['data' => 'user.roles.name', 'name' => 'user.roles.role', 'searchable' => 'true', 'orderable' => 'true'],
62+
['data' => 'user.name', 'name' => 'user.name', 'searchable' => 'true', 'orderable' => 'true'],
63+
['data' => 'user.email', 'name' => 'user.email', 'searchable' => 'true', 'orderable' => 'true'],
64+
['data' => 'title', 'name' => 'title', 'searchable' => 'true', 'orderable' => 'true'],
65+
],
66+
];
67+
68+
return $this->call('GET', '/relations/deep', array_merge($data, $params));
69+
}
70+
}

0 commit comments

Comments
 (0)