You are here

public function DateTest::testFormatTimeDiffSince in Drupal 10

Same name and namespace in other branches
  1. 8 core/tests/Drupal/Tests/Core/Datetime/DateTest.php \Drupal\Tests\Core\Datetime\DateTest::testFormatTimeDiffSince()
  2. 9 core/tests/Drupal/Tests/Core/Datetime/DateTest.php \Drupal\Tests\Core\Datetime\DateTest::testFormatTimeDiffSince()

Tests the formatTimeDiffSince method.

@covers ::formatTimeDiffSince

File

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

Class

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

Namespace

Drupal\Tests\Core\Datetime

Code

public function testFormatTimeDiffSince() {
  $expected = '1 second';
  $timestamp = $this
    ->createTimestamp('2013-12-11 10:09:07');
  $request_time = $this
    ->createTimestamp('2013-12-11 10:09:08');
  $options = [];

  // Mocks the formatDiff function of the dateformatter object.
  $this->dateFormatterStub
    ->expects($this
    ->exactly(2))
    ->method('formatDiff')
    ->willReturnMap([
    [
      $request_time,
      $timestamp,
      $options,
      $expected,
    ],
    [
      $request_time,
      $timestamp,
      $options + [
        'return_as_object' => TRUE,
      ],
      new FormattedDateDiff('1 second', 1),
    ],
  ]);
  $request = Request::createFromGlobals();
  $request->server
    ->set('REQUEST_TIME', $request_time);

  // Mocks a the request stack getting the current request.
  $this->requestStack
    ->expects($this
    ->any())
    ->method('getCurrentRequest')
    ->willReturn($request);
  $this
    ->assertEquals($expected, $this->dateFormatterStub
    ->formatTimeDiffUntil($timestamp, $options));
  $options['return_as_object'] = TRUE;
  $expected_object = new FormattedDateDiff('1 second', 1);
  $this
    ->assertEquals($expected_object, $this->dateFormatterStub
    ->formatTimeDiffUntil($timestamp, $options));
}