11
11
namespace chillerlan \QRCodeTest \Data ;
12
12
13
13
use chillerlan \QRCode \Common \{EccLevel , MaskPattern , Mode , Version };
14
- use PHPUnit \Framework \Attributes \DataProvider ;
15
14
use chillerlan \QRCode \Data \{QRCodeDataException , QRData , QRDataModeInterface , QRMatrix };
16
15
use chillerlan \QRCode \QROptions ;
17
16
use chillerlan \QRCodeTest \QRMaxLengthTrait ;
18
- use Exception , Generator ;
17
+ use Exception , Generator ;
18
+ use PHPUnit \Framework \Attributes \DataProvider ;
19
19
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 ;
29
21
30
22
/**
31
23
* The data interface test abstract
@@ -35,26 +27,15 @@ abstract class DataInterfaceTestAbstract extends TestCase{
35
27
36
28
protected QRData $ QRData ;
37
29
protected QRDataModeInterface $ dataMode ;
38
- protected static string $ FQN ;
39
- protected static string $ testdata ;
30
+
31
+ protected const testData = '' ;
40
32
41
33
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);
43
36
}
44
37
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 ;
58
39
59
40
/**
60
41
* @return int[][]
@@ -70,7 +51,7 @@ public static function maskPatternProvider():array{
70
51
public function testInitMatrix (int $ pattern ):void {
71
52
$ maskPattern = new MaskPattern ($ pattern );
72
53
73
- $ this ->QRData ->setData ([new static:: $ FQN ( static :: $ testdata ) ]);
54
+ $ this ->QRData ->setData ([$ this -> dataMode ]);
74
55
75
56
$ matrix = $ this ->QRData ->writeMatrix ()->setFormatInfo ($ maskPattern )->mask ($ maskPattern );
76
57
@@ -85,18 +66,16 @@ abstract public static function stringValidateProvider():array;
85
66
*/
86
67
#[DataProvider('stringValidateProvider ' )]
87
68
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 ));
90
70
}
91
71
92
72
/**
93
- * Tests if a binary string is properly validated as false
73
+ * Tests if a random binary string is properly validated as false
94
74
*
95
75
* @see https://github.com/chillerlan/php-qrcode/issues/182
96
76
*/
97
77
public function testBinaryStringInvalid ():void {
98
- /** @noinspection PhpUndefinedMethodInspection */
99
- $ this ::assertFalse (static ::$ FQN ::validateString (hex2bin ('01015989f47dff8e852122117e04c90b9f15defc1c36477b1fe1 ' )));
78
+ $ this ::assertFalse ($ this ->dataMode ::validateString (hex2bin ('01015989f47dff8e852122117e04c90b9f15defc1c36477b1fe1 ' )));
100
79
}
101
80
102
81
/**
@@ -114,16 +93,14 @@ public function testDecodeSegment(int $version):void{
114
93
$ options = new QROptions ;
115
94
$ options ->version = $ version ;
116
95
117
- // invoke a datamode interface
118
- $ this ->dataMode = new static::$ FQN (static ::$ testdata );
119
96
// invoke a QRData instance and write data
120
97
$ this ->QRData = new QRData ($ options , [$ this ->dataMode ]);
121
98
// get the filled bitbuffer
122
99
$ bitBuffer = $ this ->QRData ->getBitBuffer ();
123
100
// read the first 4 bits
124
101
$ this ::assertSame ($ this ->dataMode ::DATAMODE , $ bitBuffer ->read (4 ));
125
102
// 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 ));
127
104
}
128
105
129
106
/**
@@ -139,15 +116,17 @@ public function testDecodeSegment(int $version):void{
139
116
*/
140
117
public static function maxLengthProvider ():Generator {
141
118
$ 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 );
144
123
145
124
for ($ v = 1 ; $ v <= 40 ; $ v ++){
146
125
$ version = new Version ($ v );
147
126
148
127
foreach ($ eccLevels as $ eccLevel ){
149
128
// maximum characters per ecc/mode
150
- $ len = static ::getMaxLengthForMode (static :: $ FQN :: DATAMODE , $ version , $ eccLevel );
129
+ $ len = static ::getMaxLengthForMode ($ dataMode , $ version , $ eccLevel );
151
130
// a string that contains the maximum amount of characters for the given mode
152
131
$ val = ($ mb ) ? mb_substr ($ str , 0 , $ len ) : substr ($ str , 0 , $ len );
153
132
// same as above but character count exceeds
@@ -171,8 +150,10 @@ public function testMaxLength(Version $version, EccLevel $eccLevel, string $str)
171
150
$ options = new QROptions ;
172
151
$ options ->version = $ version ->getVersionNumber ();
173
152
$ options ->eccLevel = $ eccLevel ->getLevel ();
174
- $ this ->dataMode = new static::$ FQN ($ str );
153
+
154
+ $ this ->dataMode = static ::getDataModeInterface ($ str );
175
155
$ this ->QRData = new QRData ($ options , [$ this ->dataMode ]);
156
+
176
157
$ bitBuffer = $ this ->QRData ->getBitBuffer ();
177
158
178
159
$ this ::assertSame ($ this ->dataMode ::DATAMODE , $ bitBuffer ->read (4 ));
@@ -187,8 +168,10 @@ public function testGetMinimumVersion(Version $version, EccLevel $eccLevel, stri
187
168
$ options = new QROptions ;
188
169
$ options ->version = Version::AUTO ;
189
170
$ options ->eccLevel = $ eccLevel ->getLevel ();
190
- $ this ->dataMode = new static::$ FQN ($ str );
171
+
172
+ $ this ->dataMode = static ::getDataModeInterface ($ str );
191
173
$ this ->QRData = new QRData ($ options , [$ this ->dataMode ]);
174
+
192
175
$ bitBuffer = $ this ->QRData ->getBitBuffer ();
193
176
194
177
$ this ::assertLessThanOrEqual ($ eccLevel ->getMaxBitsForVersion ($ version ), $ this ->QRData ->estimateTotalBitLength ());
@@ -214,7 +197,7 @@ public function testMaxLengthOverflowException(Version $version, EccLevel $eccLe
214
197
$ options ->eccLevel = $ eccLevel ->getLevel ();
215
198
216
199
/** @phan-suppress-next-line PhanNoopNew */
217
- new QRData ($ options , [new static::$ FQN ($ str1 )]);
200
+ new QRData ($ options , [static ::getDataModeInterface ($ str1 )]);
218
201
}
219
202
220
203
/**
@@ -224,7 +207,7 @@ public function testGetMinimumVersionException():void{
224
207
$ this ->expectException (QRCodeDataException::class);
225
208
$ this ->expectExceptionMessage ('data exceeds ' );
226
209
227
- $ this ->QRData ->setData ([new static::$ FQN (str_repeat (static ::$ testdata , 1337 ))]);
210
+ $ this ->QRData ->setData ([static ::getDataModeInterface (str_repeat (static ::testData , 1337 ))]);
228
211
}
229
212
230
213
/**
@@ -234,8 +217,7 @@ public function testInvalidDataException():void{
234
217
$ this ->expectException (QRCodeDataException::class);
235
218
$ this ->expectExceptionMessage ('invalid data ' );
236
219
237
- /** @phan-suppress-next-line PhanNoopNew */
238
- new static::$ FQN ('## ' );
220
+ static ::getDataModeInterface ('## ' );
239
221
}
240
222
241
223
/**
@@ -245,8 +227,7 @@ public function testInvalidDataOnEmptyException():void{
245
227
$ this ->expectException (QRCodeDataException::class);
246
228
$ this ->expectExceptionMessage ('invalid data ' );
247
229
248
- /** @phan-suppress-next-line PhanNoopNew */
249
- new static::$ FQN ('' );
230
+ static ::getDataModeInterface ('' );
250
231
}
251
232
252
233
}
0 commit comments