Skip to content

Commit 6239074

Browse files
committed
🛀 test cleanup
1 parent c27973a commit 6239074

30 files changed

+369
-259
lines changed

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\TestCase;
1616

1717
/**
@@ -31,15 +31,17 @@ public static function bitProvider():array{
3131
'alphanum' => [Mode::ALPHANUM, 32],
3232
'byte' => [Mode::BYTE, 64],
3333
'kanji' => [Mode::KANJI, 128],
34+
'hanzi' => [Mode::HANZI, 208],
3435
];
3536
}
3637

3738
/**
3839
* @dataProvider bitProvider
3940
*/
40-
public function testPut(int $data, int $value):void{
41+
public function testPut(int $data, int $expected):void{
4142
$this->bitBuffer->put($data, 4);
42-
$this::assertSame($value, $this->bitBuffer->getBuffer()[0]);
43+
44+
$this::assertSame($expected, $this->bitBuffer->getBuffer()[0]);
4345
$this::assertSame(4, $this->bitBuffer->getLength());
4446
}
4547

tests/Common/MaskPatternTest.php

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -129,36 +129,36 @@ public function testInvalidMaskPatternException():void{
129129

130130
public function testPenaltyRule1():void{
131131
// horizontal
132-
$this::assertSame(0, MaskPattern::testRule1([[0, 0, 0, 0]], 1, 4));
133-
$this::assertSame(3, MaskPattern::testRule1([[0, 0, 0, 0, 0, 1]], 1, 6));
134-
$this::assertSame(4, MaskPattern::testRule1([[0, 0, 0, 0, 0, 0]], 1, 6));
132+
$this::assertSame(0, MaskPattern::testRule1([[false, false, false, false]], 1, 4));
133+
$this::assertSame(3, MaskPattern::testRule1([[false, false, false, false, false, true]], 1, 6));
134+
$this::assertSame(4, MaskPattern::testRule1([[false, false, false, false, false, false]], 1, 6));
135135
// vertical
136-
$this::assertSame(0, MaskPattern::testRule1([[0], [0], [0], [0]], 4, 1));
137-
$this::assertSame(3, MaskPattern::testRule1([[0], [0], [0], [0], [0], [1]], 6, 1));
138-
$this::assertSame(4, MaskPattern::testRule1([[0], [0], [0], [0], [0], [0]], 6, 1));
136+
$this::assertSame(0, MaskPattern::testRule1([[false], [false], [false], [false]], 4, 1));
137+
$this::assertSame(3, MaskPattern::testRule1([[false], [false], [false], [false], [false], [true]], 6, 1));
138+
$this::assertSame(4, MaskPattern::testRule1([[false], [false], [false], [false], [false], [false]], 6, 1));
139139
}
140140

141141
public function testPenaltyRule2():void{
142-
$this::assertSame(0, MaskPattern::testRule2([[0]], 1, 1));
143-
$this::assertSame(0, MaskPattern::testRule2([[0, 0], [0, 1]], 2, 2));
144-
$this::assertSame(3, MaskPattern::testRule2([[0, 0], [0, 0]], 2, 2));
145-
$this::assertSame(12, MaskPattern::testRule2([[0, 0, 0], [0, 0, 0], [0, 0, 0]], 3, 3));
142+
$this::assertSame(0, MaskPattern::testRule2([[false]], 1, 1));
143+
$this::assertSame(0, MaskPattern::testRule2([[false, false], [false, true]], 2, 2));
144+
$this::assertSame(3, MaskPattern::testRule2([[false, false], [false, false]], 2, 2));
145+
$this::assertSame(12, MaskPattern::testRule2([[false, false, false], [false, false, false], [false, false, false]], 3, 3));
146146
}
147147

148148
public function testPenaltyRule3():void{
149149
// horizontal
150-
$this::assertSame(40, MaskPattern::testRule3([[0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1]], 1, 11));
151-
$this::assertSame(40, MaskPattern::testRule3([[1, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0]], 1, 11));
152-
$this::assertSame(0, MaskPattern::testRule3([[1, 0, 1, 1, 1, 0, 1]], 1, 7));
150+
$this::assertSame(40, MaskPattern::testRule3([[false, false, false, false, true, false, true, true, true, false, true]], 1, 11));
151+
$this::assertSame(40, MaskPattern::testRule3([[true, false, true, true, true, false, true, false, false, false, false]], 1, 11));
152+
$this::assertSame(0, MaskPattern::testRule3([[true, false, true, true, true, false, true]], 1, 7));
153153
// vertical
154-
$this::assertSame(40, MaskPattern::testRule3([[0], [0], [0], [0], [1], [0], [1], [1], [1], [0], [1]], 11, 1));
155-
$this::assertSame(40, MaskPattern::testRule3([[1], [0], [1], [1], [1], [0], [1], [0], [0], [0], [0]], 11, 1));
156-
$this::assertSame(0, MaskPattern::testRule3([[1], [0], [1], [1], [1], [0], [1]], 7, 1));
154+
$this::assertSame(40, MaskPattern::testRule3([[false], [false], [false], [false], [true], [false], [true], [true], [true], [false], [true]], 11, 1));
155+
$this::assertSame(40, MaskPattern::testRule3([[true], [false], [true], [true], [true], [false], [true], [false], [false], [false], [false]], 11, 1));
156+
$this::assertSame(0, MaskPattern::testRule3([[true], [false], [true], [true], [true], [false], [true]], 7, 1));
157157
}
158158

159159
public function testPenaltyRule4():void{
160-
$this::assertSame(100, MaskPattern::testRule4([[0]], 1, 1));
161-
$this::assertSame(0, MaskPattern::testRule4([[0, 1]], 1, 2));
162-
$this::assertSame(30, MaskPattern::testRule4([[0, 1, 1, 1, 1, 0]], 1, 6));
160+
$this::assertSame(100, MaskPattern::testRule4([[false]], 1, 1));
161+
$this::assertSame(0, MaskPattern::testRule4([[false, true]], 1, 2));
162+
$this::assertSame(30, MaskPattern::testRule4([[false, true, true, true, true, false]], 1, 6));
163163
}
164164
}

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: 31 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,9 @@
1414
use chillerlan\QRCode\Data\{QRCodeDataException, QRData, QRDataModeInterface, QRMatrix};
1515
use chillerlan\QRCode\QROptions;
1616
use chillerlan\QRCodeTest\QRMaxLengthTrait;
17-
use Exception, Generator;
17+
use Exception, Generator;
1818
use PHPUnit\Framework\TestCase;
19-
20-
use function array_map;
21-
use function hex2bin;
22-
use function mb_strlen;
23-
use function mb_substr;
24-
use function sprintf;
25-
use function str_repeat;
26-
use function strlen;
27-
use function substr;
19+
use function array_map, hex2bin, mb_strlen, mb_substr, sprintf, str_repeat, strlen, substr;
2820

2921
/**
3022
* The data interface test abstract
@@ -34,26 +26,15 @@ abstract class DataInterfaceTestAbstract extends TestCase{
3426

3527
protected QRData $QRData;
3628
protected QRDataModeInterface $dataMode;
37-
protected static string $FQN;
38-
protected static string $testdata;
29+
30+
protected const testData = '';
3931

4032
protected function setUp():void{
41-
$this->QRData = new QRData(new QROptions);
33+
$this->QRData = new QRData(new QROptions);
34+
$this->dataMode = static::getDataModeInterface(static::testData);
4235
}
4336

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

5839
/**
5940
* @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

@@ -86,18 +67,16 @@ abstract public static function stringValidateProvider():array;
8667
* @dataProvider stringValidateProvider
8768
*/
8869
public function testValidateString(string $string, bool $expected):void{
89-
/** @noinspection PhpUndefinedMethodInspection */
90-
$this::assertSame($expected, static::$FQN::validateString($string));
70+
$this::assertSame($expected, $this->dataMode::validateString($string));
9171
}
9272

9373
/**
94-
* Tests if a binary string is properly validated as false
74+
* Tests if a random binary string is properly validated as false
9575
*
9676
* @see https://github.com/chillerlan/php-qrcode/issues/182
9777
*/
9878
public function testBinaryStringInvalid():void{
99-
/** @noinspection PhpUndefinedMethodInspection */
100-
$this::assertFalse(static::$FQN::validateString(hex2bin('01015989f47dff8e852122117e04c90b9f15defc1c36477b1fe1')));
79+
$this::assertFalse($this->dataMode::validateString(hex2bin('01015989f47dff8e852122117e04c90b9f15defc1c36477b1fe1')));
10180
}
10281

10382
/**
@@ -111,21 +90,20 @@ public static function versionBreakpointProvider():array{
11190
* Tests decoding a data segment from a given BitBuffer
11291
*
11392
* @dataProvider versionBreakpointProvider
93+
* @phan-suppress PhanAccessClassConstantInternal
11494
*/
11595
public function testDecodeSegment(int $version):void{
11696
$options = new QROptions;
11797
$options->version = $version;
11898

119-
// invoke a datamode interface
120-
$this->dataMode = new static::$FQN(static::$testdata);
12199
// invoke a QRData instance and write data
122100
$this->QRData = new QRData($options, [$this->dataMode]);
123101
// get the filled bitbuffer
124102
$bitBuffer = $this->QRData->getBitBuffer();
125103
// read the first 4 bits
126104
$this::assertSame($this->dataMode::DATAMODE, $bitBuffer->read(4));
127105
// decode the data
128-
$this::assertSame(static::$testdata, $this->dataMode::decodeSegment($bitBuffer, $options->version));
106+
$this::assertSame(static::testData, $this->dataMode::decodeSegment($bitBuffer, $options->version));
129107
}
130108

131109
/**
@@ -138,18 +116,21 @@ public function testDecodeSegment(int $version):void{
138116
* - the maximum allowed character length
139117
*
140118
* @throws \Exception
119+
* @phan-suppress PhanAccessClassConstantInternal
141120
*/
142121
public static function maxLengthProvider():Generator{
143122
$eccLevels = array_map(fn(int $ecc):EccLevel => new EccLevel($ecc), [EccLevel::L, EccLevel::M, EccLevel::Q, EccLevel::H]);
144-
$str = str_repeat(static::$testdata, 1000);
145-
$mb = (static::$FQN::DATAMODE === Mode::KANJI || static::$FQN::DATAMODE === Mode::HANZI);
123+
$str = str_repeat(static::testData, 1000);
124+
/** @phan-suppress-next-line PhanAbstractStaticMethodCallInStatic */
125+
$dataMode = static::getDataModeInterface(static::testData)::DATAMODE;
126+
$mb = ($dataMode === Mode::KANJI || $dataMode === Mode::HANZI);
146127

147128
for($v = 1; $v <= 40; $v++){
148129
$version = new Version($v);
149130

150131
foreach($eccLevels as $eccLevel){
151132
// maximum characters per ecc/mode
152-
$len = static::getMaxLengthForMode(static::$FQN::DATAMODE, $version, $eccLevel);
133+
$len = static::getMaxLengthForMode($dataMode, $version, $eccLevel);
153134
// a string that contains the maximum amount of characters for the given mode
154135
$val = ($mb) ? mb_substr($str, 0, $len) : substr($str, 0, $len);
155136
// same as above but character count exceeds
@@ -170,13 +151,16 @@ public static function maxLengthProvider():Generator{
170151

171152
/**
172153
* @dataProvider maxLengthProvider
154+
* @phan-suppress PhanAccessClassConstantInternal
173155
*/
174156
public function testMaxLength(Version $version, EccLevel $eccLevel, string $str):void{
175157
$options = new QROptions;
176158
$options->version = $version->getVersionNumber();
177159
$options->eccLevel = $eccLevel->getLevel();
178-
$this->dataMode = new static::$FQN($str);
160+
161+
$this->dataMode = static::getDataModeInterface($str);
179162
$this->QRData = new QRData($options, [$this->dataMode]);
163+
180164
$bitBuffer = $this->QRData->getBitBuffer();
181165

182166
$this::assertSame($this->dataMode::DATAMODE, $bitBuffer->read(4));
@@ -187,13 +171,16 @@ public function testMaxLength(Version $version, EccLevel $eccLevel, string $str)
187171
* Tests getting the minimum QR version for the given data
188172
*
189173
* @dataProvider maxLengthProvider
174+
* @phan-suppress PhanAccessClassConstantInternal
190175
*/
191176
public function testGetMinimumVersion(Version $version, EccLevel $eccLevel, string $str):void{
192177
$options = new QROptions;
193178
$options->version = Version::AUTO;
194179
$options->eccLevel = $eccLevel->getLevel();
195-
$this->dataMode = new static::$FQN($str);
180+
181+
$this->dataMode = static::getDataModeInterface($str);
196182
$this->QRData = new QRData($options, [$this->dataMode]);
183+
197184
$bitBuffer = $this->QRData->getBitBuffer();
198185

199186
$this::assertLessThanOrEqual($eccLevel->getMaxBitsForVersion($version), $this->QRData->estimateTotalBitLength());
@@ -220,7 +207,7 @@ public function testMaxLengthOverflowException(Version $version, EccLevel $eccLe
220207
$options->eccLevel = $eccLevel->getLevel();
221208

222209
/** @phan-suppress-next-line PhanNoopNew */
223-
new QRData($options, [new static::$FQN($str1)]);
210+
new QRData($options, [static::getDataModeInterface($str1)]);
224211
}
225212

226213
/**
@@ -230,7 +217,7 @@ public function testGetMinimumVersionException():void{
230217
$this->expectException(QRCodeDataException::class);
231218
$this->expectExceptionMessage('data exceeds');
232219

233-
$this->QRData->setData([new static::$FQN(str_repeat(static::$testdata, 1337))]);
220+
$this->QRData->setData([static::getDataModeInterface(str_repeat(static::testData, 1337))]);
234221
}
235222

236223
/**
@@ -240,8 +227,7 @@ public function testInvalidDataException():void{
240227
$this->expectException(QRCodeDataException::class);
241228
$this->expectExceptionMessage('invalid data');
242229

243-
/** @phan-suppress-next-line PhanNoopNew */
244-
new static::$FQN('##');
230+
static::getDataModeInterface('##');
245231
}
246232

247233
/**
@@ -251,8 +237,7 @@ public function testInvalidDataOnEmptyException():void{
251237
$this->expectException(QRCodeDataException::class);
252238
$this->expectExceptionMessage('invalid data');
253239

254-
/** @phan-suppress-next-line PhanNoopNew */
255-
new static::$FQN('');
240+
static::getDataModeInterface('');
256241
}
257242

258243
}

tests/Data/ECITest.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@
2020
*/
2121
final class ECITest extends TestCase{
2222

23-
protected QRData $QRData;
24-
protected string $testdata = '无可奈何燃花作香';
25-
private int $testCharset = ECICharset::GB18030;
23+
private QRData $QRData;
24+
private int $testCharset = ECICharset::GB18030;
25+
26+
private const testData = '无可奈何燃花作香';
2627

2728
protected function setUp():void{
2829
$this->QRData = new QRData(new QROptions);
@@ -31,11 +32,11 @@ protected function setUp():void{
3132
private function getDataSegments():array{
3233
return [
3334
new ECI($this->testCharset),
34-
new Byte(mb_convert_encoding($this->testdata, ECICharset::MB_ENCODINGS[$this->testCharset], mb_internal_encoding())),
35+
/** @phan-suppress-next-line PhanParamSuspiciousOrder */
36+
new Byte(mb_convert_encoding(self::testData, ECICharset::MB_ENCODINGS[$this->testCharset], mb_internal_encoding())),
3537
];
3638
}
3739

38-
/** @inheritDoc */
3940
public function testDataModeInstance():void{
4041
$datamode = new ECI($this->testCharset);
4142

@@ -67,10 +68,9 @@ public function testDecodeSegment(int $version):void{
6768
// read the first 4 bits
6869
$this::assertSame($segments[0]::DATAMODE, $bitBuffer->read(4));
6970
// decode the data
70-
$this::assertSame($this->testdata, ECI::decodeSegment($bitBuffer, $options->version));
71+
$this::assertSame(self::testData, ECI::decodeSegment($bitBuffer, $options->version));
7172
}
7273

73-
/** @inheritDoc */
7474
public function testInvalidDataException():void{
7575
$this->expectException(QRCodeDataException::class);
7676
$this->expectExceptionMessage('invalid encoding id:');
@@ -81,8 +81,6 @@ public function testInvalidDataException():void{
8181
/**
8282
* since the ECI class only accepts integer values,
8383
* we'll use this test to check for the upper end of the accepted input range
84-
*
85-
* @inheritDoc
8684
*/
8785
public function testInvalidDataOnEmptyException():void{
8886
$this->expectException(QRCodeDataException::class);
@@ -129,7 +127,9 @@ public function testDecodeECISegmentFollowedByInvalidModeException():void{
129127
// follow the ECI segment by a non-8bit-byte segment
130128
$segments[1] = new Number('1');
131129
$bitBuffer = (new QRData($options, $segments))->getBitBuffer();
130+
// verify the ECI mode indicator
132131
$this::assertSame(Mode::ECI, $bitBuffer->read(4));
132+
// throw
133133
ECI::decodeSegment($bitBuffer, $options->version);
134134
}
135135

0 commit comments

Comments
 (0)