mirror of
https://github.com/chillerlan/php-qrcode.git
synced 2026-08-28 02:57:57 +00:00
🛀 test overhaul
This commit is contained in:
+33
-72
@@ -15,7 +15,7 @@ namespace chillerlan\QRCodeTest;
|
||||
|
||||
use chillerlan\QRCode\{QRCodeException, QROptions};
|
||||
use chillerlan\QRCode\Common\{EccLevel, Version};
|
||||
use PHPUnit\Framework\Attributes\DataProvider;
|
||||
use PHPUnit\Framework\Attributes\{Test, TestWith};
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
@@ -23,45 +23,29 @@ use PHPUnit\Framework\TestCase;
|
||||
*/
|
||||
final class QROptionsTest extends TestCase{
|
||||
|
||||
/**
|
||||
* @return int[][]
|
||||
*/
|
||||
public static function VersionProvider():array{
|
||||
return [
|
||||
'values > 40 should be clamped to 40' => [42, 40],
|
||||
'values < 1 should be clamped to 1' => [-42, 1],
|
||||
'values in between shold not be touched' => [21, 21],
|
||||
'value -1 should be treated as is (default)' => [Version::AUTO, -1],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the $version clamping
|
||||
*/
|
||||
#[DataProvider('VersionProvider')]
|
||||
public function testVersionClamp(int $version, int $expected):void{
|
||||
#[Test]
|
||||
#[TestWith([42, 40], 'values > 40 should be clamped to 40')]
|
||||
#[TestWith([-42, 1], 'values < 1 should be clamped to 1')]
|
||||
#[TestWith([21, 21], 'values in between should not be touched')]
|
||||
#[TestWith([Version::AUTO, -1], 'value -1 should be treated as is (default)')]
|
||||
public function versionClamp(int $version, int $expected):void{
|
||||
$o = new QROptions(['version' => $version]);
|
||||
|
||||
$this::assertSame($expected, $o->version);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int[][]
|
||||
*/
|
||||
public static function VersionMinMaxProvider():array{
|
||||
return [
|
||||
'normal clamp' => [5, 10, 5, 10],
|
||||
'exceeding values' => [-42, 42, 1, 40],
|
||||
'min > max' => [10, 5, 5, 10],
|
||||
'min > max, exceeding' => [42, -42, 1, 40],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the $versionMin/$versionMax clamping
|
||||
*/
|
||||
#[DataProvider('VersionMinMaxProvider')]
|
||||
public function testVersionMinMaxClamp(int $versionMin, int $versionMax, int $expectedMin, int $expectedMax):void{
|
||||
#[Test]
|
||||
#[TestWith([5, 10, 5, 10], 'normal clamp')]
|
||||
#[TestWith([-42, 42, 1, 40], 'exceeding values')]
|
||||
#[TestWith([10, 5, 5, 10], 'min > max' )]
|
||||
#[TestWith([42, -42, 1, 40], 'min > max, exceeding')]
|
||||
public function versionMinMaxClamp(int $versionMin, int $versionMax, int $expectedMin, int $expectedMax):void{
|
||||
$o = new QROptions(['versionMin' => $versionMin, 'versionMax' => $versionMax]);
|
||||
|
||||
$this::assertSame($expectedMin, $o->versionMin);
|
||||
@@ -73,7 +57,8 @@ final class QROptionsTest extends TestCase{
|
||||
*
|
||||
* @phan-suppress PhanTypeMismatchPropertyProbablyReal
|
||||
*/
|
||||
public function testSetEccLevel():void{
|
||||
#[Test]
|
||||
public function setEccLevel():void{
|
||||
$o = new QROptions(['eccLevel' => EccLevel::H]);
|
||||
|
||||
$this::assertSame(EccLevel::H, $o->eccLevel);
|
||||
@@ -86,7 +71,8 @@ final class QROptionsTest extends TestCase{
|
||||
/**
|
||||
* Tests if an exception is thrown when attempting to set an invalid ECC level integer
|
||||
*/
|
||||
public function testSetEccLevelFromIntException():void{
|
||||
#[Test]
|
||||
public function setEccLevelFromIntException():void{
|
||||
$this->expectException(QRCodeException::class);
|
||||
$this->expectExceptionMessage('Invalid ECC level: "42"');
|
||||
|
||||
@@ -96,42 +82,24 @@ final class QROptionsTest extends TestCase{
|
||||
/**
|
||||
* Tests if an exception is thrown when attempting to set an invalid ECC level string
|
||||
*/
|
||||
public function testSetEccLevelFromStringException():void{
|
||||
#[Test]
|
||||
public function setEccLevelFromStringException():void{
|
||||
$this->expectException(QRCodeException::class);
|
||||
$this->expectExceptionMessage('Invalid ECC level: "FOO"');
|
||||
|
||||
new QROptions(['eccLevel' => 'foo']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int[][][]
|
||||
*/
|
||||
public static function RGBProvider():array{
|
||||
return [
|
||||
'exceeding values' => [[-1, 0, 999], [0, 0 ,255]],
|
||||
'too few values' => [[1, 2], [255, 255, 255]],
|
||||
'too many values' => [[1, 2, 3, 4, 5], [1, 2, 3]],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int[][]
|
||||
*/
|
||||
public static function logoSpaceValueProvider():array{
|
||||
return [
|
||||
'negative' => [ -1, 0],
|
||||
'zero' => [ 0, 0],
|
||||
'normal' => [ 69, 69],
|
||||
'max' => [177, 177],
|
||||
'exceed' => [178, 177],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the clamping (between 0 and 177) of the logo space values
|
||||
*/
|
||||
#[DataProvider('logoSpaceValueProvider')]
|
||||
public function testClampLogoSpaceValue(int $value, int $expected):void{
|
||||
#[Test]
|
||||
#[TestWith([ -1, 0], 'negative')]
|
||||
#[TestWith([ 0, 0], 'zero')]
|
||||
#[TestWith([ 69, 69], 'normal')]
|
||||
#[TestWith([177, 177], 'max')]
|
||||
#[TestWith([178, 177], 'exceed')]
|
||||
public function clampLogoSpaceValue(int $value, int $expected):void{
|
||||
$o = new QROptions;
|
||||
|
||||
foreach(['logoSpaceWidth', 'logoSpaceHeight', 'logoSpaceStartX', 'logoSpaceStartY'] as $prop){
|
||||
@@ -144,7 +112,8 @@ final class QROptionsTest extends TestCase{
|
||||
/**
|
||||
* Tests if the optional logo space start values are nullable
|
||||
*/
|
||||
public function testLogoSpaceStartNullable():void{
|
||||
#[Test]
|
||||
public function logoSpaceStartNullable():void{
|
||||
$o = new QROptions([
|
||||
'logoSpaceStartX' => 42,
|
||||
'logoSpaceStartY' => 69,
|
||||
@@ -160,22 +129,14 @@ final class QROptionsTest extends TestCase{
|
||||
$this::assertNull($o->logoSpaceStartY);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return float[][]
|
||||
*/
|
||||
public static function circleRadiusProvider():array{
|
||||
return [
|
||||
[0.0, 0.1],
|
||||
[0.5, 0.5],
|
||||
[1.5, 0.75],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests clamping of the circle radius
|
||||
*/
|
||||
#[DataProvider('circleRadiusProvider')]
|
||||
public function testClampCircleRadius(float $value, float $expected):void{
|
||||
#[Test]
|
||||
#[TestWith([0.0, 0.1], 'min')]
|
||||
#[TestWith([0.5, 0.5], 'no clamp')]
|
||||
#[TestWith([1.5, 0.75], 'max')]
|
||||
public function clampCircleRadius(float $value, float $expected):void{
|
||||
$o = new QROptions(['circleRadius' => $value]);
|
||||
|
||||
$this::assertSame($expected, $o->circleRadius);
|
||||
|
||||
Reference in New Issue
Block a user