Skip to content

Commit

Permalink
refactoring test to use a data provider
Browse files Browse the repository at this point in the history
  • Loading branch information
weaverryan committed May 9, 2022
1 parent 87a5144 commit e9624db
Showing 1 changed file with 25 additions and 23 deletions.
48 changes: 25 additions & 23 deletions Tests/DateTimeFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,32 +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'),
array('+ 5 years', null, 'diff.in.year'),
array('now', null, 'diff.empty'),
);
$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 = $test[1] ? new \Datetime(date('Y-m-d H:i:s', strtotime($test[1]))) : null;
$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 e9624db

Please sign in to comment.