DataInterfaceTestAbstract.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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 $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. */
  73. public function testValidateString(string $string, bool $expected):void{
  74. /** @noinspection PhpUndefinedMethodInspection */
  75. $this::assertSame($expected, static::$FQN::validateString($string));
  76. }
  77. /**
  78. * Tests if a binary string is properly validated as false
  79. *
  80. * @see https://github.com/chillerlan/php-qrcode/issues/182
  81. */
  82. public function testBinaryStringInvalid():void{
  83. /** @noinspection PhpUndefinedMethodInspection */
  84. $this::assertFalse(static::$FQN::validateString(hex2bin('01015989f47dff8e852122117e04c90b9f15defc1c36477b1fe1')));
  85. }
  86. /**
  87. * returns versions within the version breakpoints 1-9, 10-26 and 27-40
  88. */
  89. public static function versionBreakpointProvider():array{
  90. return ['1-9' => [7], '10-26' => [15], '27-40' => [30]];
  91. }
  92. /**
  93. * Tests decoding a data segment from a given BitBuffer
  94. *
  95. * @dataProvider versionBreakpointProvider
  96. */
  97. public function testDecodeSegment(int $version):void{
  98. $options = new QROptions;
  99. $options->version = $version;
  100. // invoke a datamode interface
  101. $this->dataMode = new static::$FQN(static::$testdata);
  102. // invoke a QRData instance and write data
  103. $this->QRData = new QRData($options, [$this->dataMode]);
  104. // get the filled bitbuffer
  105. $bitBuffer = $this->QRData->getBitBuffer();
  106. // read the first 4 bits
  107. $this::assertSame($this->dataMode::DATAMODE, $bitBuffer->read(4));
  108. // decode the data
  109. $this::assertSame(static::$testdata, $this->dataMode::decodeSegment($bitBuffer, $options->version));
  110. }
  111. /**
  112. * Generates test data for each data mode:
  113. *
  114. * - version
  115. * - ECC level
  116. * - a string that contains the maximum amount of characters for the given mode
  117. * - a string that contains characters for the given mode and that exceeds the maximum length by one/two character(s)
  118. * - the maximum allowed character length
  119. *
  120. * @throws \Exception
  121. */
  122. public static function maxLengthProvider():Generator{
  123. $eccLevels = array_map(fn(int $ecc):EccLevel => new EccLevel($ecc), [EccLevel::L, EccLevel::M, EccLevel::Q, EccLevel::H]);
  124. $str = str_repeat(static::$testdata, 1000);
  125. $mb = (static::$FQN::DATAMODE === Mode::KANJI || static::$FQN::DATAMODE === Mode::HANZI);
  126. for($v = 1; $v <= 40; $v++){
  127. $version = new Version($v);
  128. foreach($eccLevels as $eccLevel){
  129. // maximum characters per ecc/mode
  130. $len = static::getMaxLengthForMode(static::$FQN::DATAMODE, $version, $eccLevel);
  131. // a string that contains the maximum amount of characters for the given mode
  132. $val = ($mb) ? mb_substr($str, 0, $len) : substr($str, 0, $len);
  133. // same as above but character count exceeds
  134. // kanji/hanzi may have space for a single character, so we add 2 to make sure we exceed
  135. $val1 = ($mb) ? mb_substr($str, 0, ($len + 2)) : substr($str, 0, ($len + 1));
  136. // array key
  137. $key = sprintf('version: %s%s (%s)', $version, $eccLevel, $len);
  138. if((($mb) ? mb_strlen($val) : strlen($val)) !== $len){
  139. throw new Exception('output length does not match');
  140. }
  141. yield $key => [$version, $eccLevel, $val, $val1, $len];
  142. }
  143. }
  144. }
  145. /**
  146. * @dataProvider maxLengthProvider
  147. */
  148. public function testMaxLength(Version $version, EccLevel $eccLevel, string $str):void{
  149. $options = new QROptions;
  150. $options->version = $version->getVersionNumber();
  151. $options->eccLevel = $eccLevel->getLevel();
  152. $this->dataMode = new static::$FQN($str);
  153. $this->QRData = new QRData($options, [$this->dataMode]);
  154. $bitBuffer = $this->QRData->getBitBuffer();
  155. $this::assertSame($this->dataMode::DATAMODE, $bitBuffer->read(4));
  156. $this::assertSame($str, $this->dataMode::decodeSegment($bitBuffer, $options->version));
  157. }
  158. /**
  159. * Tests getting the minimum QR version for the given data
  160. *
  161. * @dataProvider maxLengthProvider
  162. */
  163. public function testGetMinimumVersion(Version $version, EccLevel $eccLevel, string $str):void{
  164. $options = new QROptions;
  165. $options->version = Version::AUTO;
  166. $options->eccLevel = $eccLevel->getLevel();
  167. $this->dataMode = new static::$FQN($str);
  168. $this->QRData = new QRData($options, [$this->dataMode]);
  169. $bitBuffer = $this->QRData->getBitBuffer();
  170. $this::assertLessThanOrEqual($eccLevel->getMaxBitsForVersion($version), $this->QRData->estimateTotalBitLength());
  171. $minimumVersionNumber = $this->QRData->getMinimumVersion()->getVersionNumber();
  172. $this::assertSame($version->getVersionNumber(), $minimumVersionNumber);
  173. // verify the encoded data
  174. $this::assertSame($this->dataMode::DATAMODE, $bitBuffer->read(4));
  175. $this::assertSame($str, $this->dataMode::decodeSegment($bitBuffer, $minimumVersionNumber));
  176. }
  177. /**
  178. * Tests if an exception is thrown on data overflow
  179. *
  180. * @dataProvider maxLengthProvider
  181. */
  182. public function testMaxLengthOverflowException(Version $version, EccLevel $eccLevel, string $str, string $str1):void{
  183. $this->expectException(QRCodeDataException::class);
  184. $this->expectExceptionMessage('code length overflow');
  185. $options = new QROptions;
  186. $options->version = $version->getVersionNumber();
  187. $options->eccLevel = $eccLevel->getLevel();
  188. /** @phan-suppress-next-line PhanNoopNew */
  189. new QRData($options, [new static::$FQN($str1)]);
  190. }
  191. /**
  192. * Tests if an exception is thrown when the data exceeds the maximum version while auto-detecting
  193. */
  194. public function testGetMinimumVersionException():void{
  195. $this->expectException(QRCodeDataException::class);
  196. $this->expectExceptionMessage('data exceeds');
  197. $this->QRData->setData([new static::$FQN(str_repeat(static::$testdata, 1337))]);
  198. }
  199. /**
  200. * Tests if an exception is thrown when an invalid character is encountered
  201. */
  202. public function testInvalidDataException():void{
  203. $this->expectException(QRCodeDataException::class);
  204. $this->expectExceptionMessage('invalid data');
  205. /** @phan-suppress-next-line PhanNoopNew */
  206. new static::$FQN('##');
  207. }
  208. /**
  209. * Tests if an exception is thrown if the given string is empty
  210. */
  211. public function testInvalidDataOnEmptyException():void{
  212. $this->expectException(QRCodeDataException::class);
  213. $this->expectExceptionMessage('invalid data');
  214. /** @phan-suppress-next-line PhanNoopNew */
  215. new static::$FQN('');
  216. }
  217. }