MDL-81074 core: Add assertTimeStringMatches PHPUnit Assertion

Also raised as https://github.com/sebastianbergmann/phpunit/issues/5757
This commit is contained in:
Andrew Nicols
2024-03-25 09:51:25 +08:00
parent 23b110b28b
commit d265739f7d
2 changed files with 103 additions and 0 deletions
+25
View File
@@ -121,6 +121,31 @@ abstract class base_testcase extends PHPUnit\Framework\TestCase {
return $hash;
}
/**
* Assert that two Date/Time strings are equal.
*
* The strings generated by \DateTime, \strtotime, \date, \time, etc. are generated outside of our control.
* From time-to-time string changes are made.
* One such example is from ICU 72.1 which changed the time format to include a narrow-non-breaking-space (U+202F)
* between the time and AM/PM.
*
* We should not update our tests to match these changes, as it is not our code that is
* generating the strings and they may change again.
* In addition, the changes are not equal amongst all systems as they depend on the version of ICU installed.
*
* @param string $expected
* @param string $actual
* @param string $message
*/
public function assertEqualsIgnoringWhitespace($expected, $actual, string $message = ''): void {
// ICU 72.1 introduced the use of a narrow-non-breaking-space (U+202F) between the time and the AM/PM.
// Normalise all whitespace when performing the comparison.
$expected = preg_replace('/\s+/u', ' ', $expected);
$actual = preg_replace('/\s+/u', ' ', $actual);
$this->assertEquals($expected, $actual, $message);
}
/**
* Parse out the options from the tag using DOM object tree.
*