StringHelperInvalidCharTest.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheetTests\Shared;
  3. use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
  4. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  5. use PHPUnit\Framework\TestCase;
  6. class StringHelperInvalidCharTest extends TestCase
  7. {
  8. public function testInvalidChar(): void
  9. {
  10. $spreadsheet = new Spreadsheet();
  11. $sheet = $spreadsheet->getActiveSheet();
  12. $substitution = '�';
  13. $array = [
  14. ['Normal string', 'Hello', 'Hello'],
  15. ['integer', 2, 2],
  16. ['float', 2.1, 2.1],
  17. ['boolean true', true, true],
  18. ['illegal FFFE/FFFF', "H\xef\xbf\xbe\xef\xbf\xbfello", "H{$substitution}{$substitution}ello"],
  19. ['illegal character', "H\xef\x00\x00ello", "H{$substitution}\x00\x00ello"],
  20. ['overlong character', "H\xc0\xa0ello", "H{$substitution}{$substitution}ello"],
  21. ['Osmanya as single character', "H\xf0\x90\x90\x80ello", 'H𐐀ello'],
  22. ['Osmanya as surrogate pair (x)', "\xed\xa0\x81\xed\xb0\x80", "{$substitution}{$substitution}{$substitution}{$substitution}{$substitution}{$substitution}"],
  23. ['Osmanya as surrogate pair (u)', "\u{d801}\u{dc00}", "{$substitution}{$substitution}{$substitution}{$substitution}{$substitution}{$substitution}"],
  24. ['Half surrogate pair (u)', "\u{d801}", "{$substitution}{$substitution}{$substitution}"],
  25. ['Control character', "\u{7}", "\u{7}"],
  26. ];
  27. $sheet->fromArray($array);
  28. $row = 0;
  29. foreach ($array as $value) {
  30. self::assertSame($value[1] === $value[2], StringHelper::isUTF8((string) $value[1]));
  31. ++$row;
  32. $expected = $value[2];
  33. self::assertSame(
  34. $expected,
  35. $sheet->getCell("B$row")->getValue(),
  36. $sheet->getCell("A$row")->getValue()
  37. );
  38. }
  39. }
  40. }