QRCodeReaderTest.php 4.0 KB

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