QRCodeReaderTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. ];
  52. }
  53. /**
  54. * @dataProvider qrCodeProvider
  55. */
  56. public function testReaderGD(string $img, string $expected):void{
  57. $this->options->useImagickIfAvailable = false;
  58. $reader = new QRCode($this->options);
  59. $this::assertSame($expected, (string)$reader->readFromFile(__DIR__.'/qrcodes/'.$img));
  60. }
  61. /**
  62. * @dataProvider qrCodeProvider
  63. */
  64. public function testReaderImagick(string $img, string $expected):void{
  65. if(!extension_loaded('imagick')){
  66. $this::markTestSkipped('imagick not installed');
  67. }
  68. $this->options->useImagickIfAvailable = true;
  69. $reader = new QRCode($this->options);
  70. $this::assertSame($expected, (string)$reader->readFromFile(__DIR__.'/qrcodes/'.$img));
  71. }
  72. public function dataTestProvider():array{
  73. $data = [];
  74. $str = str_repeat($this::loremipsum, 5);
  75. foreach(range(1, 40) as $v){
  76. $version = new Version($v);
  77. foreach(EccLevel::MODES as $ecc => $_){
  78. $eccLevel = new EccLevel($ecc);
  79. $data['version: '.$version.$eccLevel] = [
  80. $version,
  81. $eccLevel,
  82. /** @phan-suppress-next-line PhanTypeMismatchArgumentNullableInternal */
  83. substr($str, 0, $version->getMaxLengthForMode(Mode::DATA_BYTE, $eccLevel))
  84. ];
  85. }
  86. }
  87. return $data;
  88. }
  89. /**
  90. * @dataProvider dataTestProvider
  91. */
  92. public function testReadData(Version $version, EccLevel $ecc, string $expected):void{
  93. # $this->options->imageTransparent = false;
  94. $this->options->eccLevel = $ecc->getLevel();
  95. $this->options->version = $version->getVersionNumber();
  96. $this->options->imageBase64 = false;
  97. $this->options->scale = 1; // what's interesting is that a smaller scale seems to produce fewer reader errors???
  98. $this->options->useImagickIfAvailable = true;
  99. try{
  100. $qrcode = new QRCode($this->options);
  101. $imagedata = $qrcode->render($expected);
  102. $result = $qrcode->readFromBlob($imagedata);
  103. }
  104. catch(Exception $e){
  105. $this::markTestSkipped($version.$ecc.': '.$e->getMessage());
  106. }
  107. $this::assertSame($expected, $result->getText());
  108. $this::assertSame($version->getVersionNumber(), $result->getVersion()->getVersionNumber());
  109. $this::assertSame($ecc->getLevel(), $result->getEccLevel()->getLevel());
  110. }
  111. }