Skip to content

Commit f9c573a

Browse files
authored
test(assertions): Simplify assertResultErrors to avoid closure serialization (#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.
1 parent 98a639a commit f9c573a

File tree

1 file changed

+9
-16
lines changed

1 file changed

+9
-16
lines changed

tests/src/Traits/QueryResultAssertionTrait.php

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use Drupal\Core\Cache\CacheableMetadata;
66
use Drupal\graphql\GraphQL\Execution\ExecutionResult;
7-
use GraphQL\Error\Error;
87
use GraphQL\Server\OperationParams;
98

109
/**
@@ -145,34 +144,28 @@ private function assertResultData(ExecutionResult $result, $expected): void {
145144
* @internal
146145
*/
147146
private function assertResultErrors(ExecutionResult $result, array $expected): void {
148-
// Retrieve the list of error strings.
149-
$errors = array_map(function (Error $error) {
150-
return $error->getMessage();
151-
}, $result->errors);
152-
153147
// Initalize the status.
154148
$unexpected = [];
155-
$matchCount = array_map(function () {
156-
return 0;
157-
}, array_flip($expected));
149+
$matchCount = array_fill_keys($expected, 0);
158150

159151
// Iterate through error messages.
160152
// Collect unmatched errors and count pattern hits.
161-
while ($error = array_pop($errors)) {
153+
foreach ($result->errors as $error) {
154+
$error_message = $error->getMessage();
162155
$match = FALSE;
163156
foreach ($expected as $pattern) {
164-
if (@preg_match($pattern, $error) === FALSE) {
165-
$match = $match || $pattern == $error;
157+
if (@preg_match($pattern, $error_message) === FALSE) {
158+
$match = $match || $pattern == $error_message;
166159
$matchCount[$pattern]++;
167160
}
168161
else {
169-
$match = $match || preg_match($pattern, $error);
162+
$match = $match || preg_match($pattern, $error_message);
170163
$matchCount[$pattern]++;
171164
}
172165
}
173166

174167
if (!$match) {
175-
$unexpected[] = $error;
168+
$unexpected[] = $error_message;
176169
}
177170
}
178171

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

184-
$this->assertEquals([], $missing, "Missing errors:\n* " . implode("\n* ", $missing));
185-
$this->assertEquals([], $unexpected, "Unexpected errors:\n* " . implode("\n* ", $unexpected));
177+
self::assertEmpty($missing, "Missing errors:\n* " . implode("\n* ", $missing));
178+
self::assertEmpty($unexpected, "Unexpected errors:\n* " . implode("\n* ", $unexpected));
186179
}
187180

188181
/**

0 commit comments

Comments
 (0)