QRCodeReaderTestAbstract.php 6.6 KB

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