DataInterfaceTestAbstract.php 8.3 KB

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