function GenericCacheBackendUnitTestBase::testSetMultiple

Same name in other branches
  1. 8.9.x core/modules/system/src/Tests/Cache/GenericCacheBackendUnitTestBase.php \Drupal\system\Tests\Cache\GenericCacheBackendUnitTestBase::testSetMultiple()
  2. 8.9.x core/tests/Drupal/KernelTests/Core/Cache/GenericCacheBackendUnitTestBase.php \Drupal\KernelTests\Core\Cache\GenericCacheBackendUnitTestBase::testSetMultiple()
  3. 10 core/tests/Drupal/KernelTests/Core/Cache/GenericCacheBackendUnitTestBase.php \Drupal\KernelTests\Core\Cache\GenericCacheBackendUnitTestBase::testSetMultiple()
  4. 11.x 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 390

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->assertEquals($items['cid_1']['data'], $cached['cid_1']->data, 'Over-written cache item set correctly.');
    $this->assertTrue($cached['cid_1']->valid, 'Item is marked as valid.');
    $this->assertGreaterThanOrEqual(REQUEST_TIME, $cached['cid_1']->created);
    $this->assertLessThanOrEqual(round(microtime(TRUE), 3), $cached['cid_1']->created);
    $this->assertEquals(CacheBackendInterface::CACHE_PERMANENT, $cached['cid_1']->expire, 'Cache expiration defaults to permanent.');
    $this->assertEquals($items['cid_2']['data'], $cached['cid_2']->data, 'New cache item set correctly.');
    $this->assertEquals(CacheBackendInterface::CACHE_PERMANENT, $cached['cid_2']->expire, 'Cache expiration defaults to permanent.');
    $this->assertEquals($items['cid_3']['data'], $cached['cid_3']->data, 'New cache item with serialized data set correctly.');
    $this->assertEquals(CacheBackendInterface::CACHE_PERMANENT, $cached['cid_3']->expire, 'Cache expiration defaults to permanent.');
    $this->assertEquals($items['cid_4']['data'], $cached['cid_4']->data, 'New cache item set correctly.');
    $this->assertEquals($future_expiration, $cached['cid_4']->expire, 'Cache expiration has been correctly set.');
    $this->assertEquals($items['cid_5']['data'], $cached['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.
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.