forked from yajra/laravel-datatables-buttons
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataTableServiceTest.php
116 lines (90 loc) · 3.41 KB
/
DataTableServiceTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php
namespace Yajra\DataTables\Buttons\Tests;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Http\Response;
use Illuminate\Support\Collection;
use PHPUnit\Framework\Attributes\Test;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Yajra\DataTables\Buttons\Tests\DataTables\UsersDataTable;
use Yajra\DataTables\Buttons\Tests\Models\User;
use Yajra\DataTables\EloquentDataTable;
use Yajra\DataTables\Services\DataTable;
class DataTableServiceTest extends TestCase
{
use DatabaseTransactions;
#[Test]
public function it_can_handle_ajax_request(): void
{
$response = $this->getAjax('/users');
$response->assertJson([
'draw' => 0,
'recordsTotal' => 20,
'recordsFiltered' => 20,
]);
}
#[Test]
public function it_returns_view_on_normal_get_request(): void
{
$response = $this->get('users');
$response->assertSeeText('users-table');
$response->assertSeeText('LaravelDataTables');
}
#[Test]
public function it_can_return_a_csv_file(): void
{
$response = $this->get('users?action=csv');
$this->assertInstanceOf(BinaryFileResponse::class, $response->baseResponse);
}
#[Test]
public function it_can_return_a_xls_file(): void
{
$response = $this->get('users?action=excel');
$this->assertInstanceOf(BinaryFileResponse::class, $response->baseResponse);
}
#[Test]
public function it_can_return_a_pdf_file(): void
{
$response = $this->get('users?action=pdf');
$this->assertInstanceOf(Response::class, $response->baseResponse);
}
#[Test]
public function it_allows_before_response_callback(): void
{
$response = $this->getAjax('users/before');
$response->assertOk();
$row = $response['data'][0];
$this->assertEquals($row['name'].'X', $row['nameX']);
}
#[Test]
public function it_allows_response_callback(): void
{
$response = $this->getAjax('users/response');
$response->assertOk();
$this->assertEquals(2, $response->json('recordsTotal'));
$this->assertEquals(1, $response->json('recordsFiltered'));
}
#[Test]
public function it_is_macroable(): void
{
$dataTable = new class extends DataTable {};
$this->assertObjectHasProperty('macros', $dataTable);
$this->assertTrue(method_exists($dataTable, 'macro'), 'Method macro does not exist.');
$this->assertTrue(method_exists($dataTable, 'mixin'), 'Method mixin does not exist.');
DataTable::macro('macroMethod', fn () => 'macro');
$this->assertEquals('macro', $dataTable->macroMethod());
}
protected function setUp(): void
{
parent::setUp();
$router = $this->app['router'];
$router->get('/users', fn (UsersDataTable $dataTable) => $dataTable->render('tests::users'));
$router->get('/users/before', fn (UsersDataTable $dataTable) => $dataTable->before(function (EloquentDataTable $dataTable) {
$dataTable->addColumn('nameX', fn (User $user) => $user->name.'X');
})->render('tests::users'));
$router->get('/users/response', fn (UsersDataTable $dataTable) => $dataTable->response(function (Collection $data) {
$data['recordsTotal'] = 2;
$data['recordsFiltered'] = 1;
return $data;
})->render('tests::users'));
}
}