HanziTest.php 2.3 KB

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