QRCodeReaderTest.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. /**
  21. * Tests the QR Code reader
  22. */
  23. final 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. protected SettingsContainerInterface $options;
  33. protected function setUp():void{
  34. $this->options = new QROptions;
  35. }
  36. public function qrCodeProvider():array{
  37. return [
  38. 'helloworld' => ['hello_world.png', 'Hello world!', false],
  39. // covers mirroring
  40. 'mirrored' => ['hello_world_mirrored.png', 'Hello world!', false],
  41. // data modes
  42. 'byte' => ['byte.png', 'https://smiley.codes/qrcode/', true],
  43. 'numeric' => ['numeric.png', '123456789012345678901234567890', false],
  44. 'alphanum' => ['alphanum.png', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 $%*+-./:', false],
  45. 'kanji' => ['kanji.png', '茗荷茗荷茗荷茗荷', false],
  46. // covers most of ReedSolomonDecoder
  47. 'damaged' => ['damaged.png', 'https://smiley.codes/qrcode/', false],
  48. // covers Binarizer::getHistogramBlackMatrix()
  49. 'smol' => ['smol.png', 'https://smiley.codes/qrcode/', false],
  50. // tilted 22° CCW
  51. 'tilted' => ['tilted.png', 'Hello world!', false],
  52. // rotated 90° CW
  53. 'rotated' => ['rotated.png', 'Hello world!', false],
  54. // color gradient (from old svg example)
  55. 'gradient' => ['example_svg.png', 'https://www.youtube.com/watch?v=DLzxrzFCyOs&t=43s', true],
  56. // color gradient (from svg example)
  57. 'dots' => ['example_svg_dots.png', 'https://www.youtube.com/watch?v=dQw4w9WgXcQ', true],
  58. ];
  59. }
  60. /**
  61. * @dataProvider qrCodeProvider
  62. */
  63. public function testReaderGD(string $img, string $expected, bool $grayscale):void{
  64. if($grayscale){
  65. $this->options->readerGrayscale = true;
  66. $this->options->readerIncreaseContrast = true;
  67. }
  68. $this::assertSame($expected, (string)(new QRCode)
  69. ->readFromSource(GDLuminanceSource::fromFile(__DIR__.'/samples/'.$img, $this->options)));
  70. }
  71. /**
  72. * @dataProvider qrCodeProvider
  73. */
  74. public function testReaderImagick(string $img, string $expected, bool $grayscale):void{
  75. if(!extension_loaded('imagick')){
  76. $this::markTestSkipped('imagick not installed');
  77. }
  78. if($grayscale){
  79. $this->options->readerGrayscale = true;
  80. $this->options->readerIncreaseContrast = true;
  81. }
  82. $this::assertSame($expected, (string)(new QRCode)
  83. ->readFromSource(IMagickLuminanceSource::fromFile(__DIR__.'/samples/'.$img, $this->options)));
  84. }
  85. public function testReaderMultiSegment():void{
  86. $this->options->outputType = QRCode::OUTPUT_IMAGE_PNG;
  87. $this->options->imageBase64 = false;
  88. $numeric = '123456789012345678901234567890';
  89. $alphanum = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 $%*+-./:';
  90. $kanji = '茗荷茗荷茗荷茗荷';
  91. $byte = 'https://smiley.codes/qrcode/';
  92. $qrcode = (new QRCode($this->options))
  93. ->addNumericSegment($numeric)
  94. ->addAlphaNumSegment($alphanum)
  95. ->addKanjiSegment($kanji)
  96. ->addByteSegment($byte)
  97. ;
  98. $this::assertSame($numeric.$alphanum.$kanji.$byte, (string)$qrcode->readFromBlob($qrcode->render()));
  99. }
  100. public function dataTestProvider():Generator{
  101. $str = str_repeat($this::loremipsum, 5);
  102. foreach(range(1, 40) as $v){
  103. $version = new Version($v);
  104. foreach([EccLevel::L, EccLevel::M, EccLevel::Q, EccLevel::H] as $ecc){
  105. $eccLevel = new EccLevel($ecc);
  106. $expected = substr($str, 0, $version->getMaxLengthForMode(Mode::BYTE, $eccLevel) ?? '');
  107. yield 'version: '.$version.$eccLevel => [$version, $eccLevel, $expected];
  108. }
  109. }
  110. }
  111. /**
  112. * @dataProvider dataTestProvider
  113. */
  114. public function testReadData(Version $version, EccLevel $ecc, string $expected):void{
  115. $this->options->outputType = QRCode::OUTPUT_IMAGE_PNG;
  116. # $this->options->imageTransparent = false;
  117. $this->options->eccLevel = $ecc->getLevel();
  118. $this->options->version = $version->getVersionNumber();
  119. $this->options->imageBase64 = false;
  120. $this->options->readerUseImagickIfAvailable = true;
  121. // what's interesting is that a smaller scale seems to produce fewer reader errors???
  122. // usually from version 20 up, independend of the luminance source
  123. // scale 1-2 produces none, scale 3: 1 error, scale 4: 6 errors, scale 5: 5 errors, scale 10: 10 errors
  124. // @see \chillerlan\QRCode\Detector\GridSampler::checkAndNudgePoints()
  125. $this->options->scale = 2;
  126. try{
  127. $qrcode = new QRCode($this->options);
  128. $imagedata = $qrcode->render($expected);
  129. $result = $qrcode->readFromBlob($imagedata);
  130. }
  131. catch(Exception $e){
  132. $this::markTestSkipped(sprintf('skipped version %s%s: %s', $version, $ecc, $e->getMessage()));
  133. }
  134. $this::assertSame($expected, $result->text);
  135. $this::assertSame($version->getVersionNumber(), $result->version->getVersionNumber());
  136. $this::assertSame($ecc->getLevel(), $result->eccLevel->getLevel());
  137. }
  138. }