Skip to content

Commit 4f24980

Browse files
committed
Fix accidental formatter issue on eloquent
Add formatColumn test for eloquent
1 parent 9840f33 commit 4f24980

File tree

4 files changed

+64
-27
lines changed

4 files changed

+64
-27
lines changed

src/Processors/DataProcessor.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ protected function addColumns(array $data, $row): array
143143
if ($content instanceof Formatter) {
144144
$column = str_replace('_formatted', '', $value['name']);
145145

146+
$value['content'] = $content->format($data[$column], $row);
146147
if (isset($data[$column])) {
147148
$value['content'] = $content->format($data[$column], $row);
148149
}

tests/Formatters/DateFormatter.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Yajra\DataTables\Tests\Formatters;
4+
5+
use Carbon\Carbon;
6+
use DateTime;
7+
use Yajra\DataTables\Contracts\Formatter;
8+
9+
class DateFormatter implements Formatter
10+
{
11+
public string $format;
12+
13+
public function __construct(string $format = 'Y-m-d h:i a')
14+
{
15+
$this->format = $format;
16+
}
17+
18+
public function format($value, $row): string
19+
{
20+
if ($value instanceof DateTime) {
21+
return $value->format($this->format);
22+
}
23+
24+
return $value ? Carbon::parse($value)->format($this->format) : '';
25+
}
26+
}

tests/Integration/EloquentDataTableTest.php

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22

33
namespace Yajra\DataTables\Tests\Integration;
44

5+
use Carbon\Carbon;
56
use Illuminate\Foundation\Testing\DatabaseTransactions;
67
use Illuminate\Http\JsonResponse;
78
use Yajra\DataTables\DataTables;
89
use Yajra\DataTables\EloquentDataTable;
910
use Yajra\DataTables\Facades\DataTables as DatatablesFacade;
11+
use Yajra\DataTables\Tests\Formatters\DateFormatter;
1012
use Yajra\DataTables\Tests\Models\Post;
1113
use Yajra\DataTables\Tests\Models\User;
1214
use 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
}

tests/Integration/QueryDataTableTest.php

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
use Illuminate\Foundation\Testing\DatabaseTransactions;
88
use Illuminate\Http\JsonResponse;
99
use Illuminate\Support\Facades\DB;
10-
use Yajra\DataTables\Contracts\Formatter;
1110
use Yajra\DataTables\DataTables;
1211
use Yajra\DataTables\Facades\DataTables as DatatablesFacade;
1312
use Yajra\DataTables\QueryDataTable;
13+
use Yajra\DataTables\Tests\Formatters\DateFormatter;
1414
use Yajra\DataTables\Tests\Models\User;
1515
use Yajra\DataTables\Tests\TestCase;
1616

@@ -453,22 +453,3 @@ protected function setUp(): void
453453
});
454454
}
455455
}
456-
457-
class DateFormatter implements Formatter
458-
{
459-
protected ?string $format;
460-
461-
public function __construct(string $format = null)
462-
{
463-
$this->format = $format;
464-
}
465-
466-
public function format($value, $row)
467-
{
468-
if ($this->format) {
469-
return Carbon::parse($value)->format($this->format);
470-
}
471-
472-
return Carbon::parse($value)->diffForHumans();
473-
}
474-
}

0 commit comments

Comments
 (0)