function DrupalDateTimeTest::testSleep

Same name in other branches
  1. 10 core/tests/Drupal/Tests/Core/Datetime/DrupalDateTimeTest.php \Drupal\Tests\Core\Datetime\DrupalDateTimeTest::testSleep()

Test to avoid serialization of formatTranslationCache.

File

core/tests/Drupal/Tests/Core/Datetime/DrupalDateTimeTest.php, line 277

Class

DrupalDateTimeTest
@coversDefaultClass \Drupal\Core\Datetime\DrupalDateTime @group Datetime

Namespace

Drupal\Tests\Core\Datetime

Code

public function testSleep() : void {
    $tz = new \DateTimeZone(date_default_timezone_get());
    $date = new DrupalDateTime('now', $tz, [
        'langcode' => 'en',
    ]);
    // Override timestamp before serialize.
    $date->setTimestamp(12345678);
    $vars = $date->__sleep();
    $this->assertContains('langcode', $vars);
    $this->assertContains('dateTimeObject', $vars);
    $this->assertNotContains('formatTranslationCache', $vars);
    $unserialized_date = unserialize(serialize($date));
    $this->assertSame(12345678, $unserialized_date->getTimestamp());
}

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