2
2
3
3
namespace Yajra \DataTables \Tests \Integration ;
4
4
5
+ use Carbon \Carbon ;
5
6
use Illuminate \Foundation \Testing \DatabaseTransactions ;
6
7
use Illuminate \Http \JsonResponse ;
7
8
use Yajra \DataTables \DataTables ;
8
9
use Yajra \DataTables \EloquentDataTable ;
9
10
use Yajra \DataTables \Facades \DataTables as DatatablesFacade ;
11
+ use Yajra \DataTables \Tests \Formatters \DateFormatter ;
10
12
use Yajra \DataTables \Tests \Models \Post ;
11
13
use Yajra \DataTables \Tests \Models \User ;
12
14
use Yajra \DataTables \Tests \TestCase ;
@@ -20,8 +22,8 @@ public function it_returns_all_records_when_no_parameters_is_passed()
20
22
{
21
23
$ crawler = $ this ->call ('GET ' , '/eloquent/users ' );
22
24
$ crawler ->assertJson ([
23
- 'draw ' => 0 ,
24
- 'recordsTotal ' => 20 ,
25
+ 'draw ' => 0 ,
26
+ 'recordsTotal ' => 20 ,
25
27
'recordsFiltered ' => 20 ,
26
28
]);
27
29
}
@@ -34,12 +36,12 @@ public function it_can_perform_global_search()
34
36
['data ' => 'name ' , 'name ' => 'name ' , 'searchable ' => 'true ' , 'orderable ' => 'true ' ],
35
37
['data ' => 'email ' , 'name ' => 'email ' , 'searchable ' => 'true ' , 'orderable ' => 'true ' ],
36
38
],
37
- 'search ' => ['value ' => 'Record-19 ' ],
39
+ 'search ' => ['value ' => 'Record-19 ' ],
38
40
]);
39
41
40
42
$ crawler ->assertJson ([
41
- 'draw ' => 0 ,
42
- 'recordsTotal ' => 20 ,
43
+ 'draw ' => 0 ,
44
+ 'recordsTotal ' => 20 ,
43
45
'recordsFiltered ' => 1 ,
44
46
]);
45
47
}
@@ -99,18 +101,45 @@ public function it_returns_only_the_selected_columns_with_dotted_notation()
99
101
$ this ->assertArrayHasKey ('name ' , $ json ['data ' ][0 ]['user ' ]);
100
102
}
101
103
104
+ /** @test */
105
+ public function it_can_return_formatted_columns ()
106
+ {
107
+ $ crawler = $ this ->call ('GET ' , '/eloquent/formatColumn ' );
108
+
109
+ $ crawler ->assertJson ([
110
+ 'draw ' => 0 ,
111
+ 'recordsTotal ' => 20 ,
112
+ 'recordsFiltered ' => 20 ,
113
+ ]);
114
+
115
+ $ user = User::find (1 );
116
+ $ data = $ crawler ->json ('data ' )[0 ];
117
+
118
+ $ this ->assertTrue (isset ($ data ['created_at ' ]));
119
+ $ this ->assertTrue (isset ($ data ['created_at_formatted ' ]));
120
+
121
+ $ this ->assertEquals (Carbon::parse ($ user ->created_at )->format ('Y-m-d ' ), $ data ['created_at_formatted ' ]);
122
+ }
123
+
102
124
protected function setUp (): void
103
125
{
104
126
parent ::setUp ();
105
127
106
- $ this ->app ['router ' ]->get ('/eloquent/users ' , function (DataTables $ datatables ) {
128
+ $ router = $ this ->app ['router ' ];
129
+ $ router ->get ('/eloquent/users ' , function (DataTables $ datatables ) {
107
130
return $ datatables ->eloquent (User::query ())->toJson ();
108
131
});
109
132
110
- $ this -> app [ ' router ' ] ->get ('/eloquent/only ' , function (DataTables $ datatables ) {
133
+ $ router ->get ('/eloquent/only ' , function (DataTables $ datatables ) {
111
134
return $ datatables ->eloquent (Post::with ('user ' ))
112
135
->only (['title ' , 'user.name ' ])
113
136
->toJson ();
114
137
});
138
+
139
+ $ router ->get ('/eloquent/formatColumn ' , function (DataTables $ dataTable ) {
140
+ return $ dataTable ->eloquent (User::query ())
141
+ ->formatColumn ('created_at ' , new DateFormatter ('Y-m-d ' ))
142
+ ->toJson ();
143
+ });
115
144
}
116
145
}
0 commit comments