Skip to content

Commit 9db842d

Browse files
committed
:octocat: +several coverage tests & cleanup
1 parent 4aa66dd commit 9db842d

15 files changed

+334
-18
lines changed

src/Common/BitBuffer.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public function available():int{
112112
*/
113113
public function read(int $numBits):int{
114114

115-
if($numBits < 1 || $numBits > 32 || $numBits > $this->available()){
115+
if($numBits < 1 || $numBits > $this->available()){
116116
throw new QRCodeException('invalid $numBits: '.$numBits);
117117
}
118118

src/Common/EccLevel.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ final class EccLevel{
3333
/**
3434
* ISO/IEC 18004:2000 Tables 7-11 - Number of symbol characters and input data capacity for versions 1 to 40
3535
*
36-
* @var int [][]
36+
* @var int[][]
3737
*/
3838
private const MAX_BITS = [
3939
// [ L, M, Q, H] // v => modules

src/Common/Mode.php

+8-1
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,16 @@ public static function getLengthBitsForVersion(int $mode, int $version):int{
9090

9191
/**
9292
* returns the array of length bits for the given mode
93+
*
94+
* @throws \chillerlan\QRCode\QRCodeException
9395
*/
9496
public static function getLengthBitsForMode(int $mode):array{
95-
return self::LENGTH_BITS[$mode];
97+
98+
if(isset(self::LENGTH_BITS[$mode])){
99+
return self::LENGTH_BITS[$mode];
100+
}
101+
102+
throw new QRCodeException('invalid mode given');
96103
}
97104

98105
}

src/Common/Version.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ final class Version{
217217
public function __construct(int $version){
218218

219219
if($version < 1 || $version > 40){
220-
throw new QRCodeException('invalid version number');
220+
throw new QRCodeException('invalid version given');
221221
}
222222

223223
$this->version = $version;

src/Data/AlphaNum.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public static function decodeSegment(BitBuffer $bitBuffer, int $versionNumber):s
109109
while($length > 1){
110110

111111
if($bitBuffer->available() < 11){
112-
throw new QRCodeDataException('not enough bits available');
112+
throw new QRCodeDataException('not enough bits available'); // @codeCoverageIgnore
113113
}
114114

115115
$nextTwoCharsBits = $bitBuffer->read(11);
@@ -121,7 +121,7 @@ public static function decodeSegment(BitBuffer $bitBuffer, int $versionNumber):s
121121
if($length === 1){
122122
// special case: one character left
123123
if($bitBuffer->available() < 6){
124-
throw new QRCodeDataException('not enough bits available');
124+
throw new QRCodeDataException('not enough bits available'); // @codeCoverageIgnore
125125
}
126126

127127
$result .= $toAlphaNumericChar($bitBuffer->read(6));

src/Data/Byte.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public static function decodeSegment(BitBuffer $bitBuffer, int $versionNumber):s
7070
$length = $bitBuffer->read(self::getLengthBits($versionNumber));
7171

7272
if($bitBuffer->available() < 8 * $length){
73-
throw new QRCodeDataException('not enough bits available');
73+
throw new QRCodeDataException('not enough bits available'); // @codeCoverageIgnore
7474
}
7575

7676
$readBytes = '';

src/Data/Kanji.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public static function decodeSegment(BitBuffer $bitBuffer, int $versionNumber):s
119119
$length = $bitBuffer->read(self::getLengthBits($versionNumber));
120120

121121
if($bitBuffer->available() < $length * 13){
122-
throw new QRCodeDataException('not enough bits available');
122+
throw new QRCodeDataException('not enough bits available'); // @codeCoverageIgnore
123123
}
124124

125125
$buffer = [];

src/Data/Number.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public static function decodeSegment(BitBuffer $bitBuffer, int $versionNumber):s
126126
while($length >= 3){
127127
// Each 10 bits encodes three digits
128128
if($bitBuffer->available() < 10){
129-
throw new QRCodeDataException('not enough bits available');
129+
throw new QRCodeDataException('not enough bits available'); // @codeCoverageIgnore
130130
}
131131

132132
$threeDigitsBits = $bitBuffer->read(10);
@@ -145,7 +145,7 @@ public static function decodeSegment(BitBuffer $bitBuffer, int $versionNumber):s
145145
if($length === 2){
146146
// Two digits left over to read, encoded in 7 bits
147147
if($bitBuffer->available() < 7){
148-
throw new QRCodeDataException('not enough bits available');
148+
throw new QRCodeDataException('not enough bits available'); // @codeCoverageIgnore
149149
}
150150

151151
$twoDigitsBits = $bitBuffer->read(7);
@@ -160,7 +160,7 @@ public static function decodeSegment(BitBuffer $bitBuffer, int $versionNumber):s
160160
elseif($length === 1){
161161
// One digit left over to read
162162
if($bitBuffer->available() < 4){
163-
throw new QRCodeDataException('not enough bits available');
163+
throw new QRCodeDataException('not enough bits available'); // @codeCoverageIgnore
164164
}
165165

166166
$digitBits = $bitBuffer->read(4);

src/Data/QRMatrix.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -534,17 +534,17 @@ public function setLogoSpace(int $width, int $height, int $startX = null, int $s
534534
throw new QRCodeDataException('ECC level "H" required to add logo space');
535535
}
536536

537-
// if width and height happen to be exactly 0 (default value), just return - nothing to do
538-
if($width === 0 || $height === 0){
539-
return $this;
537+
// if width and height happen to be negative or 0 (default value), just return - nothing to do
538+
if($width <= 0 || $height <= 0){
539+
return $this; // @codeCoverageIgnore
540540
}
541541

542542
// $this->moduleCount includes the quiet zone (if created), we need the QR size here
543543
$length = $this->version->getDimension();
544544

545-
// throw if the size is negative or exceeds the qrcode size
546-
if($width < 0 || $height < 0 || $width > $length || $height > $length){
547-
throw new QRCodeDataException('invalid logo dimensions');
545+
// throw if the size is exceeds the qrcode size
546+
if($width > $length || $height > $length){
547+
throw new QRCodeDataException('logo dimensions exceed matrix size');
548548
}
549549

550550
// we need uneven sizes to center the logo space, adjust if needed

src/Decoder/Binarizer.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ private function estimateBlackPoint(array $buckets):int{
103103
// If there is too little contrast in the image to pick a meaningful black point, throw rather
104104
// than waste time trying to decode the image, and risk false positives.
105105
if($secondPeak - $firstPeak <= $numBuckets / 16){
106-
throw new QRCodeDecoderException('no meaningful dark point found');
106+
throw new QRCodeDecoderException('no meaningful dark point found'); // @codeCoverageIgnore
107107
}
108108

109109
// Find a valley between them that is low and closer to the white peak.

tests/Common/BitBufferTest.php

+9
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
namespace chillerlan\QRCodeTest\Common;
1212

1313
use chillerlan\QRCode\Common\{BitBuffer, Mode};
14+
use chillerlan\QRCode\QRCodeException;
1415
use PHPUnit\Framework\TestCase;
1516

1617
/**
@@ -42,4 +43,12 @@ public function testPut(int $data, int $value):void{
4243
$this::assertSame(4, $this->bitBuffer->getLength());
4344
}
4445

46+
public function testReadException():void{
47+
$this->expectException(QRCodeException::class);
48+
$this->expectExceptionMessage('invalid $numBits');
49+
50+
$this->bitBuffer->put(Mode::KANJI, 4);
51+
$this->bitBuffer->read($this->bitBuffer->available() + 1);
52+
}
53+
4554
}

tests/Common/EccLevelTest.php

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
/**
3+
* Class EccLevelTest
4+
*
5+
* @created 25.07.2022
6+
* @author smiley <[email protected]>
7+
* @copyright 2022 smiley
8+
* @license MIT
9+
*/
10+
11+
namespace chillerlan\QRCodeTest\Common;
12+
13+
use chillerlan\QRCode\Common\EccLevel;
14+
use chillerlan\QRCode\Common\MaskPattern;
15+
use chillerlan\QRCode\QRCodeException;
16+
use PHPUnit\Framework\TestCase;
17+
18+
/**
19+
* EccLevel coverage test
20+
*/
21+
final class EccLevelTest extends TestCase{
22+
23+
public function testConstructInvalidEccException():void{
24+
$this->expectException(QRCodeException::class);
25+
$this->expectExceptionMessage('invalid ECC level');
26+
27+
$ecc = new EccLevel(69);
28+
}
29+
30+
public function testToString():void{
31+
$ecc = new EccLevel(EccLevel::L);
32+
33+
$this::assertSame('L', (string)$ecc);
34+
}
35+
36+
public function testGetLevel():void{
37+
$ecc = new EccLevel(EccLevel::L);
38+
39+
$this::assertSame(EccLevel::L, $ecc->getLevel());
40+
}
41+
42+
public function testGetOrdinal():void{
43+
$ecc = new EccLevel(EccLevel::L);
44+
45+
$this::assertSame(0, $ecc->getOrdinal());
46+
}
47+
48+
public function testGetformatPattern():void{
49+
$ecc = new EccLevel(EccLevel::Q);
50+
51+
$this::assertSame(0b010010010110100, $ecc->getformatPattern(new MaskPattern(4)));
52+
}
53+
54+
public function getMaxBits():void{
55+
$ecc = new EccLevel(EccLevel::Q);
56+
57+
$this::assertSame(4096, $ecc->getMaxBits()[21]);
58+
}
59+
60+
}

tests/Common/ModeTest.php

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
/**
3+
* Class ModeTest
4+
*
5+
* @created 25.07.2022
6+
* @author smiley <[email protected]>
7+
* @copyright 2022 smiley
8+
* @license MIT
9+
*/
10+
11+
namespace chillerlan\QRCodeTest\Common;
12+
13+
use chillerlan\QRCode\Common\Mode;
14+
use chillerlan\QRCode\QRCodeException;
15+
use PHPUnit\Framework\TestCase;
16+
17+
/**
18+
* Mode coverage test
19+
*/
20+
final class ModeTest extends TestCase{
21+
22+
/**
23+
* version breakpoints for numeric mode
24+
*/
25+
public function versionProvider():array{
26+
return [
27+
[ 1, 10],
28+
[ 9, 10],
29+
[10, 12],
30+
[26, 12],
31+
[27, 14],
32+
[40, 14],
33+
];
34+
}
35+
36+
/**
37+
* @dataProvider versionProvider
38+
*/
39+
public function testGetLengthBitsForVersionBreakpoints(int $version, int $expected):void{
40+
$this::assertSame($expected, Mode::getLengthBitsForVersion(Mode::NUMBER, $version));
41+
}
42+
43+
public function testGetLengthBitsForVersionInvalidModeException():void{
44+
$this->expectException(QRCodeException::class);
45+
$this->expectExceptionMessage('invalid mode given');
46+
47+
$bits = Mode::getLengthBitsForVersion(42, 69);
48+
}
49+
50+
public function testGetLengthBitsForVersionInvalidVersionException():void{
51+
$this->expectException(QRCodeException::class);
52+
$this->expectExceptionMessage('invalid version number');
53+
54+
$bits = Mode::getLengthBitsForVersion(Mode::BYTE, 69);
55+
}
56+
57+
public function testGetLengthBitsForModeInvalidModeException():void{
58+
$this->expectException(QRCodeException::class);
59+
$this->expectExceptionMessage('invalid mode given');
60+
61+
$bits = Mode::getLengthBitsForMode(42);
62+
}
63+
64+
}

tests/Common/VersionTest.php

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
/**
3+
* Class VersionTest
4+
*
5+
* @created 25.07.2022
6+
* @author smiley <[email protected]>
7+
* @copyright 2022 smiley
8+
* @license MIT
9+
*/
10+
11+
namespace chillerlan\QRCodeTest\Common;
12+
13+
use chillerlan\QRCode\Common\EccLevel;
14+
use chillerlan\QRCode\Common\Mode;
15+
use chillerlan\QRCode\Common\Version;
16+
use chillerlan\QRCode\QRCodeException;
17+
use PHPUnit\Framework\TestCase;
18+
19+
/**
20+
* Version coverage test
21+
*/
22+
final class VersionTest extends TestCase{
23+
24+
private Version $version;
25+
26+
protected function setUp():void{
27+
$this->version = new Version(7);
28+
}
29+
30+
public function testToString():void{
31+
$this::assertSame('7', (string)$this->version);
32+
}
33+
34+
public function testGetVersionNumber():void{
35+
$this::assertSame(7, $this->version->getVersionNumber());
36+
}
37+
38+
public function testGetDimension():void{
39+
$this::assertSame(45, $this->version->getDimension());
40+
}
41+
42+
public function testGetVersionPattern():void{
43+
$this::assertSame(0b000111110010010100, $this->version->getVersionPattern());
44+
// no pattern for version < 7
45+
$this::assertNull((new Version(6))->getVersionPattern());
46+
}
47+
48+
public function testGetAlignmentPattern():void{
49+
$this::assertSame([6, 22, 38], $this->version->getAlignmentPattern());
50+
}
51+
52+
public function testGetRSBlocks():void{
53+
$this::assertSame([18, [[2, 14], [4, 15]]], $this->version->getRSBlocks(new EccLevel(EccLevel::Q)));
54+
}
55+
56+
public function testGetTotalCodewords():void{
57+
$this::assertSame(196, $this->version->getTotalCodewords());
58+
}
59+
60+
public function testConstructInvalidVersion():void{
61+
$this->expectException(QRCodeException::class);
62+
$this->expectExceptionMessage('invalid version given');
63+
64+
$version = new Version(69);
65+
}
66+
67+
}

0 commit comments

Comments
 (0)