From bcdb83c462e77ba13df02e1095b645254fa875a8 Mon Sep 17 00:00:00 2001 From: smiley Date: Fri, 19 Sep 2025 15:03:54 +0200 Subject: [PATCH] :octocat: DataInterfaceTestAbstract::testValidateString() added an additional test (see #313) (cherry picked from commit 01ff29439dc451d1ac34a577a63d1f9ceab57766) --- tests/Data/DataInterfaceTestAbstract.php | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/tests/Data/DataInterfaceTestAbstract.php b/tests/Data/DataInterfaceTestAbstract.php index be970cf78..df6221209 100644 --- a/tests/Data/DataInterfaceTestAbstract.php +++ b/tests/Data/DataInterfaceTestAbstract.php @@ -10,7 +10,7 @@ namespace chillerlan\QRCodeTest\Data; -use chillerlan\QRCode\Common\{EccLevel, MaskPattern, Mode, Version}; +use chillerlan\QRCode\Common\{BitBuffer, EccLevel, MaskPattern, Mode, Version}; use chillerlan\QRCode\Data\{QRCodeDataException, QRData, QRDataModeInterface, QRMatrix}; use chillerlan\QRCode\QROptions; use chillerlan\QRCodeTest\QRMaxLengthTrait; @@ -68,6 +68,24 @@ abstract class DataInterfaceTestAbstract extends TestCase{ */ public function testValidateString(string $string, bool $expected):void{ $this::assertSame($expected, $this->dataMode::validateString($string)); + + // back out on potentially invalid strings + if($expected === false){ + return; + } + + $dataModeInterface = static::getDataModeInterface($string); + $bitBuffer = new BitBuffer; + // check if the validated string encodes without error + // https://github.com/chillerlan/php-qrcode/pull/313 + $dataModeInterface->write($bitBuffer, 40); + $bitBuffer->rewind(); + // read 4 bits (data mode indicator) + $bitBuffer->read(4); + // decode and check against the given string + $decoded = $dataModeInterface::decodeSegment($bitBuffer, 40); + + $this::assertSame($string, $decoded); } /**