ECITest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. /**
  3. * ECITest.php
  4. *
  5. * @created 12.03.2023
  6. * @author smiley <smiley@chillerlan.net>
  7. * @copyright 2023 smiley
  8. * @license MIT
  9. */
  10. declare(strict_types=1);
  11. namespace chillerlan\QRCodeTest\Data;
  12. use chillerlan\QRCode\QROptions;
  13. use chillerlan\QRCode\Common\{BitBuffer, ECICharset, Mode};
  14. use chillerlan\QRCode\Data\{Byte, ECI, Hanzi, QRCodeDataException, QRData, QRDataModeInterface};
  15. use PHPUnit\Framework\Attributes\DataProvider;
  16. use PHPUnit\Framework\TestCase;
  17. /**
  18. * Tests the ECI class
  19. */
  20. final class ECITest extends TestCase{
  21. private QRData $QRData;
  22. private int $testCharset = ECICharset::GB18030;
  23. private const testData = '无可奈何燃花作香';
  24. protected function setUp():void{
  25. $this->QRData = new QRData(new QROptions);
  26. }
  27. /**
  28. * @phpstan-return array{0: ECI, 1: Byte}
  29. */
  30. private function getDataSegments():array{
  31. return [
  32. new ECI($this->testCharset),
  33. /** @phan-suppress-next-line PhanParamSuspiciousOrder */
  34. new Byte(mb_convert_encoding(self::testData, ECICharset::MB_ENCODINGS[$this->testCharset], mb_internal_encoding())),
  35. ];
  36. }
  37. public function testDataModeInstance():void{
  38. $datamode = new ECI($this->testCharset);
  39. $this::assertInstanceOf(QRDataModeInterface::class, $datamode);
  40. }
  41. /**
  42. * returns versions within the version breakpoints 1-9, 10-26 and 27-40
  43. *
  44. * @phpstan-return array<string, array{0: int}>
  45. */
  46. public static function versionBreakpointProvider():array{
  47. return ['1-9' => [7], '10-26' => [15], '27-40' => [30]];
  48. }
  49. #[DataProvider('versionBreakpointProvider')]
  50. public function testDecodeSegment(int $version):void{
  51. $options = new QROptions;
  52. $options->version = $version;
  53. /** @var \chillerlan\QRCode\Data\QRDataModeInterface[] $segments */
  54. $segments = $this->getDataSegments();
  55. // invoke a QRData instance and write data
  56. $this->QRData = new QRData($options, $segments);
  57. // get the filled bitbuffer
  58. $bitBuffer = $this->QRData->getBitBuffer();
  59. // read the first 4 bits
  60. $this::assertSame($segments[0]::DATAMODE, $bitBuffer->read(4));
  61. // decode the data
  62. $this::assertSame(self::testData, ECI::decodeSegment($bitBuffer, $options->version));
  63. }
  64. public function testInvalidDataException():void{
  65. $this->expectException(QRCodeDataException::class);
  66. $this->expectExceptionMessage('invalid encoding id:');
  67. new ECI(-1);
  68. }
  69. /**
  70. * since the ECI class only accepts integer values,
  71. * we'll use this test to check for the upper end of the accepted input range
  72. */
  73. public function testInvalidDataOnEmptyException():void{
  74. $this->expectException(QRCodeDataException::class);
  75. $this->expectExceptionMessage('invalid encoding id:');
  76. new ECI(1000000);
  77. }
  78. /**
  79. * @phpstan-return array<int, array{0: int, 1: int}>
  80. */
  81. public static function eciCharsetIdProvider():array{
  82. return [
  83. [ 0, 8],
  84. [ 127, 8],
  85. [ 128, 16],
  86. [ 16383, 16],
  87. [ 16384, 24],
  88. [999999, 24],
  89. ];
  90. }
  91. #[DataProvider('eciCharsetIdProvider')]
  92. public function testReadWrite(int $id, int $lengthInBits):void{
  93. $bitBuffer = new BitBuffer;
  94. $eci = (new ECI($id))->write($bitBuffer, 1);
  95. $this::assertSame($lengthInBits, $eci->getLengthInBits());
  96. $this::assertSame(Mode::ECI, $bitBuffer->read(4));
  97. $this::assertSame($id, ECI::parseValue($bitBuffer)->getID());
  98. }
  99. /**
  100. * Tests if and exception is thrown when the ECI segment is followed by a mode that is not 8-bit byte
  101. */
  102. public function testDecodeECISegmentFollowedByInvalidModeException():void{
  103. $this->expectException(QRCodeDataException::class);
  104. $this->expectExceptionMessage('ECI designator followed by invalid mode:');
  105. $options = new QROptions;
  106. $options->version = 5;
  107. /** @var \chillerlan\QRCode\Data\QRDataModeInterface[] $segments */
  108. $segments = $this->getDataSegments();
  109. // follow the ECI segment by a non-8bit-byte segment
  110. $segments[1] = new Hanzi(self::testData);
  111. $bitBuffer = (new QRData($options, $segments))->getBitBuffer();
  112. // verify the ECI mode indicator
  113. $this::assertSame(Mode::ECI, $bitBuffer->read(4));
  114. // throw
  115. ECI::decodeSegment($bitBuffer, $options->version);
  116. }
  117. /**
  118. * @phpstan-return array<string, array{0: int, 1: string}>
  119. */
  120. public static function unknownEncodingDataProvider():array{
  121. return [
  122. 'CP437' => [0, "\x41\x42\x43"],
  123. 'ISO_IEC_8859_1_GLI' => [1, "\x41\x42\x43"],
  124. ];
  125. }
  126. /**
  127. * Tests detection of an unknown character set
  128. */
  129. #[DataProvider('unknownEncodingDataProvider')]
  130. public function testConvertUnknownEncoding(int $id, string $data):void{
  131. $options = new QROptions;
  132. $options->version = 5;
  133. $segments = [new ECI($id), new Byte($data)];
  134. $bitBuffer = (new QRData($options, $segments))->getBitBuffer();
  135. $this::assertSame(Mode::ECI, $bitBuffer->read(4));
  136. $this::assertSame($data, ECI::decodeSegment($bitBuffer, $options->version));
  137. }
  138. }