Skip to content

Commit 4726f58

Browse files
committed
:octocat: added matrix console debugging
1 parent 53f6dcf commit 4726f58

File tree

1 file changed

+140
-59
lines changed

1 file changed

+140
-59
lines changed

tests/Data/QRMatrixTest.php

Lines changed: 140 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010

1111
namespace chillerlan\QRCodeTest\Data;
1212

13-
use chillerlan\QRCode\Common\{EccLevel, MaskPattern, Version};
1413
use chillerlan\QRCode\{QRCode, QROptions};
14+
use chillerlan\QRCode\Common\{EccLevel, MaskPattern, Version};
1515
use chillerlan\QRCode\Data\{QRCodeDataException, QRMatrix};
16+
use chillerlan\QRCode\Output\QRString;
1617
use PHPUnit\Framework\TestCase;
18+
use PHPUnit\Util\Color;
1719
use Generator;
1820

1921
/**
@@ -28,14 +30,73 @@ final class QRMatrixTest extends TestCase{
2830
* invokes a QRMatrix object
2931
*/
3032
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";
3285
}
3386

3487
/**
35-
* shortcut
88+
* debugging shortcut qirth limit to a single version when using with matrixProvider
89+
*
90+
* @see QRMatrixTest::matrixProvider()
3691
*/
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);
39100
}
40101

41102
/**
@@ -93,68 +154,77 @@ public function testGetSetCheck():void{
93154
/**
94155
* Version data provider for several pattern tests
95156
*/
96-
public function versionProvider():Generator{
157+
public function matrixProvider():Generator{
158+
$ecc = new EccLevel(EccLevel::L);
159+
$mask = new MaskPattern(MaskPattern::PATTERN_000);
160+
97161
foreach(range(1, 40) as $i){
98-
yield 'version: '.$i => [$i];
162+
yield 'version: '.$i => [new QRMatrix(new Version($i), $ecc, $mask)];
99163
}
100164
}
101165

102166
/**
103167
* Tests setting the dark module and verifies its position
104168
*
105-
* @dataProvider versionProvider
169+
* @dataProvider matrixProvider
106170
*/
107-
public function testSetDarkModule(int $version):void{
108-
$matrix = $this->getMatrix($version)->setDarkModule();
171+
public function testSetDarkModule(QRMatrix $matrix):void{
172+
$matrix->setDarkModule();
109173

110174
$this::assertSame(QRMatrix::M_DARKMODULE | QRMatrix::IS_DARK, $matrix->get(8, $matrix->size() - 8));
175+
176+
$this->dm($matrix);
111177
}
112178

113179
/**
114180
* Tests setting the finder patterns and verifies their positions
115181
*
116-
* @dataProvider versionProvider
182+
* @dataProvider matrixProvider
117183
*/
118-
public function testSetFinderPattern(int $version):void{
119-
$matrix = $this->getMatrix($version)->setFinderPattern();
184+
public function testSetFinderPattern(QRMatrix $matrix):void{
185+
$matrix->setFinderPattern();
120186

121187
$this::assertSame(QRMatrix::M_FINDER | QRMatrix::IS_DARK, $matrix->get(0, 0));
122188
$this::assertSame(QRMatrix::M_FINDER | QRMatrix::IS_DARK, $matrix->get(0, $matrix->size() - 1));
123189
$this::assertSame(QRMatrix::M_FINDER | QRMatrix::IS_DARK, $matrix->get($matrix->size() - 1, 0));
190+
191+
$this->dm($matrix);
124192
}
125193

126194
/**
127195
* Tests the separator patterns and verifies their positions
128196
*
129-
* @dataProvider versionProvider
197+
* @dataProvider matrixProvider
130198
*/
131-
public function testSetSeparators(int $version):void{
132-
$matrix = $this->getMatrix($version)->setSeparators();
199+
public function testSetSeparators(QRMatrix $matrix):void{
200+
$matrix->setSeparators();
133201

134202
$this::assertSame(QRMatrix::M_SEPARATOR, $matrix->get(7, 0));
135203
$this::assertSame(QRMatrix::M_SEPARATOR, $matrix->get(0, 7));
136204
$this::assertSame(QRMatrix::M_SEPARATOR, $matrix->get(0, $matrix->size() - 8));
137205
$this::assertSame(QRMatrix::M_SEPARATOR, $matrix->get($matrix->size() - 8, 0));
206+
207+
$this->dm($matrix);
138208
}
139209

140210
/**
141211
* Tests the alignment patterns and verifies their positions - version 1 (no pattern) skipped
142212
*
143-
* @dataProvider versionProvider
213+
* @dataProvider matrixProvider
144214
*/
145-
public function testSetAlignmentPattern(int $version):void{
215+
public function testSetAlignmentPattern(QRMatrix $matrix):void{
216+
$version = $matrix->version();
146217

147-
if($version === 1){
218+
if($version->getVersionNumber() === 1){
148219
$this::markTestSkipped('N/A (Version 1 has no alignment pattern)');
149220
}
150221

151-
$matrix = $this
152-
->getMatrix($version)
222+
$matrix
153223
->setFinderPattern()
154224
->setAlignmentPattern()
155225
;
156226

157-
$alignmentPattern = (new Version($version))->getAlignmentPattern();
227+
$alignmentPattern = $version->getAlignmentPattern();
158228

159229
foreach($alignmentPattern as $py){
160230
foreach($alignmentPattern as $px){
@@ -168,17 +238,18 @@ public function testSetAlignmentPattern(int $version):void{
168238
}
169239
}
170240

241+
$this->dm($matrix);
171242
}
172243

173244
/**
174245
* Tests the timing patterns and verifies their positions
175246
*
176-
* @dataProvider versionProvider
247+
* @dataProvider matrixProvider
177248
*/
178-
public function testSetTimingPattern(int $version):void{
249+
public function testSetTimingPattern(QRMatrix $matrix):void{
179250

180-
$matrix = $this
181-
->getMatrix($version)
251+
$matrix
252+
->setFinderPattern()
182253
->setAlignmentPattern()
183254
->setTimingPattern()
184255
;
@@ -197,49 +268,53 @@ public function testSetTimingPattern(int $version):void{
197268
$this::assertSame(QRMatrix::M_TIMING | QRMatrix::IS_DARK, $matrix->get($i, 6));
198269
}
199270
}
271+
272+
$this->dm($matrix);
200273
}
201274

202275
/**
203276
* Tests the version patterns and verifies their positions - version < 7 skipped
204277
*
205-
* @dataProvider versionProvider
278+
* @dataProvider matrixProvider
206279
*/
207-
public function testSetVersionNumber(int $version):void{
280+
public function testSetVersionNumber(QRMatrix $matrix):void{
208281

209-
if($version < 7){
282+
if($matrix->version()->getVersionNumber() < 7){
210283
$this::markTestSkipped('N/A (Version < 7)');
211284
}
212285

213-
$matrix = $this->getMatrix($version)->setVersionNumber();
286+
$matrix->setVersionNumber();
214287

215288
$this::assertTrue($matrix->checkType($matrix->size() - 9, 0, QRMatrix::M_VERSION));
216289
$this::assertTrue($matrix->checkType($matrix->size() - 11, 5, QRMatrix::M_VERSION));
217290
$this::assertTrue($matrix->checkType(0, $matrix->size() - 9, QRMatrix::M_VERSION));
218291
$this::assertTrue($matrix->checkType(5, $matrix->size() - 11, QRMatrix::M_VERSION));
292+
293+
$this->dm($matrix);
219294
}
220295

221296
/**
222297
* Tests the format patterns and verifies their positions
223298
*
224-
* @dataProvider versionProvider
299+
* @dataProvider matrixProvider
225300
*/
226-
public function testSetFormatInfo(int $version):void{
227-
$matrix = $this->getMatrix($version)->setFormatInfo();
301+
public function testSetFormatInfo(QRMatrix $matrix):void{
302+
$matrix->setFormatInfo();
228303

229304
$this::assertTrue($matrix->checkType(8, 0, QRMatrix::M_FORMAT));
230305
$this::assertTrue($matrix->checkType(0, 8, QRMatrix::M_FORMAT));
231306
$this::assertTrue($matrix->checkType($matrix->size() - 1, 8, QRMatrix::M_FORMAT));
232307
$this::assertTrue($matrix->checkType($matrix->size() - 8, 8, QRMatrix::M_FORMAT));
308+
309+
$this->dm($matrix);
233310
}
234311

235312
/**
236313
* Tests the quiet zone pattern and verifies its position
237314
*
238-
* @dataProvider versionProvider
315+
* @dataProvider matrixProvider
239316
*/
240-
public function testSetQuietZone(int $version):void{
241-
$matrix = $this->getMatrix($version);
242-
317+
public function testSetQuietZone(QRMatrix $matrix):void{
243318
$size = $matrix->size();
244319
$q = 5;
245320

@@ -257,6 +332,8 @@ public function testSetQuietZone(int $version):void{
257332

258333
$this::assertSame(QRMatrix::M_TEST | QRMatrix::IS_DARK, $matrix->get($q, $q));
259334
$this::assertSame(QRMatrix::M_TEST | QRMatrix::IS_DARK, $matrix->get($size - 1 - $q, $size - 1 - $q));
335+
336+
$this->dm($matrix);
260337
}
261338

262339
/**
@@ -289,6 +366,8 @@ public function testSetLogoSpaceOrientation():void{
289366
// SE corner
290367
$this::assertTrue($matrix->checkType(38, 35, QRMatrix::M_LOGO));
291368
$this::assertFalse($matrix->checkType(39, 36, QRMatrix::M_LOGO));
369+
370+
self::debugMatrix($matrix);
292371
}
293372

294373
/**
@@ -301,25 +380,27 @@ public function testSetLogoSpacePosition():void{
301380
$o->addQuietzone = true;
302381
$o->quietzoneSize = 10;
303382

304-
$m = (new QRCode($o))->addByteSegment('testdata')->getMatrix();
383+
$matrix = (new QRCode($o))->addByteSegment('testdata')->getMatrix();
305384

306385
// 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));
316395

317396
// i just realized that setLogoSpace() could be called multiple times
318397
// 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);
323404
}
324405

325406
/**
@@ -351,22 +432,22 @@ public function testSetLogoSpaceMaxSizeException():void{
351432
*/
352433
public function testFlip():void{
353434
// using the dark module here because i'm lazy
354-
$matrix = $this->getMatrix(10)->setDarkModule();
435+
$this->matrix->setDarkModule();
355436
$x = 8;
356-
$y = $matrix->size() - 8;
437+
$y = $this->matrix->size() - 8;
357438

358439
// cover checkType()
359-
$this::assertTrue($matrix->checkType($x, $y, QRMatrix::M_DARKMODULE));
440+
$this::assertTrue($this->matrix->checkType($x, $y, QRMatrix::M_DARKMODULE));
360441
// 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));
362443
// flip
363-
$matrix->flip($x, $y);
444+
$this->matrix->flip($x, $y);
364445
// verify flip
365-
$this::assertSame(QRMatrix::M_DARKMODULE, $matrix->get($x, $y));
446+
$this::assertSame(QRMatrix::M_DARKMODULE, $this->matrix->get($x, $y));
366447
// flip again
367-
$matrix->flip($x, $y);
448+
$this->matrix->flip($x, $y);
368449
// 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));
370451
}
371452

372453
}

0 commit comments

Comments
 (0)