You are here

public function ResultCacheTest::testTags in GraphQL 8.4

Same name and namespace in other branches
  1. 8.3 tests/src/Kernel/Framework/ResultCacheTest.php \Drupal\Tests\graphql\Kernel\Framework\ResultCacheTest::testTags()

Test if results cache properly acts on cache tag clears.

File

tests/src/Kernel/Framework/ResultCacheTest.php, line 262

Class

ResultCacheTest
Test query result caching.

Namespace

Drupal\Tests\graphql\Kernel\Framework

Code

public function testTags() : void {
  $cacheable = $this
    ->getMockBuilder(CacheableDependencyInterface::class)
    ->setMethods([
    'getCacheTags',
    'getCacheMaxAge',
    'getCacheContexts',
  ])
    ->getMock();
  $cacheable
    ->expects($this
    ->any())
    ->method('getCacheTags')
    ->willReturn([
    'a',
    'b',
  ]);
  $cacheable
    ->expects($this
    ->any())
    ->method('getCacheMaxAge')
    ->willReturn(45);
  $cacheable
    ->expects($this
    ->any())
    ->method('getCacheContexts')
    ->willReturn([]);
  $dummy = $this
    ->getMockBuilder(Server::class)
    ->disableOriginalConstructor()
    ->setMethods([
    'id',
  ])
    ->getMock();
  $dummy
    ->expects($this
    ->exactly(2))
    ->method('id')
    ->willReturn('test');
  $this
    ->mockResolver('Query', 'root', $this->builder
    ->compose($this->builder
    ->fromValue($cacheable), $this->builder
    ->callback(function () use ($dummy) {
    return $dummy
      ->id();
  })));

  // First call that will be cached.
  $this
    ->query('{ root }');

  // Invalidate a tag that is part of the result metadata.
  $this->container
    ->get('cache_tags.invalidator')
    ->invalidateTags([
    'a',
  ]);

  // Another call will invoke the processor a second time.
  $this
    ->query('{ root }');

  // Invalidate a tag that is NOT part of the result metadata.
  $this->container
    ->get('cache_tags.invalidator')
    ->invalidateTags([
    'c',
  ]);

  // Result will be served from cache.
  $this
    ->query('{ root }');
}