Skip to content

Commit

Permalink
Merge pull request #169 from weaverryan/optional-to-arg
Browse files Browse the repository at this point in the history
[DX] Make "to" argument optional
  • Loading branch information
jrushlow authored May 10, 2022
2 parents c38faf6 + e9624db commit b47ff3b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 22 deletions.
6 changes: 5 additions & 1 deletion DateTimeFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@ public function __construct(TranslatorInterface $translator)
/**
* Returns a formatted diff for the given from and to datetimes
*/
public function formatDiff(DateTimeInterface $from, DateTimeInterface $to, string $locale = null): string
public function formatDiff(DateTimeInterface $from, DateTimeInterface $to = null, string $locale = null): string
{
if (null === $to) {
$to = new \DateTimeImmutable('now');
}

static $units = array(
'y' => 'year',
'm' => 'month',
Expand Down
46 changes: 25 additions & 21 deletions Tests/DateTimeFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,30 +22,34 @@ public function setUp(): void
$this->formatter = new DateTimeFormatter($translator);
}

public function testFormatDiff(): void
/**
* @dataProvider getFormatDiffTests
*/
public function testFormatDiff(string $fromString, ?string $toString, string $expected): void
{
$tests = array(
array('- 5 years', 'now', 'diff.ago.year'),
array('- 10 months', 'now', 'diff.ago.month'),
array('- 15 days', 'now', 'diff.ago.day'),
array('- 20 hours', 'now', 'diff.ago.hour'),
array('- 25 minutes', 'now', 'diff.ago.minute'),
array('- 30 seconds', 'now', 'diff.ago.second'),
array('now', 'now', 'diff.empty'),
array('+ 30 seconds', 'now', 'diff.in.second'),
array('+ 25 minutes', 'now', 'diff.in.minute'),
array('+ 20 hours', 'now', 'diff.in.hour'),
array('+ 15 days', 'now', 'diff.in.day'),
array('+ 10 months', 'now', 'diff.in.month'),
array('+ 5 years', 'now', 'diff.in.year')
);
$from = new \DatetimeImmutable(date('Y-m-d H:i:s', strtotime($fromString)));
$to = $toString !== null ? new \Datetime(date('Y-m-d H:i:s', strtotime($toString))) : null;

foreach ($tests as $test) {
$from = new \DatetimeImmutable(date('Y-m-d H:i:s', strtotime($test[0])));
$to = new \Datetime(date('Y-m-d H:i:s', strtotime($test[1])));
$this->assertSame($expected, $this->formatter->formatDiff($from, $to));
}

$this->assertEquals($test[2], $this->formatter->formatDiff($from, $to));
}
public function getFormatDiffTests(): \Generator
{
yield array('- 5 years', 'now', 'diff.ago.year');
yield array('- 10 months', 'now', 'diff.ago.month');
yield array('- 15 days', 'now', 'diff.ago.day');
yield array('- 20 hours', 'now', 'diff.ago.hour');
yield array('- 25 minutes', 'now', 'diff.ago.minute');
yield array('- 30 seconds', 'now', 'diff.ago.second');
yield array('now', 'now', 'diff.empty');
yield array('+ 30 seconds', 'now', 'diff.in.second');
yield array('+ 25 minutes', 'now', 'diff.in.minute');
yield array('+ 20 hours', 'now', 'diff.in.hour');
yield array('+ 15 days', 'now', 'diff.in.day');
yield array('+ 10 months', 'now', 'diff.in.month');
yield array('+ 5 years', 'now', 'diff.in.year');
yield array('+ 5 years', null, 'diff.in.year');
yield array('now', null, 'diff.empty');
}

public function testGetDiffMessage(): void
Expand Down

0 comments on commit b47ff3b

Please sign in to comment.