You are here

public function DateTimePlusTest::assertDateTimestamp in Drupal 8

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

Assertion helper for testTimestamp and testDateTimestamp since they need different dataProviders.

@input mixed $input The original input passed to the test method.

Parameters

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

array $initial: @see testTimestamp()

array $transform: @see testTimestamp()

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

File

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

Class

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

Namespace

Drupal\Tests\Component\Datetime

Code

public function assertDateTimestamp($date, $input, $initial, $transform) {

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