You are here

public function GenericCacheBackendUnitTestBase::testSetMultiple in Drupal 8

Same name in this branch
  1. 8 core/tests/Drupal/KernelTests/Core/Cache/GenericCacheBackendUnitTestBase.php \Drupal\KernelTests\Core\Cache\GenericCacheBackendUnitTestBase::testSetMultiple()
  2. 8 core/modules/system/src/Tests/Cache/GenericCacheBackendUnitTestBase.php \Drupal\system\Tests\Cache\GenericCacheBackendUnitTestBase::testSetMultiple()
Same name and namespace in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Cache/GenericCacheBackendUnitTestBase.php \Drupal\KernelTests\Core\Cache\GenericCacheBackendUnitTestBase::testSetMultiple()
  2. 10 core/tests/Drupal/KernelTests/Core/Cache/GenericCacheBackendUnitTestBase.php \Drupal\KernelTests\Core\Cache\GenericCacheBackendUnitTestBase::testSetMultiple()

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

File

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

Class

GenericCacheBackendUnitTestBase
Tests any cache backend.

Namespace

Drupal\KernelTests\Core\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 = [
    'cid_1' => [
      'data' => 1,
    ],
    'cid_2' => [
      'data' => 2,
    ],
    'cid_3' => [
      'data' => [
        1,
        2,
      ],
    ],
    'cid_4' => [
      'data' => 1,
      'expire' => $future_expiration,
    ],
    'cid_5' => [
      'data' => 1,
      'tags' => [
        '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' => [
        'data' => 1,
        'tags' => [],
      ],
      'exception_test_2' => [
        'data' => 2,
        'tags' => [
          'valid',
        ],
      ],
      'exception_test_3' => [
        'data' => 3,
        'tags' => [
          'node' => [
            3,
            5,
            7,
          ],
        ],
      ],
    ];
    $backend
      ->setMultiple($items);
    $this
      ->fail('::setMultiple() was called with invalid cache tags, but runtime assertion did not fail.');
  } catch (\AssertionError $e) {

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