Skip to content

Commit 8517184

Browse files
authored
Fix #19868: Added whitespace sanitation for tests, due to updates in ICU 72
1 parent 6c511d9 commit 8517184

File tree

4 files changed

+87
-21
lines changed

4 files changed

+87
-21
lines changed

framework/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Yii Framework 2 Change Log
77
- Bug #18859: Fix `yii\web\Controller::bindInjectedParams()` to not throw error when argument of `ReflectionUnionType` type is passed (bizley)
88
- Enh #19841: Allow jQuery 3.7 to be installed (wouter90)
99
- Enh #19853: Added support for default value for `\yii\helpers\Console::select()` (rhertogh)
10+
- Bug #19868: Added whitespace sanitation for tests, due to updates in ICU 72 (schmunk42)
1011

1112

1213
2.0.48.1 May 24, 2023

tests/TestCase.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,39 @@ protected function assertEqualsWithoutLE($expected, $actual, $message = '')
123123
$this->assertEquals($expected, $actual, $message);
124124
}
125125

126+
/**
127+
* Asserting two strings equality ignoring unicode whitespaces.
128+
* @param string $expected
129+
* @param string $actual
130+
* @param string $message
131+
*/
132+
protected function assertEqualsAnyWhitespace($expected, $actual, $message = ''){
133+
$expected = $this->sanitizeWhitespaces($expected);
134+
$actual = $this->sanitizeWhitespaces($actual);
135+
136+
$this->assertEquals($expected, $actual, $message);
137+
}
138+
139+
/**
140+
* Asserts that two variables have the same type and value and sanitizes value if it is a string.
141+
* Used on objects, it asserts that two variables reference
142+
* the same object.
143+
*
144+
* @param mixed $expected
145+
* @param mixed $actual
146+
* @param string $message
147+
*/
148+
protected function assertSameAnyWhitespace($expected, $actual, $message = ''){
149+
if (is_string($expected)) {
150+
$expected = $this->sanitizeWhitespaces($expected);
151+
}
152+
if (is_string($actual)) {
153+
$actual = $this->sanitizeWhitespaces($actual);
154+
}
155+
156+
$this->assertSame($expected, $actual, $message);
157+
}
158+
126159
/**
127160
* Asserts that a haystack contains a needle ignoring line endings.
128161
*
@@ -138,6 +171,17 @@ protected function assertContainsWithoutLE($needle, $haystack, $message = '')
138171
$this->assertContains($needle, $haystack, $message);
139172
}
140173

174+
/**
175+
* Replaces unicode whitespaces with standard whitespace
176+
*
177+
* @see https://github.com/yiisoft/yii2/issues/19868 (ICU 72 changes)
178+
* @param $string
179+
* @return string
180+
*/
181+
protected function sanitizeWhitespaces($string){
182+
return preg_replace("/[\pZ\pC]/u", " ", $string);
183+
}
184+
141185
/**
142186
* Invokes a inaccessible method.
143187
* @param $object

tests/framework/helpers/FormatConverterTest.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ protected function tearDown()
3939

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

4646
public function testIntlIcuToPhpShortFormDefaultLang()
@@ -53,13 +53,13 @@ public function testIntlIcuToPhpShortFormDefaultLang()
5353

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

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

209209
public function testIntlIcuToJuiShortFormTime()
210210
{
211-
$this->assertEquals(': ', FormatConverter::convertDateIcuToJui('short', 'time', 'en-US'));
212-
$this->assertEquals(':', FormatConverter::convertDateIcuToJui('short', 'time', 'de-DE'));
211+
$this->assertEqualsAnyWhitespace(': ', FormatConverter::convertDateIcuToJui('short', 'time', 'en-US'));
212+
$this->assertEqualsAnyWhitespace(':', FormatConverter::convertDateIcuToJui('short', 'time', 'de-DE'));
213213
}
214214

215215
public function testIntlIcuToJuiShortFormDateTime()
216216
{
217-
$this->assertEquals('m/d/y, : ', FormatConverter::convertDateIcuToJui('short', 'datetime', 'en-US'));
217+
$this->assertEqualsAnyWhitespace('m/d/y, : ', FormatConverter::convertDateIcuToJui('short', 'datetime', 'en-US'));
218218
$this->assertEquals(
219219
PHP_VERSION_ID < 50600 ? 'dd.mm.y :' : 'dd.mm.y, :',
220220
FormatConverter::convertDateIcuToJui('short', 'datetime', 'de-DE')

tests/framework/i18n/FormatterDateTest.php

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -143,23 +143,23 @@ public function testIntlAsTime()
143143
public function testAsTime()
144144
{
145145
$value = time();
146-
$this->assertSame(date('g:i:s A', $value), $this->formatter->asTime($value));
146+
$this->assertSameAnyWhitespace(date('g:i:s A', $value), $this->formatter->asTime($value));
147147
$this->assertSame(date('h:i:s A', $value), $this->formatter->asTime($value, 'php:h:i:s A'));
148148

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

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

159159
// empty input
160-
$this->assertSame('12:00:00 AM', $this->formatter->asTime(''));
161-
$this->assertSame('12:00:00 AM', $this->formatter->asTime(0));
162-
$this->assertSame('12:00:00 AM', $this->formatter->asTime(false));
160+
$this->assertSameAnyWhitespace('12:00:00 AM', $this->formatter->asTime(''));
161+
$this->assertSameAnyWhitespace('12:00:00 AM', $this->formatter->asTime(0));
162+
$this->assertSameAnyWhitespace('12:00:00 AM', $this->formatter->asTime(false));
163163
// null display
164164
$this->assertSame($this->formatter->nullDisplay, $this->formatter->asTime(null));
165165
}
@@ -178,23 +178,35 @@ public function testIntlAsDatetime()
178178
public function testAsDatetime()
179179
{
180180
$value = time();
181-
$this->assertRegExp(date('~M j, Y,? g:i:s A~', $value), $this->formatter->asDatetime($value));
181+
$this->assertRegExp(
182+
$this->sanitizeWhitespaces(date('~M j, Y,? g:i:s A~', $value)),
183+
$this->sanitizeWhitespaces($this->formatter->asDatetime($value))
184+
);
182185
$this->assertSame(date('Y/m/d h:i:s A', $value), $this->formatter->asDatetime($value, 'php:Y/m/d h:i:s A'));
183186

184187
$value = new DateTime();
185-
$this->assertRegExp(date('~M j, Y,? g:i:s A~', $value->getTimestamp()), $this->formatter->asDatetime($value));
188+
$this->assertRegExp(
189+
$this->sanitizeWhitespaces(date('~M j, Y,? g:i:s A~', $value->getTimestamp())),
190+
$this->sanitizeWhitespaces($this->formatter->asDatetime($value))
191+
);
186192
$this->assertSame(date('Y/m/d h:i:s A', $value->getTimestamp()), $this->formatter->asDatetime($value, 'php:Y/m/d h:i:s A'));
187193

188194
// empty time
189195
$value = new DateTime();
190196
$date = $value->format('Y-m-d');
191197
$value = new DateTime($date);
192-
$this->assertRegExp(date('~M j, Y,? g:i:s A~', $value->getTimestamp()), $this->formatter->asDatetime($date));
198+
$this->assertRegExp(
199+
$this->sanitizeWhitespaces(date('~M j, Y,? g:i:s A~', $value->getTimestamp())),
200+
$this->sanitizeWhitespaces($this->formatter->asDatetime($date))
201+
);
193202
$this->assertSame(date('Y/m/d h:i:s A', $value->getTimestamp()), $this->formatter->asDatetime($date, 'php:Y/m/d h:i:s A'));
194203

195204
if (PHP_VERSION_ID >= 50500) {
196205
$value = new \DateTimeImmutable();
197-
$this->assertRegExp(date('~M j, Y,? g:i:s A~', $value->getTimestamp()), $this->formatter->asDatetime($value));
206+
$this->assertRegExp(
207+
$this->sanitizeWhitespaces(date('~M j, Y,? g:i:s A~', $value->getTimestamp())),
208+
$this->sanitizeWhitespaces($this->formatter->asDatetime($value))
209+
);
198210
$this->assertSame(date('Y/m/d h:i:s A', $value->getTimestamp()), $this->formatter->asDatetime($value, 'php:Y/m/d h:i:s A'));
199211
}
200212

@@ -205,9 +217,18 @@ public function testAsDatetime()
205217
}
206218

207219
// empty input
208-
$this->assertRegExp('~Jan 1, 1970,? 12:00:00 AM~', $this->formatter->asDatetime(''));
209-
$this->assertRegExp('~Jan 1, 1970,? 12:00:00 AM~', $this->formatter->asDatetime(0));
210-
$this->assertRegExp('~Jan 1, 1970,? 12:00:00 AM~', $this->formatter->asDatetime(false));
220+
$this->assertRegExp(
221+
$this->sanitizeWhitespaces('~Jan 1, 1970,? 12:00:00 AM~'),
222+
$this->sanitizeWhitespaces($this->formatter->asDatetime(''))
223+
);
224+
$this->assertRegExp(
225+
$this->sanitizeWhitespaces('~Jan 1, 1970,? 12:00:00 AM~'),
226+
$this->sanitizeWhitespaces($this->formatter->asDatetime(0))
227+
);
228+
$this->assertRegExp(
229+
$this->sanitizeWhitespaces('~Jan 1, 1970,? 12:00:00 AM~'),
230+
$this->sanitizeWhitespaces($this->formatter->asDatetime(false))
231+
);
211232
// null display
212233
$this->assertSame($this->formatter->nullDisplay, $this->formatter->asDatetime(null));
213234
}

0 commit comments

Comments
 (0)