CachedStorageTest.php

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

Namespace

Drupal\KernelTests\Core\Config\Storage

File

core/tests/Drupal/KernelTests/Core/Config/Storage/CachedStorageTest.php

View source
<?php

declare (strict_types=1);
namespace Drupal\KernelTests\Core\Config\Storage;

use Drupal\Core\Config\FileStorage;
use Drupal\Core\Config\CachedStorage;
use Drupal\Core\StreamWrapper\PublicStream;

/**
 * Tests CachedStorage operations.
 *
 * @group config
 */
class CachedStorageTest extends ConfigStorageTestBase {
    
    /**
     * The cache backend the cached storage is using.
     *
     * @var \Drupal\Core\Cache\CacheBackendInterface
     */
    protected $cache;
    
    /**
     * The file storage the cached storage is using.
     *
     * @var \Drupal\Core\Config\FileStorage
     */
    protected $fileStorage;
    
    /**
     * {@inheritdoc}
     */
    protected function setUp() : void {
        parent::setUp();
        // Create a directory.
        $dir = PublicStream::basePath() . '/config';
        $this->fileStorage = new FileStorage($dir);
        $this->storage = new CachedStorage($this->fileStorage, \Drupal::service('cache.config'));
        $this->cache = \Drupal::service('cache_factory')->get('config');
    }
    
    /**
     * {@inheritdoc}
     */
    public function testInvalidStorage() : void {
        $this->markTestSkipped('No-op as this test does not make sense');
    }
    
    /**
     * {@inheritdoc}
     */
    protected function read($name) {
        $data = $this->cache
            ->get($name);
        // Cache misses fall through to the underlying storage.
        return $data ? $data->data : $this->fileStorage
            ->read($name);
    }
    
    /**
     * {@inheritdoc}
     */
    protected function insert($name, $data) {
        $this->fileStorage
            ->write($name, $data);
        $this->cache
            ->set($name, $data);
    }
    
    /**
     * {@inheritdoc}
     */
    protected function update($name, $data) {
        $this->fileStorage
            ->write($name, $data);
        $this->cache
            ->set($name, $data);
    }
    
    /**
     * {@inheritdoc}
     */
    protected function delete($name) {
        $this->cache
            ->delete($name);
        unlink($this->fileStorage
            ->getFilePath($name));
    }

}

Classes

Title Deprecated Summary
CachedStorageTest Tests CachedStorage operations.

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