public function testFormatAsInterval()
{
// Now
$this->assertEquals(_('Now'), Time::now()->formatAsInterval());
$this->assertEquals(_('Now'), Time::get('-8 seconds')->formatAsInterval());
$this->assertEquals(_('Now'), Time::get('+8 seconds')->formatAsInterval());
$this->assertNotEquals(_('Now'), Time::get('-10 seconds')->formatAsInterval());
$this->assertNotEquals(_('Now'), Time::get('+10 seconds')->formatAsInterval());
// Most intervals
foreach (['minute', 'hour', 'day', 'year'] as $period) {
foreach ([1, 2] as $count) {
$this->assertEquals(sprintf(ngettext(sprintf('%s ago', ucfirst($period)), sprintf('%%s %ss ago', $period), $count), $count), Time::get(sprintf('-%s %s', $count, $period))->formatAsInterval());
$this->assertEquals(sprintf(ngettext(sprintf('%s', ucfirst($period)), sprintf('%%s %ss', $period), $count), $count), Time::get(sprintf('+%s %s', $count, $period))->formatAsInterval(false));
$this->assertEquals(sprintf(ngettext(sprintf('In %s', $period), sprintf('In %%s %ss', $period), $count), $count), Time::get(sprintf('+%s %s', $count, $period))->formatAsInterval());
}
}
// Months have to be tested separately because of Feb, which is shorter
foreach ([1, 2] as $count) {
try {
$actual = Time::get(sprintf('-%s month', $count))->formatAsInterval();
$this->assertEquals(sprintf(ngettext('Month ago', '%s months ago', $count), $count), $actual);
} catch (\PHPUnit_Framework_ExpectationFailedException $e) {
$this->assertEquals(sprintf(ngettext('Week ago', '%s weeks ago', 4), 4), $actual);
}
try {
$actual = Time::get(sprintf('+%s month', $count))->formatAsInterval(false);
$this->assertEquals(sprintf(ngettext('Month', '%s months', $count), $count), $actual);
} catch (\PHPUnit_Framework_ExpectationFailedException $e) {
$this->assertEquals(sprintf(ngettext('Week', '%s weeks', 4), 4), $actual);
}
try {
$actual = Time::get(sprintf('+%s month', $count))->formatAsInterval();
$this->assertEquals(sprintf(ngettext('In month', 'In %s months', $count), $count), $actual);
} catch (\PHPUnit_Framework_ExpectationFailedException $e) {
$this->assertEquals(sprintf(ngettext('In week', 'In %s weeks', 4), 4), $actual);
}
}
foreach ([1, 2] as $count) {
try {
$actual = Time::get(sprintf('-%s week', $count))->formatAsInterval();
$this->assertEquals(sprintf(ngettext('Week ago', '%s weeks ago', $count), $count), $actual);
} catch (\PHPUnit_Framework_ExpectationFailedException $e) {
$this->assertEquals(sprintf(ngettext('Day ago', '%s days ago', 7), 7), $actual);
}
try {
$actual = Time::get(sprintf('+%s week', $count))->formatAsInterval(false);
$this->assertEquals(sprintf(ngettext('Week', '%s weeks', $count), $count), $actual);
} catch (\PHPUnit_Framework_ExpectationFailedException $e) {
$this->assertEquals(sprintf(ngettext('Day', '%s days', 7), 7), $actual);
}
try {
$actual = Time::get(sprintf('+%s week', $count))->formatAsInterval();
$this->assertEquals(sprintf(ngettext('In week', 'In %s weeks', $count), $count), $actual);
} catch (\PHPUnit_Framework_ExpectationFailedException $e) {
$this->assertEquals(sprintf(ngettext('In day', 'In %s days', 7), 7), $actual);
}
}
// Seconds have to be tested separately because of "now"
$this->assertEquals(sprintf(ngettext('Second ago', '%s seconds ago', 20), 20), Time::get('-20 seconds')->formatAsInterval());
$this->assertEquals(sprintf(ngettext('Second', '%s seconds', 20), 20), Time::get('+20 seconds')->formatAsInterval(false));
$this->assertEquals(sprintf(ngettext('In second', 'In %s seconds', 20), 20), Time::get('+20 seconds')->formatAsInterval());
}