QRDataTest.php 3.1 KB

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