QRCodeReaderTestAbstract.php 6.0 KB

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