ECITest.php 4.6 KB

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