public function GenericCacheBackendUnitTestBase::testValueTypeIsKept

Tests data type preservation.

File

drupal/core/modules/system/lib/Drupal/system/Tests/Cache/GenericCacheBackendUnitTestBase.php, line 193
Definition of Drupal\system\Tests\Cache\GenericCacheBackendUnitTestBase.

Class

GenericCacheBackendUnitTestBase
Tests any cache backend.

Namespace

Drupal\system\Tests\Cache

Code

public function testValueTypeIsKept() {
  $backend = $this
    ->getCacheBackend();
  $variables = array(
    'test1' => 1,
    'test2' => '0',
    'test3' => '',
    'test4' => 12.64,
    'test5' => FALSE,
    'test6' => array(
      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
      ->assert(is_object($object), sprintf("Backend returned an object for cache id %s.", $cid));
    $this
      ->assertIdentical($value, $object->data, sprintf("Data of cached id %s kept is identical in type and value", $cid));
  }
}