function DateTest::testFormatInterval

Tests the formatInterval method.

@legacy-covers ::formatInterval

Attributes

#[DataProvider('providerTestFormatInterval')]

File

core/tests/Drupal/Tests/Core/Datetime/DateTest.php, line 107

Class

DateTest
Tests Drupal\Core\Datetime\DateFormatter.

Namespace

Drupal\Tests\Core\Datetime

Code

public function testFormatInterval($interval, $granularity, $expected, $langcode = NULL) : void {
  // Mocks a simple formatPlural implementation.
  $this->stringTranslation
    ->expects($this->any())
    ->method('translateString')
    ->willReturnCallback(function (TranslatableMarkup $arg) {
    return $arg->getUntranslatedString();
  });
  // Check if the granularity is specified.
  if ($granularity) {
    $result = $this->dateFormatter
      ->formatInterval($interval, $granularity, $langcode);
  }
  else {
    $result = $this->dateFormatter
      ->formatInterval($interval);
  }
  // phpcs:ignore Drupal.Semantics.FunctionT.NotLiteralString
  $this->assertEquals(new TranslatableMarkup($expected, [], [
    'langcode' => $langcode,
  ], $this->stringTranslation), $result);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.