From 59318bcd7cbb5287aac50dc2d9d403c0230d26b0 Mon Sep 17 00:00:00 2001 From: smiley Date: Mon, 25 Jul 2022 10:50:21 +0200 Subject: [PATCH] :shower: --- src/Common/MaskPattern.php | 12 ++++++------ src/QROptionsTrait.php | 6 +----- tests/Common/MaskPatternTest.php | 7 ++++++- tests/Data/DatainterfaceTestAbstract.php | 15 +++++++++++++-- tests/Data/QRMatrixTest.php | 20 +++++++------------- tests/QRCodeTest.php | 2 +- tests/QROptionsTest.php | 2 ++ 7 files changed, 36 insertions(+), 28 deletions(-) diff --git a/src/Common/MaskPattern.php b/src/Common/MaskPattern.php index dea00543d..569d4e0a3 100644 --- a/src/Common/MaskPattern.php +++ b/src/Common/MaskPattern.php @@ -137,15 +137,15 @@ final class MaskPattern{ */ private static function applyRule1(array $matrix, int $height, int $width, bool $isHorizontal):int{ $penalty = 0; - $iLimit = $isHorizontal ? $height : $width; - $jLimit = $isHorizontal ? $width : $height; + $yLimit = $isHorizontal ? $height : $width; + $xLimit = $isHorizontal ? $width : $height; - for($i = 0; $i < $iLimit; $i++){ + for($y = 0; $y < $yLimit; $y++){ $numSameBitCells = 0; - $prevBit = -1; + $prevBit = null; - for($j = 0; $j < $jLimit; $j++){ - $bit = $isHorizontal ? $matrix[$i][$j] : $matrix[$j][$i]; + for($x = 0; $x < $xLimit; $x++){ + $bit = $isHorizontal ? $matrix[$y][$x] : $matrix[$x][$y]; if($bit === $prevBit){ $numSameBitCells++; diff --git a/src/QROptionsTrait.php b/src/QROptionsTrait.php index 8d40e2acf..7a041ee0c 100644 --- a/src/QROptionsTrait.php +++ b/src/QROptionsTrait.php @@ -370,11 +370,7 @@ trait QROptionsTrait{ * sets/clamps the version number */ protected function set_version(int $version):void{ - - if($version !== Version::AUTO){ - $this->version = max(1, min(40, $version)); - } - + $this->version = $version !== Version::AUTO ? max(1, min(40, $version)) : Version::AUTO; // @todo } /** diff --git a/tests/Common/MaskPatternTest.php b/tests/Common/MaskPatternTest.php index 96ae86625..48dbfc2c7 100644 --- a/tests/Common/MaskPatternTest.php +++ b/tests/Common/MaskPatternTest.php @@ -92,6 +92,8 @@ final class MaskPatternTest extends TestCase{ } /** + * Tests if the mask function generates the correct pattern + * * @dataProvider maskPatternProvider */ public function testMask(int $pattern, array $expected):void{ @@ -104,7 +106,7 @@ final class MaskPatternTest extends TestCase{ for($x = 0; $x < 6; $x++){ for($y = 0; $y < 6; $y++){ - if(($expected[$y][$x] === 1) !== $mask($x, $y)){ + if($mask($x, $y) !== ($expected[$y][$x] === 1)){ return false; } } @@ -113,6 +115,9 @@ final class MaskPatternTest extends TestCase{ return true; } + /** + * Tests if an exception is thrown on an incorrect mask pattern + */ public function testInvalidMaskPatternException():void{ $this->expectException(QRCodeException::class); $this->expectExceptionMessage('invalid mask pattern'); diff --git a/tests/Data/DatainterfaceTestAbstract.php b/tests/Data/DatainterfaceTestAbstract.php index 70db90f29..1d0acbdc0 100644 --- a/tests/Data/DatainterfaceTestAbstract.php +++ b/tests/Data/DatainterfaceTestAbstract.php @@ -13,7 +13,7 @@ namespace chillerlan\QRCodeTest\Data; use chillerlan\QRCode\Common\{MaskPattern, Version}; use chillerlan\QRCode\QROptions; use PHPUnit\Framework\TestCase; -use chillerlan\QRCode\Data\{QRCodeDataException, QRData, QRMatrix}; +use chillerlan\QRCode\Data\{QRCodeDataException, QRData, QRDataModeInterface, QRMatrix}; use ReflectionClass; use function str_repeat; @@ -34,12 +34,21 @@ abstract class DatainterfaceTestAbstract extends TestCase{ } /** - * Verifies the data interface instance + * Verifies the QRData instance */ public function testInstance():void{ $this::assertInstanceOf(QRData::class, $this->QRData); } + /** + * Verifies the QRDataModeInterface instance + */ + public function testDataModeInstance():void{ + $datamode = new $this->FQN($this->testdata); + + $this::assertInstanceOf(QRDataModeInterface::class, $datamode); + } + /** * @see testInitMatrix() * @return int[][] @@ -77,6 +86,8 @@ abstract class DatainterfaceTestAbstract extends TestCase{ abstract public function stringValidateProvider():array; /** + * Tests if a string is properly validated for the respective data mode + * * @dataProvider stringValidateProvider */ public function testValidateString(string $string, bool $expected):void{ diff --git a/tests/Data/QRMatrixTest.php b/tests/Data/QRMatrixTest.php index 69b429b3f..54e2f4a85 100755 --- a/tests/Data/QRMatrixTest.php +++ b/tests/Data/QRMatrixTest.php @@ -228,13 +228,10 @@ final class QRMatrixTest extends TestCase{ foreach($alignmentPattern as $py){ foreach($alignmentPattern as $px){ - - if($matrix->checkType($px, $py, QRMatrix::M_FINDER)){ - // skip finder pattern - continue; + // skip finder pattern + if($matrix->checkTypeNotIn($px, $py, [QRMatrix::M_FINDER, QRMatrix::M_FINDER_DOT])){ + $this::assertSame(QRMatrix::M_ALIGNMENT | QRMatrix::IS_DARK, $matrix->get($px, $py)); } - - $this::assertSame(QRMatrix::M_ALIGNMENT | QRMatrix::IS_DARK, $matrix->get($px, $py)); } } @@ -258,14 +255,11 @@ final class QRMatrixTest extends TestCase{ for($i = 7; $i < $size - 7; $i++){ if($i % 2 === 0){ - - if($matrix->checkType(6, $i, QRMatrix::M_ALIGNMENT)){ - // skip alignment pattern - continue; + // skip alignment pattern + if($matrix->checkTypeNotIn(6, $i, [QRMatrix::M_ALIGNMENT])){ + $this::assertSame(QRMatrix::M_TIMING | QRMatrix::IS_DARK, $matrix->get(6, $i)); + $this::assertSame(QRMatrix::M_TIMING | QRMatrix::IS_DARK, $matrix->get($i, 6)); } - - $this::assertSame(QRMatrix::M_TIMING | QRMatrix::IS_DARK, $matrix->get(6, $i)); - $this::assertSame(QRMatrix::M_TIMING | QRMatrix::IS_DARK, $matrix->get($i, 6)); } } diff --git a/tests/QRCodeTest.php b/tests/QRCodeTest.php index 0580baa56..57b47be03 100755 --- a/tests/QRCodeTest.php +++ b/tests/QRCodeTest.php @@ -34,7 +34,7 @@ final class QRCodeTest extends TestCase{ /** * tests if an exception is thrown when an invalid (built-in) output type is specified */ - public function testInitDataInterfaceException():void{ + public function testInitOutputInterfaceException():void{ $this->expectException(QRCodeOutputException::class); $this->expectExceptionMessage('invalid output type'); diff --git a/tests/QROptionsTest.php b/tests/QROptionsTest.php index add8403a1..51f076b79 100644 --- a/tests/QROptionsTest.php +++ b/tests/QROptionsTest.php @@ -191,6 +191,8 @@ final class QROptionsTest extends TestCase{ } /** + * Tests clamping of the circle radius + * * @dataProvider circleRadiusProvider */ public function testClampCircleRadius(float $value, float $expected):void{