QRCodeReaderTestAbstract.php 5.6 KB

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