class CacheCollectorTest
@coversDefaultClass \Drupal\Core\Cache\CacheCollector
      
    
@group Cache
Hierarchy
- class \Drupal\Tests\UnitTestCase uses \Drupal\Tests\Traits\PhpUnitWarnings, \Drupal\Tests\PhpUnitCompatibilityTrait, \Prophecy\PhpUnit\ProphecyTrait, \Symfony\Bridge\PhpUnit\ExpectDeprecationTrait, \Drupal\Tests\RandomGeneratorTrait extends \PHPUnit\Framework\TestCase- class \Drupal\Tests\Core\Cache\CacheCollectorTest extends \Drupal\Tests\UnitTestCase
 
Expanded class hierarchy of CacheCollectorTest
File
- 
              core/tests/ Drupal/ Tests/ Core/ Cache/ CacheCollectorTest.php, line 14 
Namespace
Drupal\Tests\Core\CacheView source
class CacheCollectorTest extends UnitTestCase {
  
  /**
   * The cache backend that should be used.
   *
   * @var \Drupal\Core\Cache\CacheBackendInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $cacheBackend;
  
  /**
   * The cache tags invalidator.
   *
   * @var \Drupal\Core\Cache\CacheTagsInvalidatorInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $cacheTagsInvalidator;
  
  /**
   * The lock backend that should be used.
   *
   * @var \PHPUnit\Framework\MockObject\MockObject
   */
  protected $lock;
  
  /**
   * The cache id used for the test.
   *
   * @var string
   */
  protected $cid;
  
  /**
   * Cache collector implementation to test.
   *
   * @var \Drupal\Tests\Core\Cache\CacheCollectorHelper
   */
  protected $collector;
  
  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    parent::setUp();
    $this->cacheBackend = $this->createMock('Drupal\\Core\\Cache\\CacheBackendInterface');
    $this->cacheTagsInvalidator = $this->createMock('Drupal\\Core\\Cache\\CacheTagsInvalidatorInterface');
    $this->lock = $this->createMock('Drupal\\Core\\Lock\\LockBackendInterface');
    $this->cid = $this->randomMachineName();
    $this->collector = new CacheCollectorHelper($this->cid, $this->cacheBackend, $this->lock);
    $this->getContainerWithCacheTagsInvalidator($this->cacheTagsInvalidator);
  }
  
  /**
   * Tests the resolve cache miss function.
   */
  public function testResolveCacheMiss() : void {
    $key = $this->randomMachineName();
    $value = $this->randomMachineName();
    $this->collector
      ->setCacheMissData($key, $value);
    $this->assertEquals($value, $this->collector
      ->get($key));
  }
  
  /**
   * Tests setting and getting values when the cache is empty.
   */
  public function testSetAndGet() : void {
    $key = $this->randomMachineName();
    $value = $this->randomMachineName();
    $this->assertNull($this->collector
      ->get($key));
    $this->collector
      ->set($key, $value);
    $this->assertTrue($this->collector
      ->has($key));
    $this->assertEquals($value, $this->collector
      ->get($key));
  }
  
  /**
   * Makes sure that NULL is a valid value and is collected.
   */
  public function testSetAndGetNull() : void {
    $key = $this->randomMachineName();
    $value = NULL;
    $this->cacheBackend
      ->expects($this->once())
      ->method('invalidate')
      ->with($this->cid);
    $this->collector
      ->set($key, $value);
    $this->assertTrue($this->collector
      ->has($key));
    $this->assertEquals($value, $this->collector
      ->get($key));
    // Ensure that getting a value that isn't set does not mark it as
    // existent.
    $non_existing_key = $this->randomMachineName(7);
    $this->collector
      ->get($non_existing_key);
    $this->assertFalse($this->collector
      ->has($non_existing_key));
  }
  
  /**
   * Tests returning value from the collected cache.
   */
  public function testGetFromCache() : void {
    $key = $this->randomMachineName();
    $value = $this->randomMachineName();
    $cache = (object) [
      'data' => [
        $key => $value,
      ],
      'created' => (int) $_SERVER['REQUEST_TIME'],
    ];
    $this->cacheBackend
      ->expects($this->once())
      ->method('get')
      ->with($this->cid)
      ->willReturn($cache);
    $this->assertTrue($this->collector
      ->has($key));
    $this->assertEquals($value, $this->collector
      ->get($key));
    $this->assertEquals(0, $this->collector
      ->getCacheMisses());
  }
  
  /**
   * Tests setting and deleting values.
   */
  public function testDelete() : void {
    $key = $this->randomMachineName();
    $value = $this->randomMachineName();
    $this->assertNull($this->collector
      ->get($key));
    $this->collector
      ->set($key, $value);
    $this->assertTrue($this->collector
      ->has($key));
    $this->assertEquals($value, $this->collector
      ->get($key));
    $this->cacheBackend
      ->expects($this->once())
      ->method('invalidate')
      ->with($this->cid);
    $this->collector
      ->delete($key);
    $this->assertFalse($this->collector
      ->has($key));
    $this->assertEquals(NULL, $this->collector
      ->get($key));
  }
  
  /**
   * Tests updating the cache when no changes were made.
   */
  public function testUpdateCacheNoChanges() : void {
    $this->lock
      ->expects($this->never())
      ->method('acquire');
    $this->cacheBackend
      ->expects($this->never())
      ->method('set');
    // Destruct the object to trigger the update data process.
    $this->collector
      ->destruct();
  }
  
  /**
   * Tests updating the cache after a set.
   */
  public function testUpdateCache() : void {
    $key = $this->randomMachineName();
    $value = $this->randomMachineName();
    $this->collector
      ->setCacheMissData($key, $value);
    $this->collector
      ->get($key);
    // Set up mock objects for the expected calls, first a lock acquire, then
    // cache get to look for conflicting cache entries, then a cache set and
    // finally the lock is released again.
    $this->lock
      ->expects($this->once())
      ->method('acquire')
      ->with($this->cid . ':Drupal\\Core\\Cache\\CacheCollector')
      ->willReturn(TRUE);
    $this->cacheBackend
      ->expects($this->once())
      ->method('get')
      ->with($this->cid, FALSE);
    $this->cacheBackend
      ->expects($this->once())
      ->method('set')
      ->with($this->cid, [
      $key => $value,
    ], Cache::PERMANENT, []);
    $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();
  }
  
  /**
   * Tests updating the cache when the lock acquire fails.
   */
  public function testUpdateCacheLockFail() : void {
    $key = $this->randomMachineName();
    $value = $this->randomMachineName();
    $this->collector
      ->setCacheMissData($key, $value);
    $this->collector
      ->get($key);
    // The lock acquire returns false, so the method should abort.
    $this->lock
      ->expects($this->once())
      ->method('acquire')
      ->with($this->cid . ':Drupal\\Core\\Cache\\CacheCollector')
      ->willReturn(FALSE);
    $this->cacheBackend
      ->expects($this->never())
      ->method('set');
    // Destruct the object to trigger the update data process.
    $this->collector
      ->destruct();
  }
  
  /**
   * Tests updating the cache when there is a conflict after cache invalidation.
   */
  public function testUpdateCacheInvalidatedConflict() : void {
    $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((object) [
      'data' => [
        $key => $value,
      ],
      'created' => (int) $_SERVER['REQUEST_TIME'],
    ], (object) [
      'data' => [
        $key => $value,
      ],
      'created' => (int) $_SERVER['REQUEST_TIME'] + 1,
    ]);
    $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(TRUE);
    $this->cacheBackend
      ->expects($this->once())
      ->method('delete')
      ->with($this->cid);
    $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();
  }
  
  /**
   * Tests a cache hit, then item updated by a different request.
   */
  public function testUpdateCacheMerge() : void {
    $key = $this->randomMachineName();
    $value = $this->randomMachineName();
    $this->collector
      ->setCacheMissData($key, $value);
    $this->collector
      ->get($key);
    // Set up mock objects for the expected calls, first a lock acquire, then
    // cache get to look for existing cache entries, which does find
    // and then it merges them.
    $this->lock
      ->expects($this->once())
      ->method('acquire')
      ->with($this->cid . ':Drupal\\Core\\Cache\\CacheCollector')
      ->willReturn(TRUE);
    $cache = (object) [
      'data' => [
        'other key' => 'other value',
      ],
      'created' => (int) $_SERVER['REQUEST_TIME'] + 1,
    ];
    $this->collector
      ->setCacheCreated($cache->created);
    $this->cacheBackend
      ->expects($this->once())
      ->method('get')
      ->with($this->cid)
      ->willReturn($cache);
    $this->cacheBackend
      ->expects($this->once())
      ->method('set')
      ->with($this->cid, [
      'other key' => 'other value',
      $key => $value,
    ], Cache::PERMANENT, []);
    $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();
  }
  
  /**
   * Tests a cache miss, then item created by another request.
   */
  public function testUpdateCacheRace() : void {
    $key = $this->randomMachineName();
    $value = $this->randomMachineName();
    $this->collector
      ->setCacheMissData($key, $value);
    $this->collector
      ->get($key);
    // Set up mock objects for the expected calls, first a lock acquire, then
    // cache get to look for existing cache entries, which does find
    // and then it merges them.
    $this->lock
      ->expects($this->once())
      ->method('acquire')
      ->with($this->cid . ':Drupal\\Core\\Cache\\CacheCollector')
      ->willReturn(TRUE);
    $cache = (object) [
      'data' => [
        'other key' => 'other value',
      ],
      'created' => (int) $_SERVER['REQUEST_TIME'] + 1,
    ];
    $this->cacheBackend
      ->expects($this->once())
      ->method('get')
      ->with($this->cid)
      ->willReturn($cache);
    // Destruct the object to trigger the update data process.
    $this->collector
      ->destruct();
  }
  
  /**
   * Tests updating the cache after a delete.
   */
  public function testUpdateCacheDelete() : void {
    $key = $this->randomMachineName();
    $value = $this->randomMachineName();
    $cache = (object) [
      'data' => [
        $key => $value,
      ],
      'created' => (int) $_SERVER['REQUEST_TIME'],
    ];
    // Set up mock expectation, on the second call the with the second argument
    // set to TRUE because we triggered a cache invalidation.
    $allow_invalid = [
      FALSE,
      TRUE,
    ];
    $this->cacheBackend
      ->expects($this->exactly(2))
      ->method('get')
      ->with($this->cid, $this->callback(function ($value) use (&$allow_invalid) {
      return array_shift($allow_invalid) === $value;
    }))
      ->willReturn($cache);
    $this->collector
      ->delete($key);
    // Set up mock objects for the expected calls, first a lock acquire, then
    // a cache set and finally the lock is released again.
    $this->lock
      ->expects($this->once())
      ->method('acquire')
      ->with($this->cid . ':Drupal\\Core\\Cache\\CacheCollector')
      ->willReturn(TRUE);
    $this->cacheBackend
      ->expects($this->once())
      ->method('set')
      ->with($this->cid, [], Cache::PERMANENT, []);
    $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();
  }
  
  /**
   * Tests a reset of the cache collector.
   */
  public function testUpdateCacheReset() : void {
    $key = $this->randomMachineName();
    $value = $this->randomMachineName();
    // Set the data and request it.
    $this->collector
      ->setCacheMissData($key, $value);
    $this->assertEquals($value, $this->collector
      ->get($key));
    $this->assertEquals($value, $this->collector
      ->get($key));
    // Should have been added to the storage and only be requested once.
    $this->assertEquals(1, $this->collector
      ->getCacheMisses());
    // Reset the collected cache, should call it again.
    $this->collector
      ->reset();
    $this->assertEquals($value, $this->collector
      ->get($key));
    $this->assertEquals(2, $this->collector
      ->getCacheMisses());
  }
  
  /**
   * Tests a clear of the cache collector.
   */
  public function testUpdateCacheClear() : void {
    $key = $this->randomMachineName();
    $value = $this->randomMachineName();
    // Set the data and request it.
    $this->collector
      ->setCacheMissData($key, $value);
    $this->assertEquals($value, $this->collector
      ->get($key));
    $this->assertEquals($value, $this->collector
      ->get($key));
    // Should have been added to the storage and only be requested once.
    $this->assertEquals(1, $this->collector
      ->getCacheMisses());
    // Clear the collected cache, should call it again.
    $this->cacheBackend
      ->expects($this->once())
      ->method('delete')
      ->with($this->cid);
    $this->cacheTagsInvalidator
      ->expects($this->never())
      ->method('invalidateTags');
    $this->collector
      ->clear();
    $this->assertEquals($value, $this->collector
      ->get($key));
    $this->assertEquals(2, $this->collector
      ->getCacheMisses());
  }
  
  /**
   * Tests a clear of the cache collector using tags.
   */
  public function testUpdateCacheClearTags() : void {
    $key = $this->randomMachineName();
    $value = $this->randomMachineName();
    $tags = [
      $this->randomMachineName(),
    ];
    $this->collector = new CacheCollectorHelper($this->cid, $this->cacheBackend, $this->lock, $tags);
    // Set the data and request it.
    $this->collector
      ->setCacheMissData($key, $value);
    $this->assertEquals($value, $this->collector
      ->get($key));
    $this->assertEquals($value, $this->collector
      ->get($key));
    // Should have been added to the storage and only be requested once.
    $this->assertEquals(1, $this->collector
      ->getCacheMisses());
    // Clear the collected cache using the tags, should call it again.
    $this->cacheBackend
      ->expects($this->never())
      ->method('delete');
    $this->cacheTagsInvalidator
      ->expects($this->once())
      ->method('invalidateTags')
      ->with($tags);
    $this->collector
      ->clear();
    $this->assertEquals($value, $this->collector
      ->get($key));
    $this->assertEquals(2, $this->collector
      ->getCacheMisses());
  }
  
  /**
   * @group legacy
   */
  public function testDeprecatedNormalizeLockName() : void {
    $this->expectDeprecation('Drupal\\Core\\Cache\\CacheCollector::normalizeLockName is deprecated in drupal:10.3.0 and is removed from drupal:11.0.0. The lock service is responsible for normalizing the lock name. See https://www.drupal.org/node/3436961');
    $this->collector
      ->normalizeLockName('lock');
  }
}Members
| Title Sort descending | Deprecated | Modifiers | Object type | Summary | Overriden Title | Overrides | 
|---|---|---|---|---|---|---|
| CacheCollectorTest::$cacheBackend | protected | property | The cache backend that should be used. | |||
| CacheCollectorTest::$cacheTagsInvalidator | protected | property | The cache tags invalidator. | |||
| CacheCollectorTest::$cid | protected | property | The cache id used for the test. | |||
| CacheCollectorTest::$collector | protected | property | Cache collector implementation to test. | |||
| CacheCollectorTest::$lock | protected | property | The lock backend that should be used. | |||
| CacheCollectorTest::setUp | protected | function | Overrides UnitTestCase::setUp | |||
| CacheCollectorTest::testDelete | public | function | Tests setting and deleting values. | |||
| CacheCollectorTest::testDeprecatedNormalizeLockName | public | function | @group legacy | |||
| CacheCollectorTest::testGetFromCache | public | function | Tests returning value from the collected cache. | |||
| CacheCollectorTest::testResolveCacheMiss | public | function | Tests the resolve cache miss function. | |||
| CacheCollectorTest::testSetAndGet | public | function | Tests setting and getting values when the cache is empty. | |||
| CacheCollectorTest::testSetAndGetNull | public | function | Makes sure that NULL is a valid value and is collected. | |||
| CacheCollectorTest::testUpdateCache | public | function | Tests updating the cache after a set. | |||
| CacheCollectorTest::testUpdateCacheClear | public | function | Tests a clear of the cache collector. | |||
| CacheCollectorTest::testUpdateCacheClearTags | public | function | Tests a clear of the cache collector using tags. | |||
| CacheCollectorTest::testUpdateCacheDelete | public | function | Tests updating the cache after a delete. | |||
| CacheCollectorTest::testUpdateCacheInvalidatedConflict | public | function | Tests updating the cache when there is a conflict after cache invalidation. | |||
| CacheCollectorTest::testUpdateCacheLockFail | public | function | Tests updating the cache when the lock acquire fails. | |||
| CacheCollectorTest::testUpdateCacheMerge | public | function | Tests a cache hit, then item updated by a different request. | |||
| CacheCollectorTest::testUpdateCacheNoChanges | public | function | Tests updating the cache when no changes were made. | |||
| CacheCollectorTest::testUpdateCacheRace | public | function | Tests a cache miss, then item created by another request. | |||
| CacheCollectorTest::testUpdateCacheReset | public | function | Tests a reset of the cache collector. | |||
| PhpUnitWarnings::$deprecationWarnings | private static | property | Deprecation warnings from PHPUnit to raise with @trigger_error(). | |||
| PhpUnitWarnings::addWarning | public | function | Converts PHPUnit deprecation warnings to E_USER_DEPRECATED. | |||
| 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. | |||
| RandomGeneratorTrait::randomStringValidate | Deprecated | public | function | Callback for random string validation. | ||
| UnitTestCase::$root | protected | property | The app root. | 1 | ||
| 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::getConfigStorageStub | public | function | Returns a stub config storage that returns the supplied configuration. | |||
| 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::setUpBeforeClass | public static | function | ||||
| UnitTestCase::__get | public | function | 
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.
