ECITest.php 4.6 KB

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