Skip to content

Commit 7963d9f

Browse files
committed
🚿 split reader test for GD and Imagick
1 parent 317fbb6 commit 7963d9f

File tree

3 files changed

+71
-31
lines changed

3 files changed

+71
-31
lines changed

tests/QRCodeReaderGDTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
/**
3+
* Class QRCodeReaderGDTest
4+
*
5+
* @created 12.12.2021
6+
* @author smiley <[email protected]>
7+
* @copyright 2021 smiley
8+
* @license MIT
9+
*/
10+
11+
namespace chillerlan\QRCodeTest;
12+
13+
use chillerlan\QRCode\Decoder\GDLuminanceSource;
14+
15+
/**
16+
* Tests the GD based reader
17+
*/
18+
final class QRCodeReaderGDTest extends QRCodeReaderTestAbstract{
19+
20+
protected string $FQN = GDLuminanceSource::class;
21+
22+
}

tests/QRCodeReaderImagickTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
/**
3+
* Class QRCodeReaderImagickTest
4+
*
5+
* @created 12.12.2021
6+
* @author smiley <[email protected]>
7+
* @copyright 2021 smiley
8+
* @license MIT
9+
*/
10+
11+
namespace chillerlan\QRCodeTest;
12+
13+
use chillerlan\QRCode\Decoder\IMagickLuminanceSource;
14+
use function extension_loaded;
15+
16+
/**
17+
* Tests the Imagick based reader
18+
*/
19+
final class QRCodeReaderImagickTest extends QRCodeReaderTestAbstract{
20+
21+
protected string $FQN = IMagickLuminanceSource::class;
22+
23+
24+
protected function setUp():void{
25+
26+
if(!extension_loaded('imagick')){
27+
$this::markTestSkipped('imagick not installed');
28+
}
29+
30+
parent::setUp();
31+
32+
$this->options->readerUseImagickIfAvailable = true;
33+
}
34+
35+
}

tests/QRCodeReaderTest.php renamed to tests/QRCodeReaderTestAbstract.php

Lines changed: 14 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* Class QRCodeReaderTest
3+
* Class QRCodeReaderTestAbstract
44
*
55
* @created 17.01.2021
66
* @author Smiley <[email protected]>
@@ -16,14 +16,13 @@
1616
use Exception, Generator;
1717
use chillerlan\QRCode\Common\{EccLevel, Mode, Version};
1818
use chillerlan\QRCode\{QRCode, QROptions};
19-
use chillerlan\QRCode\Decoder\{GDLuminanceSource, IMagickLuminanceSource};
2019
use PHPUnit\Framework\TestCase;
21-
use function extension_loaded, range, sprintf, str_repeat, substr;
20+
use function range, sprintf, str_repeat, substr;
2221

2322
/**
2423
* Tests the QR Code reader
2524
*/
26-
final class QRCodeReaderTest extends TestCase{
25+
abstract class QRCodeReaderTestAbstract extends TestCase{
2726

2827
// https://www.bobrosslipsum.com/
2928
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{
3534
.'to upset the critics. We\'ll lay all these little funky little things in there. ';
3635

3736
protected SettingsContainerInterface $options;
37+
protected string $FQN;
3838

3939
protected function setUp():void{
4040
$this->options = new QROptions;
41+
$this->options->readerUseImagickIfAvailable = false;
4142
}
4243

4344
public function qrCodeProvider():array{
@@ -68,33 +69,16 @@ public function qrCodeProvider():array{
6869
/**
6970
* @dataProvider qrCodeProvider
7071
*/
71-
public function testReaderGD(string $img, string $expected, bool $grayscale):void{
72+
public function testReader(string $img, string $expected, bool $grayscale):void{
7273

7374
if($grayscale){
7475
$this->options->readerGrayscale = true;
7576
$this->options->readerIncreaseContrast = true;
7677
}
7778

79+
/** @noinspection PhpUndefinedMethodInspection */
7880
$this::assertSame($expected, (string)(new QRCode)
79-
->readFromSource(GDLuminanceSource::fromFile(__DIR__.'/samples/'.$img, $this->options)));
80-
}
81-
82-
/**
83-
* @dataProvider qrCodeProvider
84-
*/
85-
public function testReaderImagick(string $img, string $expected, bool $grayscale):void{
86-
87-
if(!extension_loaded('imagick')){
88-
$this::markTestSkipped('imagick not installed');
89-
}
90-
91-
if($grayscale){
92-
$this->options->readerGrayscale = true;
93-
$this->options->readerIncreaseContrast = true;
94-
}
95-
96-
$this::assertSame($expected, (string)(new QRCode)
97-
->readFromSource(IMagickLuminanceSource::fromFile(__DIR__.'/samples/'.$img, $this->options)));
81+
->readFromSource($this->FQN::fromFile(__DIR__.'/samples/'.$img, $this->options)));
9882
}
9983

10084
public function testReaderMultiSegment():void{
@@ -136,17 +120,16 @@ public function dataTestProvider():Generator{
136120
* @dataProvider dataTestProvider
137121
*/
138122
public function testReadData(Version $version, EccLevel $ecc, string $expected):void{
139-
$this->options->outputType = QRCode::OUTPUT_IMAGE_PNG;
140-
# $this->options->imageTransparent = false;
141-
$this->options->eccLevel = $ecc->getLevel();
142-
$this->options->version = $version->getVersionNumber();
143-
$this->options->imageBase64 = false;
144-
$this->options->readerUseImagickIfAvailable = true;
123+
$this->options->outputType = QRCode::OUTPUT_IMAGE_PNG;
124+
$this->options->imageTransparent = false;
125+
$this->options->eccLevel = $ecc->getLevel();
126+
$this->options->version = $version->getVersionNumber();
127+
$this->options->imageBase64 = false;
145128
// what's interesting is that a smaller scale seems to produce fewer reader errors???
146129
// usually from version 20 up, independend of the luminance source
147130
// scale 1-2 produces none, scale 3: 1 error, scale 4: 6 errors, scale 5: 5 errors, scale 10: 10 errors
148131
// @see \chillerlan\QRCode\Detector\GridSampler::checkAndNudgePoints()
149-
$this->options->scale = 2;
132+
$this->options->scale = 2;
150133

151134
try{
152135
$qrcode = new QRCode($this->options);

0 commit comments

Comments
 (0)