HanziTest.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 korean
  32. ];
  33. }
  34. /**
  35. * lists all characters in the valid GB2312 range
  36. */
  37. public function hanziProvider():array{
  38. $list = [];
  39. for($byte1 = 0xa1; $byte1 < 0xf8; $byte1 += 0x1){
  40. if($byte1 > 0xa9 && $byte1 < 0xb0){
  41. continue;
  42. }
  43. for($byte2 = 0xa1; $byte2 < 0xff; $byte2++){
  44. $list[] = [chr($byte1).chr($byte2)];
  45. }
  46. }
  47. return array_map(fn($chr) => mb_convert_encoding($chr, 'UTF-8', Hanzi::ENCODING), $list);
  48. }
  49. /**
  50. * @dataProvider hanziProvider
  51. */
  52. public function testValidateGB2312(string $chr):void{
  53. // we may run into several issues due to encoding detection failures
  54. try{
  55. $this::assertTrue(Hanzi::validateString($chr));
  56. }
  57. catch(Throwable $e){
  58. /** @noinspection PhpUndefinedConstantInspection - see phpunit.xml.dist */
  59. if(defined('TEST_IS_CI') && TEST_IS_CI === true){
  60. $this::markTestSkipped();
  61. }
  62. $this::markTestSkipped(sprintf(
  63. 'invalid glyph: %s => %s',
  64. bin2hex($chr),
  65. mb_convert_encoding($chr, Hanzi::ENCODING, mb_internal_encoding())
  66. ));
  67. }
  68. }
  69. }