Skip to content
Snippets Groups Projects

Issue #3320815: Make validation result comparison test messages more helpful

Merged Issue #3320815: Make validation result comparison test messages more helpful
1 unresolved thread
1 unresolved thread
@@ -12,18 +12,50 @@ trait ValidationTestTrait {
@@ -12,18 +12,50 @@ trait ValidationTestTrait {
/**
/**
* Asserts two validation result sets are equal.
* Asserts two validation result sets are equal.
*
*
 
* This assertion is sensitive to the order of results. For example,
 
* ['a', 'b'] is not equal to ['b', 'a'].
    • Comment on lines +15 to +16
      Author Contributor

      Note: This was already true before this change. It just wasn't made explicit. In other words, this doesn't represent a functional change of the method--just added documentation.

Please register or sign in to reply
 
*
* @param \Drupal\package_manager\ValidationResult[] $expected_results
* @param \Drupal\package_manager\ValidationResult[] $expected_results
* The expected validation results.
* The expected validation results.
* @param \Drupal\package_manager\ValidationResult[] $actual_results
* @param \Drupal\package_manager\ValidationResult[] $actual_results
* The actual validation results.
* The actual validation results.
*/
*/
protected function assertValidationResultsEqual(array $expected_results, array $actual_results): void {
protected function assertValidationResultsEqual(array $expected_results, array $actual_results): void {
$this->assertCount(count($expected_results), $actual_results);
$expected_results = $this->getValidationResultsAsArray($expected_results);
 
$actual_results = $this->getValidationResultsAsArray($actual_results);
 
 
self::assertSame($expected_results, $actual_results);
 
}
 
 
/**
 
* Gets an array representation of validation results for easy comparison.
 
*
 
* @param \Drupal\package_manager\ValidationResult[] $results
 
* An array of validation results.
 
*
 
* @return array
 
* An array of validation results details:
 
* - severity: (int) The severity code.
 
* - messages: (array) An array of strings.
 
* - summary: (string|null) A summary string if there is one or NULL if not.
 
*/
 
protected function getValidationResultsAsArray(array $results): array {
 
return array_values(array_map(static function (ValidationResult $result) {
 
$messages = array_map(static function ($message): string {
 
return (string) $message;
 
}, $result->getMessages());
 
 
$summary = $result->getSummary();
 
if ($summary !== NULL) {
 
$summary = (string) $result->getSummary();
 
}
foreach ($expected_results as $expected_result) {
return [
$actual_result = array_shift($actual_results);
'severity' => $result->getSeverity(),
$this->assertTrue(ValidationResult::isEqual($expected_result, $actual_result));
'messages' => $messages,
}
'summary' => $summary,
 
];
 
}, $results));
}
}
}
}
Loading