QRDataTest.php 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. /**
  3. * Class QRDataTest
  4. *
  5. * @created 08.08.2022
  6. * @author smiley <smiley@chillerlan.net>
  7. * @copyright 2022 smiley
  8. * @license MIT
  9. */
  10. declare(strict_types=1);
  11. namespace chillerlan\QRCodeTest\Data;
  12. use chillerlan\QRCode\Common\BitBuffer;
  13. use chillerlan\QRCode\Common\EccLevel;
  14. use chillerlan\QRCode\Common\MaskPattern;
  15. use chillerlan\QRCode\Data\Byte;
  16. use chillerlan\QRCode\Data\QRData;
  17. use chillerlan\QRCode\Output\QRGdImagePNG;
  18. use chillerlan\QRCode\QRCode;
  19. use chillerlan\QRCode\QROptions;
  20. use chillerlan\QRCodeTest\QRMatrixDebugTrait;
  21. use PHPUnit\Framework\TestCase;
  22. final class QRDataTest extends TestCase{
  23. use QRMatrixDebugTrait;
  24. /**
  25. * tests setting the BitBuffer object directly
  26. */
  27. public function testSetBitBuffer():void{
  28. $rawBytes = [
  29. 67, 22, 135, 71, 71, 7, 51, 162, 242, 247, 119, 119, 114, 231, 150, 247,
  30. 87, 71, 86, 38, 82, 230, 54, 246, 210, 247, 118, 23, 70, 54, 131, 247,
  31. 99, 212, 68, 199, 167, 135, 39, 164, 100, 55, 148, 247, 50, 103, 67, 211,
  32. 67, 55, 48, 236, 17, 236, 17, 236, 17, 236, 17, 236, 17, 236, 17, 236,
  33. 17, 236, 17, 236, 17, 236, 17, 236, 17, 236, 17, 236, 17, 236, 17, 236,
  34. 17, 236, 17, 236, 17, 236, 17, 236, 17, 236, 17, 236, 17, 236, 17, 236,
  35. 17, 236, 17, 236, 17, 236, 17, 236, 17, 236, 17, 236, 17, 236, 17, 236,
  36. 17, 236, 17, 236, 17, 236, 17, 236, 17, 236, 17, 236, 17, 236, 17, 236,
  37. 17, 236, 17, 236, 17, 236, 17, 236, 17, 236, 17, 236, 17, 236, 17, 236,
  38. 17, 236, 17, 236, 17, 236, 17, 236, 17, 236, 17, 236,
  39. ];
  40. $options = new QROptions(['version' => 3]);
  41. $bitBuffer = new BitBuffer($rawBytes);
  42. $matrix = (new QRData($options))->setBitBuffer($bitBuffer)->writeMatrix();
  43. $maskPattern = MaskPattern::getBestPattern($matrix);
  44. $matrix->setFormatInfo($maskPattern)->mask($maskPattern);
  45. $this::assertSame(3, $matrix->getVersion()->getVersionNumber());
  46. // attempt to read
  47. $options->outputBase64 = false;
  48. $options->readerUseImagickIfAvailable = false;
  49. $output = new QRGdImagePNG($options, $matrix);
  50. $decodeResult = (new QRCode($options))->readFromBlob($output->dump());
  51. $this->debugMatrix($matrix);
  52. $this::assertSame($decodeResult->data, 'https://www.youtube.com/watch?v=DLzxrzFCyOs&t=43s');
  53. }
  54. public function testEstimateTotalBitLength():void{
  55. $options = new QROptions([
  56. 'versionMin' => 10,
  57. 'quietzoneSize' => 2,
  58. 'eccLevel' => EccLevel::H,
  59. # 'outputType' => QROutputInterface::CUSTOM,
  60. # 'outputInterface' => PmaQrCodeSVG::class,
  61. 'outputBase64' => false,
  62. 'cssClass' => 'pma-2fa-qrcode',
  63. 'drawCircularModules' => true,
  64. ]);
  65. // version 10H has a maximum of 976 bits, which is the exact length of the string below
  66. // QRData::estimateTotalBitLength() used to substract 4 bits for a hypothetical data mode indicator
  67. // we're now going the safe route and do not do that anymore...
  68. $str = 'otpauth://totp/user?secret=P2SXMJFJ7DJGHLVEQYBNH2EYM4FH66CR'.
  69. '&issuer=phpMyAdmin%20%28%29&digits=6&algorithm=SHA1&period=30';
  70. $qrData = new QRData($options, [new Byte($str)]);
  71. $this::assertSame(976, $qrData->estimateTotalBitLength());
  72. $this::assertSame(11, $qrData->getMinimumVersion()->getVersionNumber()); // version adjusted to 11
  73. }
  74. }