function DateTest::testFormatTimeDiffUntil

Same name in other branches
  1. 9 core/tests/Drupal/Tests/Core/Datetime/DateTest.php \Drupal\Tests\Core\Datetime\DateTest::testFormatTimeDiffUntil()
  2. 10 core/tests/Drupal/Tests/Core/Datetime/DateTest.php \Drupal\Tests\Core\Datetime\DateTest::testFormatTimeDiffUntil()
  3. 11.x core/tests/Drupal/Tests/Core/Datetime/DateTest.php \Drupal\Tests\Core\Datetime\DateTest::testFormatTimeDiffUntil()

Tests the formatTimeDiffUntil method.

@covers ::formatTimeDiffUntil

File

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

Class

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

Namespace

Drupal\Tests\Core\Datetime

Code

public function testFormatTimeDiffUntil() {
    $expected = '1 second';
    $request_time = $this->createTimestamp('2013-12-11 10:09:08');
    $timestamp = $this->createTimestamp('2013-12-11 10:09:09');
    $options = [];
    // Mocks the formatDiff function of the dateformatter object.
    $this->dateFormatterStub
        ->expects($this->at(0))
        ->method('formatDiff')
        ->with($timestamp, $request_time, $options)
        ->will($this->returnValue($expected));
    $this->dateFormatterStub
        ->expects($this->at(1))
        ->method('formatDiff')
        ->with($timestamp, $request_time, $options + [
        'return_as_object' => TRUE,
    ])
        ->will($this->returnValue(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
        ->formatTimeDiffSince($timestamp, $options));
    $options['return_as_object'] = TRUE;
    $expected_object = new FormattedDateDiff('1 second', 1);
    $this->assertEquals($expected_object, $this->dateFormatterStub
        ->formatTimeDiffSince($timestamp, $options));
}

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