You are here

public function GenericCacheBackendUnitTestBase::testSetGet in Drupal 10

Same name and namespace in other branches
  1. 8 core/tests/Drupal/KernelTests/Core/Cache/GenericCacheBackendUnitTestBase.php \Drupal\KernelTests\Core\Cache\GenericCacheBackendUnitTestBase::testSetGet()
  2. 9 core/tests/Drupal/KernelTests/Core/Cache/GenericCacheBackendUnitTestBase.php \Drupal\KernelTests\Core\Cache\GenericCacheBackendUnitTestBase::testSetGet()

Tests the get and set methods of Drupal\Core\Cache\CacheBackendInterface.

2 methods override GenericCacheBackendUnitTestBase::testSetGet()
ApcuBackendTest::testSetGet in core/tests/Drupal/KernelTests/Core/Cache/ApcuBackendTest.php
Tests the get and set methods of Drupal\Core\Cache\CacheBackendInterface.
DatabaseBackendTest::testSetGet in core/tests/Drupal/KernelTests/Core/Cache/DatabaseBackendTest.php
Tests the get and set methods of Drupal\Core\Cache\CacheBackendInterface.

File

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

Class

GenericCacheBackendUnitTestBase
Tests any cache backend.

Namespace

Drupal\KernelTests\Core\Cache

Code

public function testSetGet() {
  $backend = $this
    ->getCacheBackend();
  $this
    ->assertFalse($backend
    ->get('test1'), "Backend does not contain data for cache id test1.");
  $with_backslash = [
    'foo' => '\\Drupal\\foo\\Bar',
  ];
  $backend
    ->set('test1', $with_backslash);
  $cached = $backend
    ->get('test1');
  $this
    ->assertIsObject($cached);
  $this
    ->assertSame($with_backslash, $cached->data);
  $this
    ->assertTrue($cached->valid, 'Item is marked as valid.');

  // We need to round because microtime may be rounded up in the backend.
  $this
    ->assertGreaterThanOrEqual(REQUEST_TIME, $cached->created);
  $this
    ->assertLessThanOrEqual(round(microtime(TRUE), 3), $cached->created);
  $this
    ->assertEquals(Cache::PERMANENT, $cached->expire, 'Expire time is correct.');
  $this
    ->assertFalse($backend
    ->get('test2'), "Backend does not contain data for cache id test2.");
  $backend
    ->set('test2', [
    'value' => 3,
  ], REQUEST_TIME + 3);
  $cached = $backend
    ->get('test2');
  $this
    ->assertIsObject($cached);
  $this
    ->assertSame([
    'value' => 3,
  ], $cached->data);
  $this
    ->assertTrue($cached->valid, 'Item is marked as valid.');
  $this
    ->assertGreaterThanOrEqual(REQUEST_TIME, $cached->created);
  $this
    ->assertLessThanOrEqual(round(microtime(TRUE), 3), $cached->created);
  $this
    ->assertEquals(REQUEST_TIME + 3, $cached->expire, 'Expire time is correct.');
  $backend
    ->set('test3', 'foobar', REQUEST_TIME - 3);
  $this
    ->assertFalse($backend
    ->get('test3'), 'Invalid item not returned.');
  $cached = $backend
    ->get('test3', TRUE);
  $this
    ->assertIsObject($cached);
  $this
    ->assertFalse($cached->valid, 'Item is marked as valid.');
  $this
    ->assertGreaterThanOrEqual(REQUEST_TIME, $cached->created);
  $this
    ->assertLessThanOrEqual(round(microtime(TRUE), 3), $cached->created);
  $this
    ->assertEquals(REQUEST_TIME - 3, $cached->expire, 'Expire time is correct.');
  $this
    ->assertFalse($backend
    ->get('test4'), "Backend does not contain data for cache id test4.");
  $with_eof = [
    'foo' => "\nEOF\ndata",
  ];
  $backend
    ->set('test4', $with_eof);
  $cached = $backend
    ->get('test4');
  $this
    ->assertIsObject($cached);
  $this
    ->assertSame($with_eof, $cached->data);
  $this
    ->assertTrue($cached->valid, 'Item is marked as valid.');
  $this
    ->assertGreaterThanOrEqual(REQUEST_TIME, $cached->created);
  $this
    ->assertLessThanOrEqual(round(microtime(TRUE), 3), $cached->created);
  $this
    ->assertEquals(Cache::PERMANENT, $cached->expire, 'Expire time is correct.');
  $this
    ->assertFalse($backend
    ->get('test5'), "Backend does not contain data for cache id test5.");
  $with_eof_and_semicolon = [
    'foo' => "\nEOF;\ndata",
  ];
  $backend
    ->set('test5', $with_eof_and_semicolon);
  $cached = $backend
    ->get('test5');
  $this
    ->assertIsObject($cached);
  $this
    ->assertSame($with_eof_and_semicolon, $cached->data);
  $this
    ->assertTrue($cached->valid, 'Item is marked as valid.');
  $this
    ->assertGreaterThanOrEqual(REQUEST_TIME, $cached->created);
  $this
    ->assertLessThanOrEqual(round(microtime(TRUE), 3), $cached->created);
  $this
    ->assertEquals(Cache::PERMANENT, $cached->expire, 'Expire time is correct.');
  $with_variable = [
    'foo' => '$bar',
  ];
  $backend
    ->set('test6', $with_variable);
  $cached = $backend
    ->get('test6');
  $this
    ->assertIsObject($cached);
  $this
    ->assertSame($with_variable, $cached->data);

  // Make sure that a cached object is not affected by changing the original.
  $data = new \stdClass();
  $data->value = 1;
  $data->obj = new \stdClass();
  $data->obj->value = 2;
  $backend
    ->set('test7', $data);
  $expected_data = clone $data;

  // Add a property to the original. It should not appear in the cached data.
  $data->this_should_not_be_in_the_cache = TRUE;
  $cached = $backend
    ->get('test7');
  $this
    ->assertIsObject($cached);
  $this
    ->assertEquals($expected_data, $cached->data);
  $this
    ->assertFalse(isset($cached->data->this_should_not_be_in_the_cache));

  // Add a property to the cache data. It should not appear when we fetch
  // the data from cache again.
  $cached->data->this_should_not_be_in_the_cache = TRUE;
  $fresh_cached = $backend
    ->get('test7');
  $this
    ->assertFalse(isset($fresh_cached->data->this_should_not_be_in_the_cache));

  // Check with a long key.
  $cid = str_repeat('a', 300);
  $backend
    ->set($cid, 'test');
  $this
    ->assertEquals('test', $backend
    ->get($cid)->data);

  // Check that the cache key is case sensitive.
  $backend
    ->set('TEST8', 'value');
  $this
    ->assertEquals('value', $backend
    ->get('TEST8')->data);
  $this
    ->assertFalse($backend
    ->get('test8'));

  // Calling ::set() with invalid cache tags. This should fail an assertion.
  try {
    $backend
      ->set('assertion_test', 'value', Cache::PERMANENT, [
      'node' => [
        3,
        5,
        7,
      ],
    ]);
    $this
      ->fail('::set() was called with invalid cache tags, but runtime assertion did not fail.');
  } catch (\AssertionError $e) {

    // Do nothing; continue testing in extending classes.
  }
}