QRMatrixTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  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. declare(strict_types=1);
  11. namespace chillerlan\QRCodeTest\Data;
  12. use chillerlan\QRCode\{QRCode, QROptions};
  13. use chillerlan\QRCode\Common\{EccLevel, MaskPattern, Version};
  14. use chillerlan\QRCode\Data\{QRCodeDataException, QRMatrix};
  15. use chillerlan\QRCodeTest\Traits\QRMatrixDebugTrait;
  16. use PHPUnit\Framework\TestCase;
  17. use PHPUnit\Framework\Attributes\DataProvider;
  18. use Generator;
  19. /**
  20. * Tests the QRMatrix class
  21. */
  22. final class QRMatrixTest extends TestCase{
  23. use QRMatrixDebugTrait;
  24. private const version = 40;
  25. private QRMatrix $matrix;
  26. /**
  27. * invokes a QRMatrix object
  28. */
  29. protected function setUp():void{
  30. $this->matrix = new QRMatrix(
  31. new Version($this::version),
  32. new EccLevel(EccLevel::L),
  33. );
  34. }
  35. /**
  36. * Tests if size() returns the actual matrix size/count
  37. */
  38. public function testGetSize():void{
  39. $this::assertCount($this->matrix->getSize(), $this->matrix->getBooleanMatrix());
  40. }
  41. /**
  42. * Tests if version() returns the current (given) version
  43. */
  44. public function testGetVersion():void{
  45. $this::assertSame($this::version, $this->matrix->getVersion()->getVersionNumber());
  46. }
  47. /**
  48. * Tests if eccLevel() returns the current (given) ECC level
  49. */
  50. public function testGetECC():void{
  51. $this::assertSame(EccLevel::L, $this->matrix->getEccLevel()->getLevel());
  52. }
  53. /**
  54. * Tests if maskPattern() returns the current (or default) mask pattern
  55. */
  56. public function testGetMaskPattern():void{
  57. // set via matrix evaluation
  58. $matrix = (new QRCode)->addByteSegment('testdata')->getQRMatrix();
  59. $this::assertInstanceOf(MaskPattern::class, $matrix->getMaskPattern());
  60. $this::assertSame(MaskPattern::PATTERN_100, $matrix->getMaskPattern()->getPattern());
  61. }
  62. /**
  63. * Tests the set(), get() and check() methods
  64. */
  65. public function testGetSetCheck():void{
  66. $this->matrix->set(10, 10, true, QRMatrix::M_LOGO);
  67. $this::assertSame(QRMatrix::M_LOGO_DARK, $this->matrix->get(10, 10));
  68. $this::assertTrue($this->matrix->check(10, 10));
  69. $this->matrix->set(20, 20, false, QRMatrix::M_LOGO);
  70. $this::assertSame(QRMatrix::M_LOGO, $this->matrix->get(20, 20));
  71. $this::assertFalse($this->matrix->check(20, 20));
  72. // get proper results when using a *_DARK constant
  73. $this->matrix->set(30, 30, true, QRMatrix::M_LOGO_DARK);
  74. $this::assertSame(QRMatrix::M_LOGO_DARK, $this->matrix->get(30, 30));
  75. $this->matrix->set(40, 40, false, QRMatrix::M_LOGO_DARK);
  76. $this::assertSame(QRMatrix::M_LOGO, $this->matrix->get(40, 40));
  77. // out of range
  78. $this::assertFalse($this->matrix->check(-1, -1));
  79. $this::assertSame(-1, $this->matrix->get(-1, -1));
  80. }
  81. /**
  82. * Version data provider for several pattern tests
  83. */
  84. public static function matrixProvider():Generator{
  85. $ecc = new EccLevel(EccLevel::L);
  86. for($i = 1; $i <= 40; $i++){
  87. yield 'version: '.$i => [new QRMatrix(new Version($i), $ecc)];
  88. }
  89. }
  90. /**
  91. * Tests setting the dark module and verifies its position
  92. */
  93. #[DataProvider('matrixProvider')]
  94. public function testSetDarkModule(QRMatrix $matrix):void{
  95. $matrix->setDarkModule();
  96. $this->dm($matrix);
  97. $this::assertSame(QRMatrix::M_DARKMODULE, $matrix->get(8, ($matrix->getSize() - 8)));
  98. }
  99. /**
  100. * Tests setting the finder patterns and verifies their positions
  101. */
  102. #[DataProvider('matrixProvider')]
  103. public function testSetFinderPattern(QRMatrix $matrix):void{
  104. $matrix->setFinderPattern();
  105. $this->dm($matrix);
  106. $this::assertSame(QRMatrix::M_FINDER_DARK, $matrix->get(0, 0));
  107. $this::assertSame(QRMatrix::M_FINDER_DARK, $matrix->get(0, ($matrix->getSize() - 1)));
  108. $this::assertSame(QRMatrix::M_FINDER_DARK, $matrix->get(($matrix->getSize() - 1), 0));
  109. }
  110. /**
  111. * Tests the separator patterns and verifies their positions
  112. */
  113. #[DataProvider('matrixProvider')]
  114. public function testSetSeparators(QRMatrix $matrix):void{
  115. $matrix->setSeparators();
  116. $this->dm($matrix);
  117. $this::assertSame(QRMatrix::M_SEPARATOR, $matrix->get(7, 0));
  118. $this::assertSame(QRMatrix::M_SEPARATOR, $matrix->get(0, 7));
  119. $this::assertSame(QRMatrix::M_SEPARATOR, $matrix->get(0, ($matrix->getSize() - 8)));
  120. $this::assertSame(QRMatrix::M_SEPARATOR, $matrix->get(($matrix->getSize() - 8), 0));
  121. }
  122. /**
  123. * Tests the alignment patterns and verifies their positions - version 1 (no pattern) skipped
  124. */
  125. #[DataProvider('matrixProvider')]
  126. public function testSetAlignmentPattern(QRMatrix $matrix):void{
  127. $version = $matrix->getVersion();
  128. if($version->getVersionNumber() === 1){
  129. $this::markTestSkipped('N/A (Version 1 has no alignment pattern)');
  130. }
  131. $matrix
  132. ->setFinderPattern()
  133. ->setAlignmentPattern()
  134. ;
  135. $this->dm($matrix);
  136. $alignmentPattern = $version->getAlignmentPattern();
  137. foreach($alignmentPattern as $py){
  138. foreach($alignmentPattern as $px){
  139. // skip finder pattern
  140. if(!$matrix->checkTypeIn($px, $py, [QRMatrix::M_FINDER, QRMatrix::M_FINDER_DOT])){
  141. $this::assertSame(QRMatrix::M_ALIGNMENT_DARK, $matrix->get($px, $py));
  142. }
  143. }
  144. }
  145. }
  146. /**
  147. * Tests the timing patterns and verifies their positions
  148. */
  149. #[DataProvider('matrixProvider')]
  150. public function testSetTimingPattern(QRMatrix $matrix):void{
  151. $matrix
  152. ->setFinderPattern()
  153. ->setAlignmentPattern()
  154. ->setTimingPattern()
  155. ;
  156. $this->dm($matrix);
  157. $size = $matrix->getSize();
  158. for($i = 7; $i < ($size - 7); $i++){
  159. if(($i % 2) === 0){
  160. // skip alignment pattern
  161. if(!$matrix->checkTypeIn(6, $i, [QRMatrix::M_ALIGNMENT])){
  162. $this::assertSame(QRMatrix::M_TIMING_DARK, $matrix->get(6, $i));
  163. $this::assertSame(QRMatrix::M_TIMING_DARK, $matrix->get($i, 6));
  164. }
  165. }
  166. }
  167. }
  168. /**
  169. * Tests the version patterns and verifies their positions - version < 7 skipped
  170. */
  171. #[DataProvider('matrixProvider')]
  172. public function testSetVersionNumber(QRMatrix $matrix):void{
  173. if($matrix->getVersion()->getVersionNumber() < 7){
  174. $this::markTestSkipped('N/A (Version < 7)');
  175. }
  176. $matrix->setVersionNumber();
  177. $this->dm($matrix);
  178. $this::assertTrue($matrix->checkType(($matrix->getSize() - 9), 0, QRMatrix::M_VERSION));
  179. $this::assertTrue($matrix->checkType(($matrix->getSize() - 11), 5, QRMatrix::M_VERSION));
  180. $this::assertTrue($matrix->checkType(0, ($matrix->getSize() - 9), QRMatrix::M_VERSION));
  181. $this::assertTrue($matrix->checkType(5, ($matrix->getSize() - 11), QRMatrix::M_VERSION));
  182. }
  183. /**
  184. * Tests the format patterns and verifies their positions
  185. */
  186. #[DataProvider('matrixProvider')]
  187. public function testSetFormatInfo(QRMatrix $matrix):void{
  188. $matrix->setFormatInfo(new MaskPattern(MaskPattern::PATTERN_000));
  189. $this->dm($matrix);
  190. $this::assertTrue($matrix->checkType(8, 0, QRMatrix::M_FORMAT));
  191. $this::assertTrue($matrix->checkType(0, 8, QRMatrix::M_FORMAT));
  192. $this::assertTrue($matrix->checkType(($matrix->getSize() - 1), 8, QRMatrix::M_FORMAT));
  193. $this::assertTrue($matrix->checkType(($matrix->getSize() - 8), 8, QRMatrix::M_FORMAT));
  194. }
  195. /**
  196. * Tests the quiet zone pattern and verifies its position
  197. */
  198. #[DataProvider('matrixProvider')]
  199. public function testSetQuietZone(QRMatrix $matrix):void{
  200. $size = $matrix->getSize();
  201. $quietZoneSize = 5;
  202. $matrix->set(0, 0, true, QRMatrix::M_LOGO);
  203. $matrix->set(($size - 1), ($size - 1), true, QRMatrix::M_LOGO);
  204. $matrix->setQuietZone($quietZoneSize);
  205. $s = ($size + 2 * $quietZoneSize);
  206. $this::assertCount($s, $matrix->getBooleanMatrix());
  207. $this::assertCount($s, $matrix->getBooleanMatrix()[($size - 1)]);
  208. $size = $matrix->getSize();
  209. $this->dm($matrix);
  210. $this::assertTrue($matrix->checkType(0, 0, QRMatrix::M_QUIETZONE));
  211. $this::assertTrue($matrix->checkType(($size - 1), ($size - 1), QRMatrix::M_QUIETZONE));
  212. $s = ($size - 1 - $quietZoneSize);
  213. $this::assertSame(QRMatrix::M_LOGO_DARK, $matrix->get($quietZoneSize, $quietZoneSize));
  214. $this::assertSame(QRMatrix::M_LOGO_DARK, $matrix->get($s, $s));
  215. }
  216. /**
  217. * Tests if an exception is thrown in an attempt to create the quiet zone before data was written
  218. */
  219. public function testSetQuietZoneException():void{
  220. $this->expectException(QRCodeDataException::class);
  221. $this->expectExceptionMessage('use only after writing data');
  222. $this->matrix->setQuietZone(42);
  223. }
  224. /**
  225. * Tests rotating the matrix by 90 degrees CW
  226. */
  227. #[DataProvider('matrixProvider')]
  228. public function testRotate90(QRMatrix $matrix):void{
  229. $matrix->initFunctionalPatterns();
  230. // matrix size
  231. $size = $matrix->getSize();
  232. // quiet zone size
  233. $qz = (($size - $matrix->getVersion()->getDimension()) / 2);
  234. // initial dark module position
  235. $this::assertSame(QRMatrix::M_DARKMODULE, $matrix->get((8 + $qz), ($size - 8 - $qz)));
  236. // first rotation
  237. $matrix->rotate90();
  238. $this->dm($matrix);
  239. $this::assertSame(QRMatrix::M_DARKMODULE, $matrix->get((7 + $qz), (8 + $qz)));
  240. // second rotation
  241. $matrix->rotate90();
  242. $this->dm($matrix);
  243. $this::assertSame(QRMatrix::M_DARKMODULE, $matrix->get(($size - 9 - $qz), (7 + $qz)));
  244. // third rotation
  245. $matrix->rotate90();
  246. $this->dm($matrix);
  247. $this::assertSame(QRMatrix::M_DARKMODULE, $matrix->get(($size - 8 - $qz), ($size - 9 - $qz)));
  248. // fourth rotation
  249. $matrix->rotate90();
  250. $this->dm($matrix);
  251. $this::assertSame(QRMatrix::M_DARKMODULE, $matrix->get((8 + $qz), ($size - 8 - $qz)));
  252. }
  253. /**
  254. * Tests if the logo space is drawn square if one of the dimensions is omitted
  255. */
  256. public function testSetLogoSpaceOmitDimension():void{
  257. $o = new QROptions;
  258. $o->version = 2;
  259. $o->eccLevel = EccLevel::H;
  260. $o->addQuietzone = false;
  261. $o->addLogoSpace = true;
  262. $o->logoSpaceHeight = 5;
  263. $matrix = (new QRCode($o))->addByteSegment('testdata')->getQRMatrix();
  264. $this->debugMatrix($matrix);
  265. $this::assertFalse($matrix->checkType(9, 9, QRMatrix::M_LOGO));
  266. $this::assertTrue($matrix->checkType(10, 10, QRMatrix::M_LOGO));
  267. $this::assertTrue($matrix->checkType(14, 14, QRMatrix::M_LOGO));
  268. $this::assertFalse($matrix->checkType(15, 15, QRMatrix::M_LOGO));
  269. }
  270. /**
  271. * Tests the auto orientation of the logo space
  272. */
  273. public function testSetLogoSpaceOrientation():void{
  274. $o = new QROptions;
  275. $o->version = 10;
  276. $o->eccLevel = EccLevel::H;
  277. $o->addQuietzone = false;
  278. $matrix = (new QRCode($o))->addByteSegment('testdata')->getQRMatrix();
  279. // also testing size adjustment to uneven numbers
  280. $matrix->setLogoSpace(20, 14);
  281. $this->debugMatrix($matrix);
  282. // NW corner
  283. $this::assertFalse($matrix->checkType(17, 20, QRMatrix::M_LOGO));
  284. $this::assertTrue($matrix->checkType(18, 21, QRMatrix::M_LOGO));
  285. // SE corner
  286. $this::assertTrue($matrix->checkType(38, 35, QRMatrix::M_LOGO));
  287. $this::assertFalse($matrix->checkType(39, 36, QRMatrix::M_LOGO));
  288. }
  289. /**
  290. * Tests the manual positioning of the logo space
  291. */
  292. public function testSetLogoSpacePosition():void{
  293. $o = new QROptions;
  294. $o->version = 10;
  295. $o->eccLevel = EccLevel::H;
  296. $o->addQuietzone = true;
  297. $o->quietzoneSize = 10;
  298. $matrix = (new QRCode($o))->addByteSegment('testdata')->getQRMatrix();
  299. $matrix->setLogoSpace(21, 21, -10, -10);
  300. $this->debugMatrix($matrix);
  301. $this::assertSame(QRMatrix::M_QUIETZONE, $matrix->get(9, 9));
  302. $this::assertSame(QRMatrix::M_LOGO, $matrix->get(10, 10));
  303. $this::assertSame(QRMatrix::M_LOGO, $matrix->get(20, 20));
  304. $this::assertNotSame(QRMatrix::M_LOGO, $matrix->get(21, 21));
  305. // I just realized that setLogoSpace() could be called multiple times
  306. // on the same instance, and I'm not going to do anything about it :P
  307. $matrix->setLogoSpace(21, 21, 45, 45);
  308. $this->debugMatrix($matrix);
  309. $this::assertNotSame(QRMatrix::M_LOGO, $matrix->get(54, 54));
  310. $this::assertSame(QRMatrix::M_LOGO, $matrix->get(55, 55));
  311. $this::assertSame(QRMatrix::M_QUIETZONE, $matrix->get(67, 67));
  312. }
  313. /**
  314. * Tests whether an exception is thrown when an ECC level other than "H" is set when attempting to add logo space
  315. */
  316. public function testSetLogoSpaceInvalidEccException():void{
  317. $this->expectException(QRCodeDataException::class);
  318. $this->expectExceptionMessage('ECC level "H" required to add logo space');
  319. (new QRCode)->addByteSegment('testdata')->getQRMatrix()->setLogoSpace(50, 50);
  320. }
  321. /**
  322. * Tests whether an exception is thrown when width or height exceed the matrix size
  323. */
  324. public function testSetLogoSpaceExceedsException():void{
  325. $this->expectException(QRCodeDataException::class);
  326. $this->expectExceptionMessage('logo dimensions exceed matrix size');
  327. $o = new QROptions;
  328. $o->version = 5;
  329. $o->eccLevel = EccLevel::H;
  330. (new QRCode($o))->addByteSegment('testdata')->getQRMatrix()->setLogoSpace(69, 1);
  331. }
  332. /**
  333. * Tests whether an exception is thrown when the logo space size exceeds the maximum ECC capacity
  334. */
  335. public function testSetLogoSpaceMaxSizeException():void{
  336. $this->expectException(QRCodeDataException::class);
  337. $this->expectExceptionMessage('logo space exceeds the maximum error correction capacity');
  338. $o = new QROptions;
  339. $o->version = 5;
  340. $o->eccLevel = EccLevel::H;
  341. (new QRCode($o))->addByteSegment('testdata')->getQRMatrix()->setLogoSpace(37, 37);
  342. }
  343. /**
  344. * Tests flipping the value of a module
  345. */
  346. public function testFlip():void{
  347. $this->matrix->set(20, 20, true, QRMatrix::M_LOGO);
  348. // cover checkType()
  349. $this::assertTrue($this->matrix->checkType(20, 20, QRMatrix::M_LOGO));
  350. // verify the current state (dark)
  351. $this::assertSame(QRMatrix::M_LOGO_DARK, $this->matrix->get(20, 20));
  352. // flip
  353. $this->matrix->flip(20, 20);
  354. // verify flip
  355. $this::assertSame(QRMatrix::M_LOGO, $this->matrix->get(20, 20));
  356. // flip again
  357. $this->matrix->flip(20, 20);
  358. // verify flip
  359. $this::assertSame(QRMatrix::M_LOGO_DARK, $this->matrix->get(20, 20));
  360. }
  361. /**
  362. * Tests checking whether the M_TYPE of a module is not one of an array of M_TYPES
  363. */
  364. public function testCheckTypeIn():void{
  365. $this->matrix->set(10, 10, true, QRMatrix::M_QUIETZONE);
  366. $this::assertFalse($this->matrix->checkTypeIn(10, 10, [QRMatrix::M_DATA, QRMatrix::M_FINDER]));
  367. $this::assertTrue($this->matrix->checkTypeIn(10, 10, [QRMatrix::M_QUIETZONE, QRMatrix::M_FINDER]));
  368. }
  369. /**
  370. * Tests checking the adjacent modules
  371. */
  372. public function testCheckNeighbours():void{
  373. $this->matrix
  374. ->setFinderPattern()
  375. ->setAlignmentPattern()
  376. ;
  377. /*
  378. * center of finder pattern (surrounded by all dark)
  379. *
  380. * # # # # # # #
  381. * # #
  382. * # # # # #
  383. * # # 0 # #
  384. * # # # # #
  385. * # #
  386. * # # # # # # #
  387. */
  388. $this::assertSame(0b11111111, $this->matrix->checkNeighbours(3, 3));
  389. /*
  390. * center of alignment pattern (surrounded by all light)
  391. *
  392. * # # # # #
  393. * # #
  394. * # 0 #
  395. * # #
  396. * # # # # #
  397. */
  398. $this::assertSame(0b00000000, $this->matrix->checkNeighbours(30, 30));
  399. /*
  400. * top left light block of finder pattern
  401. *
  402. * # # #
  403. * # 0
  404. * # #
  405. */
  406. $this::assertSame(0b11010111, $this->matrix->checkNeighbours(1, 1));
  407. /*
  408. * bottom left light block of finder pattern
  409. *
  410. * # #
  411. * # 0
  412. * # # #
  413. */
  414. $this::assertSame(0b11110101, $this->matrix->checkNeighbours(1, 5));
  415. /*
  416. * top right light block of finder pattern
  417. *
  418. * # # #
  419. * 0 #
  420. * # #
  421. */
  422. $this::assertSame(0b01011111, $this->matrix->checkNeighbours(5, 1));
  423. /*
  424. * bottom right light block of finder pattern
  425. *
  426. * # #
  427. * 0 #
  428. * # # #
  429. */
  430. $this::assertSame(0b01111101, $this->matrix->checkNeighbours(5, 5));
  431. /*
  432. * M_TYPE check
  433. *
  434. * # # #
  435. * 0
  436. * X X X
  437. */
  438. $this::assertSame(0b00000111, $this->matrix->checkNeighbours(3, 1, QRMatrix::M_FINDER));
  439. $this::assertSame(0b01110000, $this->matrix->checkNeighbours(3, 1, QRMatrix::M_FINDER_DOT));
  440. }
  441. }