QRCodeReaderTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. /**
  3. * Class QRCodeReaderTest
  4. *
  5. * @created 17.01.2021
  6. * @author Smiley <smiley@chillerlan.net>
  7. * @copyright 2021 Smiley
  8. * @license MIT
  9. *
  10. * @noinspection PhpComposerExtensionStubsInspection
  11. */
  12. namespace chillerlan\QRCodeTest;
  13. use Exception, Generator;
  14. use chillerlan\QRCode\Common\{EccLevel, Mode, Version};
  15. use chillerlan\QRCode\{QRCode, QROptions};
  16. use chillerlan\QRCode\Decoder\{GDLuminanceSource, IMagickLuminanceSource};
  17. use PHPUnit\Framework\TestCase;
  18. use function extension_loaded, range, sprintf, str_repeat, substr;
  19. use const PHP_OS_FAMILY, PHP_VERSION_ID;
  20. /**
  21. * Tests the QR Code reader
  22. */
  23. class QRCodeReaderTest extends TestCase{
  24. // https://www.bobrosslipsum.com/
  25. protected const loremipsum = 'Just let this happen. We just let this flow right out of our minds. '
  26. .'Anyone can paint. We touch the canvas, the canvas takes what it wants. From all of us here, '
  27. .'I want to wish you happy painting and God bless, my friends. A tree cannot be straight if it has a crooked trunk. '
  28. .'You have to make almighty decisions when you\'re the creator. I guess that would be considered a UFO. '
  29. .'A big cotton ball in the sky. I\'m gonna add just a tiny little amount of Prussian Blue. '
  30. .'They say everything looks better with odd numbers of things. But sometimes I put even numbers—just '
  31. .'to upset the critics. We\'ll lay all these little funky little things in there. ';
  32. public function qrCodeProvider():array{
  33. return [
  34. 'helloworld' => ['hello_world.png', 'Hello world!'],
  35. // covers mirroring
  36. 'mirrored' => ['hello_world_mirrored.png', 'Hello world!'],
  37. // data modes
  38. 'byte' => ['byte.png', 'https://smiley.codes/qrcode/'],
  39. 'numeric' => ['numeric.png', '123456789012345678901234567890'],
  40. 'alphanum' => ['alphanum.png', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 $%*+-./:'],
  41. 'kanji' => ['kanji.png', '茗荷茗荷茗荷茗荷'],
  42. // covers most of ReedSolomonDecoder
  43. 'damaged' => ['damaged.png', 'https://smiley.codes/qrcode/'],
  44. // covers Binarizer::getHistogramBlackMatrix()
  45. 'smol' => ['smol.png', 'https://smiley.codes/qrcode/'],
  46. 'tilted' => ['tilted.png', 'Hello world!'], // tilted 22° CCW
  47. 'rotated' => ['rotated.png', 'Hello world!'], // rotated 90° CW
  48. 'gradient' => ['example_svg.png', 'https://www.youtube.com/watch?v=DLzxrzFCyOs&t=43s'], // color gradient (from svg example)
  49. ];
  50. }
  51. /**
  52. * @dataProvider qrCodeProvider
  53. */
  54. public function testReaderGD(string $img, string $expected):void{
  55. $this::assertSame($expected, (string)(new QRCode)->readFromSource(GDLuminanceSource::fromFile(__DIR__.'/qrcodes/'.$img)));
  56. }
  57. /**
  58. * @dataProvider qrCodeProvider
  59. */
  60. public function testReaderImagick(string $img, string $expected):void{
  61. if(!extension_loaded('imagick')){
  62. $this::markTestSkipped('imagick not installed');
  63. }
  64. // Y THO?? https://github.com/chillerlan/php-qrcode/runs/4270411373
  65. // "could not find enough finder patterns"
  66. if($img === 'example_svg.png' && PHP_OS_FAMILY === 'Windows' && PHP_VERSION_ID < 80100){
  67. $this::markTestSkipped('random gradient example issue??');
  68. }
  69. $this::assertSame($expected, (string)(new QRCode)->readFromSource(IMagickLuminanceSource::fromFile(__DIR__.'/qrcodes/'.$img)));
  70. }
  71. public function testReaderMultiSegment():void{
  72. $options = new QROptions;
  73. $options->imageBase64 = false;
  74. $numeric = '123456789012345678901234567890';
  75. $alphanum = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 $%*+-./:';
  76. $kanji = '茗荷茗荷茗荷茗荷';
  77. $byte = 'https://smiley.codes/qrcode/';
  78. $qrcode = (new QRCode($options))
  79. ->addNumericSegment($numeric)
  80. ->addAlphaNumSegment($alphanum)
  81. ->addKanjiSegment($kanji)
  82. ->addByteSegment($byte)
  83. ;
  84. $this::assertSame($numeric.$alphanum.$kanji.$byte, (string)$qrcode->readFromBlob($qrcode->render()));
  85. }
  86. public function dataTestProvider():Generator{
  87. $str = str_repeat($this::loremipsum, 5);
  88. foreach(range(1, 40) as $v){
  89. $version = new Version($v);
  90. foreach(EccLevel::MODES as $ecc => $_){
  91. $eccLevel = new EccLevel($ecc);
  92. yield 'version: '.$version.$eccLevel => [
  93. $version,
  94. $eccLevel,
  95. substr($str, 0, $version->getMaxLengthForMode(Mode::BYTE, $eccLevel) ?? '')
  96. ];
  97. }
  98. }
  99. }
  100. /**
  101. * @dataProvider dataTestProvider
  102. */
  103. public function testReadData(Version $version, EccLevel $ecc, string $expected):void{
  104. $options = new QROptions;
  105. # $options->imageTransparent = false;
  106. $options->eccLevel = $ecc->getLevel();
  107. $options->version = $version->getVersionNumber();
  108. $options->imageBase64 = false;
  109. $options->useImagickIfAvailable = true;
  110. // what's interesting is that a smaller scale seems to produce fewer reader errors???
  111. // usually from version 20 up, independend of the luminance source
  112. // scale 1-2 produces none, scale 3: 1 error, scale 4: 6 errors, scale 5: 5 errors, scale 10: 10 errors
  113. // @see \chillerlan\QRCode\Detector\GridSampler::checkAndNudgePoints()
  114. $options->scale = 2;
  115. try{
  116. $qrcode = new QRCode($options);
  117. $imagedata = $qrcode->render($expected);
  118. $result = $qrcode->readFromBlob($imagedata);
  119. }
  120. catch(Exception $e){
  121. $this::markTestSkipped(sprintf('skipped version %s%s: %s', $version, $ecc, $e->getMessage()));
  122. }
  123. $this::assertSame($expected, $result->getText());
  124. $this::assertSame($version->getVersionNumber(), $result->getVersion()->getVersionNumber());
  125. $this::assertSame($ecc->getLevel(), $result->getEccLevel()->getLevel());
  126. }
  127. }