From 7963d9f15108178c8dc11ede66400c54d4f8d805 Mon Sep 17 00:00:00 2001 From: codemasher Date: Sun, 12 Dec 2021 03:03:45 +0100 Subject: [PATCH] :shower: split reader test for GD and Imagick --- tests/QRCodeReaderGDTest.php | 22 +++++++++ tests/QRCodeReaderImagickTest.php | 35 +++++++++++++++ ...rTest.php => QRCodeReaderTestAbstract.php} | 45 ++++++------------- 3 files changed, 71 insertions(+), 31 deletions(-) create mode 100644 tests/QRCodeReaderGDTest.php create mode 100644 tests/QRCodeReaderImagickTest.php rename tests/{QRCodeReaderTest.php => QRCodeReaderTestAbstract.php} (78%) diff --git a/tests/QRCodeReaderGDTest.php b/tests/QRCodeReaderGDTest.php new file mode 100644 index 000000000..4d5b4f8f5 --- /dev/null +++ b/tests/QRCodeReaderGDTest.php @@ -0,0 +1,22 @@ + + * @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; + +} diff --git a/tests/QRCodeReaderImagickTest.php b/tests/QRCodeReaderImagickTest.php new file mode 100644 index 000000000..ac21512c9 --- /dev/null +++ b/tests/QRCodeReaderImagickTest.php @@ -0,0 +1,35 @@ + + * @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; + } + +} diff --git a/tests/QRCodeReaderTest.php b/tests/QRCodeReaderTestAbstract.php similarity index 78% rename from tests/QRCodeReaderTest.php rename to tests/QRCodeReaderTestAbstract.php index 26f4cbbed..6e581f8fb 100644 --- a/tests/QRCodeReaderTest.php +++ b/tests/QRCodeReaderTestAbstract.php @@ -1,6 +1,6 @@ @@ -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);