private function QueryResultAssertionTrait::assertResultErrors in GraphQL 8.4
Same name and namespace in other branches
- 8.3 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\ExecutionResult $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 167
Class
- QueryResultAssertionTrait
- Trait for easier assertion on GraphQL query results.
Namespace
Drupal\Tests\graphql\TraitsCode
private function assertResultErrors(ExecutionResult $result, array $expected) : void {
// Initalize the status.
$unexpected = [];
$matchCount = array_fill_keys($expected, 0);
// Iterate through error messages.
// Collect unmatched errors and count pattern hits.
foreach ($result->errors as $error) {
$error_message = $error
->getMessage();
$match = FALSE;
foreach ($expected as $pattern) {
if (@preg_match($pattern, $error_message) === FALSE) {
$match = $match || $pattern == $error_message;
$matchCount[$pattern]++;
}
else {
$match = $match || preg_match($pattern, $error_message);
$matchCount[$pattern]++;
}
}
if (!$match) {
// Add error location information of the original error in the chain to
// show developers where to look.
$original_error = $error;
while ($original_error
->getPrevious() !== NULL) {
$original_error = $original_error
->getPrevious();
}
$unexpected[] = "Error message: {$error_message}\n Originated in: {$original_error->getFile()}:{$original_error->getLine()}";
}
}
// Create a list of patterns that never matched.
$missing = array_keys(array_filter($matchCount, function ($count) {
return $count == 0;
}));
self::assertEmpty($missing, "Missing errors:\n* " . implode("\n* ", $missing));
self::assertEmpty($unexpected, "Unexpected errors:\n* " . implode("\n* ", $unexpected));
}