QRMatrixTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. <?php
  2. /**
  3. * Class QRMatrixTest
  4. *
  5. * @created 17.11.2017
  6. * @author Smiley <smiley@chillerlan.net>
  7. * @copyright 2017 Smiley
  8. * @license MIT
  9. */
  10. namespace chillerlan\QRCodeTest\Data;
  11. use chillerlan\QRCode\{QRCode, QROptions};
  12. use chillerlan\QRCode\Common\{EccLevel, MaskPattern, Version};
  13. use chillerlan\QRCode\Data\{QRCodeDataException, QRMatrix};
  14. use chillerlan\QRCode\Output\{QROutputInterface, QRString};
  15. use PHPUnit\Framework\TestCase;
  16. use PHPUnit\Util\Color;
  17. use Generator;
  18. /**
  19. * Tests the QRMatix class
  20. */
  21. final class QRMatrixTest extends TestCase{
  22. private const version = 40;
  23. private QRMatrix $matrix;
  24. /**
  25. * invokes a QRMatrix object
  26. */
  27. protected function setUp():void{
  28. $this->matrix = new QRMatrix(
  29. new Version($this::version),
  30. new EccLevel(EccLevel::L),
  31. new MaskPattern(MaskPattern::PATTERN_000)
  32. );
  33. }
  34. /**
  35. * Matrix debugging console output
  36. */
  37. public static function debugMatrix(QRMatrix $matrix):void{
  38. $opt = new QROptions;
  39. $opt->outputType = QROutputInterface::STRING_TEXT;
  40. $opt->eol = Color::colorize('reset', "\x00\n");
  41. $opt->moduleValues = [
  42. // finder
  43. QRMatrix::M_FINDER | QRMatrix::IS_DARK => Color::colorize('fg-black', '🔴'), // dark (true)
  44. QRMatrix::M_FINDER => Color::colorize('fg-black', '⭕'), // light (false)
  45. QRMatrix::M_FINDER_DOT | QRMatrix::IS_DARK => Color::colorize('fg-black', '🔴'), // finder dot, dark (true)
  46. // alignment
  47. QRMatrix::M_ALIGNMENT | QRMatrix::IS_DARK => Color::colorize('fg-blue', '🔴'),
  48. QRMatrix::M_ALIGNMENT => Color::colorize('fg-blue', '⭕'),
  49. // timing
  50. QRMatrix::M_TIMING | QRMatrix::IS_DARK => Color::colorize('fg-red', '🔴'),
  51. QRMatrix::M_TIMING => Color::colorize('fg-red', '⭕'),
  52. // format
  53. QRMatrix::M_FORMAT | QRMatrix::IS_DARK => Color::colorize('fg-magenta', '🔴'),
  54. QRMatrix::M_FORMAT => Color::colorize('fg-magenta', '⭕'),
  55. // version
  56. QRMatrix::M_VERSION | QRMatrix::IS_DARK => Color::colorize('fg-green', '🔴'),
  57. QRMatrix::M_VERSION => Color::colorize('fg-green', '⭕'),
  58. // data
  59. QRMatrix::M_DATA | QRMatrix::IS_DARK => Color::colorize('fg-white', '🔴'),
  60. QRMatrix::M_DATA => Color::colorize('fg-white', '⭕'),
  61. // darkmodule
  62. QRMatrix::M_DARKMODULE | QRMatrix::IS_DARK => Color::colorize('fg-black', '🔴'),
  63. // separator
  64. QRMatrix::M_SEPARATOR => Color::colorize('fg-cyan', '⭕'),
  65. // quietzone
  66. QRMatrix::M_QUIETZONE => Color::colorize('fg-cyan', '⭕'),
  67. // logo space
  68. QRMatrix::M_LOGO => Color::colorize('fg-yellow', '⭕'),
  69. // empty
  70. QRMatrix::M_NULL => Color::colorize('fg-black', '⭕'),
  71. // data
  72. QRMatrix::M_TEST | QRMatrix::IS_DARK => Color::colorize('fg-white', '🔴'),
  73. QRMatrix::M_TEST => Color::colorize('fg-black', '⭕'),
  74. ];
  75. $out = (new QRString($opt, $matrix))->dump();
  76. echo "\n\n".$out."\n\n";
  77. }
  78. /**
  79. * debugging shortcut qirth limit to a single version when using with matrixProvider
  80. *
  81. * @see QRMatrixTest::matrixProvider()
  82. */
  83. protected function dm(QRMatrix $matrix):void{
  84. // limit
  85. if($matrix->version()->getVersionNumber() !== 7){
  86. return;
  87. }
  88. self::debugMatrix($matrix);
  89. }
  90. /**
  91. * Validates the QRMatrix instance
  92. */
  93. public function testInstance():void{
  94. $this::assertInstanceOf(QRMatrix::class, $this->matrix);
  95. }
  96. /**
  97. * Tests if size() returns the actual matrix size/count
  98. */
  99. public function testSize():void{
  100. $this::assertCount($this->matrix->size(), $this->matrix->matrix());
  101. }
  102. /**
  103. * Tests if version() returns the current (given) version
  104. */
  105. public function testVersion():void{
  106. $this::assertSame($this::version, $this->matrix->version()->getVersionNumber());
  107. }
  108. /**
  109. * Tests if eccLevel() returns the current (given) ECC level
  110. */
  111. public function testECC():void{
  112. $this::assertSame(EccLevel::L, $this->matrix->eccLevel()->getLevel());
  113. }
  114. /**
  115. * Tests if maskPattern() returns the current (or default) mask pattern
  116. */
  117. public function testMaskPattern():void{
  118. // set via matrix evaluation
  119. $matrix = (new QRCode)->addByteSegment('testdata')->getMatrix();
  120. $this::assertInstanceOf(MaskPattern::class, $matrix->maskPattern());
  121. $this::assertSame(MaskPattern::PATTERN_100, $matrix->maskPattern()->getPattern());
  122. }
  123. /**
  124. * Tests the set(), get() and check() methods
  125. */
  126. public function testGetSetCheck():void{
  127. $this->matrix->set(10, 10, true, QRMatrix::M_TEST);
  128. $this::assertSame(QRMatrix::M_TEST | QRMatrix::IS_DARK, $this->matrix->get(10, 10));
  129. $this::assertTrue($this->matrix->check(10, 10));
  130. $this->matrix->set(20, 20, false, QRMatrix::M_TEST);
  131. $this::assertSame(QRMatrix::M_TEST, $this->matrix->get(20, 20));
  132. $this::assertFalse($this->matrix->check(20, 20));
  133. }
  134. /**
  135. * Version data provider for several pattern tests
  136. */
  137. public function matrixProvider():Generator{
  138. $ecc = new EccLevel(EccLevel::L);
  139. $mask = new MaskPattern(MaskPattern::PATTERN_000);
  140. foreach(range(1, 40) as $i){
  141. yield 'version: '.$i => [new QRMatrix(new Version($i), $ecc, $mask)];
  142. }
  143. }
  144. /**
  145. * Tests setting the dark module and verifies its position
  146. *
  147. * @dataProvider matrixProvider
  148. */
  149. public function testSetDarkModule(QRMatrix $matrix):void{
  150. $matrix->setDarkModule();
  151. $this::assertSame(QRMatrix::M_DARKMODULE | QRMatrix::IS_DARK, $matrix->get(8, $matrix->size() - 8));
  152. $this->dm($matrix);
  153. }
  154. /**
  155. * Tests setting the finder patterns and verifies their positions
  156. *
  157. * @dataProvider matrixProvider
  158. */
  159. public function testSetFinderPattern(QRMatrix $matrix):void{
  160. $matrix->setFinderPattern();
  161. $this::assertSame(QRMatrix::M_FINDER | QRMatrix::IS_DARK, $matrix->get(0, 0));
  162. $this::assertSame(QRMatrix::M_FINDER | QRMatrix::IS_DARK, $matrix->get(0, $matrix->size() - 1));
  163. $this::assertSame(QRMatrix::M_FINDER | QRMatrix::IS_DARK, $matrix->get($matrix->size() - 1, 0));
  164. $this->dm($matrix);
  165. }
  166. /**
  167. * Tests the separator patterns and verifies their positions
  168. *
  169. * @dataProvider matrixProvider
  170. */
  171. public function testSetSeparators(QRMatrix $matrix):void{
  172. $matrix->setSeparators();
  173. $this::assertSame(QRMatrix::M_SEPARATOR, $matrix->get(7, 0));
  174. $this::assertSame(QRMatrix::M_SEPARATOR, $matrix->get(0, 7));
  175. $this::assertSame(QRMatrix::M_SEPARATOR, $matrix->get(0, $matrix->size() - 8));
  176. $this::assertSame(QRMatrix::M_SEPARATOR, $matrix->get($matrix->size() - 8, 0));
  177. $this->dm($matrix);
  178. }
  179. /**
  180. * Tests the alignment patterns and verifies their positions - version 1 (no pattern) skipped
  181. *
  182. * @dataProvider matrixProvider
  183. */
  184. public function testSetAlignmentPattern(QRMatrix $matrix):void{
  185. $version = $matrix->version();
  186. if($version->getVersionNumber() === 1){
  187. $this::markTestSkipped('N/A (Version 1 has no alignment pattern)');
  188. }
  189. $matrix
  190. ->setFinderPattern()
  191. ->setAlignmentPattern()
  192. ;
  193. $alignmentPattern = $version->getAlignmentPattern();
  194. foreach($alignmentPattern as $py){
  195. foreach($alignmentPattern as $px){
  196. // skip finder pattern
  197. if($matrix->checkTypeNotIn($px, $py, [QRMatrix::M_FINDER, QRMatrix::M_FINDER_DOT])){
  198. $this::assertSame(QRMatrix::M_ALIGNMENT | QRMatrix::IS_DARK, $matrix->get($px, $py));
  199. }
  200. }
  201. }
  202. $this->dm($matrix);
  203. }
  204. /**
  205. * Tests the timing patterns and verifies their positions
  206. *
  207. * @dataProvider matrixProvider
  208. */
  209. public function testSetTimingPattern(QRMatrix $matrix):void{
  210. $matrix
  211. ->setFinderPattern()
  212. ->setAlignmentPattern()
  213. ->setTimingPattern()
  214. ;
  215. $size = $matrix->size();
  216. for($i = 7; $i < $size - 7; $i++){
  217. if($i % 2 === 0){
  218. // skip alignment pattern
  219. if($matrix->checkTypeNotIn(6, $i, [QRMatrix::M_ALIGNMENT])){
  220. $this::assertSame(QRMatrix::M_TIMING | QRMatrix::IS_DARK, $matrix->get(6, $i));
  221. $this::assertSame(QRMatrix::M_TIMING | QRMatrix::IS_DARK, $matrix->get($i, 6));
  222. }
  223. }
  224. }
  225. $this->dm($matrix);
  226. }
  227. /**
  228. * Tests the version patterns and verifies their positions - version < 7 skipped
  229. *
  230. * @dataProvider matrixProvider
  231. */
  232. public function testSetVersionNumber(QRMatrix $matrix):void{
  233. if($matrix->version()->getVersionNumber() < 7){
  234. $this::markTestSkipped('N/A (Version < 7)');
  235. }
  236. $matrix->setVersionNumber();
  237. $this::assertTrue($matrix->checkType($matrix->size() - 9, 0, QRMatrix::M_VERSION));
  238. $this::assertTrue($matrix->checkType($matrix->size() - 11, 5, QRMatrix::M_VERSION));
  239. $this::assertTrue($matrix->checkType(0, $matrix->size() - 9, QRMatrix::M_VERSION));
  240. $this::assertTrue($matrix->checkType(5, $matrix->size() - 11, QRMatrix::M_VERSION));
  241. $this->dm($matrix);
  242. }
  243. /**
  244. * Tests the format patterns and verifies their positions
  245. *
  246. * @dataProvider matrixProvider
  247. */
  248. public function testSetFormatInfo(QRMatrix $matrix):void{
  249. $matrix->setFormatInfo();
  250. $this::assertTrue($matrix->checkType(8, 0, QRMatrix::M_FORMAT));
  251. $this::assertTrue($matrix->checkType(0, 8, QRMatrix::M_FORMAT));
  252. $this::assertTrue($matrix->checkType($matrix->size() - 1, 8, QRMatrix::M_FORMAT));
  253. $this::assertTrue($matrix->checkType($matrix->size() - 8, 8, QRMatrix::M_FORMAT));
  254. $this->dm($matrix);
  255. }
  256. /**
  257. * Tests the quiet zone pattern and verifies its position
  258. *
  259. * @dataProvider matrixProvider
  260. */
  261. public function testSetQuietZone(QRMatrix $matrix):void{
  262. $size = $matrix->size();
  263. $q = 5;
  264. $matrix->set(0, 0, true, QRMatrix::M_TEST);
  265. $matrix->set($size - 1, $size - 1, true, QRMatrix::M_TEST);
  266. $matrix->setQuietZone($q);
  267. $this::assertCount($size + 2 * $q, $matrix->matrix());
  268. $this::assertCount($size + 2 * $q, $matrix->matrix()[$size - 1]);
  269. $size = $matrix->size();
  270. $this::assertTrue($matrix->checkType(0, 0, QRMatrix::M_QUIETZONE));
  271. $this::assertTrue($matrix->checkType($size - 1, $size - 1, QRMatrix::M_QUIETZONE));
  272. $this::assertSame(QRMatrix::M_TEST | QRMatrix::IS_DARK, $matrix->get($q, $q));
  273. $this::assertSame(QRMatrix::M_TEST | QRMatrix::IS_DARK, $matrix->get($size - 1 - $q, $size - 1 - $q));
  274. $this->dm($matrix);
  275. }
  276. /**
  277. * Tests if an exception is thrown in an attempt to create the quiet zone before data was written
  278. */
  279. public function testSetQuietZoneException():void{
  280. $this->expectException(QRCodeDataException::class);
  281. $this->expectExceptionMessage('use only after writing data');
  282. $this->matrix->setQuietZone(42);
  283. }
  284. /**
  285. * Tests the auto orientation of the logo space
  286. */
  287. public function testSetLogoSpaceOrientation():void{
  288. $o = new QROptions;
  289. $o->version = 10;
  290. $o->eccLevel = EccLevel::H;
  291. $o->addQuietzone = false;
  292. $matrix = (new QRCode($o))->addByteSegment('testdata')->getMatrix();
  293. // also testing size adjustment to uneven numbers
  294. $matrix->setLogoSpace(20, 14);
  295. // NW corner
  296. $this::assertFalse($matrix->checkType(17, 20, QRMatrix::M_LOGO));
  297. $this::assertTrue($matrix->checkType(18, 21, QRMatrix::M_LOGO));
  298. // SE corner
  299. $this::assertTrue($matrix->checkType(38, 35, QRMatrix::M_LOGO));
  300. $this::assertFalse($matrix->checkType(39, 36, QRMatrix::M_LOGO));
  301. self::debugMatrix($matrix);
  302. }
  303. /**
  304. * Tests the manual positioning of the logo space
  305. */
  306. public function testSetLogoSpacePosition():void{
  307. $o = new QROptions;
  308. $o->version = 10;
  309. $o->eccLevel = EccLevel::H;
  310. $o->addQuietzone = true;
  311. $o->quietzoneSize = 10;
  312. $matrix = (new QRCode($o))->addByteSegment('testdata')->getMatrix();
  313. // logo space should not overwrite quiet zone & function patterns
  314. $matrix->setLogoSpace(21, 21, -10, -10);
  315. $this::assertSame(QRMatrix::M_QUIETZONE, $matrix->get(9, 9));
  316. $this::assertSame(QRMatrix::M_FINDER | QRMatrix::IS_DARK, $matrix->get(10, 10));
  317. $this::assertSame(QRMatrix::M_FINDER | QRMatrix::IS_DARK, $matrix->get(16, 16));
  318. $this::assertSame(QRMatrix::M_SEPARATOR, $matrix->get(17, 17));
  319. $this::assertSame(QRMatrix::M_FORMAT | QRMatrix::IS_DARK, $matrix->get(18, 18));
  320. $this::assertSame(QRMatrix::M_LOGO, $matrix->get(19, 19));
  321. $this::assertSame(QRMatrix::M_LOGO, $matrix->get(20, 20));
  322. $this::assertNotSame(QRMatrix::M_LOGO, $matrix->get(21, 21));
  323. // i just realized that setLogoSpace() could be called multiple times
  324. // on the same instance and i'm not going to do anything about it :P
  325. $matrix->setLogoSpace(21, 21, 45, 45);
  326. $this::assertNotSame(QRMatrix::M_LOGO, $matrix->get(54, 54));
  327. $this::assertSame(QRMatrix::M_LOGO, $matrix->get(55, 55));
  328. $this::assertSame(QRMatrix::M_QUIETZONE, $matrix->get(67, 67));
  329. self::debugMatrix($matrix);
  330. }
  331. /**
  332. * Tests whether an exception is thrown when an ECC level other than "H" is set when attempting to add logo space
  333. */
  334. public function testSetLogoSpaceInvalidEccException():void{
  335. $this->expectException(QRCodeDataException::class);
  336. $this->expectExceptionMessage('ECC level "H" required to add logo space');
  337. (new QRCode)->addByteSegment('testdata')->getMatrix()->setLogoSpace(50, 50);
  338. }
  339. /**
  340. * Tests whether an exception is thrown when the logo space size exceeds the maximum ECC capacity
  341. */
  342. public function testSetLogoSpaceMaxSizeException():void{
  343. $this->expectException(QRCodeDataException::class);
  344. $this->expectExceptionMessage('logo space exceeds the maximum error correction capacity');
  345. $o = new QROptions;
  346. $o->version = 5;
  347. $o->eccLevel = EccLevel::H;
  348. (new QRCode($o))->addByteSegment('testdata')->getMatrix()->setLogoSpace(37, 37);
  349. }
  350. /**
  351. * Tests flipping the value of a module
  352. */
  353. public function testFlip():void{
  354. // using the dark module here because i'm lazy
  355. $this->matrix->setDarkModule();
  356. $x = 8;
  357. $y = $this->matrix->size() - 8;
  358. // cover checkType()
  359. $this::assertTrue($this->matrix->checkType($x, $y, QRMatrix::M_DARKMODULE));
  360. // verify the current state (dark)
  361. $this::assertSame(QRMatrix::M_DARKMODULE | QRMatrix::IS_DARK, $this->matrix->get($x, $y));
  362. // flip
  363. $this->matrix->flip($x, $y);
  364. // verify flip
  365. $this::assertSame(QRMatrix::M_DARKMODULE, $this->matrix->get($x, $y));
  366. // flip again
  367. $this->matrix->flip($x, $y);
  368. // verify flip
  369. $this::assertSame(QRMatrix::M_DARKMODULE | QRMatrix::IS_DARK, $this->matrix->get($x, $y));
  370. }
  371. }