QRDataTest.php 3.1 KB

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