function DateTest::testFormatDiff

Same name in other branches
  1. 8.9.x core/tests/Drupal/Tests/Core/Datetime/DateTest.php \Drupal\Tests\Core\Datetime\DateTest::testformatDiff()
  2. 10 core/tests/Drupal/Tests/Core/Datetime/DateTest.php \Drupal\Tests\Core\Datetime\DateTest::testFormatDiff()
  3. 11.x 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 251

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));
}

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