KanjiTest.php 2.4 KB

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