DataInterfaceTestAbstract.php 8.2 KB

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