QRMatrixTest.php 16 KB

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