function DateTimePlusTest::assertDateTimestamp

Same name in other branches
  1. 9 core/tests/Drupal/Tests/Component/Datetime/DateTimePlusTest.php \Drupal\Tests\Component\Datetime\DateTimePlusTest::assertDateTimestamp()
  2. 8.9.x core/tests/Drupal/Tests/Component/Datetime/DateTimePlusTest.php \Drupal\Tests\Component\Datetime\DateTimePlusTest::assertDateTimestamp()
  3. 10 core/tests/Drupal/Tests/Component/Datetime/DateTimePlusTest.php \Drupal\Tests\Component\Datetime\DateTimePlusTest::assertDateTimestamp()

Asserts a DateTimePlus value.

@internal

Parameters

\Drupal\Component\Datetime\DateTimePlus $date: DateTimePlus to test.

string|int $input: The original input passed to the test method.

array $initial: @see testTimestamp()

array $transform: @see testTimestamp()

2 calls to DateTimePlusTest::assertDateTimestamp()
DateTimePlusTest::testDateTimestamp in core/tests/Drupal/Tests/Component/Datetime/DateTimePlusTest.php
Tests creating dates from datetime strings.
DateTimePlusTest::testTimestamp in core/tests/Drupal/Tests/Component/Datetime/DateTimePlusTest.php
Tests creating dates from timestamps, and manipulating timezones.

File

core/tests/Drupal/Tests/Component/Datetime/DateTimePlusTest.php, line 200

Class

DateTimePlusTest
@coversDefaultClass \Drupal\Component\Datetime\DateTimePlus @group Datetime

Namespace

Drupal\Tests\Component\Datetime

Code

public function assertDateTimestamp(DateTimePlus $date, string|int $input, array $initial, array $transform) : void {
    // Check format.
    $value = $date->format($initial['format']);
    $this->assertEquals($initial['expected_date'], $value, sprintf("Test new DateTimePlus(%s, %s): should be %s, found %s.", $input, $initial['timezone'], $initial['expected_date'], $value));
    // Check timezone name.
    $value = $date->getTimeZone()
        ->getName();
    $this->assertEquals($initial['expected_timezone'], $value, sprintf("The current timezone is %s: should be %s.", $value, $initial['expected_timezone']));
    // Check offset.
    $value = $date->getOffset();
    $this->assertEquals($initial['expected_offset'], $value, sprintf("The current offset is %s: should be %s.", $value, $initial['expected_offset']));
    // Transform the date to another timezone.
    $date->setTimezone(new \DateTimeZone($transform['timezone']));
    // Check transformed format.
    $value = $date->format($transform['format']);
    $this->assertEquals($transform['expected_date'], $value, sprintf("Test \$date->setTimezone(new \\DateTimeZone(%s)): should be %s, found %s.", $transform['timezone'], $transform['expected_date'], $value));
    // Check transformed timezone.
    $value = $date->getTimeZone()
        ->getName();
    $this->assertEquals($transform['expected_timezone'], $value, sprintf("The current timezone should be %s, found %s.", $transform['expected_timezone'], $value));
    // Check transformed offset.
    $value = $date->getOffset();
    $this->assertEquals($transform['expected_offset'], $value, sprintf("The current offset should be %s, found %s.", $transform['expected_offset'], $value));
}

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