function DateTest::testFormatDiff

Same name and namespace in other branches
  1. 9 core/tests/Drupal/Tests/Core/Datetime/DateTest.php \Drupal\Tests\Core\Datetime\DateTest::testFormatDiff()
  2. 8.9.x core/tests/Drupal/Tests/Core/Datetime/DateTest.php \Drupal\Tests\Core\Datetime\DateTest::testformatDiff()
  3. 10 core/tests/Drupal/Tests/Core/Datetime/DateTest.php \Drupal\Tests\Core\Datetime\DateTest::testFormatDiff()

Tests the formatDiff method.

@dataProvider providerTestFormatDiff

@covers ::formatDiff

File

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

Class

DateTest
@coversDefaultClass \Drupal\Core\Datetime\DateFormatter[[api-linebreak]] @group Datetime

Namespace

Drupal\Tests\Core\Datetime

Code

public function testFormatDiff(string $expected, int $max_age, int $timestamp1, int $timestamp2, array $options = []) : void {
  // Mocks a simple translateString implementation.
  $this->stringTranslation
    ->expects($this->any())
    ->method('translateString')
    ->willReturnCallback(function (TranslatableMarkup $arg) {
    return $arg->getUntranslatedString();
  });
  if (isset($options['langcode'])) {
    // phpcs:ignore Drupal.Semantics.FunctionT.NotLiteralString
    $expected_markup = new TranslatableMarkup($expected, [], [
      'langcode' => $options['langcode'],
    ], $this->stringTranslation);
  }
  else {
    // phpcs:ignore Drupal.Semantics.FunctionT.NotLiteralString
    $expected_markup = new TranslatableMarkup($expected, [], [], $this->stringTranslation);
  }
  $this->assertEquals($expected_markup, $this->dateFormatter
    ->formatDiff($timestamp1, $timestamp2, $options));
  $options['return_as_object'] = TRUE;
  $expected_object = new FormattedDateDiff($expected, $max_age);
  $this->assertEquals($expected_object, $this->dateFormatter
    ->formatDiff($timestamp1, $timestamp2, $options));
}

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