diff --git a/src/Data/QRData.php b/src/Data/QRData.php index 6468be920..10230b77e 100644 --- a/src/Data/QRData.php +++ b/src/Data/QRData.php @@ -164,9 +164,9 @@ public function estimateTotalBitLength():int{ // it seems that in some cases the estimated total length is not 100% accurate, // so we substract 4 bits from the total when not in mixed mode -# if(count($this->dataSegments) <= 1){ -# $length -= 4; -# } + if(count($this->dataSegments) <= 1){ + $length -= 4; + } // we've got a match! // or let's see if there's a higher version number available @@ -194,7 +194,7 @@ public function getMinimumVersion():Version{ // guess the version number within the given range for($version = $this->options->versionMin; $version <= $this->options->versionMax; $version++){ - if($total <= $this->maxBitsForEcc[$version]){ + if($total <= ($this->maxBitsForEcc[$version] - 4)){ return new Version($version); } } diff --git a/tests/Data/DataInterfaceTestAbstract.php b/tests/Data/DataInterfaceTestAbstract.php index 58632eecb..79fdbf673 100644 --- a/tests/Data/DataInterfaceTestAbstract.php +++ b/tests/Data/DataInterfaceTestAbstract.php @@ -16,6 +16,7 @@ use chillerlan\QRCodeTest\QRMaxLengthTrait; use Exception, Generator; use PHPUnit\Framework\Attributes\DataProvider; +use PHPUnit\Framework\ExpectationFailedException; use PHPUnit\Framework\TestCase; use function array_map, hex2bin, mb_strlen, mb_substr, sprintf, str_repeat, strlen, substr; @@ -178,7 +179,13 @@ public function testGetMinimumVersion(Version $version, EccLevel $eccLevel, stri $minimumVersionNumber = $this->QRData->getMinimumVersion()->getVersionNumber(); - $this::assertSame($version->getVersionNumber(), $minimumVersionNumber); + try{ + $this::assertSame($version->getVersionNumber(), $minimumVersionNumber); + } + catch(ExpectationFailedException $e){ + $this::assertSame(($version->getVersionNumber() + 1), $minimumVersionNumber, 'safety margin'); + } + // verify the encoded data $this::assertSame($this->dataMode::DATAMODE, $bitBuffer->read(4)); $this::assertSame($str, $this->dataMode::decodeSegment($bitBuffer, $minimumVersionNumber)); diff --git a/tests/Data/QRDataTest.php b/tests/Data/QRDataTest.php index 2bbc3d2a6..b47bc0f3d 100644 --- a/tests/Data/QRDataTest.php +++ b/tests/Data/QRDataTest.php @@ -86,7 +86,8 @@ public function testEstimateTotalBitLength():void{ $qrData = new QRData($options, [new Byte($str)]); - $this::assertSame(980, $qrData->estimateTotalBitLength()); + $this::assertSame(976, $qrData->estimateTotalBitLength()); + $this::assertSame(11, $qrData->getMinimumVersion()->getVersionNumber()); // version adjusted to 11 } }