QRMatrixTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  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 Generator;
  17. use function defined;
  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. /** @noinspection PhpUndefinedConstantInspection - see phpunit.xml.dist */
  39. if(defined('TEST_IS_CI') && TEST_IS_CI === true){
  40. return;
  41. }
  42. $opt = new QROptions;
  43. $opt->outputType = QROutputInterface::STRING_TEXT;
  44. $opt->eol = "\n";
  45. $opt->moduleValues = [
  46. // this is not ideal but it seems it's not possible anymore to colorize emoji via ansi codes
  47. // 🔴 🟠 🟡 🟢 🔵 🟣 ⚫️ ⚪️ 🟤
  48. // 🟥 🟧 🟨 🟩 🟦 🟪 ⬛ ⬜ 🟫
  49. // finder
  50. (QRMatrix::M_FINDER | QRMatrix::IS_DARK) => '🟥', // dark (true)
  51. QRMatrix::M_FINDER => '🔴', // light (false)
  52. (QRMatrix::M_FINDER_DOT | QRMatrix::IS_DARK) => '🟥', // finder dot, dark (true)
  53. // alignment
  54. (QRMatrix::M_ALIGNMENT | QRMatrix::IS_DARK) => '🟧',
  55. QRMatrix::M_ALIGNMENT => '🟠',
  56. // timing
  57. (QRMatrix::M_TIMING | QRMatrix::IS_DARK) => '🟨',
  58. QRMatrix::M_TIMING => '🟡',
  59. // format
  60. (QRMatrix::M_FORMAT | QRMatrix::IS_DARK) => '🟪',
  61. QRMatrix::M_FORMAT => '🟣',
  62. // version
  63. (QRMatrix::M_VERSION | QRMatrix::IS_DARK) => '🟩',
  64. QRMatrix::M_VERSION => '🟢',
  65. // data
  66. (QRMatrix::M_DATA | QRMatrix::IS_DARK) => '🟦',
  67. QRMatrix::M_DATA => '🔵',
  68. // darkmodule
  69. (QRMatrix::M_DARKMODULE | QRMatrix::IS_DARK) => '🟫',
  70. // separator
  71. QRMatrix::M_SEPARATOR => '⚪️',
  72. // quietzone
  73. QRMatrix::M_QUIETZONE => '⬜',
  74. // logo space
  75. QRMatrix::M_LOGO => '⬜',
  76. // empty
  77. QRMatrix::M_NULL => '🟤',
  78. // data
  79. (QRMatrix::M_TEST | QRMatrix::IS_DARK) => '⬛',
  80. QRMatrix::M_TEST => '⚫️',
  81. ];
  82. $out = (new QRString($opt, $matrix))->dump();
  83. echo "\n\n".$out."\n\n";
  84. }
  85. /**
  86. * debugging shortcut qirth limit to a single version when using with matrixProvider
  87. *
  88. * @see QRMatrixTest::matrixProvider()
  89. */
  90. protected function dm(QRMatrix $matrix):void{
  91. // limit
  92. if($matrix->version()->getVersionNumber() !== 7){
  93. return;
  94. }
  95. self::debugMatrix($matrix);
  96. }
  97. /**
  98. * Validates the QRMatrix instance
  99. */
  100. public function testInstance():void{
  101. $this::assertInstanceOf(QRMatrix::class, $this->matrix);
  102. }
  103. /**
  104. * Tests if size() returns the actual matrix size/count
  105. */
  106. public function testSize():void{
  107. $this::assertCount($this->matrix->size(), $this->matrix->matrix());
  108. }
  109. /**
  110. * Tests if version() returns the current (given) version
  111. */
  112. public function testVersion():void{
  113. $this::assertSame($this::version, $this->matrix->version()->getVersionNumber());
  114. }
  115. /**
  116. * Tests if eccLevel() returns the current (given) ECC level
  117. */
  118. public function testECC():void{
  119. $this::assertSame(EccLevel::L, $this->matrix->eccLevel()->getLevel());
  120. }
  121. /**
  122. * Tests if maskPattern() returns the current (or default) mask pattern
  123. */
  124. public function testMaskPattern():void{
  125. // set via matrix evaluation
  126. $matrix = (new QRCode)->addByteSegment('testdata')->getMatrix();
  127. $this::assertInstanceOf(MaskPattern::class, $matrix->maskPattern());
  128. $this::assertSame(MaskPattern::PATTERN_100, $matrix->maskPattern()->getPattern());
  129. }
  130. /**
  131. * Tests the set(), get() and check() methods
  132. */
  133. public function testGetSetCheck():void{
  134. $this->matrix->set(10, 10, true, QRMatrix::M_TEST);
  135. $this::assertSame((QRMatrix::M_TEST | QRMatrix::IS_DARK), $this->matrix->get(10, 10));
  136. $this::assertTrue($this->matrix->check(10, 10));
  137. $this->matrix->set(20, 20, false, QRMatrix::M_TEST);
  138. $this::assertSame(QRMatrix::M_TEST, $this->matrix->get(20, 20));
  139. $this::assertFalse($this->matrix->check(20, 20));
  140. // out of range
  141. $this::assertFalse($this->matrix->check(-1, -1));
  142. $this::assertSame(-1, $this->matrix->get(-1, -1));
  143. }
  144. /**
  145. * Version data provider for several pattern tests
  146. */
  147. public static function matrixProvider():Generator{
  148. $ecc = new EccLevel(EccLevel::L);
  149. $mask = new MaskPattern(MaskPattern::PATTERN_000);
  150. foreach(range(1, 40) as $i){
  151. yield 'version: '.$i => [new QRMatrix(new Version($i), $ecc, $mask)];
  152. }
  153. }
  154. /**
  155. * Tests setting the dark module and verifies its position
  156. *
  157. * @dataProvider matrixProvider
  158. */
  159. public function testSetDarkModule(QRMatrix $matrix):void{
  160. $matrix->setDarkModule();
  161. $this->dm($matrix);
  162. $this::assertSame((QRMatrix::M_DARKMODULE | QRMatrix::IS_DARK), $matrix->get(8, ($matrix->size() - 8)));
  163. }
  164. /**
  165. * Tests setting the finder patterns and verifies their positions
  166. *
  167. * @dataProvider matrixProvider
  168. */
  169. public function testSetFinderPattern(QRMatrix $matrix):void{
  170. $matrix->setFinderPattern();
  171. $this->dm($matrix);
  172. $this::assertSame((QRMatrix::M_FINDER | QRMatrix::IS_DARK), $matrix->get(0, 0));
  173. $this::assertSame((QRMatrix::M_FINDER | QRMatrix::IS_DARK), $matrix->get(0, ($matrix->size() - 1)));
  174. $this::assertSame((QRMatrix::M_FINDER | QRMatrix::IS_DARK), $matrix->get(($matrix->size() - 1), 0));
  175. }
  176. /**
  177. * Tests the separator patterns and verifies their positions
  178. *
  179. * @dataProvider matrixProvider
  180. */
  181. public function testSetSeparators(QRMatrix $matrix):void{
  182. $matrix->setSeparators();
  183. $this->dm($matrix);
  184. $this::assertSame(QRMatrix::M_SEPARATOR, $matrix->get(7, 0));
  185. $this::assertSame(QRMatrix::M_SEPARATOR, $matrix->get(0, 7));
  186. $this::assertSame(QRMatrix::M_SEPARATOR, $matrix->get(0, ($matrix->size() - 8)));
  187. $this::assertSame(QRMatrix::M_SEPARATOR, $matrix->get(($matrix->size() - 8), 0));
  188. }
  189. /**
  190. * Tests the alignment patterns and verifies their positions - version 1 (no pattern) skipped
  191. *
  192. * @dataProvider matrixProvider
  193. */
  194. public function testSetAlignmentPattern(QRMatrix $matrix):void{
  195. $version = $matrix->version();
  196. if($version->getVersionNumber() === 1){
  197. $this::markTestSkipped('N/A (Version 1 has no alignment pattern)');
  198. }
  199. $matrix
  200. ->setFinderPattern()
  201. ->setAlignmentPattern()
  202. ;
  203. $this->dm($matrix);
  204. $alignmentPattern = $version->getAlignmentPattern();
  205. foreach($alignmentPattern as $py){
  206. foreach($alignmentPattern as $px){
  207. // skip finder pattern
  208. if(!$matrix->checkTypeIn($px, $py, [QRMatrix::M_FINDER, QRMatrix::M_FINDER_DOT])){
  209. $this::assertSame((QRMatrix::M_ALIGNMENT | QRMatrix::IS_DARK), $matrix->get($px, $py));
  210. }
  211. }
  212. }
  213. }
  214. /**
  215. * Tests the timing patterns and verifies their positions
  216. *
  217. * @dataProvider matrixProvider
  218. */
  219. public function testSetTimingPattern(QRMatrix $matrix):void{
  220. $matrix
  221. ->setFinderPattern()
  222. ->setAlignmentPattern()
  223. ->setTimingPattern()
  224. ;
  225. $this->dm($matrix);
  226. $size = $matrix->size();
  227. for($i = 7; $i < ($size - 7); $i++){
  228. if(($i % 2) === 0){
  229. // skip alignment pattern
  230. if(!$matrix->checkTypeIn(6, $i, [QRMatrix::M_ALIGNMENT])){
  231. $this::assertSame((QRMatrix::M_TIMING | QRMatrix::IS_DARK), $matrix->get(6, $i));
  232. $this::assertSame((QRMatrix::M_TIMING | QRMatrix::IS_DARK), $matrix->get($i, 6));
  233. }
  234. }
  235. }
  236. }
  237. /**
  238. * Tests the version patterns and verifies their positions - version < 7 skipped
  239. *
  240. * @dataProvider matrixProvider
  241. */
  242. public function testSetVersionNumber(QRMatrix $matrix):void{
  243. if($matrix->version()->getVersionNumber() < 7){
  244. $this::markTestSkipped('N/A (Version < 7)');
  245. }
  246. $matrix->setVersionNumber();
  247. $this->dm($matrix);
  248. $this::assertTrue($matrix->checkType(($matrix->size() - 9), 0, QRMatrix::M_VERSION));
  249. $this::assertTrue($matrix->checkType(($matrix->size() - 11), 5, QRMatrix::M_VERSION));
  250. $this::assertTrue($matrix->checkType(0, ($matrix->size() - 9), QRMatrix::M_VERSION));
  251. $this::assertTrue($matrix->checkType(5, ($matrix->size() - 11), QRMatrix::M_VERSION));
  252. }
  253. /**
  254. * Tests the format patterns and verifies their positions
  255. *
  256. * @dataProvider matrixProvider
  257. */
  258. public function testSetFormatInfo(QRMatrix $matrix):void{
  259. $matrix->setFormatInfo();
  260. $this->dm($matrix);
  261. $this::assertTrue($matrix->checkType(8, 0, QRMatrix::M_FORMAT));
  262. $this::assertTrue($matrix->checkType(0, 8, QRMatrix::M_FORMAT));
  263. $this::assertTrue($matrix->checkType(($matrix->size() - 1), 8, QRMatrix::M_FORMAT));
  264. $this::assertTrue($matrix->checkType(($matrix->size() - 8), 8, QRMatrix::M_FORMAT));
  265. }
  266. /**
  267. * Tests the quiet zone pattern and verifies its position
  268. *
  269. * @dataProvider matrixProvider
  270. */
  271. public function testSetQuietZone(QRMatrix $matrix):void{
  272. $size = $matrix->size();
  273. $quietZoneSize = 5;
  274. $matrix->set(0, 0, true, QRMatrix::M_TEST);
  275. $matrix->set(($size - 1), ($size - 1), true, QRMatrix::M_TEST);
  276. $matrix->setQuietZone($quietZoneSize);
  277. $s = ($size + 2 * $quietZoneSize);
  278. $this::assertCount($s, $matrix->matrix());
  279. $this::assertCount($s, $matrix->matrix()[($size - 1)]);
  280. $size = $matrix->size();
  281. $this->dm($matrix);
  282. $this::assertTrue($matrix->checkType(0, 0, QRMatrix::M_QUIETZONE));
  283. $this::assertTrue($matrix->checkType(($size - 1), ($size - 1), QRMatrix::M_QUIETZONE));
  284. $s = ($size - 1 - $quietZoneSize);
  285. $this::assertSame((QRMatrix::M_TEST | QRMatrix::IS_DARK), $matrix->get($quietZoneSize, $quietZoneSize));
  286. $this::assertSame((QRMatrix::M_TEST | QRMatrix::IS_DARK), $matrix->get($s, $s));
  287. }
  288. /**
  289. * Tests if an exception is thrown in an attempt to create the quiet zone before data was written
  290. */
  291. public function testSetQuietZoneException():void{
  292. $this->expectException(QRCodeDataException::class);
  293. $this->expectExceptionMessage('use only after writing data');
  294. $this->matrix->setQuietZone(42);
  295. }
  296. /**
  297. * Tests if the logo space is drawn square if one of the dimensions is omitted
  298. */
  299. public function testSetLogoSpaceOmitHeight():void{
  300. $o = new QROptions;
  301. $o->version = 2;
  302. $o->eccLevel = EccLevel::H;
  303. $o->addQuietzone = false;
  304. $o->addLogoSpace = true;
  305. $o->logoSpaceHeight = 5;
  306. $matrix = (new QRCode($o))->addByteSegment('testdata')->getMatrix();
  307. self::debugMatrix($matrix);
  308. $this::assertFalse($matrix->checkType(9, 9, QRMatrix::M_LOGO));
  309. $this::assertTrue($matrix->checkType(10, 10, QRMatrix::M_LOGO));
  310. $this::assertTrue($matrix->checkType(14, 14, QRMatrix::M_LOGO));
  311. $this::assertFalse($matrix->checkType(15, 15, QRMatrix::M_LOGO));
  312. }
  313. /**
  314. * Tests the auto orientation of the logo space
  315. */
  316. public function testSetLogoSpaceOrientation():void{
  317. $o = new QROptions;
  318. $o->version = 10;
  319. $o->eccLevel = EccLevel::H;
  320. $o->addQuietzone = false;
  321. $matrix = (new QRCode($o))->addByteSegment('testdata')->getMatrix();
  322. // also testing size adjustment to uneven numbers
  323. $matrix->setLogoSpace(20, 14);
  324. self::debugMatrix($matrix);
  325. // NW corner
  326. $this::assertFalse($matrix->checkType(17, 20, QRMatrix::M_LOGO));
  327. $this::assertTrue($matrix->checkType(18, 21, QRMatrix::M_LOGO));
  328. // SE corner
  329. $this::assertTrue($matrix->checkType(38, 35, QRMatrix::M_LOGO));
  330. $this::assertFalse($matrix->checkType(39, 36, QRMatrix::M_LOGO));
  331. }
  332. /**
  333. * Tests the manual positioning of the logo space
  334. */
  335. public function testSetLogoSpacePosition():void{
  336. $o = new QROptions;
  337. $o->version = 10;
  338. $o->eccLevel = EccLevel::H;
  339. $o->addQuietzone = true;
  340. $o->quietzoneSize = 10;
  341. $matrix = (new QRCode($o))->addByteSegment('testdata')->getMatrix();
  342. self::debugMatrix($matrix);
  343. // logo space should not overwrite quiet zone & function patterns
  344. $matrix->setLogoSpace(21, 21, -10, -10);
  345. $this::assertSame(QRMatrix::M_QUIETZONE, $matrix->get(9, 9));
  346. $this::assertSame((QRMatrix::M_FINDER | QRMatrix::IS_DARK), $matrix->get(10, 10));
  347. $this::assertSame((QRMatrix::M_FINDER | QRMatrix::IS_DARK), $matrix->get(16, 16));
  348. $this::assertSame(QRMatrix::M_SEPARATOR, $matrix->get(17, 17));
  349. $this::assertSame((QRMatrix::M_FORMAT | QRMatrix::IS_DARK), $matrix->get(18, 18));
  350. $this::assertSame(QRMatrix::M_LOGO, $matrix->get(19, 19));
  351. $this::assertSame(QRMatrix::M_LOGO, $matrix->get(20, 20));
  352. $this::assertNotSame(QRMatrix::M_LOGO, $matrix->get(21, 21));
  353. // i just realized that setLogoSpace() could be called multiple times
  354. // on the same instance and i'm not going to do anything about it :P
  355. $matrix->setLogoSpace(21, 21, 45, 45);
  356. $this::assertNotSame(QRMatrix::M_LOGO, $matrix->get(54, 54));
  357. $this::assertSame(QRMatrix::M_LOGO, $matrix->get(55, 55));
  358. $this::assertSame(QRMatrix::M_QUIETZONE, $matrix->get(67, 67));
  359. }
  360. /**
  361. * Tests whether an exception is thrown when an ECC level other than "H" is set when attempting to add logo space
  362. */
  363. public function testSetLogoSpaceInvalidEccException():void{
  364. $this->expectException(QRCodeDataException::class);
  365. $this->expectExceptionMessage('ECC level "H" required to add logo space');
  366. (new QRCode)->addByteSegment('testdata')->getMatrix()->setLogoSpace(50, 50);
  367. }
  368. /**
  369. * Tests whether an exception is thrown when width or height exceed the matrix size
  370. */
  371. public function testSetLogoSpaceExceedsException():void{
  372. $this->expectException(QRCodeDataException::class);
  373. $this->expectExceptionMessage('logo dimensions exceed matrix size');
  374. $o = new QROptions;
  375. $o->version = 5;
  376. $o->eccLevel = EccLevel::H;
  377. (new QRCode($o))->addByteSegment('testdata')->getMatrix()->setLogoSpace(69, 1);
  378. }
  379. /**
  380. * Tests whether an exception is thrown when the logo space size exceeds the maximum ECC capacity
  381. */
  382. public function testSetLogoSpaceMaxSizeException():void{
  383. $this->expectException(QRCodeDataException::class);
  384. $this->expectExceptionMessage('logo space exceeds the maximum error correction capacity');
  385. $o = new QROptions;
  386. $o->version = 5;
  387. $o->eccLevel = EccLevel::H;
  388. (new QRCode($o))->addByteSegment('testdata')->getMatrix()->setLogoSpace(37, 37);
  389. }
  390. /**
  391. * Tests flipping the value of a module
  392. */
  393. public function testFlip():void{
  394. // using the dark module here because i'm lazy
  395. $this->matrix->setDarkModule();
  396. $x = 8;
  397. $y = ($this->matrix->size() - 8);
  398. // cover checkType()
  399. $this::assertTrue($this->matrix->checkType($x, $y, QRMatrix::M_DARKMODULE));
  400. // verify the current state (dark)
  401. $this::assertSame((QRMatrix::M_DARKMODULE | QRMatrix::IS_DARK), $this->matrix->get($x, $y));
  402. // flip
  403. $this->matrix->flip($x, $y);
  404. // verify flip
  405. $this::assertSame(QRMatrix::M_DARKMODULE, $this->matrix->get($x, $y));
  406. // flip again
  407. $this->matrix->flip($x, $y);
  408. // verify flip
  409. $this::assertSame((QRMatrix::M_DARKMODULE | QRMatrix::IS_DARK), $this->matrix->get($x, $y));
  410. }
  411. /**
  412. * Tests checking whether the M_TYPE of a module is not one of an array of M_TYPES
  413. */
  414. public function testCheckTypeIn():void{
  415. $this->matrix->set(10, 10, true, QRMatrix::M_QUIETZONE);
  416. $this::assertFalse($this->matrix->checkTypeIn(10, 10, [QRMatrix::M_DATA, QRMatrix::M_FINDER]));
  417. $this::assertTrue($this->matrix->checkTypeIn(10, 10, [QRMatrix::M_QUIETZONE, QRMatrix::M_FINDER]));
  418. }
  419. /**
  420. * Tests checking the adjacent modules
  421. */
  422. public function testCheckNeighbours():void{
  423. $this->matrix
  424. ->setFinderPattern()
  425. ->setAlignmentPattern()
  426. ;
  427. /*
  428. * center of finder pattern (surrounded by all dark)
  429. *
  430. * # # # # # # #
  431. * # #
  432. * # # # # #
  433. * # # 0 # #
  434. * # # # # #
  435. * # #
  436. * # # # # # # #
  437. */
  438. $this::assertSame(0b11111111, $this->matrix->checkNeighbours(3, 3));
  439. /*
  440. * center of alignment pattern (surrounded by all light)
  441. *
  442. * # # # # #
  443. * # #
  444. * # 0 #
  445. * # #
  446. * # # # # #
  447. */
  448. $this::assertSame(0b00000000, $this->matrix->checkNeighbours(30, 30));
  449. /*
  450. * top left light block of finder pattern
  451. *
  452. * # # #
  453. * # 0
  454. * # #
  455. */
  456. $this::assertSame(0b11010111, $this->matrix->checkNeighbours(1, 1));
  457. /*
  458. * bottom left light block of finder pattern
  459. *
  460. * # #
  461. * # 0
  462. * # # #
  463. */
  464. $this::assertSame(0b11110101, $this->matrix->checkNeighbours(1, 5));
  465. /*
  466. * top right light block of finder pattern
  467. *
  468. * # # #
  469. * 0 #
  470. * # #
  471. */
  472. $this::assertSame(0b01011111, $this->matrix->checkNeighbours(5, 1));
  473. /*
  474. * bottom right light block of finder pattern
  475. *
  476. * # #
  477. * 0 #
  478. * # # #
  479. */
  480. $this::assertSame(0b01111101, $this->matrix->checkNeighbours(5, 5));
  481. /*
  482. * M_TYPE check
  483. *
  484. * # # #
  485. * 0
  486. * X X X
  487. */
  488. $this::assertSame(0b00000111, $this->matrix->checkNeighbours(3, 1, QRMatrix::M_FINDER));
  489. $this::assertSame(0b01110000, $this->matrix->checkNeighbours(3, 1, QRMatrix::M_FINDER_DOT));
  490. }
  491. }