You are here

public function GenericCacheBackendUnitTestBase::testSetMultiple in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/modules/system/src/Tests/Cache/GenericCacheBackendUnitTestBase.php \Drupal\system\Tests\Cache\GenericCacheBackendUnitTestBase::testSetMultiple()

Tests \Drupal\Core\Cache\CacheBackendInterface::setMultiple().

1 call to GenericCacheBackendUnitTestBase::testSetMultiple()
ApcuBackendUnitTest::testSetMultiple in core/modules/system/src/Tests/Cache/ApcuBackendUnitTest.php
Tests \Drupal\Core\Cache\CacheBackendInterface::setMultiple().
1 method overrides GenericCacheBackendUnitTestBase::testSetMultiple()
ApcuBackendUnitTest::testSetMultiple in core/modules/system/src/Tests/Cache/ApcuBackendUnitTest.php
Tests \Drupal\Core\Cache\CacheBackendInterface::setMultiple().

File

core/modules/system/src/Tests/Cache/GenericCacheBackendUnitTestBase.php, line 381
Contains \Drupal\system\Tests\Cache\GenericCacheBackendUnitTestBase.

Class

GenericCacheBackendUnitTestBase
Tests any cache backend.

Namespace

Drupal\system\Tests\Cache

Code

public function testSetMultiple() {
  $backend = $this
    ->getCacheBackend();
  $future_expiration = REQUEST_TIME + 100;

  // Set multiple testing keys.
  $backend
    ->set('cid_1', 'Some other value');
  $items = array(
    'cid_1' => array(
      'data' => 1,
    ),
    'cid_2' => array(
      'data' => 2,
    ),
    'cid_3' => array(
      'data' => array(
        1,
        2,
      ),
    ),
    'cid_4' => array(
      'data' => 1,
      'expire' => $future_expiration,
    ),
    'cid_5' => array(
      'data' => 1,
      'tags' => array(
        'test:a',
        'test:b',
      ),
    ),
  );
  $backend
    ->setMultiple($items);
  $cids = array_keys($items);
  $cached = $backend
    ->getMultiple($cids);
  $this
    ->assertEqual($cached['cid_1']->data, $items['cid_1']['data'], 'Over-written cache item set correctly.');
  $this
    ->assertTrue($cached['cid_1']->valid, 'Item is marked as valid.');
  $this
    ->assertTrue($cached['cid_1']->created >= REQUEST_TIME && $cached['cid_1']->created <= round(microtime(TRUE), 3), 'Created time is correct.');
  $this
    ->assertEqual($cached['cid_1']->expire, CacheBackendInterface::CACHE_PERMANENT, 'Cache expiration defaults to permanent.');
  $this
    ->assertEqual($cached['cid_2']->data, $items['cid_2']['data'], 'New cache item set correctly.');
  $this
    ->assertEqual($cached['cid_2']->expire, CacheBackendInterface::CACHE_PERMANENT, 'Cache expiration defaults to permanent.');
  $this
    ->assertEqual($cached['cid_3']->data, $items['cid_3']['data'], 'New cache item with serialized data set correctly.');
  $this
    ->assertEqual($cached['cid_3']->expire, CacheBackendInterface::CACHE_PERMANENT, 'Cache expiration defaults to permanent.');
  $this
    ->assertEqual($cached['cid_4']->data, $items['cid_4']['data'], 'New cache item set correctly.');
  $this
    ->assertEqual($cached['cid_4']->expire, $future_expiration, 'Cache expiration has been correctly set.');
  $this
    ->assertEqual($cached['cid_5']->data, $items['cid_5']['data'], 'New cache item set correctly.');

  // Calling ::setMultiple() with invalid cache tags. This should fail an
  // assertion.
  try {
    $items = [
      'exception_test_1' => array(
        'data' => 1,
        'tags' => [],
      ),
      'exception_test_2' => array(
        'data' => 2,
        'tags' => [
          'valid',
        ],
      ),
      'exception_test_3' => array(
        'data' => 3,
        'tags' => [
          'node' => [
            3,
            5,
            7,
          ],
        ],
      ),
    ];
    $backend
      ->setMultiple($items);
    $this
      ->fail('::setMultiple() was called with invalid cache tags, runtime assertion did not fail.');
  } catch (\AssertionError $e) {
    $this
      ->pass('::setMultiple() was called with invalid cache tags, runtime assertion failed.');
  }
}