Skip to content

Commit 2d2a00f

Browse files
committed
🔥 v6.0 third pass: test clean-up (again)
1 parent 2478f75 commit 2d2a00f

37 files changed

+398
-249
lines changed

phpunit.xml.dist

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,7 @@
3737
<const name="TEST_IS_CI" value="true"/>
3838
<!-- limit the maximum version for the reader test to speed up things -->
3939
<const name="READER_TEST_MAX_VERSION" value="40"/>
40+
<!-- the QR version to display the matrix in when called from the shortcut method in QRMatrixDebugTrait -->
41+
<const name="MATRIX_DEBUG_VERSION" value="7"/>
4042
</php>
4143
</phpunit>

tests/Common/BitBufferTest.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010

1111
namespace chillerlan\QRCodeTest\Common;
1212

13-
use chillerlan\QRCode\Common\{BitBuffer, Mode};
1413
use chillerlan\QRCode\QRCodeException;
14+
use chillerlan\QRCode\Common\{BitBuffer, Mode};
1515
use PHPUnit\Framework\Attributes\DataProvider;
1616
use PHPUnit\Framework\TestCase;
1717

@@ -32,13 +32,15 @@ public static function bitProvider():array{
3232
'alphanum' => [Mode::ALPHANUM, 32],
3333
'byte' => [Mode::BYTE, 64],
3434
'kanji' => [Mode::KANJI, 128],
35+
'hanzi' => [Mode::HANZI, 208],
3536
];
3637
}
3738

3839
#[DataProvider('bitProvider')]
39-
public function testPut(int $data, int $value):void{
40+
public function testPut(int $data, int $expected):void{
4041
$this->bitBuffer->put($data, 4);
41-
$this::assertSame($value, $this->bitBuffer->getBuffer()[0]);
42+
43+
$this::assertSame($expected, $this->bitBuffer->getBuffer()[0]);
4244
$this::assertSame(4, $this->bitBuffer->getLength());
4345
}
4446

tests/Common/ECICharsetTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010

1111
namespace chillerlan\QRCodeTest\Common;
1212

13-
use chillerlan\QRCode\Common\ECICharset;
1413
use chillerlan\QRCode\QRCodeException;
14+
use chillerlan\QRCode\Common\ECICharset;
1515
use PHPUnit\Framework\Attributes\DataProvider;
1616
use PHPUnit\Framework\TestCase;
1717

tests/Common/EccLevelTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@
1010

1111
namespace chillerlan\QRCodeTest\Common;
1212

13-
use chillerlan\QRCode\Common\EccLevel;
14-
use chillerlan\QRCode\Common\MaskPattern;
1513
use chillerlan\QRCode\QRCodeException;
14+
use chillerlan\QRCode\Common\{EccLevel, MaskPattern};
1615
use PHPUnit\Framework\TestCase;
1716

1817
/**

tests/Common/MaskPatternTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313

1414
namespace chillerlan\QRCodeTest\Common;
1515

16-
use chillerlan\QRCode\Common\MaskPattern;
1716
use chillerlan\QRCode\QRCodeException;
18-
use Closure;
17+
use chillerlan\QRCode\Common\MaskPattern;
1918
use PHPUnit\Framework\Attributes\DataProvider;
2019
use PHPUnit\Framework\TestCase;
20+
use Closure;
2121

2222
/**
2323
* @see https://github.com/zxing/zxing/blob/f4f3c2971dc794346d8b6e14752200008cb90716/core/src/test/java/com/google/zxing/qrcode/encoder/MaskUtilTestCase.java

tests/Common/ModeTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010

1111
namespace chillerlan\QRCodeTest\Common;
1212

13-
use chillerlan\QRCode\Common\Mode;
1413
use chillerlan\QRCode\QRCodeException;
14+
use chillerlan\QRCode\Common\Mode;
1515
use PHPUnit\Framework\Attributes\DataProvider;
1616
use PHPUnit\Framework\TestCase;
1717

tests/Common/VersionTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@
1010

1111
namespace chillerlan\QRCodeTest\Common;
1212

13-
use chillerlan\QRCode\Common\EccLevel;
14-
use chillerlan\QRCode\Common\Version;
1513
use chillerlan\QRCode\QRCodeException;
14+
use chillerlan\QRCode\Common\{EccLevel, Version};
1615
use PHPUnit\Framework\TestCase;
1716

1817
/**

tests/Data/AlphaNumTest.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,18 @@
1111
namespace chillerlan\QRCodeTest\Data;
1212

1313
use chillerlan\QRCode\Data\AlphaNum;
14+
use chillerlan\QRCode\Data\QRDataModeInterface;
1415

1516
/**
1617
* Tests the AlphaNum class
1718
*/
1819
final class AlphaNumTest extends DataInterfaceTestAbstract{
1920

20-
protected static string $FQN = AlphaNum::class;
21-
protected static string $testdata = '0 $%*+-./:';
21+
protected const testData = '0 $%*+-./:';
22+
23+
protected static function getDataModeInterface(string $data):QRDataModeInterface{
24+
return new AlphaNum($data);
25+
}
2226

2327
/**
2428
* isAlphaNum() should pass on the 45 defined characters and fail on anything else (e.g. lowercase)

tests/Data/ByteTest.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,18 @@
1111
namespace chillerlan\QRCodeTest\Data;
1212

1313
use chillerlan\QRCode\Data\Byte;
14+
use chillerlan\QRCode\Data\QRDataModeInterface;
1415

1516
/**
1617
* Tests the Byte class
1718
*/
1819
final class ByteTest extends DataInterfaceTestAbstract{
1920

20-
protected static string $FQN = Byte::class;
21-
protected static string $testdata = '[¯\_(ツ)_/¯]';
21+
protected const testData = '[¯\_(ツ)_/¯]';
22+
23+
protected static function getDataModeInterface(string $data):QRDataModeInterface{
24+
return new Byte($data);
25+
}
2226

2327
/**
2428
* isByte() passses any binary string and only fails on empty strings

tests/Data/DataInterfaceTestAbstract.php

Lines changed: 28 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,13 @@
1111
namespace chillerlan\QRCodeTest\Data;
1212

1313
use chillerlan\QRCode\Common\{EccLevel, MaskPattern, Mode, Version};
14-
use PHPUnit\Framework\Attributes\DataProvider;
1514
use chillerlan\QRCode\Data\{QRCodeDataException, QRData, QRDataModeInterface, QRMatrix};
1615
use chillerlan\QRCode\QROptions;
1716
use chillerlan\QRCodeTest\QRMaxLengthTrait;
18-
use Exception, Generator;
17+
use Exception, Generator;
18+
use PHPUnit\Framework\Attributes\DataProvider;
1919
use PHPUnit\Framework\TestCase;
20-
21-
use function array_map;
22-
use function hex2bin;
23-
use function mb_strlen;
24-
use function mb_substr;
25-
use function sprintf;
26-
use function str_repeat;
27-
use function strlen;
28-
use function substr;
20+
use function array_map, hex2bin, mb_strlen, mb_substr, sprintf, str_repeat, strlen, substr;
2921

3022
/**
3123
* The data interface test abstract
@@ -35,26 +27,15 @@ abstract class DataInterfaceTestAbstract extends TestCase{
3527

3628
protected QRData $QRData;
3729
protected QRDataModeInterface $dataMode;
38-
protected static string $FQN;
39-
protected static string $testdata;
30+
31+
protected const testData = '';
4032

4133
protected function setUp():void{
42-
$this->QRData = new QRData(new QROptions);
34+
$this->QRData = new QRData(new QROptions);
35+
$this->dataMode = static::getDataModeInterface(static::testData);
4336
}
4437

45-
/**
46-
* Verifies the QRData instance
47-
*/
48-
public function testInstance():void{
49-
$this::assertInstanceOf(QRData::class, $this->QRData);
50-
}
51-
52-
/**
53-
* Verifies the QRDataModeInterface instance
54-
*/
55-
public function testDataModeInstance():void{
56-
$this::assertInstanceOf(QRDataModeInterface::class, new static::$FQN(static::$testdata));
57-
}
38+
abstract protected static function getDataModeInterface(string $data):QRDataModeInterface;
5839

5940
/**
6041
* @return int[][]
@@ -70,7 +51,7 @@ public static function maskPatternProvider():array{
7051
public function testInitMatrix(int $pattern):void{
7152
$maskPattern = new MaskPattern($pattern);
7253

73-
$this->QRData->setData([new static::$FQN(static::$testdata)]);
54+
$this->QRData->setData([$this->dataMode]);
7455

7556
$matrix = $this->QRData->writeMatrix()->setFormatInfo($maskPattern)->mask($maskPattern);
7657

@@ -85,18 +66,16 @@ abstract public static function stringValidateProvider():array;
8566
*/
8667
#[DataProvider('stringValidateProvider')]
8768
public function testValidateString(string $string, bool $expected):void{
88-
/** @noinspection PhpUndefinedMethodInspection */
89-
$this::assertSame($expected, static::$FQN::validateString($string));
69+
$this::assertSame($expected, $this->dataMode::validateString($string));
9070
}
9171

9272
/**
93-
* Tests if a binary string is properly validated as false
73+
* Tests if a random binary string is properly validated as false
9474
*
9575
* @see https://github.com/chillerlan/php-qrcode/issues/182
9676
*/
9777
public function testBinaryStringInvalid():void{
98-
/** @noinspection PhpUndefinedMethodInspection */
99-
$this::assertFalse(static::$FQN::validateString(hex2bin('01015989f47dff8e852122117e04c90b9f15defc1c36477b1fe1')));
78+
$this::assertFalse($this->dataMode::validateString(hex2bin('01015989f47dff8e852122117e04c90b9f15defc1c36477b1fe1')));
10079
}
10180

10281
/**
@@ -114,16 +93,14 @@ public function testDecodeSegment(int $version):void{
11493
$options = new QROptions;
11594
$options->version = $version;
11695

117-
// invoke a datamode interface
118-
$this->dataMode = new static::$FQN(static::$testdata);
11996
// invoke a QRData instance and write data
12097
$this->QRData = new QRData($options, [$this->dataMode]);
12198
// get the filled bitbuffer
12299
$bitBuffer = $this->QRData->getBitBuffer();
123100
// read the first 4 bits
124101
$this::assertSame($this->dataMode::DATAMODE, $bitBuffer->read(4));
125102
// decode the data
126-
$this::assertSame(static::$testdata, $this->dataMode::decodeSegment($bitBuffer, $options->version));
103+
$this::assertSame(static::testData, $this->dataMode::decodeSegment($bitBuffer, $options->version));
127104
}
128105

129106
/**
@@ -139,15 +116,17 @@ public function testDecodeSegment(int $version):void{
139116
*/
140117
public static function maxLengthProvider():Generator{
141118
$eccLevels = array_map(fn(int $ecc):EccLevel => new EccLevel($ecc), [EccLevel::L, EccLevel::M, EccLevel::Q, EccLevel::H]);
142-
$str = str_repeat(static::$testdata, 1000);
143-
$mb = (static::$FQN::DATAMODE === Mode::KANJI || static::$FQN::DATAMODE === Mode::HANZI);
119+
$str = str_repeat(static::testData, 1000);
120+
/** @phan-suppress-next-line PhanAbstractStaticMethodCallInStatic */
121+
$dataMode = static::getDataModeInterface(static::testData)::DATAMODE;
122+
$mb = ($dataMode === Mode::KANJI || $dataMode === Mode::HANZI);
144123

145124
for($v = 1; $v <= 40; $v++){
146125
$version = new Version($v);
147126

148127
foreach($eccLevels as $eccLevel){
149128
// maximum characters per ecc/mode
150-
$len = static::getMaxLengthForMode(static::$FQN::DATAMODE, $version, $eccLevel);
129+
$len = static::getMaxLengthForMode($dataMode, $version, $eccLevel);
151130
// a string that contains the maximum amount of characters for the given mode
152131
$val = ($mb) ? mb_substr($str, 0, $len) : substr($str, 0, $len);
153132
// same as above but character count exceeds
@@ -171,8 +150,10 @@ public function testMaxLength(Version $version, EccLevel $eccLevel, string $str)
171150
$options = new QROptions;
172151
$options->version = $version->getVersionNumber();
173152
$options->eccLevel = $eccLevel->getLevel();
174-
$this->dataMode = new static::$FQN($str);
153+
154+
$this->dataMode = static::getDataModeInterface($str);
175155
$this->QRData = new QRData($options, [$this->dataMode]);
156+
176157
$bitBuffer = $this->QRData->getBitBuffer();
177158

178159
$this::assertSame($this->dataMode::DATAMODE, $bitBuffer->read(4));
@@ -187,8 +168,10 @@ public function testGetMinimumVersion(Version $version, EccLevel $eccLevel, stri
187168
$options = new QROptions;
188169
$options->version = Version::AUTO;
189170
$options->eccLevel = $eccLevel->getLevel();
190-
$this->dataMode = new static::$FQN($str);
171+
172+
$this->dataMode = static::getDataModeInterface($str);
191173
$this->QRData = new QRData($options, [$this->dataMode]);
174+
192175
$bitBuffer = $this->QRData->getBitBuffer();
193176

194177
$this::assertLessThanOrEqual($eccLevel->getMaxBitsForVersion($version), $this->QRData->estimateTotalBitLength());
@@ -214,7 +197,7 @@ public function testMaxLengthOverflowException(Version $version, EccLevel $eccLe
214197
$options->eccLevel = $eccLevel->getLevel();
215198

216199
/** @phan-suppress-next-line PhanNoopNew */
217-
new QRData($options, [new static::$FQN($str1)]);
200+
new QRData($options, [static::getDataModeInterface($str1)]);
218201
}
219202

220203
/**
@@ -224,7 +207,7 @@ public function testGetMinimumVersionException():void{
224207
$this->expectException(QRCodeDataException::class);
225208
$this->expectExceptionMessage('data exceeds');
226209

227-
$this->QRData->setData([new static::$FQN(str_repeat(static::$testdata, 1337))]);
210+
$this->QRData->setData([static::getDataModeInterface(str_repeat(static::testData, 1337))]);
228211
}
229212

230213
/**
@@ -234,8 +217,7 @@ public function testInvalidDataException():void{
234217
$this->expectException(QRCodeDataException::class);
235218
$this->expectExceptionMessage('invalid data');
236219

237-
/** @phan-suppress-next-line PhanNoopNew */
238-
new static::$FQN('##');
220+
static::getDataModeInterface('##');
239221
}
240222

241223
/**
@@ -245,8 +227,7 @@ public function testInvalidDataOnEmptyException():void{
245227
$this->expectException(QRCodeDataException::class);
246228
$this->expectExceptionMessage('invalid data');
247229

248-
/** @phan-suppress-next-line PhanNoopNew */
249-
new static::$FQN('');
230+
static::getDataModeInterface('');
250231
}
251232

252233
}

0 commit comments

Comments
 (0)