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. namespace chillerlan\QRCodeTest\Data;
  11. use chillerlan\QRCode\Data\Kanji;
  12. use chillerlan\QRCode\Data\QRDataModeInterface;
  13. use Generator, Throwable;
  14. use PHPUnit\Framework\Attributes\DataProvider;
  15. use PHPUnit\Framework\Attributes\Group;
  16. use function bin2hex, chr, defined, sprintf;
  17. /**
  18. * Tests the Kanji class
  19. */
  20. final class KanjiTest extends DataInterfaceTestAbstract{
  21. protected const testData = '漂う花の香り';
  22. protected static function getDataModeInterface(string $data):QRDataModeInterface{
  23. return new Kanji($data);
  24. }
  25. /**
  26. * isKanji() should pass on Kanji/SJIS characters and fail on everything else
  27. *
  28. * @phpstan-return array<int, array{0: string, 1: bool}>
  29. */
  30. public static function stringValidateProvider():array{
  31. return [
  32. ['茗荷', true],
  33. ['Ã', false], // this will fail in SJIS-2004
  34. ['ABC', false],
  35. ['123', false],
  36. ['漂う花の香り', true], // https://genshin-impact.fandom.com/wiki/Floral_Incense
  37. ['꽃잎 향초의 기도', false], // same as above in korean
  38. ];
  39. }
  40. /**
  41. * lists the valid SJIS kanji
  42. */
  43. public static function kanjiProvider():Generator{
  44. $key = fn(int $byte1, int $byte2):string => sprintf('0x%X', (($byte1 << 8) | $byte2));
  45. $val = fn(int $byte1, int $byte2):string => mb_convert_encoding(chr($byte1).chr($byte2), 'UTF-8', Kanji::ENCODING);
  46. for($byte1 = 0x81; $byte1 < 0xeb; $byte1++){
  47. // skip invalid/vendor ranges
  48. if(($byte1 > 0x84 && $byte1 < 0x88) || ($byte1 > 0x9f && $byte1 < 0xe0)){
  49. continue;
  50. }
  51. // second byte of a double-byte JIS X 0208 character whose first half of the JIS sequence was odd
  52. if(($byte1 % 2) !== 0){
  53. for($byte2 = 0x40; $byte2 < 0x9f; $byte2++){
  54. if($byte2 === 0x7f){
  55. continue;
  56. }
  57. $chr = $val($byte1, $byte2);
  58. if($chr === '?'){ // skip unknown glyphs
  59. continue;
  60. }
  61. yield $key($byte1, $byte2) => [$chr];
  62. }
  63. }
  64. // second byte if the first half of the JIS sequence was even
  65. else{
  66. for($byte2 = 0x9f; $byte2 < 0xfd; $byte2++){
  67. $chr = $val($byte1, $byte2);
  68. if($chr === '?'){
  69. continue;
  70. }
  71. yield $key($byte1, $byte2) => [$chr];
  72. }
  73. }
  74. }
  75. }
  76. #[Group('slow')]
  77. #[DataProvider('kanjiProvider')]
  78. public function testValidateSJIS(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. }