function MemoryBackendTest::testGarbageCollection

Tests that expired cache items are removed from memory.

@legacy-covers ::garbageCollection

File

core/tests/Drupal/Tests/Core/Cache/MemoryBackendTest.php, line 26

Class

MemoryBackendTest
Tests the MemoryBackend cache.

Namespace

Drupal\Tests\Core\Cache

Code

public function testGarbageCollection() : void {
  $time = $this->createMock(TimeInterface::class);
  $time->expects($this->any())
    ->method('getRequestTime')
    ->willReturn(100);
  $backend = new MemoryBackend($time);
  // Set a cache item that is expired.
  $backend->set('foo', 0, 10);
  // Set a cache item that is not expired.
  $backend->set('bar', 1, 200);
  // Set a permanent cache item.
  $backend->set('baz', 2, Cache::PERMANENT);
  // Verify that the cache entries were set.
  $this->assertInstanceOf(\stdClass::class, $backend->get('foo', TRUE));
  $this->assertInstanceOf(\stdClass::class, $backend->get('bar', TRUE));
  $this->assertInstanceOf(\stdClass::class, $backend->get('baz', TRUE));
  $backend->garbageCollection();
  // Verify that the cache entries were cleared or retained correctly.
  $this->assertFalse($backend->get('foo', TRUE));
  $this->assertInstanceOf(\stdClass::class, $backend->get('bar', TRUE));
  $this->assertInstanceOf(\stdClass::class, $backend->get('baz', TRUE));
}

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