You are here

public function ResultCacheTest::testUncacheableResultAnnotation 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::testUncacheableResultAnnotation()

Verify that fields with uncacheable annotations are not cached.

File

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

Class

ResultCacheTest
Test query result caching.

Namespace

Drupal\Tests\graphql\Kernel\Framework

Code

public function testUncacheableResultAnnotation() : void {
  $cacheable = $this
    ->getMockBuilder(CacheableDependencyInterface::class)
    ->setMethods([
    'getCacheTags',
    'getCacheMaxAge',
    'getCacheContexts',
  ])
    ->getMock();
  $cacheable
    ->expects($this
    ->any())
    ->method('getCacheTags')
    ->willReturn([]);
  $cacheable
    ->expects($this
    ->any())
    ->method('getCacheMaxAge')
    ->willReturn(0);
  $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();
  })));

  // The first request that is not supposed to be cached.
  $this
    ->query('{ root }');

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