forked from exercism/php-test-runner
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle invalid UTF-8 and a "DEL" ASCII code
In user output, e.g. for debugging, and in test failure messages there may be invalid UTF-8 or an ASCII DEL character. Invalid UTF-8 stopped PHP from producing JSON completely. This was found while working on Rotational Cipher, where ASCII characters are rotated and might result in invalid UTF-8. ASCII DEL character was also produced during that exercise and resulted in wrongly displayed output vs. JSON. PHPUnit detects ASCII control chars and turns output to "binary string" - but not for invisible DEL character (0x7F), which consumes the following character on screen.
- Loading branch information
Showing
7 changed files
with
49 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
function helloWorld() | ||
{ | ||
$invalidChars = [ | ||
"\u{7F}", // Delete | ||
"\xFF\xFF\xFF\xFF\xFF\xFF", // Invalid UTF-8 | ||
]; | ||
|
||
return 'Handle invalid chars: ' . implode(' ', $invalidChars) . '!'; | ||
} |
16 changes: 16 additions & 0 deletions
16
tests/fail-with-invalid-chars-in-message/HelloWorldTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
class HelloWorldTest extends PHPUnit\Framework\TestCase | ||
{ | ||
public static function setUpBeforeClass(): void | ||
{ | ||
require_once 'HelloWorld.php'; | ||
} | ||
|
||
public function testHelloWorld(): void | ||
{ | ||
$this->assertEquals('Never matches', helloWorld()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"version":3,"status":"fail","tests":[{"name":"Hello world","status":"fail","test_code":"$this->assertEquals('Never matches', helloWorld());\n","message":"HelloWorldTest::testHelloWorld\nFailed asserting that two strings are equal.\n--- Expected\n+++ Actual\n@@ @@\n-'Never matches'\n+'Handle invalid chars: \ufffd \ufffd\ufffd\ufffd\ufffd\ufffd\ufffd!'\n\nHelloWorldTest.php:14"}]} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
{"version":3,"status":"pass","tests":[{"name":"Hello world","status":"pass","test_code":"$this->assertEquals('Hello, World!', helloWorld());\n","output":"Some 'user \u00fc\u00e2`|| \r\toutput\ncontaining \\ various \"problematic\" and UTF-8 chars\nobject(stdClass)#79 (0) {\n}\n"}]} | ||
{"version":3,"status":"pass","tests":[{"name":"Hello world","status":"pass","test_code":"$this->assertEquals('Hello, World!', helloWorld());\n","output":"Some 'user \u00fc\u00e2`|| \ufffd \r\toutput \ufffd\ufffd\ufffd\ufffd\ufffd\ufffd \ncontaining \\ various \"problematic\" and UTF-8 chars\nobject(stdClass)#79 (0) {\n}\n"}]} |