22
33namespace Yajra \DataTables \Tests \Integration ;
44
5+ use Carbon \Carbon ;
56use Illuminate \Foundation \Testing \DatabaseTransactions ;
67use Illuminate \Http \JsonResponse ;
78use Yajra \DataTables \DataTables ;
89use Yajra \DataTables \EloquentDataTable ;
910use Yajra \DataTables \Facades \DataTables as DatatablesFacade ;
11+ use Yajra \DataTables \Tests \Formatters \DateFormatter ;
1012use Yajra \DataTables \Tests \Models \Post ;
1113use Yajra \DataTables \Tests \Models \User ;
1214use Yajra \DataTables \Tests \TestCase ;
@@ -20,8 +22,8 @@ public function it_returns_all_records_when_no_parameters_is_passed()
2022 {
2123 $ crawler = $ this ->call ('GET ' , '/eloquent/users ' );
2224 $ crawler ->assertJson ([
23- 'draw ' => 0 ,
24- 'recordsTotal ' => 20 ,
25+ 'draw ' => 0 ,
26+ 'recordsTotal ' => 20 ,
2527 'recordsFiltered ' => 20 ,
2628 ]);
2729 }
@@ -34,12 +36,12 @@ public function it_can_perform_global_search()
3436 ['data ' => 'name ' , 'name ' => 'name ' , 'searchable ' => 'true ' , 'orderable ' => 'true ' ],
3537 ['data ' => 'email ' , 'name ' => 'email ' , 'searchable ' => 'true ' , 'orderable ' => 'true ' ],
3638 ],
37- 'search ' => ['value ' => 'Record-19 ' ],
39+ 'search ' => ['value ' => 'Record-19 ' ],
3840 ]);
3941
4042 $ crawler ->assertJson ([
41- 'draw ' => 0 ,
42- 'recordsTotal ' => 20 ,
43+ 'draw ' => 0 ,
44+ 'recordsTotal ' => 20 ,
4345 'recordsFiltered ' => 1 ,
4446 ]);
4547 }
@@ -99,18 +101,45 @@ public function it_returns_only_the_selected_columns_with_dotted_notation()
99101 $ this ->assertArrayHasKey ('name ' , $ json ['data ' ][0 ]['user ' ]);
100102 }
101103
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+
102124 protected function setUp (): void
103125 {
104126 parent ::setUp ();
105127
106- $ this ->app ['router ' ]->get ('/eloquent/users ' , function (DataTables $ datatables ) {
128+ $ router = $ this ->app ['router ' ];
129+ $ router ->get ('/eloquent/users ' , function (DataTables $ datatables ) {
107130 return $ datatables ->eloquent (User::query ())->toJson ();
108131 });
109132
110- $ this -> app [ ' router ' ] ->get ('/eloquent/only ' , function (DataTables $ datatables ) {
133+ $ router ->get ('/eloquent/only ' , function (DataTables $ datatables ) {
111134 return $ datatables ->eloquent (Post::with ('user ' ))
112135 ->only (['title ' , 'user.name ' ])
113136 ->toJson ();
114137 });
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+ });
115144 }
116145}
0 commit comments