Skip to content

Commit

Permalink
minor #52968 [VarDumper] Test intl formatter broken since dumper does…
Browse files Browse the repository at this point in the history
… not replace the nnbsp character by standard space (CDR)

This PR was squashed before being merged into the 6.3 branch.

Discussion
----------

[VarDumper] Test intl formatter broken since dumper does not replace the nnbsp character by standard space

| Q             | A
| ------------- | ---
| Branch?       | 6.3
| Bug fix?      | no
| New feature?  | no
| Deprecations? | no
| Issues        |  I don't know, saw the problem on my machine
| License       | MIT

Fix the \Symfony\Component\VarDumper\Tests\Caster\IntlCasterTest::testCastDateFormatter failure on machines that have Intl >= 72.1 :

Failed asserting that two strings are identical.
--- Expected
+++ Actual
@@ @@
'IntlDateFormatter {
locale: "en"
-  pattern: "EEEE, MMMM d, y 'at' h:mm:ss a zzzz"  <--- Notice that between 'ss' and 'a', it look's like a space, but it is an invisible \u{202F} character
+  pattern: "EEEE, MMMM d, y 'at' h:mm:ss\u{202F}a zzzz"

Commit 6936845 says 'display invisible characters'. Since Intl 72.1 version, the getPattern method of IntlDateFormatter returns a string containing an invisible character \u{202F} (alia NNBSP).
I replaced it by \\u{202F} in the expected output, but I guess maybe we could go further and do this replacement in the assertDumpEquals method

For example, here is a piece of code I wrote when debugging the problem :

```
        // Check what happens with Unicode caracters
        $u202F = "\u{202F}";
        $escapedU202F = "\\u{202F}";
        // Obviously those two strings are not equals
        $this->assertNotEquals($u202F, $escapedU202F);

        // this test fails :
        // $this->assertDumpEquals('"' . $u202F . '"', $u202F);
        // Output :
        /// Failed asserting that two strings are identical.
        // -'" "'
        // +'"\u{202F}"'

        // the dump of the first one \u{202F} is equal to the second one \\u{202F}
        // this test passes :
        $this->assertDumpEquals('"' . $escapedU202F . '"', $u202F);

        // since Intl 72, $var->getPatterns returns EEEE, MMMM d, y 'at' h:mm:ss\u{202F}a zzzz
        // The test fails as :
        // - it expects "EEEE, MMMM d, y 'at' h:mm:ss\u{202F}a zzzz" (\u{202F} is invisible, so in the console output it look's like "EEEE, MMMM d, y 'at' h:mm:ss a zzzz"
        // - but gets "EEEE, MMMM d, y 'at' h:mm:ss\\u{202F}a zzzz" (escaped version produced by dump since commit 6936845)
        // $expectedPattern = str_replace("\u{202F}", "\\u{202F}", $var->getPattern());
        $expectedPattern = $this->normalizeU202F($var->getPattern());
```

Commits
-------

46b1edb [VarDumper] Test intl formatter broken since dumper does not replace the nnbsp character by standard space
  • Loading branch information
fabpot committed Dec 9, 2023
2 parents c481dba + 46b1edb commit 83b5c67
Showing 1 changed file with 6 additions and 1 deletion.
Expand Up @@ -234,7 +234,7 @@ public function testCastDateFormatter()
$var = new \IntlDateFormatter('en', \IntlDateFormatter::TRADITIONAL, \IntlDateFormatter::TRADITIONAL);

$expectedLocale = $var->getLocale();
$expectedPattern = $var->getPattern();
$expectedPattern = $this->normalizeNarrowNoBreakSpaceCharacter($var->getPattern());
$expectedCalendar = $var->getCalendar();
$expectedTimeZoneId = $var->getTimeZoneId();
$expectedTimeType = $var->getTimeType();
Expand Down Expand Up @@ -294,4 +294,9 @@ public function testCastDateFormatter()
EOTXT;
$this->assertDumpEquals($expected, $var);
}

private function normalizeNarrowNoBreakSpaceCharacter(string $input): string
{
return str_replace("\u{202F}", '\\u{202F}', $input);
}
}

0 comments on commit 83b5c67

Please sign in to comment.