DataInterfaceTestAbstract.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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\{MaskPattern, Version};
  12. use chillerlan\QRCode\QROptions;
  13. use PHPUnit\Framework\TestCase;
  14. use chillerlan\QRCode\Data\{Hanzi, QRCodeDataException, QRData, QRDataModeInterface, QRMatrix};
  15. use ReflectionClass;
  16. use function hex2bin;
  17. use function str_repeat;
  18. /**
  19. * The data interface test abstract
  20. */
  21. abstract class DataInterfaceTestAbstract extends TestCase{
  22. protected QRData $QRData;
  23. protected string $FQN;
  24. protected string $testdata;
  25. protected function setUp():void{
  26. $this->QRData = new QRData(new QROptions);
  27. }
  28. /**
  29. * Verifies the QRData instance
  30. */
  31. public function testInstance():void{
  32. $this::assertInstanceOf(QRData::class, $this->QRData);
  33. }
  34. /**
  35. * Verifies the QRDataModeInterface instance
  36. */
  37. public function testDataModeInstance():void{
  38. $datamode = new $this->FQN($this->testdata);
  39. $this::assertInstanceOf(QRDataModeInterface::class, $datamode);
  40. }
  41. /**
  42. * @see testInitMatrix()
  43. * @return int[][]
  44. */
  45. public static function maskPatternProvider():array{
  46. return [[0], [1], [2], [3], [4], [5], [6], [7]];
  47. }
  48. /**
  49. * Tests initializing the data matrix
  50. *
  51. * @dataProvider maskPatternProvider
  52. */
  53. public function testInitMatrix(int $maskPattern):void{
  54. $this->QRData->setData([new $this->FQN($this->testdata)]);
  55. $matrix = $this->QRData->writeMatrix(new MaskPattern($maskPattern));
  56. $this::assertInstanceOf(QRMatrix::class, $matrix);
  57. $this::assertSame($maskPattern, $matrix->maskPattern()->getPattern());
  58. }
  59. /**
  60. * Tests getting the minimum QR version for the given data
  61. */
  62. public function testGetMinimumVersion():void{
  63. $this->QRData->setData([new $this->FQN($this->testdata)]);
  64. $reflection = new ReflectionClass(QRData::class);
  65. $getMinimumVersion = $reflection->getMethod('getMinimumVersion');
  66. $getMinimumVersion->setAccessible(true);
  67. /** @var \chillerlan\QRCode\Common\Version $version */
  68. $version = $getMinimumVersion->invoke($this->QRData);
  69. $this::assertInstanceOf(Version::class, $version);
  70. $this::assertSame(1, $version->getVersionNumber());
  71. }
  72. abstract public static function stringValidateProvider():array;
  73. /**
  74. * Tests if a string is properly validated for the respective data mode
  75. *
  76. * @dataProvider stringValidateProvider
  77. */
  78. public function testValidateString(string $string, bool $expected):void{
  79. /** @noinspection PhpUndefinedMethodInspection */
  80. $this::assertSame($expected, $this->FQN::validateString($string));
  81. }
  82. /**
  83. * Tests if a binary string is properly validated as false
  84. *
  85. * @see https://github.com/chillerlan/php-qrcode/issues/182
  86. */
  87. public function testBinaryStringInvalid():void{
  88. /** @noinspection PhpUndefinedMethodInspection */
  89. $this::assertFalse($this->FQN::validateString(hex2bin('01015989f47dff8e852122117e04c90b9f15defc1c36477b1fe1')));
  90. }
  91. /**
  92. * returns versions within the version breakpoints 1-9, 10-26 and 27-40
  93. */
  94. public static function versionBreakpointProvider():array{
  95. return ['1-9' => [7], '10-26' => [15], '27-40' => [30]];
  96. }
  97. /**
  98. * Tests decoding a data segment from a given BitBuffer
  99. *
  100. * @dataProvider versionBreakpointProvider
  101. */
  102. public function testDecodeSegment(int $version):void{
  103. $options = new QROptions;
  104. $options->version = $version;
  105. // invoke a datamode interface
  106. /** @var \chillerlan\QRCode\Data\QRDataModeInterface $datamodeInterface */
  107. $datamodeInterface = new $this->FQN($this->testdata);
  108. // invoke a QRData instance and write data
  109. $this->QRData = new QRData($options, [$datamodeInterface]);
  110. // get the filled bitbuffer
  111. $bitBuffer = $this->QRData->getBitBuffer();
  112. // read the first 4 bits
  113. $this::assertSame($datamodeInterface->getDataMode(), $bitBuffer->read(4));
  114. // hanzi mode starts with a subset indicator
  115. if($datamodeInterface instanceof Hanzi){
  116. $this::assertSame(Hanzi::GB2312_SUBSET, $bitBuffer->read(4));
  117. }
  118. // decode the data
  119. /** @noinspection PhpUndefinedMethodInspection */
  120. $this::assertSame($this->testdata, $this->FQN::decodeSegment($bitBuffer, $options->version));
  121. }
  122. /**
  123. * Tests if an exception is thrown when the data exceeds the maximum version while auto-detecting
  124. */
  125. public function testGetMinimumVersionException():void{
  126. $this->expectException(QRCodeDataException::class);
  127. $this->expectExceptionMessage('data exceeds');
  128. $this->QRData->setData([new $this->FQN(str_repeat($this->testdata, 1337))]);
  129. }
  130. /**
  131. * Tests if an exception is thrown on data overflow
  132. */
  133. public function testCodeLengthOverflowException():void{
  134. $this->expectException(QRCodeDataException::class);
  135. $this->expectExceptionMessage('code length overflow');
  136. $this->QRData = new QRData(
  137. new QROptions(['version' => 4]),
  138. [new $this->FQN(str_repeat($this->testdata, 1337))]
  139. );
  140. }
  141. /**
  142. * Tests if an exception is thrown when an invalid character is encountered
  143. */
  144. public function testInvalidDataException():void{
  145. $this->expectException(QRCodeDataException::class);
  146. $this->expectExceptionMessage('invalid data');
  147. /** @phan-suppress-next-line PhanNoopNew */
  148. new $this->FQN('##');
  149. }
  150. /**
  151. * Tests if an exception is thrown if the given string is empty
  152. */
  153. public function testInvalidDataOnEmptyException():void{
  154. $this->expectException(QRCodeDataException::class);
  155. $this->expectExceptionMessage('invalid data');
  156. /** @phan-suppress-next-line PhanNoopNew */
  157. new $this->FQN('');
  158. }
  159. }