Skip to content

Commit

Permalink
test(assertions): Simplify assertResultErrors to avoid closure serial…
Browse files Browse the repository at this point in the history
…ization (#1159)

This commit changes some loops into built-in PHP functions to simplify
the code and eliminate the need for closures.

Additionally this switches over to an `assertEmpty` from `assertEquals`.
This achieves the same test result and in the message still provides the
errors. However it prevents test failures with weird serialization
complaints that are caused by closures in function arguments of GraphQL
Error stacktraces. See sebastianbergmann/phpunit#4371 (comment)

The downside to this change is that we may lose some contextual
information that the full error in the array comparison may provide. The
upside is that we can actually see why tests fail since in a lot of
cases this beneficial diff doesn't make it into the test output anyway.
So all-in-all this is a step forward.
  • Loading branch information
Kingdutch committed Feb 16, 2021
1 parent 98a639a commit f9c573a
Showing 1 changed file with 9 additions and 16 deletions.
25 changes: 9 additions & 16 deletions tests/src/Traits/QueryResultAssertionTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Drupal\Core\Cache\CacheableMetadata;
use Drupal\graphql\GraphQL\Execution\ExecutionResult;
use GraphQL\Error\Error;
use GraphQL\Server\OperationParams;

/**
Expand Down Expand Up @@ -145,34 +144,28 @@ private function assertResultData(ExecutionResult $result, $expected): void {
* @internal
*/
private function assertResultErrors(ExecutionResult $result, array $expected): void {
// 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));
$matchCount = array_fill_keys($expected, 0);

// Iterate through error messages.
// Collect unmatched errors and count pattern hits.
while ($error = array_pop($errors)) {
foreach ($result->errors as $error) {
$error_message = $error->getMessage();
$match = FALSE;
foreach ($expected as $pattern) {
if (@preg_match($pattern, $error) === FALSE) {
$match = $match || $pattern == $error;
if (@preg_match($pattern, $error_message) === FALSE) {
$match = $match || $pattern == $error_message;
$matchCount[$pattern]++;
}
else {
$match = $match || preg_match($pattern, $error);
$match = $match || preg_match($pattern, $error_message);
$matchCount[$pattern]++;
}
}

if (!$match) {
$unexpected[] = $error;
$unexpected[] = $error_message;
}
}

Expand All @@ -181,8 +174,8 @@ private function assertResultErrors(ExecutionResult $result, array $expected): v
return $count == 0;
}));

$this->assertEquals([], $missing, "Missing errors:\n* " . implode("\n* ", $missing));
$this->assertEquals([], $unexpected, "Unexpected errors:\n* " . implode("\n* ", $unexpected));
self::assertEmpty($missing, "Missing errors:\n* " . implode("\n* ", $missing));
self::assertEmpty($unexpected, "Unexpected errors:\n* " . implode("\n* ", $unexpected));
}

/**
Expand Down

0 comments on commit f9c573a

Please sign in to comment.