10
10
11
11
namespace chillerlan \QRCodeTest \Data ;
12
12
13
- use chillerlan \QRCode \Common \{EccLevel , MaskPattern , Version };
14
13
use chillerlan \QRCode \{QRCode , QROptions };
14
+ use chillerlan \QRCode \Common \{EccLevel , MaskPattern , Version };
15
15
use chillerlan \QRCode \Data \{QRCodeDataException , QRMatrix };
16
+ use chillerlan \QRCode \Output \QRString ;
16
17
use PHPUnit \Framework \TestCase ;
18
+ use PHPUnit \Util \Color ;
17
19
use Generator ;
18
20
19
21
/**
@@ -28,14 +30,73 @@ final class QRMatrixTest extends TestCase{
28
30
* invokes a QRMatrix object
29
31
*/
30
32
protected function setUp ():void {
31
- $ this ->matrix = $ this ->getMatrix ($ this ::version);
33
+ $ this ->matrix = new QRMatrix (
34
+ new Version ($ this ::version),
35
+ new EccLevel (EccLevel::L),
36
+ new MaskPattern (MaskPattern::PATTERN_000 )
37
+ );
38
+ }
39
+
40
+ /**
41
+ * Matrix debugging console output
42
+ */
43
+ public static function debugMatrix (QRMatrix $ matrix ):void {
44
+ $ opt = new QROptions ;
45
+ $ opt ->outputType = QRCode::OUTPUT_STRING_TEXT ;
46
+ $ opt ->eol = Color::colorize ('reset ' , "\x00\n" );
47
+ $ opt ->moduleValues = [
48
+ // finder
49
+ QRMatrix::M_FINDER | QRMatrix::IS_DARK => Color::colorize ('fg-black ' , '🔴 ' ), // dark (true)
50
+ QRMatrix::M_FINDER => Color::colorize ('fg-black ' , '⭕ ' ), // light (false)
51
+ QRMatrix::M_FINDER_DOT | QRMatrix::IS_DARK => Color::colorize ('fg-black ' , '🔴 ' ), // finder dot, dark (true)
52
+ // alignment
53
+ QRMatrix::M_ALIGNMENT | QRMatrix::IS_DARK => Color::colorize ('fg-blue ' , '🔴 ' ),
54
+ QRMatrix::M_ALIGNMENT => Color::colorize ('fg-blue ' , '⭕ ' ),
55
+ // timing
56
+ QRMatrix::M_TIMING | QRMatrix::IS_DARK => Color::colorize ('fg-red ' , '🔴 ' ),
57
+ QRMatrix::M_TIMING => Color::colorize ('fg-red ' , '⭕ ' ),
58
+ // format
59
+ QRMatrix::M_FORMAT | QRMatrix::IS_DARK => Color::colorize ('fg-magenta ' , '🔴 ' ),
60
+ QRMatrix::M_FORMAT => Color::colorize ('fg-magenta ' , '⭕ ' ),
61
+ // version
62
+ QRMatrix::M_VERSION | QRMatrix::IS_DARK => Color::colorize ('fg-green ' , '🔴 ' ),
63
+ QRMatrix::M_VERSION => Color::colorize ('fg-green ' , '⭕ ' ),
64
+ // data
65
+ QRMatrix::M_DATA | QRMatrix::IS_DARK => Color::colorize ('fg-white ' , '🔴 ' ),
66
+ QRMatrix::M_DATA => Color::colorize ('fg-white ' , '⭕ ' ),
67
+ // darkmodule
68
+ QRMatrix::M_DARKMODULE | QRMatrix::IS_DARK => Color::colorize ('fg-black ' , '🔴 ' ),
69
+ // separator
70
+ QRMatrix::M_SEPARATOR => Color::colorize ('fg-cyan ' , '⭕ ' ),
71
+ // quietzone
72
+ QRMatrix::M_QUIETZONE => Color::colorize ('fg-cyan ' , '⭕ ' ),
73
+ // logo space
74
+ QRMatrix::M_LOGO => Color::colorize ('fg-yellow ' , '⭕ ' ),
75
+ // empty
76
+ QRMatrix::M_NULL => Color::colorize ('fg-black ' , '⭕ ' ),
77
+ // data
78
+ QRMatrix::M_TEST | QRMatrix::IS_DARK => Color::colorize ('fg-white ' , '🔴 ' ),
79
+ QRMatrix::M_TEST => Color::colorize ('fg-black ' , '⭕ ' ),
80
+ ];
81
+
82
+ $ out = (new QRString ($ opt , $ matrix ))->dump ();
83
+
84
+ echo $ out ."\n\n" ;
32
85
}
33
86
34
87
/**
35
- * shortcut
88
+ * debugging shortcut qirth limit to a single version when using with matrixProvider
89
+ *
90
+ * @see QRMatrixTest::matrixProvider()
36
91
*/
37
- protected function getMatrix (int $ version ):QRMatrix {
38
- return new QRMatrix (new Version ($ version ), new EccLevel (EccLevel::L), new MaskPattern (MaskPattern::PATTERN_000 ));
92
+ protected function dm (QRMatrix $ matrix ):void {
93
+
94
+ // limit
95
+ if ($ matrix ->version ()->getVersionNumber () !== 7 ){
96
+ return ;
97
+ }
98
+
99
+ self ::debugMatrix ($ matrix );
39
100
}
40
101
41
102
/**
@@ -93,68 +154,77 @@ public function testGetSetCheck():void{
93
154
/**
94
155
* Version data provider for several pattern tests
95
156
*/
96
- public function versionProvider ():Generator {
157
+ public function matrixProvider ():Generator {
158
+ $ ecc = new EccLevel (EccLevel::L);
159
+ $ mask = new MaskPattern (MaskPattern::PATTERN_000 );
160
+
97
161
foreach (range (1 , 40 ) as $ i ){
98
- yield 'version: ' .$ i => [$ i ];
162
+ yield 'version: ' .$ i => [new QRMatrix ( new Version ( $ i ), $ ecc , $ mask ) ];
99
163
}
100
164
}
101
165
102
166
/**
103
167
* Tests setting the dark module and verifies its position
104
168
*
105
- * @dataProvider versionProvider
169
+ * @dataProvider matrixProvider
106
170
*/
107
- public function testSetDarkModule (int $ version ):void {
108
- $ matrix = $ this -> getMatrix ( $ version ) ->setDarkModule ();
171
+ public function testSetDarkModule (QRMatrix $ matrix ):void {
172
+ $ matrix ->setDarkModule ();
109
173
110
174
$ this ::assertSame (QRMatrix::M_DARKMODULE | QRMatrix::IS_DARK , $ matrix ->get (8 , $ matrix ->size () - 8 ));
175
+
176
+ $ this ->dm ($ matrix );
111
177
}
112
178
113
179
/**
114
180
* Tests setting the finder patterns and verifies their positions
115
181
*
116
- * @dataProvider versionProvider
182
+ * @dataProvider matrixProvider
117
183
*/
118
- public function testSetFinderPattern (int $ version ):void {
119
- $ matrix = $ this -> getMatrix ( $ version ) ->setFinderPattern ();
184
+ public function testSetFinderPattern (QRMatrix $ matrix ):void {
185
+ $ matrix ->setFinderPattern ();
120
186
121
187
$ this ::assertSame (QRMatrix::M_FINDER | QRMatrix::IS_DARK , $ matrix ->get (0 , 0 ));
122
188
$ this ::assertSame (QRMatrix::M_FINDER | QRMatrix::IS_DARK , $ matrix ->get (0 , $ matrix ->size () - 1 ));
123
189
$ this ::assertSame (QRMatrix::M_FINDER | QRMatrix::IS_DARK , $ matrix ->get ($ matrix ->size () - 1 , 0 ));
190
+
191
+ $ this ->dm ($ matrix );
124
192
}
125
193
126
194
/**
127
195
* Tests the separator patterns and verifies their positions
128
196
*
129
- * @dataProvider versionProvider
197
+ * @dataProvider matrixProvider
130
198
*/
131
- public function testSetSeparators (int $ version ):void {
132
- $ matrix = $ this -> getMatrix ( $ version ) ->setSeparators ();
199
+ public function testSetSeparators (QRMatrix $ matrix ):void {
200
+ $ matrix ->setSeparators ();
133
201
134
202
$ this ::assertSame (QRMatrix::M_SEPARATOR , $ matrix ->get (7 , 0 ));
135
203
$ this ::assertSame (QRMatrix::M_SEPARATOR , $ matrix ->get (0 , 7 ));
136
204
$ this ::assertSame (QRMatrix::M_SEPARATOR , $ matrix ->get (0 , $ matrix ->size () - 8 ));
137
205
$ this ::assertSame (QRMatrix::M_SEPARATOR , $ matrix ->get ($ matrix ->size () - 8 , 0 ));
206
+
207
+ $ this ->dm ($ matrix );
138
208
}
139
209
140
210
/**
141
211
* Tests the alignment patterns and verifies their positions - version 1 (no pattern) skipped
142
212
*
143
- * @dataProvider versionProvider
213
+ * @dataProvider matrixProvider
144
214
*/
145
- public function testSetAlignmentPattern (int $ version ):void {
215
+ public function testSetAlignmentPattern (QRMatrix $ matrix ):void {
216
+ $ version = $ matrix ->version ();
146
217
147
- if ($ version === 1 ){
218
+ if ($ version-> getVersionNumber () === 1 ){
148
219
$ this ::markTestSkipped ('N/A (Version 1 has no alignment pattern) ' );
149
220
}
150
221
151
- $ matrix = $ this
152
- ->getMatrix ($ version )
222
+ $ matrix
153
223
->setFinderPattern ()
154
224
->setAlignmentPattern ()
155
225
;
156
226
157
- $ alignmentPattern = ( new Version ( $ version)) ->getAlignmentPattern ();
227
+ $ alignmentPattern = $ version ->getAlignmentPattern ();
158
228
159
229
foreach ($ alignmentPattern as $ py ){
160
230
foreach ($ alignmentPattern as $ px ){
@@ -168,17 +238,18 @@ public function testSetAlignmentPattern(int $version):void{
168
238
}
169
239
}
170
240
241
+ $ this ->dm ($ matrix );
171
242
}
172
243
173
244
/**
174
245
* Tests the timing patterns and verifies their positions
175
246
*
176
- * @dataProvider versionProvider
247
+ * @dataProvider matrixProvider
177
248
*/
178
- public function testSetTimingPattern (int $ version ):void {
249
+ public function testSetTimingPattern (QRMatrix $ matrix ):void {
179
250
180
- $ matrix = $ this
181
- ->getMatrix ( $ version )
251
+ $ matrix
252
+ ->setFinderPattern ( )
182
253
->setAlignmentPattern ()
183
254
->setTimingPattern ()
184
255
;
@@ -197,49 +268,53 @@ public function testSetTimingPattern(int $version):void{
197
268
$ this ::assertSame (QRMatrix::M_TIMING | QRMatrix::IS_DARK , $ matrix ->get ($ i , 6 ));
198
269
}
199
270
}
271
+
272
+ $ this ->dm ($ matrix );
200
273
}
201
274
202
275
/**
203
276
* Tests the version patterns and verifies their positions - version < 7 skipped
204
277
*
205
- * @dataProvider versionProvider
278
+ * @dataProvider matrixProvider
206
279
*/
207
- public function testSetVersionNumber (int $ version ):void {
280
+ public function testSetVersionNumber (QRMatrix $ matrix ):void {
208
281
209
- if ($ version < 7 ){
282
+ if ($ matrix -> version ()-> getVersionNumber () < 7 ){
210
283
$ this ::markTestSkipped ('N/A (Version < 7) ' );
211
284
}
212
285
213
- $ matrix = $ this -> getMatrix ( $ version ) ->setVersionNumber ();
286
+ $ matrix ->setVersionNumber ();
214
287
215
288
$ this ::assertTrue ($ matrix ->checkType ($ matrix ->size () - 9 , 0 , QRMatrix::M_VERSION ));
216
289
$ this ::assertTrue ($ matrix ->checkType ($ matrix ->size () - 11 , 5 , QRMatrix::M_VERSION ));
217
290
$ this ::assertTrue ($ matrix ->checkType (0 , $ matrix ->size () - 9 , QRMatrix::M_VERSION ));
218
291
$ this ::assertTrue ($ matrix ->checkType (5 , $ matrix ->size () - 11 , QRMatrix::M_VERSION ));
292
+
293
+ $ this ->dm ($ matrix );
219
294
}
220
295
221
296
/**
222
297
* Tests the format patterns and verifies their positions
223
298
*
224
- * @dataProvider versionProvider
299
+ * @dataProvider matrixProvider
225
300
*/
226
- public function testSetFormatInfo (int $ version ):void {
227
- $ matrix = $ this -> getMatrix ( $ version ) ->setFormatInfo ();
301
+ public function testSetFormatInfo (QRMatrix $ matrix ):void {
302
+ $ matrix ->setFormatInfo ();
228
303
229
304
$ this ::assertTrue ($ matrix ->checkType (8 , 0 , QRMatrix::M_FORMAT ));
230
305
$ this ::assertTrue ($ matrix ->checkType (0 , 8 , QRMatrix::M_FORMAT ));
231
306
$ this ::assertTrue ($ matrix ->checkType ($ matrix ->size () - 1 , 8 , QRMatrix::M_FORMAT ));
232
307
$ this ::assertTrue ($ matrix ->checkType ($ matrix ->size () - 8 , 8 , QRMatrix::M_FORMAT ));
308
+
309
+ $ this ->dm ($ matrix );
233
310
}
234
311
235
312
/**
236
313
* Tests the quiet zone pattern and verifies its position
237
314
*
238
- * @dataProvider versionProvider
315
+ * @dataProvider matrixProvider
239
316
*/
240
- public function testSetQuietZone (int $ version ):void {
241
- $ matrix = $ this ->getMatrix ($ version );
242
-
317
+ public function testSetQuietZone (QRMatrix $ matrix ):void {
243
318
$ size = $ matrix ->size ();
244
319
$ q = 5 ;
245
320
@@ -257,6 +332,8 @@ public function testSetQuietZone(int $version):void{
257
332
258
333
$ this ::assertSame (QRMatrix::M_TEST | QRMatrix::IS_DARK , $ matrix ->get ($ q , $ q ));
259
334
$ this ::assertSame (QRMatrix::M_TEST | QRMatrix::IS_DARK , $ matrix ->get ($ size - 1 - $ q , $ size - 1 - $ q ));
335
+
336
+ $ this ->dm ($ matrix );
260
337
}
261
338
262
339
/**
@@ -289,6 +366,8 @@ public function testSetLogoSpaceOrientation():void{
289
366
// SE corner
290
367
$ this ::assertTrue ($ matrix ->checkType (38 , 35 , QRMatrix::M_LOGO ));
291
368
$ this ::assertFalse ($ matrix ->checkType (39 , 36 , QRMatrix::M_LOGO ));
369
+
370
+ self ::debugMatrix ($ matrix );
292
371
}
293
372
294
373
/**
@@ -301,25 +380,27 @@ public function testSetLogoSpacePosition():void{
301
380
$ o ->addQuietzone = true ;
302
381
$ o ->quietzoneSize = 10 ;
303
382
304
- $ m = (new QRCode ($ o ))->addByteSegment ('testdata ' )->getMatrix ();
383
+ $ matrix = (new QRCode ($ o ))->addByteSegment ('testdata ' )->getMatrix ();
305
384
306
385
// logo space should not overwrite quiet zone & function patterns
307
- $ m ->setLogoSpace (21 , 21 , -10 , -10 );
308
- $ this ::assertSame (QRMatrix::M_QUIETZONE , $ m ->get (9 , 9 ));
309
- $ this ::assertSame (QRMatrix::M_FINDER | QRMatrix::IS_DARK , $ m ->get (10 , 10 ));
310
- $ this ::assertSame (QRMatrix::M_FINDER | QRMatrix::IS_DARK , $ m ->get (16 , 16 ));
311
- $ this ::assertSame (QRMatrix::M_SEPARATOR , $ m ->get (17 , 17 ));
312
- $ this ::assertSame (QRMatrix::M_FORMAT | QRMatrix::IS_DARK , $ m ->get (18 , 18 ));
313
- $ this ::assertSame (QRMatrix::M_LOGO , $ m ->get (19 , 19 ));
314
- $ this ::assertSame (QRMatrix::M_LOGO , $ m ->get (20 , 20 ));
315
- $ this ::assertNotSame (QRMatrix::M_LOGO , $ m ->get (21 , 21 ));
386
+ $ matrix ->setLogoSpace (21 , 21 , -10 , -10 );
387
+ $ this ::assertSame (QRMatrix::M_QUIETZONE , $ matrix ->get (9 , 9 ));
388
+ $ this ::assertSame (QRMatrix::M_FINDER | QRMatrix::IS_DARK , $ matrix ->get (10 , 10 ));
389
+ $ this ::assertSame (QRMatrix::M_FINDER | QRMatrix::IS_DARK , $ matrix ->get (16 , 16 ));
390
+ $ this ::assertSame (QRMatrix::M_SEPARATOR , $ matrix ->get (17 , 17 ));
391
+ $ this ::assertSame (QRMatrix::M_FORMAT | QRMatrix::IS_DARK , $ matrix ->get (18 , 18 ));
392
+ $ this ::assertSame (QRMatrix::M_LOGO , $ matrix ->get (19 , 19 ));
393
+ $ this ::assertSame (QRMatrix::M_LOGO , $ matrix ->get (20 , 20 ));
394
+ $ this ::assertNotSame (QRMatrix::M_LOGO , $ matrix ->get (21 , 21 ));
316
395
317
396
// i just realized that setLogoSpace() could be called multiple times
318
397
// on the same instance and i'm not going to do anything about it :P
319
- $ m ->setLogoSpace (21 , 21 , 45 , 45 );
320
- $ this ::assertNotSame (QRMatrix::M_LOGO , $ m ->get (54 , 54 ));
321
- $ this ::assertSame (QRMatrix::M_LOGO , $ m ->get (55 , 55 ));
322
- $ this ::assertSame (QRMatrix::M_QUIETZONE , $ m ->get (67 , 67 ));
398
+ $ matrix ->setLogoSpace (21 , 21 , 45 , 45 );
399
+ $ this ::assertNotSame (QRMatrix::M_LOGO , $ matrix ->get (54 , 54 ));
400
+ $ this ::assertSame (QRMatrix::M_LOGO , $ matrix ->get (55 , 55 ));
401
+ $ this ::assertSame (QRMatrix::M_QUIETZONE , $ matrix ->get (67 , 67 ));
402
+
403
+ self ::debugMatrix ($ matrix );
323
404
}
324
405
325
406
/**
@@ -351,22 +432,22 @@ public function testSetLogoSpaceMaxSizeException():void{
351
432
*/
352
433
public function testFlip ():void {
353
434
// using the dark module here because i'm lazy
354
- $ matrix = $ this ->getMatrix ( 10 ) ->setDarkModule ();
435
+ $ this ->matrix ->setDarkModule ();
355
436
$ x = 8 ;
356
- $ y = $ matrix ->size () - 8 ;
437
+ $ y = $ this -> matrix ->size () - 8 ;
357
438
358
439
// cover checkType()
359
- $ this ::assertTrue ($ matrix ->checkType ($ x , $ y , QRMatrix::M_DARKMODULE ));
440
+ $ this ::assertTrue ($ this -> matrix ->checkType ($ x , $ y , QRMatrix::M_DARKMODULE ));
360
441
// verify the current state (dark)
361
- $ this ::assertSame (QRMatrix::M_DARKMODULE | QRMatrix::IS_DARK , $ matrix ->get ($ x , $ y ));
442
+ $ this ::assertSame (QRMatrix::M_DARKMODULE | QRMatrix::IS_DARK , $ this -> matrix ->get ($ x , $ y ));
362
443
// flip
363
- $ matrix ->flip ($ x , $ y );
444
+ $ this -> matrix ->flip ($ x , $ y );
364
445
// verify flip
365
- $ this ::assertSame (QRMatrix::M_DARKMODULE , $ matrix ->get ($ x , $ y ));
446
+ $ this ::assertSame (QRMatrix::M_DARKMODULE , $ this -> matrix ->get ($ x , $ y ));
366
447
// flip again
367
- $ matrix ->flip ($ x , $ y );
448
+ $ this -> matrix ->flip ($ x , $ y );
368
449
// verify flip
369
- $ this ::assertSame (QRMatrix::M_DARKMODULE | QRMatrix::IS_DARK , $ matrix ->get ($ x , $ y ));
450
+ $ this ::assertSame (QRMatrix::M_DARKMODULE | QRMatrix::IS_DARK , $ this -> matrix ->get ($ x , $ y ));
370
451
}
371
452
372
453
}
0 commit comments