CacheCollectorTest.php

Same filename in this branch
  1. 8.9.x core/tests/Drupal/Tests/Core/Cache/CacheCollectorTest.php
Same filename in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Cache/CacheCollectorTest.php
  2. 9 core/tests/Drupal/Tests/Core/Cache/CacheCollectorTest.php
  3. 10 core/tests/Drupal/KernelTests/Core/Cache/CacheCollectorTest.php
  4. 10 core/tests/Drupal/Tests/Core/Cache/CacheCollectorTest.php
  5. 11.x core/tests/Drupal/KernelTests/Core/Cache/CacheCollectorTest.php
  6. 11.x core/tests/Drupal/Tests/Core/Cache/CacheCollectorTest.php

Namespace

Drupal\KernelTests\Core\Cache

File

core/tests/Drupal/KernelTests/Core/Cache/CacheCollectorTest.php

View source
<?php

namespace Drupal\KernelTests\Core\Cache;

use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\KernelTests\KernelTestBase;
use Drupal\Tests\Core\Cache\CacheCollectorHelper;
use Symfony\Component\DependencyInjection\Reference;

/**
 * Tests DatabaseBackend cache tag implementation.
 *
 * @group Cache
 */
class CacheCollectorTest extends KernelTestBase {
    
    /**
     * {@inheritdoc}
     */
    public function register(ContainerBuilder $container) {
        parent::register($container);
        // Change container to database cache backends.
        $container->register('cache_factory', 'Drupal\\Core\\Cache\\CacheFactory')
            ->addArgument(new Reference('settings'))
            ->addMethodCall('setContainer', [
            new Reference('service_container'),
        ]);
        // Change container to use database lock backends.
        $container->register('lock', 'Drupal\\Core\\Lock\\DatabaseLockBackend')
            ->addArgument(new Reference('database'));
    }
    
    /**
     * Tests setting and invalidating
     *
     * @dataProvider providerTestInvalidCharacters
     */
    public function testCacheCollector($cid, $key, $value) {
        $collector = new CacheCollectorHelper($cid, $this->container
            ->get('cache.default'), $this->container
            ->get('lock'));
        $this->assertNull($collector->get($key));
        $collector->set($key, $value);
        $this->assertEquals($value, $collector->get($key));
        $collector->destruct();
        // @todo Shouldn't this be empty after destruction?
        $this->assertEquals($value, $collector->get($key));
    }
    
    /**
     * Data provider for ::testCacheCollector().
     */
    public function providerTestInvalidCharacters() {
        return [
            // Nothing special.
[
                'foo',
                'bar',
                'baz',
            ],
            // Invalid characters in CID.
[
                'éøïвβ中國書۞',
                'foo',
                'bar',
            ],
            // Really long CID.
[
                $this->randomString(1024),
                'foo',
                'bar',
            ],
        ];
    }

}

Classes

Title Deprecated Summary
CacheCollectorTest Tests DatabaseBackend cache tag implementation.

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