🚿 split reader test for GD and Imagick

This commit is contained in:
codemasher
2021-12-12 03:03:45 +01:00
parent 317fbb6f24
commit 7963d9f151
3 changed files with 71 additions and 31 deletions
+22
View File
@@ -0,0 +1,22 @@
<?php
/**
* Class QRCodeReaderGDTest
*
* @created 12.12.2021
* @author smiley <smiley@chillerlan.net>
* @copyright 2021 smiley
* @license MIT
*/
namespace chillerlan\QRCodeTest;
use chillerlan\QRCode\Decoder\GDLuminanceSource;
/**
* Tests the GD based reader
*/
final class QRCodeReaderGDTest extends QRCodeReaderTestAbstract{
protected string $FQN = GDLuminanceSource::class;
}
+35
View File
@@ -0,0 +1,35 @@
<?php
/**
* Class QRCodeReaderImagickTest
*
* @created 12.12.2021
* @author smiley <smiley@chillerlan.net>
* @copyright 2021 smiley
* @license MIT
*/
namespace chillerlan\QRCodeTest;
use chillerlan\QRCode\Decoder\IMagickLuminanceSource;
use function extension_loaded;
/**
* Tests the Imagick based reader
*/
final class QRCodeReaderImagickTest extends QRCodeReaderTestAbstract{
protected string $FQN = IMagickLuminanceSource::class;
protected function setUp():void{
if(!extension_loaded('imagick')){
$this::markTestSkipped('imagick not installed');
}
parent::setUp();
$this->options->readerUseImagickIfAvailable = true;
}
}
@@ -1,6 +1,6 @@
<?php
/**
* Class QRCodeReaderTest
* Class QRCodeReaderTestAbstract
*
* @created 17.01.2021
* @author Smiley <smiley@chillerlan.net>
@@ -16,14 +16,13 @@ use chillerlan\Settings\SettingsContainerInterface;
use Exception, Generator;
use chillerlan\QRCode\Common\{EccLevel, Mode, Version};
use chillerlan\QRCode\{QRCode, QROptions};
use chillerlan\QRCode\Decoder\{GDLuminanceSource, IMagickLuminanceSource};
use PHPUnit\Framework\TestCase;
use function extension_loaded, range, sprintf, str_repeat, substr;
use function range, sprintf, str_repeat, substr;
/**
* Tests the QR Code reader
*/
final class QRCodeReaderTest extends TestCase{
abstract class QRCodeReaderTestAbstract extends TestCase{
// https://www.bobrosslipsum.com/
protected const loremipsum = 'Just let this happen. We just let this flow right out of our minds. '
@@ -35,9 +34,11 @@ final class QRCodeReaderTest extends TestCase{
.'to upset the critics. We\'ll lay all these little funky little things in there. ';
protected SettingsContainerInterface $options;
protected string $FQN;
protected function setUp():void{
$this->options = new QROptions;
$this->options->readerUseImagickIfAvailable = false;
}
public function qrCodeProvider():array{
@@ -68,33 +69,16 @@ final class QRCodeReaderTest extends TestCase{
/**
* @dataProvider qrCodeProvider
*/
public function testReaderGD(string $img, string $expected, bool $grayscale):void{
public function testReader(string $img, string $expected, bool $grayscale):void{
if($grayscale){
$this->options->readerGrayscale = true;
$this->options->readerIncreaseContrast = true;
}
/** @noinspection PhpUndefinedMethodInspection */
$this::assertSame($expected, (string)(new QRCode)
->readFromSource(GDLuminanceSource::fromFile(__DIR__.'/samples/'.$img, $this->options)));
}
/**
* @dataProvider qrCodeProvider
*/
public function testReaderImagick(string $img, string $expected, bool $grayscale):void{
if(!extension_loaded('imagick')){
$this::markTestSkipped('imagick not installed');
}
if($grayscale){
$this->options->readerGrayscale = true;
$this->options->readerIncreaseContrast = true;
}
$this::assertSame($expected, (string)(new QRCode)
->readFromSource(IMagickLuminanceSource::fromFile(__DIR__.'/samples/'.$img, $this->options)));
->readFromSource($this->FQN::fromFile(__DIR__.'/samples/'.$img, $this->options)));
}
public function testReaderMultiSegment():void{
@@ -136,17 +120,16 @@ final class QRCodeReaderTest extends TestCase{
* @dataProvider dataTestProvider
*/
public function testReadData(Version $version, EccLevel $ecc, string $expected):void{
$this->options->outputType = QRCode::OUTPUT_IMAGE_PNG;
# $this->options->imageTransparent = false;
$this->options->eccLevel = $ecc->getLevel();
$this->options->version = $version->getVersionNumber();
$this->options->imageBase64 = false;
$this->options->readerUseImagickIfAvailable = true;
$this->options->outputType = QRCode::OUTPUT_IMAGE_PNG;
$this->options->imageTransparent = false;
$this->options->eccLevel = $ecc->getLevel();
$this->options->version = $version->getVersionNumber();
$this->options->imageBase64 = false;
// what's interesting is that a smaller scale seems to produce fewer reader errors???
// usually from version 20 up, independend of the luminance source
// scale 1-2 produces none, scale 3: 1 error, scale 4: 6 errors, scale 5: 5 errors, scale 10: 10 errors
// @see \chillerlan\QRCode\Detector\GridSampler::checkAndNudgePoints()
$this->options->scale = 2;
$this->options->scale = 2;
try{
$qrcode = new QRCode($this->options);