QRCodeReaderTest.php 6.0 KB

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