KanjiTest.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. /**
  3. * Class KanjiTest
  4. *
  5. * @created 24.11.2017
  6. * @author Smiley <smiley@chillerlan.net>
  7. * @copyright 2017 Smiley
  8. * @license MIT
  9. */
  10. declare(strict_types=1);
  11. namespace chillerlan\QRCodeTest\Data;
  12. use chillerlan\QRCode\Data\{Kanji, QRDataModeInterface};
  13. use PHPUnit\Framework\Attributes\{DataProvider, Group, Test};
  14. use Generator, Throwable;
  15. use function bin2hex, chr, defined, sprintf;
  16. /**
  17. * Tests the Kanji class
  18. */
  19. final class KanjiTest extends DataInterfaceTestAbstract{
  20. protected const testData = '漂う花の香り';
  21. protected static function getDataModeInterface(string $data):QRDataModeInterface{
  22. return new Kanji($data);
  23. }
  24. /**
  25. * isKanji() should pass on Kanji/SJIS characters and fail on everything else
  26. *
  27. * @phpstan-return array<int, array{0: string, 1: bool}>
  28. */
  29. public static function stringValidateProvider():array{
  30. return [
  31. ['茗荷', true],
  32. ['Ã', false], // this will fail in SJIS-2004
  33. ['ABC', false],
  34. ['123', false],
  35. ['漂う花の香り', true], // https://genshin-impact.fandom.com/wiki/Floral_Incense
  36. ['꽃잎 향초의 기도', false], // same as above in korean
  37. ];
  38. }
  39. /**
  40. * lists the valid SJIS kanji
  41. */
  42. public static function kanjiProvider():Generator{
  43. $key = fn(int $byte1, int $byte2):string => sprintf('0x%X', (($byte1 << 8) | $byte2));
  44. $val = fn(int $byte1, int $byte2):string => mb_convert_encoding(chr($byte1).chr($byte2), 'UTF-8', Kanji::ENCODING);
  45. for($byte1 = 0x81; $byte1 < 0xeb; $byte1++){
  46. // skip invalid/vendor ranges
  47. if(($byte1 > 0x84 && $byte1 < 0x88) || ($byte1 > 0x9f && $byte1 < 0xe0)){
  48. continue;
  49. }
  50. // second byte of a double-byte JIS X 0208 character whose first half of the JIS sequence was odd
  51. if(($byte1 % 2) !== 0){
  52. for($byte2 = 0x40; $byte2 < 0x9f; $byte2++){
  53. if($byte2 === 0x7f){
  54. continue;
  55. }
  56. $chr = $val($byte1, $byte2);
  57. if($chr === '?'){ // skip unknown glyphs
  58. continue;
  59. }
  60. yield $key($byte1, $byte2) => [$chr];
  61. }
  62. }
  63. // second byte if the first half of the JIS sequence was even
  64. else{
  65. for($byte2 = 0x9f; $byte2 < 0xfd; $byte2++){
  66. $chr = $val($byte1, $byte2);
  67. if($chr === '?'){
  68. continue;
  69. }
  70. yield $key($byte1, $byte2) => [$chr];
  71. }
  72. }
  73. }
  74. }
  75. #[Test]
  76. #[Group('slow')]
  77. #[DataProvider('kanjiProvider')]
  78. public function validateSJIS(string $chr):void{
  79. // we may run into several issues due to encoding detection failures
  80. try{
  81. $this::assertTrue(Kanji::validateString($chr));
  82. }
  83. catch(Throwable){
  84. /** @noinspection PhpUndefinedConstantInspection - see phpunit.xml.dist */
  85. if(defined('TEST_IS_CI') && TEST_IS_CI === true){
  86. $this::markTestSkipped();
  87. }
  88. $this::markTestSkipped(sprintf(
  89. 'invalid glyph: %s => %s',
  90. bin2hex(mb_convert_encoding($chr, Kanji::ENCODING, 'UTF-8')),
  91. $chr,
  92. ));
  93. }
  94. }
  95. }