ECITest.php 4.7 KB

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