HanziTest.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. /**
  3. * Class HanziTest
  4. *
  5. * @created 20.11.2021
  6. * @author smiley <smiley@chillerlan.net>
  7. * @copyright 2021 smiley
  8. * @license MIT
  9. */
  10. namespace chillerlan\QRCodeTest\Data;
  11. use chillerlan\QRCode\Data\Hanzi;
  12. use Generator, Throwable;
  13. use function bin2hex, chr, defined, sprintf;
  14. /**
  15. * Tests the Hanzi/GB2312 class
  16. */
  17. final class HanziTest extends DataInterfaceTestAbstract{
  18. protected string $FQN = Hanzi::class;
  19. protected string $testdata = '无可奈何燃花作香';
  20. /**
  21. * isGB2312() should pass on Hanzi/GB2312 characters and fail on everything else
  22. */
  23. public static function stringValidateProvider():array{
  24. return [
  25. ['原神', true],
  26. ['ABC', false],
  27. ['123', false],
  28. ['无可奈何燃花作香', true], // https://genshin-impact.fandom.com/wiki/Floral_Incense
  29. ['無可奈何燃花作香', false], // same as above in traditional Chinese
  30. ['꽃잎 향초의 기도', false], // same as above in Korean
  31. ];
  32. }
  33. /**
  34. * lists all characters in the valid GB2312 range
  35. */
  36. public static function hanziProvider():Generator{
  37. for($byte1 = 0xa1; $byte1 < 0xf8; $byte1++){
  38. if($byte1 > 0xa9 && $byte1 < 0xb0){
  39. continue;
  40. }
  41. for($byte2 = 0xa1; $byte2 < 0xff; $byte2++){
  42. yield sprintf('0x%X', ($byte1 << 8 | $byte2)) => [
  43. mb_convert_encoding(chr($byte1).chr($byte2), 'UTF-8', Hanzi::ENCODING),
  44. ];
  45. }
  46. }
  47. }
  48. /**
  49. * @dataProvider hanziProvider
  50. */
  51. public function testValidateGB2312(string $chr):void{
  52. // we may run into several issues due to encoding detection failures
  53. try{
  54. $this::assertTrue(Hanzi::validateString($chr));
  55. }
  56. catch(Throwable $e){
  57. /** @noinspection PhpUndefinedConstantInspection - see phpunit.xml.dist */
  58. if(defined('TEST_IS_CI') && TEST_IS_CI === true){
  59. $this::markTestSkipped();
  60. }
  61. $this::markTestSkipped(sprintf(
  62. 'invalid glyph: %s => %s',
  63. bin2hex(mb_convert_encoding($chr, Hanzi::ENCODING, 'UTF-8')),
  64. $chr
  65. ));
  66. }
  67. }
  68. }