You are here

public function DateTest::testformatDiff in Drupal 8

Same name and namespace in other branches
  1. 9 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 256

Class

DateTest
@coversDefaultClass \Drupal\Core\Datetime\DateFormatter @group Datetime

Namespace

Drupal\Tests\Core\Datetime

Code

public function testformatDiff($expected, $max_age, $timestamp1, $timestamp2, $options = []) {

  // Mocks a simple translateString implementation.
  $this->stringTranslation
    ->expects($this
    ->any())
    ->method('translateString')
    ->willReturnCallback(function (TranslatableMarkup $arg) {
    return $arg
      ->getUntranslatedString();
  });
  if (isset($options['langcode'])) {
    $expected_markup = new TranslatableMarkup($expected, [], [
      'langcode' => $options['langcode'],
    ], $this->stringTranslation);
  }
  else {
    $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));
}