HanziTest.php 2.1 KB

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