QRCodeReaderTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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;
  15. use chillerlan\QRCode\Common\{EccLevel, Mode, Version};
  16. use chillerlan\QRCode\{QRCode, QROptions};
  17. use PHPUnit\Framework\TestCase;
  18. use function extension_loaded, range, str_repeat, substr;
  19. /**
  20. * Tests the QR Code reader
  21. */
  22. class QRCodeReaderTest extends TestCase{
  23. // https://www.bobrosslipsum.com/
  24. protected const loremipsum = 'Just let this happen. We just let this flow right out of our minds. '
  25. .'Anyone can paint. We touch the canvas, the canvas takes what it wants. From all of us here, '
  26. .'I want to wish you happy painting and God bless, my friends. A tree cannot be straight if it has a crooked trunk. '
  27. .'You have to make almighty decisions when you\'re the creator. I guess that would be considered a UFO. '
  28. .'A big cotton ball in the sky. I\'m gonna add just a tiny little amount of Prussian Blue. '
  29. .'They say everything looks better with odd numbers of things. But sometimes I put even numbers—just '
  30. .'to upset the critics. We\'ll lay all these little funky little things in there. ';
  31. private SettingsContainerInterface $options;
  32. protected function setUp():void{
  33. $this->options = new QROptions;
  34. }
  35. public function qrCodeProvider():array{
  36. return [
  37. 'helloworld' => ['hello_world.png', 'Hello world!'],
  38. // covers mirroring
  39. 'mirrored' => ['hello_world_mirrored.png', 'Hello world!'],
  40. // data modes
  41. 'byte' => ['byte.png', 'https://smiley.codes/qrcode/'],
  42. 'numeric' => ['numeric.png', '123456789012345678901234567890'],
  43. 'alphanum' => ['alphanum.png', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 $%*+-./:'],
  44. 'kanji' => ['kanji.png', '茗荷茗荷茗荷茗荷'],
  45. // covers most of ReedSolomonDecoder
  46. 'damaged' => ['damaged.png', 'https://smiley.codes/qrcode/'],
  47. // covers Binarizer::getHistogramBlackMatrix()
  48. 'smol' => ['smol.png', 'https://smiley.codes/qrcode/'],
  49. 'tilted' => ['tilted.png', 'Hello world!'], // tilted 22° CCW
  50. 'rotated' => ['rotated.png', 'Hello world!'], // rotated 90° CW
  51. 'gradient' => ['example_svg.png', 'https://www.youtube.com/watch?v=DLzxrzFCyOs&t=43s'], // color gradient (from svg example)
  52. ];
  53. }
  54. /**
  55. * @dataProvider qrCodeProvider
  56. */
  57. public function testReaderGD(string $img, string $expected):void{
  58. $this->options->useImagickIfAvailable = false;
  59. $reader = new QRCode($this->options);
  60. $this::assertSame($expected, (string)$reader->readFromFile(__DIR__.'/qrcodes/'.$img));
  61. }
  62. /**
  63. * @dataProvider qrCodeProvider
  64. */
  65. public function testReaderImagick(string $img, string $expected):void{
  66. if(!extension_loaded('imagick')){
  67. $this::markTestSkipped('imagick not installed');
  68. }
  69. $this->options->useImagickIfAvailable = true;
  70. $reader = new QRCode($this->options);
  71. $this::assertSame($expected, (string)$reader->readFromFile(__DIR__.'/qrcodes/'.$img));
  72. }
  73. public function dataTestProvider():array{
  74. $data = [];
  75. $str = str_repeat($this::loremipsum, 5);
  76. foreach(range(1, 40) as $v){
  77. $version = new Version($v);
  78. foreach(EccLevel::MODES as $ecc => $_){
  79. $eccLevel = new EccLevel($ecc);
  80. $data['version: '.$version.$eccLevel] = [
  81. $version,
  82. $eccLevel,
  83. /** @phan-suppress-next-line PhanTypeMismatchArgumentNullableInternal */
  84. substr($str, 0, $version->getMaxLengthForMode(Mode::DATA_BYTE, $eccLevel))
  85. ];
  86. }
  87. }
  88. return $data;
  89. }
  90. /**
  91. * @dataProvider dataTestProvider
  92. */
  93. public function testReadData(Version $version, EccLevel $ecc, string $expected):void{
  94. # $this->options->imageTransparent = false;
  95. $this->options->eccLevel = $ecc->getLevel();
  96. $this->options->version = $version->getVersionNumber();
  97. $this->options->imageBase64 = false;
  98. $this->options->scale = 1; // what's interesting is that a smaller scale seems to produce fewer reader errors???
  99. $this->options->useImagickIfAvailable = true;
  100. try{
  101. $qrcode = new QRCode($this->options);
  102. $imagedata = $qrcode->render($expected);
  103. $result = $qrcode->readFromBlob($imagedata);
  104. }
  105. catch(Exception $e){
  106. $this::markTestSkipped($version.$ecc.': '.$e->getMessage());
  107. }
  108. $this::assertSame($expected, $result->getText());
  109. $this::assertSame($version->getVersionNumber(), $result->getVersion()->getVersionNumber());
  110. $this::assertSame($ecc->getLevel(), $result->getEccLevel()->getLevel());
  111. }
  112. }