DataInterfaceTestAbstract.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. /**
  3. * Class DataInterfaceTestAbstract
  4. *
  5. * @created 24.11.2017
  6. * @author Smiley <smiley@chillerlan.net>
  7. * @copyright 2017 Smiley
  8. * @license MIT
  9. */
  10. namespace chillerlan\QRCodeTest\Data;
  11. use chillerlan\QRCode\Common\{EccLevel, MaskPattern, Mode, Version};
  12. use chillerlan\QRCode\Data\{QRCodeDataException, QRData, QRDataModeInterface, QRMatrix};
  13. use chillerlan\QRCode\QROptions;
  14. use chillerlan\QRCodeTest\QRMaxLengthTrait;
  15. use Exception, Generator;
  16. use PHPUnit\Framework\Attributes\DataProvider;
  17. use PHPUnit\Framework\TestCase;
  18. use function array_map, hex2bin, mb_strlen, mb_substr, sprintf, str_repeat, strlen, substr;
  19. /**
  20. * The data interface test abstract
  21. */
  22. abstract class DataInterfaceTestAbstract extends TestCase{
  23. use QRMaxLengthTrait;
  24. protected QRData $QRData;
  25. protected QRDataModeInterface $dataMode;
  26. protected const testData = '';
  27. protected function setUp():void{
  28. $this->QRData = new QRData(new QROptions);
  29. $this->dataMode = static::getDataModeInterface(static::testData);
  30. }
  31. abstract protected static function getDataModeInterface(string $data):QRDataModeInterface;
  32. /**
  33. * @return int[][]
  34. */
  35. public static function maskPatternProvider():array{
  36. return [[0], [1], [2], [3], [4], [5], [6], [7]];
  37. }
  38. /**
  39. * Tests initializing the data matrix
  40. */
  41. #[DataProvider('maskPatternProvider')]
  42. public function testInitMatrix(int $pattern):void{
  43. $maskPattern = new MaskPattern($pattern);
  44. $this->QRData->setData([$this->dataMode]);
  45. $matrix = $this->QRData->writeMatrix()->setFormatInfo($maskPattern)->mask($maskPattern);
  46. $this::assertInstanceOf(QRMatrix::class, $matrix);
  47. $this::assertSame($pattern, $matrix->getMaskPattern()->getPattern());
  48. }
  49. abstract public static function stringValidateProvider():array;
  50. /**
  51. * Tests if a string is properly validated for the respective data mode
  52. */
  53. #[DataProvider('stringValidateProvider')]
  54. public function testValidateString(string $string, bool $expected):void{
  55. $this::assertSame($expected, $this->dataMode::validateString($string));
  56. }
  57. /**
  58. * Tests if a random binary string is properly validated as false
  59. *
  60. * @see https://github.com/chillerlan/php-qrcode/issues/182
  61. */
  62. public function testBinaryStringInvalid():void{
  63. $this::assertFalse($this->dataMode::validateString(hex2bin('01015989f47dff8e852122117e04c90b9f15defc1c36477b1fe1')));
  64. }
  65. /**
  66. * returns versions within the version breakpoints 1-9, 10-26 and 27-40
  67. */
  68. public static function versionBreakpointProvider():array{
  69. return ['1-9' => [7], '10-26' => [15], '27-40' => [30]];
  70. }
  71. /**
  72. * Tests decoding a data segment from a given BitBuffer
  73. */
  74. #[DataProvider('versionBreakpointProvider')]
  75. public function testDecodeSegment(int $version):void{
  76. $options = new QROptions;
  77. $options->version = $version;
  78. // invoke a QRData instance and write data
  79. $this->QRData = new QRData($options, [$this->dataMode]);
  80. // get the filled bitbuffer
  81. $bitBuffer = $this->QRData->getBitBuffer();
  82. // read the first 4 bits
  83. $this::assertSame($this->dataMode::DATAMODE, $bitBuffer->read(4));
  84. // decode the data
  85. $this::assertSame(static::testData, $this->dataMode::decodeSegment($bitBuffer, $options->version));
  86. }
  87. /**
  88. * Generates test data for each data mode:
  89. *
  90. * - version
  91. * - ECC level
  92. * - a string that contains the maximum amount of characters for the given mode
  93. * - a string that contains characters for the given mode and that exceeds the maximum length by one/two character(s)
  94. * - the maximum allowed character length
  95. *
  96. * @throws \Exception
  97. */
  98. public static function maxLengthProvider():Generator{
  99. $eccLevels = array_map(fn(int $ecc):EccLevel => new EccLevel($ecc), [EccLevel::L, EccLevel::M, EccLevel::Q, EccLevel::H]);
  100. $str = str_repeat(static::testData, 1000);
  101. /** @phan-suppress-next-line PhanAbstractStaticMethodCallInStatic */
  102. $dataMode = static::getDataModeInterface(static::testData)::DATAMODE;
  103. $mb = ($dataMode === Mode::KANJI || $dataMode === Mode::HANZI);
  104. for($v = 1; $v <= 40; $v++){
  105. $version = new Version($v);
  106. foreach($eccLevels as $eccLevel){
  107. // maximum characters per ecc/mode
  108. $len = static::getMaxLengthForMode($dataMode, $version, $eccLevel);
  109. // a string that contains the maximum amount of characters for the given mode
  110. $val = ($mb) ? mb_substr($str, 0, $len) : substr($str, 0, $len);
  111. // same as above but character count exceeds
  112. // kanji/hanzi may have space for a single character, so we add 2 to make sure we exceed
  113. $val1 = ($mb) ? mb_substr($str, 0, ($len + 2)) : substr($str, 0, ($len + 1));
  114. // array key
  115. $key = sprintf('version: %s%s (%s)', $version, $eccLevel, $len);
  116. if((($mb) ? mb_strlen($val) : strlen($val)) !== $len){
  117. throw new Exception('output length does not match');
  118. }
  119. yield $key => [$version, $eccLevel, $val, $val1, $len];
  120. }
  121. }
  122. }
  123. #[DataProvider('maxLengthProvider')]
  124. public function testMaxLength(Version $version, EccLevel $eccLevel, string $str):void{
  125. $options = new QROptions;
  126. $options->version = $version->getVersionNumber();
  127. $options->eccLevel = $eccLevel->getLevel();
  128. $this->dataMode = static::getDataModeInterface($str);
  129. $this->QRData = new QRData($options, [$this->dataMode]);
  130. $bitBuffer = $this->QRData->getBitBuffer();
  131. $this::assertSame($this->dataMode::DATAMODE, $bitBuffer->read(4));
  132. $this::assertSame($str, $this->dataMode::decodeSegment($bitBuffer, $options->version));
  133. }
  134. /**
  135. * Tests getting the minimum QR version for the given data
  136. */
  137. #[DataProvider('maxLengthProvider')]
  138. public function testGetMinimumVersion(Version $version, EccLevel $eccLevel, string $str):void{
  139. $options = new QROptions;
  140. $options->version = Version::AUTO;
  141. $options->eccLevel = $eccLevel->getLevel();
  142. $this->dataMode = static::getDataModeInterface($str);
  143. $this->QRData = new QRData($options, [$this->dataMode]);
  144. $bitBuffer = $this->QRData->getBitBuffer();
  145. $this::assertLessThanOrEqual($eccLevel->getMaxBitsForVersion($version), $this->QRData->estimateTotalBitLength());
  146. $minimumVersionNumber = $this->QRData->getMinimumVersion()->getVersionNumber();
  147. $this::assertSame($version->getVersionNumber(), $minimumVersionNumber);
  148. // verify the encoded data
  149. $this::assertSame($this->dataMode::DATAMODE, $bitBuffer->read(4));
  150. $this::assertSame($str, $this->dataMode::decodeSegment($bitBuffer, $minimumVersionNumber));
  151. }
  152. /**
  153. * Tests if an exception is thrown on data overflow
  154. */
  155. #[DataProvider('maxLengthProvider')]
  156. public function testMaxLengthOverflowException(Version $version, EccLevel $eccLevel, string $str, string $str1):void{
  157. $this->expectException(QRCodeDataException::class);
  158. $this->expectExceptionMessage('code length overflow');
  159. $options = new QROptions;
  160. $options->version = $version->getVersionNumber();
  161. $options->eccLevel = $eccLevel->getLevel();
  162. /** @phan-suppress-next-line PhanNoopNew */
  163. new QRData($options, [static::getDataModeInterface($str1)]);
  164. }
  165. /**
  166. * Tests if an exception is thrown when the data exceeds the maximum version while auto-detecting
  167. */
  168. public function testGetMinimumVersionException():void{
  169. $this->expectException(QRCodeDataException::class);
  170. $this->expectExceptionMessage('data exceeds');
  171. $this->QRData->setData([static::getDataModeInterface(str_repeat(static::testData, 1337))]);
  172. }
  173. /**
  174. * Tests if an exception is thrown when an invalid character is encountered
  175. */
  176. public function testInvalidDataException():void{
  177. $this->expectException(QRCodeDataException::class);
  178. $this->expectExceptionMessage('invalid data');
  179. static::getDataModeInterface('##');
  180. }
  181. /**
  182. * Tests if an exception is thrown if the given string is empty
  183. */
  184. public function testInvalidDataOnEmptyException():void{
  185. $this->expectException(QRCodeDataException::class);
  186. $this->expectExceptionMessage('invalid data');
  187. static::getDataModeInterface('');
  188. }
  189. }