function GenericCacheBackendUnitTestBase::testValueTypeIsKept

Same name in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Cache/GenericCacheBackendUnitTestBase.php \Drupal\KernelTests\Core\Cache\GenericCacheBackendUnitTestBase::testValueTypeIsKept()
  2. 8.9.x core/modules/system/src/Tests/Cache/GenericCacheBackendUnitTestBase.php \Drupal\system\Tests\Cache\GenericCacheBackendUnitTestBase::testValueTypeIsKept()
  3. 8.9.x core/tests/Drupal/KernelTests/Core/Cache/GenericCacheBackendUnitTestBase.php \Drupal\KernelTests\Core\Cache\GenericCacheBackendUnitTestBase::testValueTypeIsKept()
  4. 10 core/tests/Drupal/KernelTests/Core/Cache/GenericCacheBackendUnitTestBase.php \Drupal\KernelTests\Core\Cache\GenericCacheBackendUnitTestBase::testValueTypeIsKept()

Tests data type preservation.

File

core/tests/Drupal/KernelTests/Core/Cache/GenericCacheBackendUnitTestBase.php, line 277

Class

GenericCacheBackendUnitTestBase
Tests any cache backend.

Namespace

Drupal\KernelTests\Core\Cache

Code

public function testValueTypeIsKept() : void {
    $backend = $this->getCacheBackend();
    $variables = [
        'test1' => 1,
        'test2' => '0',
        'test3' => '',
        'test4' => 12.64,
        'test5' => FALSE,
        'test6' => [
            1,
            2,
            3,
        ],
    ];
    // Create cache entries.
    foreach ($variables as $cid => $data) {
        $backend->set($cid, $data);
    }
    // Retrieve and test cache objects.
    foreach ($variables as $cid => $value) {
        $object = $backend->get($cid);
        $this->assertIsObject($object, sprintf("Backend returned an object for cache id %s.", $cid));
        $this->assertSame($value, $object->data, sprintf("Data of cached id %s kept is identical in type and value", $cid));
    }
}

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