mirror of
https://github.com/chillerlan/php-qrcode.git
synced 2026-08-23 03:23:46 +00:00
:octocat: improve minimum version detection and related tests
This commit is contained in:
@@ -10,23 +10,32 @@
|
||||
|
||||
namespace chillerlan\QRCodeTest\Data;
|
||||
|
||||
use chillerlan\QRCode\Common\{MaskPattern, Version};
|
||||
use chillerlan\QRCode\QROptions;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use chillerlan\QRCode\Common\{EccLevel, MaskPattern, Mode, Version};
|
||||
use chillerlan\QRCode\Data\{QRCodeDataException, QRData, QRDataModeInterface, QRMatrix};
|
||||
use ReflectionClass;
|
||||
use chillerlan\QRCode\QROptions;
|
||||
use chillerlan\QRCodeTest\QRMaxLengthTrait;
|
||||
use Exception, Generator;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
use function array_map;
|
||||
use function hex2bin;
|
||||
use function mb_strlen;
|
||||
use function mb_substr;
|
||||
use function sprintf;
|
||||
use function str_repeat;
|
||||
use function strlen;
|
||||
use function substr;
|
||||
|
||||
/**
|
||||
* The data interface test abstract
|
||||
*/
|
||||
abstract class DataInterfaceTestAbstract extends TestCase{
|
||||
use QRMaxLengthTrait;
|
||||
|
||||
protected QRData $QRData;
|
||||
protected string $FQN;
|
||||
protected string $testdata;
|
||||
protected QRData $QRData;
|
||||
protected QRDataModeInterface $dataMode;
|
||||
protected static string $FQN;
|
||||
protected static string $testdata;
|
||||
|
||||
protected function setUp():void{
|
||||
$this->QRData = new QRData(new QROptions);
|
||||
@@ -43,13 +52,10 @@ abstract class DataInterfaceTestAbstract extends TestCase{
|
||||
* Verifies the QRDataModeInterface instance
|
||||
*/
|
||||
public function testDataModeInstance():void{
|
||||
$datamode = new $this->FQN($this->testdata);
|
||||
|
||||
$this::assertInstanceOf(QRDataModeInterface::class, $datamode);
|
||||
$this::assertInstanceOf(QRDataModeInterface::class, new static::$FQN(static::$testdata));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see testInitMatrix()
|
||||
* @return int[][]
|
||||
*/
|
||||
public static function maskPatternProvider():array{
|
||||
@@ -62,7 +68,7 @@ abstract class DataInterfaceTestAbstract extends TestCase{
|
||||
* @dataProvider maskPatternProvider
|
||||
*/
|
||||
public function testInitMatrix(int $maskPattern):void{
|
||||
$this->QRData->setData([new $this->FQN($this->testdata)]);
|
||||
$this->QRData->setData([new static::$FQN(static::$testdata)]);
|
||||
|
||||
$matrix = $this->QRData->writeMatrix(new MaskPattern($maskPattern));
|
||||
|
||||
@@ -70,22 +76,6 @@ abstract class DataInterfaceTestAbstract extends TestCase{
|
||||
$this::assertSame($maskPattern, $matrix->maskPattern()->getPattern());
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests getting the minimum QR version for the given data
|
||||
*/
|
||||
public function testGetMinimumVersion():void{
|
||||
$this->QRData->setData([new $this->FQN($this->testdata)]);
|
||||
|
||||
$reflection = new ReflectionClass(QRData::class);
|
||||
$getMinimumVersion = $reflection->getMethod('getMinimumVersion');
|
||||
$getMinimumVersion->setAccessible(true);
|
||||
/** @var \chillerlan\QRCode\Common\Version $version */
|
||||
$version = $getMinimumVersion->invoke($this->QRData);
|
||||
|
||||
$this::assertInstanceOf(Version::class, $version);
|
||||
$this::assertSame(1, $version->getVersionNumber());
|
||||
}
|
||||
|
||||
abstract public static function stringValidateProvider():array;
|
||||
|
||||
/**
|
||||
@@ -95,7 +85,7 @@ abstract class DataInterfaceTestAbstract extends TestCase{
|
||||
*/
|
||||
public function testValidateString(string $string, bool $expected):void{
|
||||
/** @noinspection PhpUndefinedMethodInspection */
|
||||
$this::assertSame($expected, $this->FQN::validateString($string));
|
||||
$this::assertSame($expected, static::$FQN::validateString($string));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -105,7 +95,7 @@ abstract class DataInterfaceTestAbstract extends TestCase{
|
||||
*/
|
||||
public function testBinaryStringInvalid():void{
|
||||
/** @noinspection PhpUndefinedMethodInspection */
|
||||
$this::assertFalse($this->FQN::validateString(hex2bin('01015989f47dff8e852122117e04c90b9f15defc1c36477b1fe1')));
|
||||
$this::assertFalse(static::$FQN::validateString(hex2bin('01015989f47dff8e852122117e04c90b9f15defc1c36477b1fe1')));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -121,21 +111,114 @@ abstract class DataInterfaceTestAbstract extends TestCase{
|
||||
* @dataProvider versionBreakpointProvider
|
||||
*/
|
||||
public function testDecodeSegment(int $version):void{
|
||||
$options = new QROptions;
|
||||
$options = new QROptions;
|
||||
$options->version = $version;
|
||||
|
||||
// invoke a datamode interface
|
||||
/** @var \chillerlan\QRCode\Data\QRDataModeInterface $datamodeInterface */
|
||||
$datamodeInterface = new $this->FQN($this->testdata);
|
||||
$this->dataMode = new static::$FQN(static::$testdata);
|
||||
// invoke a QRData instance and write data
|
||||
$this->QRData = new QRData($options, [$datamodeInterface]);
|
||||
$this->QRData = new QRData($options, [$this->dataMode]);
|
||||
// get the filled bitbuffer
|
||||
$bitBuffer = $this->QRData->getBitBuffer();
|
||||
// read the first 4 bits
|
||||
$this::assertSame($datamodeInterface::DATAMODE, $bitBuffer->read(4));
|
||||
$this::assertSame($this->dataMode::DATAMODE, $bitBuffer->read(4));
|
||||
// decode the data
|
||||
/** @noinspection PhpUndefinedMethodInspection */
|
||||
$this::assertSame($this->testdata, $this->FQN::decodeSegment($bitBuffer, $options->version));
|
||||
$this::assertSame(static::$testdata, $this->dataMode::decodeSegment($bitBuffer, $options->version));
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates test data for each data mode:
|
||||
*
|
||||
* - version
|
||||
* - ECC level
|
||||
* - a string that contains the maximum amount of characters for the given mode
|
||||
* - a string that contains characters for the given mode and that exceeds the maximum length by one/two character(s)
|
||||
* - the maximum allowed character length
|
||||
*
|
||||
* @throws \Exception
|
||||
*/
|
||||
public static function maxLengthProvider():Generator{
|
||||
$eccLevels = array_map(fn(int $ecc):EccLevel => new EccLevel($ecc), [EccLevel::L, EccLevel::M, EccLevel::Q, EccLevel::H]);
|
||||
$str = str_repeat(static::$testdata, 1000);
|
||||
$mb = (static::$FQN::DATAMODE === Mode::KANJI || static::$FQN::DATAMODE === Mode::HANZI);
|
||||
|
||||
for($v = 1; $v <= 40; $v++){
|
||||
$version = new Version($v);
|
||||
|
||||
foreach($eccLevels as $eccLevel){
|
||||
// maximum characters per ecc/mode
|
||||
$len = static::getMaxLengthForMode(static::$FQN::DATAMODE, $version, $eccLevel);
|
||||
// a string that contains the maximum amount of characters for the given mode
|
||||
$val = ($mb) ? mb_substr($str, 0, $len) : substr($str, 0, $len);
|
||||
// same as above but character count exceeds
|
||||
// kanji/hanzi may have space for a single character, so we add 2 to make sure we exceed
|
||||
$val1 = ($mb) ? mb_substr($str, 0, ($len + 2)) : substr($str, 0, ($len + 1));
|
||||
// array key
|
||||
$key = sprintf('version: %s%s (%s)', $version, $eccLevel, $len);
|
||||
|
||||
if((($mb) ? mb_strlen($val) : strlen($val)) !== $len){
|
||||
throw new Exception('output length does not match');
|
||||
}
|
||||
|
||||
yield $key => [$version, $eccLevel, $val, $val1, $len];
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider maxLengthProvider
|
||||
*/
|
||||
public function testMaxLength(Version $version, EccLevel $eccLevel, string $str):void{
|
||||
$options = new QROptions;
|
||||
$options->version = $version->getVersionNumber();
|
||||
$options->eccLevel = $eccLevel->getLevel();
|
||||
$this->dataMode = new static::$FQN($str);
|
||||
$this->QRData = new QRData($options, [$this->dataMode]);
|
||||
$bitBuffer = $this->QRData->getBitBuffer();
|
||||
|
||||
$this::assertSame($this->dataMode::DATAMODE, $bitBuffer->read(4));
|
||||
$this::assertSame($str, $this->dataMode::decodeSegment($bitBuffer, $options->version));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests getting the minimum QR version for the given data
|
||||
*
|
||||
* @dataProvider maxLengthProvider
|
||||
*/
|
||||
public function testGetMinimumVersion(Version $version, EccLevel $eccLevel, string $str):void{
|
||||
$options = new QROptions;
|
||||
$options->version = Version::AUTO;
|
||||
$options->eccLevel = $eccLevel->getLevel();
|
||||
$this->dataMode = new static::$FQN($str);
|
||||
$this->QRData = new QRData($options, [$this->dataMode]);
|
||||
$bitBuffer = $this->QRData->getBitBuffer();
|
||||
|
||||
$this::assertLessThanOrEqual($eccLevel->getMaxBitsForVersion($version), $this->QRData->estimateTotalBitLength());
|
||||
|
||||
$minimumVersionNumber = $this->QRData->getMinimumVersion()->getVersionNumber();
|
||||
|
||||
$this::assertSame($version->getVersionNumber(), $minimumVersionNumber);
|
||||
// verify the encoded data
|
||||
$this::assertSame($this->dataMode::DATAMODE, $bitBuffer->read(4));
|
||||
$this::assertSame($str, $this->dataMode::decodeSegment($bitBuffer, $minimumVersionNumber));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests if an exception is thrown on data overflow
|
||||
*
|
||||
* @dataProvider maxLengthProvider
|
||||
*/
|
||||
public function testMaxLengthOverflowException(Version $version, EccLevel $eccLevel, string $str, string $str1):void{
|
||||
$this->expectException(QRCodeDataException::class);
|
||||
$this->expectExceptionMessage('code length overflow');
|
||||
|
||||
$options = new QROptions;
|
||||
$options->version = $version->getVersionNumber();
|
||||
$options->eccLevel = $eccLevel->getLevel();
|
||||
|
||||
/** @phan-suppress-next-line PhanNoopNew */
|
||||
new QRData($options, [new static::$FQN($str1)]);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -145,20 +228,7 @@ abstract class DataInterfaceTestAbstract extends TestCase{
|
||||
$this->expectException(QRCodeDataException::class);
|
||||
$this->expectExceptionMessage('data exceeds');
|
||||
|
||||
$this->QRData->setData([new $this->FQN(str_repeat($this->testdata, 1337))]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests if an exception is thrown on data overflow
|
||||
*/
|
||||
public function testCodeLengthOverflowException():void{
|
||||
$this->expectException(QRCodeDataException::class);
|
||||
$this->expectExceptionMessage('code length overflow');
|
||||
|
||||
$this->QRData = new QRData(
|
||||
new QROptions(['version' => 4]),
|
||||
[new $this->FQN(str_repeat($this->testdata, 1337))]
|
||||
);
|
||||
$this->QRData->setData([new static::$FQN(str_repeat(static::$testdata, 1337))]);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -169,7 +239,7 @@ abstract class DataInterfaceTestAbstract extends TestCase{
|
||||
$this->expectExceptionMessage('invalid data');
|
||||
|
||||
/** @phan-suppress-next-line PhanNoopNew */
|
||||
new $this->FQN('##');
|
||||
new static::$FQN('##');
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -180,7 +250,7 @@ abstract class DataInterfaceTestAbstract extends TestCase{
|
||||
$this->expectExceptionMessage('invalid data');
|
||||
|
||||
/** @phan-suppress-next-line PhanNoopNew */
|
||||
new $this->FQN('');
|
||||
new static::$FQN('');
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user