Skip to content

Commit

Permalink
Fix DateTimeColumn behavior with default/null values
Browse files Browse the repository at this point in the history
  • Loading branch information
curry684 committed Jan 25, 2018
1 parent 43873b1 commit d83a99f
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 4 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
All notable changes to `omines\datatables-bundle` will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]
Nothing yet.
## [0.1.5] - 2018-01-25
### Fixed
- Fixed inconsistency in DateTimeColumn with default/null values

## [0.1.4] - 2018-01-21
### Added
Expand Down
1 change: 1 addition & 0 deletions docs/source/index.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,7 @@ result. If data of other types is encountered automatic conversion is attempted
Option | Type | Description
------ | ---- | -----------
format | string | A date format string as accepted by the [`date()`](http://php.net/manual/en/function.date.php) function. Default `'c'`.
nullValue | string | Raw string to display for null values. Defaults to the empty string.

## TwigColumn

Expand Down
4 changes: 3 additions & 1 deletion src/Column/DateTimeColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class DateTimeColumn extends AbstractColumn
public function normalize($value)
{
if (null === $value) {
return $this->getData();
return $this->options['nullValue'];
} elseif (!$value instanceof \DateTimeInterface) {
$value = new \DateTime($value);
}
Expand All @@ -45,8 +45,10 @@ protected function configureOptions(OptionsResolver $resolver)
$resolver
->setDefaults([
'format' => 'c',
'nullValue' => '',
])
->setAllowedTypes('format', 'string')
->setAllowedTypes('nullValue', 'string')
;

return $this;
Expand Down
2 changes: 1 addition & 1 deletion tests/Functional/FunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function testPlainDataTable()
$sample = $json->data[5];
$this->assertSame('FirstName94', $sample->firstName);
$this->assertSame('LastName94', $sample->lastName);
$this->assertNull($sample->employedSince);
$this->assertEmpty($sample->employedSince);
$this->assertSame('FirstName94 <img src="https://symfony.com/images/v5/logos/sf-positive.svg"> LastName94', $sample->fullName);
$this->assertRegExp('#href="/employee/[0-9]+"#', $sample->buttons);

Expand Down
13 changes: 13 additions & 0 deletions tests/Unit/ColumnTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
namespace Tests\Unit;

use Omines\DataTablesBundle\Column\BoolColumn;
use Omines\DataTablesBundle\Column\DateTimeColumn;
use Omines\DataTablesBundle\Column\TextColumn;
use Omines\DataTablesBundle\Column\TwigColumn;
use Omines\DataTablesBundle\DataTable;
Expand All @@ -25,6 +26,18 @@
*/
class ColumnTest extends TestCase
{
public function testDateTimeColumn()
{
$column = new DateTimeColumn();
$column->initialize('test', 1, [
'nullValue' => 'foo',
'format' => 'd-m-Y',
], (new DataTable())->setName('foo'));

$this->assertSame('03-04-2015', $column->transform('2015-04-03'));
$this->assertSame('foo', $column->transform(null));
}

public function testTextColumn()
{
$column = new TextColumn();
Expand Down

0 comments on commit d83a99f

Please sign in to comment.