HanziTest.php 1.9 KB

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