View source
<?php
namespace Drupal\Tests\graphql\Traits;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\graphql\GraphQL\Execution\QueryResult;
use GraphQL\Error\Error;
use GraphQL\Server\OperationParams;
trait QueryResultAssertionTrait {
protected abstract function getDefaultSchema();
protected abstract function defaultCacheMaxAge();
protected abstract function defaultCacheTags();
protected abstract function defaultCacheContexts();
protected function graphQlProcessor() {
return $this->container
->get('graphql.query_processor');
}
protected function defaultCacheMetaData() {
$metadata = new CacheableMetadata();
$metadata
->setCacheMaxAge($this
->defaultCacheMaxAge());
$metadata
->setCacheTags($this
->defaultCacheTags());
$metadata
->setCacheContexts($this
->defaultCacheContexts());
return $metadata;
}
protected function defaultMutationCacheMetaData() {
$metadata = new CacheableMetadata();
$metadata
->setCacheContexts($this
->defaultCacheContexts());
$metadata
->setCacheMaxAge(0);
$metadata
->setCacheTags($this
->defaultCacheTags());
return $metadata;
}
protected function assertResults($query, $variables, $expected, CacheableMetadata $metadata) {
$result = $this
->graphQlProcessor()
->processQuery($this
->getDefaultSchema(), OperationParams::create([
'query' => $query,
'variables' => $variables,
]));
$this
->assertResultErrors($result, []);
$this
->assertResultData($result, $expected);
$this
->assertResultMetadata($result, $metadata);
}
protected function assertErrors($query, $variables, $expected, CacheableMetadata $metadata) {
$result = $this
->graphQlProcessor()
->processQuery($this
->getDefaultSchema(), OperationParams::create([
'query' => $query,
'variables' => $variables,
]));
$this
->assertResultErrors($result, $expected);
$this
->assertResultMetadata($result, $metadata);
}
private function assertResultData(QueryResult $result, $expected) {
$data = $result
->toArray();
$this
->assertArrayHasKey('data', $data, 'No result data.');
$this
->assertEquals($expected, $data['data'], 'Unexpected query result.');
}
private function assertResultErrors(QueryResult $result, array $expected) {
$errors = array_map(function (Error $error) {
return $error
->getMessage();
}, $result->errors);
$unexpected = [];
$matchCount = array_map(function () {
return 0;
}, array_flip($expected));
while ($error = array_pop($errors)) {
$match = FALSE;
foreach ($expected as $pattern) {
if (@preg_match($pattern, NULL) === FALSE) {
$match = $match || $pattern == $error;
$matchCount[$pattern]++;
}
else {
$match = $match || preg_match($pattern, $error);
$matchCount[$pattern]++;
}
}
if (!$match) {
$unexpected[] = $error;
}
}
$missing = array_keys(array_filter($matchCount, function ($count) {
return $count == 0;
}));
$this
->assertEquals([], $missing, "Missing errors:\n* " . implode("\n* ", $missing));
$this
->assertEquals([], $unexpected, "Unexpected errors:\n* " . implode("\n* ", $unexpected));
}
private function assertResultMetadata(QueryResult $result, CacheableMetadata $expected) {
$this
->assertEquals($expected
->getCacheMaxAge(), $result
->getCacheMaxAge(), 'Unexpected cache max age.');
$missingContexts = array_diff($expected
->getCacheContexts(), $result
->getCacheContexts());
$this
->assertEmpty($missingContexts, 'Missing cache contexts: ' . implode(', ', $missingContexts));
$unexpectedContexts = array_diff($result
->getCacheContexts(), $expected
->getCacheContexts());
$this
->assertEmpty($unexpectedContexts, 'Unexpected cache contexts: ' . implode(', ', $unexpectedContexts));
$missingTags = array_diff($expected
->getCacheTags(), $result
->getCacheTags());
$this
->assertEmpty($missingTags, 'Missing cache tags: ' . implode(', ', $missingTags));
$unexpectedTags = array_diff($result
->getCacheTags(), $expected
->getCacheTags());
$this
->assertEmpty($unexpectedTags, 'Unexpected cache tags: ' . implode(', ', $unexpectedTags));
}
}