DataInterfaceTestAbstract.php 8.2 KB

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