You are here

trait BcTimestampNormalizerUnixTestTrait in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/rest/tests/src/Functional/BcTimestampNormalizerUnixTestTrait.php \Drupal\Tests\rest\Functional\BcTimestampNormalizerUnixTestTrait

Trait for ResourceTestBase subclasses formatting expected timestamp data.

Hierarchy

File

core/modules/rest/tests/src/Functional/BcTimestampNormalizerUnixTestTrait.php, line 10

Namespace

Drupal\Tests\rest\Functional
View source
trait BcTimestampNormalizerUnixTestTrait {

  /**
   * Formats a UNIX timestamp.
   *
   * @param int $timestamp
   *   The timestamp value to format.
   *
   * @return string
   *   The formatted RFC3339 date string.
   *
   * @see \Drupal\serialization\Normalizer\TimestampItemNormalizer
   */
  protected function formatExpectedTimestampItemValues($timestamp) {
    $date = new \DateTime();
    $date
      ->setTimestamp($timestamp);

    // Per \Drupal\Core\TypedData\Plugin\DataType\Timestamp::getDateTime(), they
    // default to string representations in the UTC timezone.
    $date
      ->setTimezone(new \DateTimeZone('UTC'));

    // Format is also added to the expected return values.
    return [
      'value' => $date
        ->format(\DateTime::RFC3339),
      'format' => \DateTime::RFC3339,
    ];
  }

}

Members