function DrupalDateTimeTest::testSleep

Same name and namespace 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 <a href="/api/drupal/core%21lib%21Drupal%21Core%21Datetime%21DrupalDateTime.php/class/DrupalDateTime/11.x" title="Extends DateTimePlus()." class="local">\Drupal\Core\Datetime\DrupalDateTime</a> @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.