QRMatrixTest.php 15 KB

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