KanjiTest.php 2.7 KB

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