QRMatrixTest.php 15 KB

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