Skip to content

Commit

Permalink
Fix #19868: Added whitespace sanitation for tests, due to updates in …
Browse files Browse the repository at this point in the history
…ICU 72
  • Loading branch information
schmunk42 authored Jun 21, 2023
1 parent 6c511d9 commit 8517184
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 21 deletions.
1 change: 1 addition & 0 deletions framework/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Yii Framework 2 Change Log
- Bug #18859: Fix `yii\web\Controller::bindInjectedParams()` to not throw error when argument of `ReflectionUnionType` type is passed (bizley)
- Enh #19841: Allow jQuery 3.7 to be installed (wouter90)
- Enh #19853: Added support for default value for `\yii\helpers\Console::select()` (rhertogh)
- Bug #19868: Added whitespace sanitation for tests, due to updates in ICU 72 (schmunk42)


2.0.48.1 May 24, 2023
Expand Down
44 changes: 44 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,39 @@ protected function assertEqualsWithoutLE($expected, $actual, $message = '')
$this->assertEquals($expected, $actual, $message);
}

/**
* Asserting two strings equality ignoring unicode whitespaces.
* @param string $expected
* @param string $actual
* @param string $message
*/
protected function assertEqualsAnyWhitespace($expected, $actual, $message = ''){
$expected = $this->sanitizeWhitespaces($expected);
$actual = $this->sanitizeWhitespaces($actual);

$this->assertEquals($expected, $actual, $message);
}

/**
* Asserts that two variables have the same type and value and sanitizes value if it is a string.
* Used on objects, it asserts that two variables reference
* the same object.
*
* @param mixed $expected
* @param mixed $actual
* @param string $message
*/
protected function assertSameAnyWhitespace($expected, $actual, $message = ''){
if (is_string($expected)) {
$expected = $this->sanitizeWhitespaces($expected);
}
if (is_string($actual)) {
$actual = $this->sanitizeWhitespaces($actual);
}

$this->assertSame($expected, $actual, $message);
}

/**
* Asserts that a haystack contains a needle ignoring line endings.
*
Expand All @@ -138,6 +171,17 @@ protected function assertContainsWithoutLE($needle, $haystack, $message = '')
$this->assertContains($needle, $haystack, $message);
}

/**
* Replaces unicode whitespaces with standard whitespace
*
* @see https://github.com/yiisoft/yii2/issues/19868 (ICU 72 changes)
* @param $string
* @return string
*/
protected function sanitizeWhitespaces($string){
return preg_replace("/[\pZ\pC]/u", " ", $string);
}

/**
* Invokes a inaccessible method.
* @param $object
Expand Down
16 changes: 8 additions & 8 deletions tests/framework/helpers/FormatConverterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ protected function tearDown()

public function testIntlIcuToPhpShortForm()
{
$this->assertEquals('n/j/y', FormatConverter::convertDateIcuToPhp('short', 'date', 'en-US'));
$this->assertEquals('d.m.y', FormatConverter::convertDateIcuToPhp('short', 'date', 'de-DE'));
$this->assertEqualsAnyWhitespace('n/j/y', FormatConverter::convertDateIcuToPhp('short', 'date', 'en-US'));
$this->assertEqualsAnyWhitespace('d.m.y', FormatConverter::convertDateIcuToPhp('short', 'date', 'de-DE'));
}

public function testIntlIcuToPhpShortFormDefaultLang()
Expand All @@ -53,13 +53,13 @@ public function testIntlIcuToPhpShortFormDefaultLang()

public function testIntlIcuToPhpShortFormTime()
{
$this->assertEquals('g:i A', FormatConverter::convertDateIcuToPhp('short', 'time', 'en-US'));
$this->assertEquals('H:i', FormatConverter::convertDateIcuToPhp('short', 'time', 'de-DE'));
$this->assertEqualsAnyWhitespace('g:i A', FormatConverter::convertDateIcuToPhp('short', 'time', 'en-US'));
$this->assertEqualsAnyWhitespace('H:i', FormatConverter::convertDateIcuToPhp('short', 'time', 'de-DE'));
}

public function testIntlIcuToPhpShortFormDateTime()
{
$this->assertEquals('n/j/y, g:i A', FormatConverter::convertDateIcuToPhp('short', 'datetime', 'en-US'));
$this->assertEqualsAnyWhitespace('n/j/y, g:i A', FormatConverter::convertDateIcuToPhp('short', 'datetime', 'en-US'));
$this->assertEquals(
PHP_VERSION_ID < 50600 ? 'd.m.y H:i' : 'd.m.y, H:i',
FormatConverter::convertDateIcuToPhp('short', 'datetime', 'de-DE')
Expand Down Expand Up @@ -208,13 +208,13 @@ public function testIntlIcuToJuiShortFormDefaultLang()

public function testIntlIcuToJuiShortFormTime()
{
$this->assertEquals(': ', FormatConverter::convertDateIcuToJui('short', 'time', 'en-US'));
$this->assertEquals(':', FormatConverter::convertDateIcuToJui('short', 'time', 'de-DE'));
$this->assertEqualsAnyWhitespace(': ', FormatConverter::convertDateIcuToJui('short', 'time', 'en-US'));
$this->assertEqualsAnyWhitespace(':', FormatConverter::convertDateIcuToJui('short', 'time', 'de-DE'));
}

public function testIntlIcuToJuiShortFormDateTime()
{
$this->assertEquals('m/d/y, : ', FormatConverter::convertDateIcuToJui('short', 'datetime', 'en-US'));
$this->assertEqualsAnyWhitespace('m/d/y, : ', FormatConverter::convertDateIcuToJui('short', 'datetime', 'en-US'));
$this->assertEquals(
PHP_VERSION_ID < 50600 ? 'dd.mm.y :' : 'dd.mm.y, :',
FormatConverter::convertDateIcuToJui('short', 'datetime', 'de-DE')
Expand Down
47 changes: 34 additions & 13 deletions tests/framework/i18n/FormatterDateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,23 +143,23 @@ public function testIntlAsTime()
public function testAsTime()
{
$value = time();
$this->assertSame(date('g:i:s A', $value), $this->formatter->asTime($value));
$this->assertSameAnyWhitespace(date('g:i:s A', $value), $this->formatter->asTime($value));
$this->assertSame(date('h:i:s A', $value), $this->formatter->asTime($value, 'php:h:i:s A'));

$value = new DateTime();
$this->assertSame(date('g:i:s A', $value->getTimestamp()), $this->formatter->asTime($value));
$this->assertSameAnyWhitespace(date('g:i:s A', $value->getTimestamp()), $this->formatter->asTime($value));
$this->assertSame(date('h:i:s A', $value->getTimestamp()), $this->formatter->asTime($value, 'php:h:i:s A'));

if (version_compare(PHP_VERSION, '5.5.0', '>=')) {
$value = new \DateTimeImmutable();
$this->assertSame(date('g:i:s A', $value->getTimestamp()), $this->formatter->asTime($value));
$this->assertSameAnyWhitespace(date('g:i:s A', $value->getTimestamp()), $this->formatter->asTime($value));
$this->assertSame(date('h:i:s A', $value->getTimestamp()), $this->formatter->asTime($value, 'php:h:i:s A'));
}

// empty input
$this->assertSame('12:00:00 AM', $this->formatter->asTime(''));
$this->assertSame('12:00:00 AM', $this->formatter->asTime(0));
$this->assertSame('12:00:00 AM', $this->formatter->asTime(false));
$this->assertSameAnyWhitespace('12:00:00 AM', $this->formatter->asTime(''));
$this->assertSameAnyWhitespace('12:00:00 AM', $this->formatter->asTime(0));
$this->assertSameAnyWhitespace('12:00:00 AM', $this->formatter->asTime(false));
// null display
$this->assertSame($this->formatter->nullDisplay, $this->formatter->asTime(null));
}
Expand All @@ -178,23 +178,35 @@ public function testIntlAsDatetime()
public function testAsDatetime()
{
$value = time();
$this->assertRegExp(date('~M j, Y,? g:i:s A~', $value), $this->formatter->asDatetime($value));
$this->assertRegExp(
$this->sanitizeWhitespaces(date('~M j, Y,? g:i:s A~', $value)),
$this->sanitizeWhitespaces($this->formatter->asDatetime($value))
);
$this->assertSame(date('Y/m/d h:i:s A', $value), $this->formatter->asDatetime($value, 'php:Y/m/d h:i:s A'));

$value = new DateTime();
$this->assertRegExp(date('~M j, Y,? g:i:s A~', $value->getTimestamp()), $this->formatter->asDatetime($value));
$this->assertRegExp(
$this->sanitizeWhitespaces(date('~M j, Y,? g:i:s A~', $value->getTimestamp())),
$this->sanitizeWhitespaces($this->formatter->asDatetime($value))
);
$this->assertSame(date('Y/m/d h:i:s A', $value->getTimestamp()), $this->formatter->asDatetime($value, 'php:Y/m/d h:i:s A'));

// empty time
$value = new DateTime();
$date = $value->format('Y-m-d');
$value = new DateTime($date);
$this->assertRegExp(date('~M j, Y,? g:i:s A~', $value->getTimestamp()), $this->formatter->asDatetime($date));
$this->assertRegExp(
$this->sanitizeWhitespaces(date('~M j, Y,? g:i:s A~', $value->getTimestamp())),
$this->sanitizeWhitespaces($this->formatter->asDatetime($date))
);
$this->assertSame(date('Y/m/d h:i:s A', $value->getTimestamp()), $this->formatter->asDatetime($date, 'php:Y/m/d h:i:s A'));

if (PHP_VERSION_ID >= 50500) {
$value = new \DateTimeImmutable();
$this->assertRegExp(date('~M j, Y,? g:i:s A~', $value->getTimestamp()), $this->formatter->asDatetime($value));
$this->assertRegExp(
$this->sanitizeWhitespaces(date('~M j, Y,? g:i:s A~', $value->getTimestamp())),
$this->sanitizeWhitespaces($this->formatter->asDatetime($value))
);
$this->assertSame(date('Y/m/d h:i:s A', $value->getTimestamp()), $this->formatter->asDatetime($value, 'php:Y/m/d h:i:s A'));
}

Expand All @@ -205,9 +217,18 @@ public function testAsDatetime()
}

// empty input
$this->assertRegExp('~Jan 1, 1970,? 12:00:00 AM~', $this->formatter->asDatetime(''));
$this->assertRegExp('~Jan 1, 1970,? 12:00:00 AM~', $this->formatter->asDatetime(0));
$this->assertRegExp('~Jan 1, 1970,? 12:00:00 AM~', $this->formatter->asDatetime(false));
$this->assertRegExp(
$this->sanitizeWhitespaces('~Jan 1, 1970,? 12:00:00 AM~'),
$this->sanitizeWhitespaces($this->formatter->asDatetime(''))
);
$this->assertRegExp(
$this->sanitizeWhitespaces('~Jan 1, 1970,? 12:00:00 AM~'),
$this->sanitizeWhitespaces($this->formatter->asDatetime(0))
);
$this->assertRegExp(
$this->sanitizeWhitespaces('~Jan 1, 1970,? 12:00:00 AM~'),
$this->sanitizeWhitespaces($this->formatter->asDatetime(false))
);
// null display
$this->assertSame($this->formatter->nullDisplay, $this->formatter->asDatetime(null));
}
Expand Down

0 comments on commit 8517184

Please sign in to comment.