public function GenericCacheBackendUnitTestBase::testSetGet in Drupal 8
Same name in this branch
- 8 core/tests/Drupal/KernelTests/Core/Cache/GenericCacheBackendUnitTestBase.php \Drupal\KernelTests\Core\Cache\GenericCacheBackendUnitTestBase::testSetGet()
- 8 core/modules/system/src/Tests/Cache/GenericCacheBackendUnitTestBase.php \Drupal\system\Tests\Cache\GenericCacheBackendUnitTestBase::testSetGet()
Same name and namespace in other branches
- 9 core/tests/Drupal/KernelTests/Core/Cache/GenericCacheBackendUnitTestBase.php \Drupal\KernelTests\Core\Cache\GenericCacheBackendUnitTestBase::testSetGet()
- 10 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 calls to 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.
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\CacheCode
public function testSetGet() {
$backend = $this
->getCacheBackend();
$this
->assertSame(FALSE, $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
->assertTrue($cached->created >= REQUEST_TIME && $cached->created <= round(microtime(TRUE), 3), 'Created time is correct.');
$this
->assertEqual($cached->expire, Cache::PERMANENT, 'Expire time is correct.');
$this
->assertSame(FALSE, $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
->assertTrue($cached->created >= REQUEST_TIME && $cached->created <= round(microtime(TRUE), 3), 'Created time is correct.');
$this
->assertEqual($cached->expire, REQUEST_TIME + 3, '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
->assertTrue($cached->created >= REQUEST_TIME && $cached->created <= round(microtime(TRUE), 3), 'Created time is correct.');
$this
->assertEqual($cached->expire, REQUEST_TIME - 3, 'Expire time is correct.');
$this
->assertSame(FALSE, $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
->assertTrue($cached->created >= REQUEST_TIME && $cached->created <= round(microtime(TRUE), 3), 'Created time is correct.');
$this
->assertEqual($cached->expire, Cache::PERMANENT, 'Expire time is correct.');
$this
->assertSame(FALSE, $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
->assertTrue($cached->created >= REQUEST_TIME && $cached->created <= round(microtime(TRUE), 3), 'Created time is correct.');
$this
->assertEqual($cached->expire, Cache::PERMANENT, '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
->assertEqual($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
->assertEqual('test', $backend
->get($cid)->data);
// Check that the cache key is case sensitive.
$backend
->set('TEST8', 'value');
$this
->assertEqual('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.
}
}