function CacheCollectorTest::testSetCacheInvalidatedConflict

Same name and namespace in other branches
  1. 11.x core/tests/Drupal/Tests/Core/Cache/CacheCollectorTest.php \Drupal\Tests\Core\Cache\CacheCollectorTest::testSetCacheInvalidatedConflict()

Tests setting to cache when there is a conflict after cache invalidation.

Attributes

#[TestWith([ TRUE, TRUE, TRUE, TRUE, ])] #[TestWith([ TRUE, TRUE, TRUE, FALSE, ])] #[TestWith([ TRUE, TRUE, FALSE, NULL, ])] #[TestWith([ TRUE, FALSE, FALSE, NULL, ])] #[TestWith([ TRUE, FALSE, TRUE, FALSE, ])] #[TestWith([ FALSE, FALSE, FALSE, NULL, ])] #[TestWith([ FALSE, TRUE, FALSE, NULL, ])] #[TestWith([ FALSE, FALSE, TRUE, FALSE, ])] #[TestWith([ FALSE, TRUE, TRUE, TRUE, ])] #[TestWith([ FALSE, TRUE, TRUE, FALSE, ])]

File

core/tests/Drupal/Tests/Core/Cache/CacheCollectorTest.php, line 249

Class

CacheCollectorTest
Tests Drupal\Core\Cache\CacheCollector.

Namespace

Drupal\Tests\Core\Cache

Code

public function testSetCacheInvalidatedConflict(bool $lock_acquired, bool $start_cache_item, bool $end_cache_item, ?bool $timestamp_matches) : void {
  if ($end_cache_item === FALSE && isset($timestamp_matches)) {
    throw new \BadMethodCallException('timestamp_matches is ignored when end_cache_item is FALSE');
  }
  $this->setUpMockCacheBackend();
  $this->setUpMockLockBackend();
  $key = $this->randomMachineName();
  $value = $this->randomMachineName();
  // Set up mock cache get with conflicting entries.
  $this->cacheBackend
    ->expects($this->exactly(2))
    ->method('get')
    ->with($this->cid)
    ->willReturnOnConsecutiveCalls($start_cache_item ? (object) [
    'data' => [
      $key => $value,
    ],
    'created' => (int) $_SERVER['REQUEST_TIME'],
  ] : FALSE, $end_cache_item ? (object) [
    'data' => [
      $key => $value,
    ],
    'created' => (int) $_SERVER['REQUEST_TIME'] + ($timestamp_matches ? 0 : 1),
  ] : FALSE);
  $this->cacheBackend
    ->expects($this->once())
    ->method('invalidate')
    ->with($this->cid);
  $this->collector
    ->set($key, 'new value');
  // Set up mock objects for the expected calls, first a lock acquire, then
  // when cache get finds conflicting entries it deletes the cache and aborts.
  $this->lock
    ->expects($this->once())
    ->method('acquire')
    ->with($this->cid . ':Drupal\\Core\\Cache\\CacheCollector')
    ->willReturn($lock_acquired);
  if ($end_cache_item) {
    $this->cacheBackend
      ->expects($this->once())
      ->method('delete')
      ->with($this->cid);
  }
  if ($lock_acquired) {
    $this->lock
      ->expects($this->once())
      ->method('release')
      ->with($this->cid . ':Drupal\\Core\\Cache\\CacheCollector');
  }
  // Destruct the object to trigger the update data process.
  $this->collector
    ->destruct();
}

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