class ChainedFastBackendTest

Same name in this branch
  1. 11.x core/tests/Drupal/KernelTests/Core/Cache/ChainedFastBackendTest.php \Drupal\KernelTests\Core\Cache\ChainedFastBackendTest
Same name and namespace in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Cache/ChainedFastBackendTest.php \Drupal\KernelTests\Core\Cache\ChainedFastBackendTest
  2. 9 core/tests/Drupal/Tests/Core/Cache/ChainedFastBackendTest.php \Drupal\Tests\Core\Cache\ChainedFastBackendTest
  3. 8.9.x core/tests/Drupal/KernelTests/Core/Cache/ChainedFastBackendTest.php \Drupal\KernelTests\Core\Cache\ChainedFastBackendTest
  4. 8.9.x core/tests/Drupal/Tests/Core/Cache/ChainedFastBackendTest.php \Drupal\Tests\Core\Cache\ChainedFastBackendTest
  5. 10 core/tests/Drupal/KernelTests/Core/Cache/ChainedFastBackendTest.php \Drupal\KernelTests\Core\Cache\ChainedFastBackendTest
  6. 10 core/tests/Drupal/Tests/Core/Cache/ChainedFastBackendTest.php \Drupal\Tests\Core\Cache\ChainedFastBackendTest

@coversDefaultClass \Drupal\Core\Cache\ChainedFastBackend
@group Cache

Hierarchy

Expanded class hierarchy of ChainedFastBackendTest

File

core/tests/Drupal/Tests/Core/Cache/ChainedFastBackendTest.php, line 17

Namespace

Drupal\Tests\Core\Cache
View source
class ChainedFastBackendTest extends UnitTestCase {
  
  /**
   * The consistent cache backend.
   *
   * @var \Drupal\Core\Cache\CacheBackendInterface
   */
  protected $consistentCache;
  
  /**
   * The fast cache backend.
   *
   * @var \Drupal\Core\Cache\CacheBackendInterface
   */
  protected $fastCache;
  
  /**
   * The cache bin.
   *
   * @var string
   */
  protected $bin;
  
  /**
   * Tests a get() on the fast backend, with no hit on the consistent backend.
   */
  public function testGetDoesNotHitConsistentBackend() : void {
    $consistent_cache = $this->createMock('Drupal\\Core\\Cache\\CacheBackendInterface');
    $timestamp_cid = ChainedFastBackend::LAST_WRITE_TIMESTAMP_PREFIX . 'cache_foo';
    // Use the request time because that is what we will be comparing against.
    $timestamp_item = (object) [
      'cid' => $timestamp_cid,
      'data' => (int) $_SERVER['REQUEST_TIME'] - 60,
    ];
    $consistent_cache->expects($this->once())
      ->method('get')
      ->with($timestamp_cid)
      ->willReturn($timestamp_item);
    $consistent_cache->expects($this->never())
      ->method('getMultiple');
    $fast_cache = new MemoryBackend(new Time());
    $fast_cache->set('foo', 'baz');
    $chained_fast_backend = new ChainedFastBackend($consistent_cache, $fast_cache, 'foo');
    $this->assertEquals('baz', $chained_fast_backend->get('foo')->data);
  }
  
  /**
   * Tests a get() on consistent backend without saving on fast backend.
   */
  public function testSetInvalidDataFastBackend() : void {
    $cid = $this->randomString();
    $item = (object) [
      'cid' => $cid,
      'data' => serialize($this->randomObject()),
      'created' => ChainedFastBackend::LAST_WRITE_TIMESTAMP_PREFIX . 'cache_foo',
      'expire' => Cache::PERMANENT,
      'tags' => [],
      'valid' => FALSE,
    ];
    $consistent_cache = $this->createMock('Drupal\\Core\\Cache\\CacheBackendInterface');
    $consistent_cache->expects($this->once())
      ->method('get')
      ->withAnyParameters()
      ->willReturn(FALSE);
    $consistent_cache->expects($this->once())
      ->method('getMultiple')
      ->withAnyParameters()
      ->willReturn([
      $item,
    ]);
    $fast_cache = new MemoryBackend(new Time());
    $chained_fast_backend = new ChainedFastBackend($consistent_cache, $fast_cache, 'foo');
    // Perform a get using the allowing invalid data parameter.
    $this->assertEquals($item, $chained_fast_backend->get($cid, TRUE));
    // Perform a get directly on the fast cache to guarantee the invalid data
    // were not saved there.
    $this->assertEquals(NULL, $fast_cache->get($cid), 'Invalid data was not saved on the fast cache.');
  }
  
  /**
   * Tests a fast cache miss gets data from the consistent cache backend.
   */
  public function testFallThroughToConsistentCache() : void {
    $timestamp_item = (object) [
      'cid' => ChainedFastBackend::LAST_WRITE_TIMESTAMP_PREFIX . 'cache_foo',
      // Time travel is easy.
'data' => time() + 60,
    ];
    $cache_item = (object) [
      'cid' => 'foo',
      'data' => 'baz',
      'created' => time(),
      'expire' => time() + 3600,
      'tags' => [
        'tag',
      ],
    ];
    $consistent_cache = $this->createMock('Drupal\\Core\\Cache\\CacheBackendInterface');
    $fast_cache = $this->createMock('Drupal\\Core\\Cache\\CacheBackendInterface');
    // We should get a call for the timestamp on the consistent backend.
    $consistent_cache->expects($this->once())
      ->method('get')
      ->with($timestamp_item->cid)
      ->willReturn($timestamp_item);
    // We should get a call for the cache item on the consistent backend.
    $consistent_cache->expects($this->once())
      ->method('getMultiple')
      ->with([
      $cache_item->cid,
    ])
      ->willReturn([
      $cache_item->cid => $cache_item,
    ]);
    // We should get a call for the cache item on the fast backend.
    $fast_cache->expects($this->once())
      ->method('getMultiple')
      ->with([
      $cache_item->cid,
    ])
      ->willReturn([
      $cache_item->cid => $cache_item,
    ]);
    // We should get a call to set the cache item on the fast backend.
    $fast_cache->expects($this->once())
      ->method('set')
      ->with($cache_item->cid, $cache_item->data, $cache_item->expire);
    $chained_fast_backend = new ChainedFastBackend($consistent_cache, $fast_cache, 'foo');
    $this->assertEquals('baz', $chained_fast_backend->get('foo')->data);
  }

}

Members

Title Sort descending Modifiers Object type Summary Overrides
ChainedFastBackendTest::$bin protected property The cache bin.
ChainedFastBackendTest::$consistentCache protected property The consistent cache backend.
ChainedFastBackendTest::$fastCache protected property The fast cache backend.
ChainedFastBackendTest::testFallThroughToConsistentCache public function Tests a fast cache miss gets data from the consistent cache backend.
ChainedFastBackendTest::testGetDoesNotHitConsistentBackend public function Tests a get() on the fast backend, with no hit on the consistent backend.
ChainedFastBackendTest::testSetInvalidDataFastBackend public function Tests a get() on consistent backend without saving on fast backend.
ExpectDeprecationTrait::expectDeprecation public function Adds an expected deprecation.
ExpectDeprecationTrait::setUpErrorHandler public function Sets up the test error handler.
ExpectDeprecationTrait::tearDownErrorHandler public function Tears down the test error handler.
RandomGeneratorTrait::getRandomGenerator protected function Gets the random generator for the utility methods.
RandomGeneratorTrait::randomMachineName protected function Generates a unique random string containing letters and numbers.
RandomGeneratorTrait::randomObject public function Generates a random PHP object.
RandomGeneratorTrait::randomString public function Generates a pseudo-random string of ASCII characters of codes 32 to 126.
UnitTestCase::$root protected property The app root.
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::setDebugDumpHandler public static function Registers the dumper CLI handler when the DebugDump extension is enabled.
UnitTestCase::setUp protected function 375
UnitTestCase::setupMockIterator protected function Set up a traversable class mock to return specific items when iterated.

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