QRMatrixTest.php 16 KB

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