private function QueryResultAssertionTrait::assertResultErrors in GraphQL 8.3
Same name and namespace in other branches
- 8.4 tests/src/Traits/QueryResultAssertionTrait.php \Drupal\Tests\graphql\Traits\QueryResultAssertionTrait::assertResultErrors()
Assert that the result contains contains a certain set of errors.
@internal
Parameters
\Drupal\graphql\GraphQL\Execution\QueryResult $result: The query result object.
array $expected: The list of expected error messages. Also allows regular expressions.
2 calls to QueryResultAssertionTrait::assertResultErrors()
- QueryResultAssertionTrait::assertErrors in tests/
src/ Traits/ QueryResultAssertionTrait.php - Assert a query result with certain errors.
- QueryResultAssertionTrait::assertResults in tests/
src/ Traits/ QueryResultAssertionTrait.php - Assert a result for a graphql query and variables.
File
- tests/
src/ Traits/ QueryResultAssertionTrait.php, line 163
Class
- QueryResultAssertionTrait
- Trait for easier assertion on GraphQL query results.
Namespace
Drupal\Tests\graphql\TraitsCode
private function assertResultErrors(QueryResult $result, array $expected) {
// Retrieve the list of error strings.
$errors = array_map(function (Error $error) {
return $error
->getMessage();
}, $result->errors);
// Initalize the status.
$unexpected = [];
$matchCount = array_map(function () {
return 0;
}, array_flip($expected));
// Iterate through error messages.
// Collect unmatched errors and count pattern hits.
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;
}
}
// Create a list of patterns that never matched.
$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));
}