QRMatrixTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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\QRCode\Output\{QROutputInterface, QRString};
  15. use PHPUnit\Framework\TestCase;
  16. use PHPUnit\Util\Color;
  17. use Generator;
  18. /**
  19. * Tests the QRMatix class
  20. */
  21. final class QRMatrixTest extends TestCase{
  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. new MaskPattern(MaskPattern::PATTERN_000)
  32. );
  33. }
  34. /**
  35. * Matrix debugging console output
  36. */
  37. public static function debugMatrix(QRMatrix $matrix):void{
  38. $opt = new QROptions;
  39. $opt->outputType = QROutputInterface::STRING_TEXT;
  40. $opt->eol = Color::colorize('reset', "\x00\n");
  41. $opt->moduleValues = [
  42. // finder
  43. QRMatrix::M_FINDER | QRMatrix::IS_DARK => Color::colorize('fg-black', '🔴'), // dark (true)
  44. QRMatrix::M_FINDER => Color::colorize('fg-black', '⭕'), // light (false)
  45. QRMatrix::M_FINDER_DOT | QRMatrix::IS_DARK => Color::colorize('fg-black', '🔴'), // finder dot, dark (true)
  46. // alignment
  47. QRMatrix::M_ALIGNMENT | QRMatrix::IS_DARK => Color::colorize('fg-blue', '🔴'),
  48. QRMatrix::M_ALIGNMENT => Color::colorize('fg-blue', '⭕'),
  49. // timing
  50. QRMatrix::M_TIMING | QRMatrix::IS_DARK => Color::colorize('fg-red', '🔴'),
  51. QRMatrix::M_TIMING => Color::colorize('fg-red', '⭕'),
  52. // format
  53. QRMatrix::M_FORMAT | QRMatrix::IS_DARK => Color::colorize('fg-magenta', '🔴'),
  54. QRMatrix::M_FORMAT => Color::colorize('fg-magenta', '⭕'),
  55. // version
  56. QRMatrix::M_VERSION | QRMatrix::IS_DARK => Color::colorize('fg-green', '🔴'),
  57. QRMatrix::M_VERSION => Color::colorize('fg-green', '⭕'),
  58. // data
  59. QRMatrix::M_DATA | QRMatrix::IS_DARK => Color::colorize('fg-white', '🔴'),
  60. QRMatrix::M_DATA => Color::colorize('fg-white', '⭕'),
  61. // darkmodule
  62. QRMatrix::M_DARKMODULE | QRMatrix::IS_DARK => Color::colorize('fg-black', '🔴'),
  63. // separator
  64. QRMatrix::M_SEPARATOR => Color::colorize('fg-cyan', '⭕'),
  65. // quietzone
  66. QRMatrix::M_QUIETZONE => Color::colorize('fg-cyan', '⭕'),
  67. // logo space
  68. QRMatrix::M_LOGO => Color::colorize('fg-yellow', '⭕'),
  69. // empty
  70. QRMatrix::M_NULL => Color::colorize('fg-black', '⭕'),
  71. // data
  72. QRMatrix::M_TEST | QRMatrix::IS_DARK => Color::colorize('fg-white', '🔴'),
  73. QRMatrix::M_TEST => Color::colorize('fg-black', '⭕'),
  74. ];
  75. $out = (new QRString($opt, $matrix))->dump();
  76. echo "\n\n".$out."\n\n";
  77. }
  78. /**
  79. * debugging shortcut qirth limit to a single version when using with matrixProvider
  80. *
  81. * @see QRMatrixTest::matrixProvider()
  82. */
  83. protected function dm(QRMatrix $matrix):void{
  84. // limit
  85. if($matrix->version()->getVersionNumber() !== 7){
  86. return;
  87. }
  88. self::debugMatrix($matrix);
  89. }
  90. /**
  91. * Validates the QRMatrix instance
  92. */
  93. public function testInstance():void{
  94. $this::assertInstanceOf(QRMatrix::class, $this->matrix);
  95. }
  96. /**
  97. * Tests if size() returns the actual matrix size/count
  98. */
  99. public function testSize():void{
  100. $this::assertCount($this->matrix->size(), $this->matrix->matrix());
  101. }
  102. /**
  103. * Tests if version() returns the current (given) version
  104. */
  105. public function testVersion():void{
  106. $this::assertSame($this::version, $this->matrix->version()->getVersionNumber());
  107. }
  108. /**
  109. * Tests if eccLevel() returns the current (given) ECC level
  110. */
  111. public function testECC():void{
  112. $this::assertSame(EccLevel::L, $this->matrix->eccLevel()->getLevel());
  113. }
  114. /**
  115. * Tests if maskPattern() returns the current (or default) mask pattern
  116. */
  117. public function testMaskPattern():void{
  118. // set via matrix evaluation
  119. $matrix = (new QRCode)->addByteSegment('testdata')->getMatrix();
  120. $this::assertInstanceOf(MaskPattern::class, $matrix->maskPattern());
  121. $this::assertSame(MaskPattern::PATTERN_100, $matrix->maskPattern()->getPattern());
  122. }
  123. /**
  124. * Tests the set(), get() and check() methods
  125. */
  126. public function testGetSetCheck():void{
  127. $this->matrix->set(10, 10, true, QRMatrix::M_TEST);
  128. $this::assertSame(QRMatrix::M_TEST | QRMatrix::IS_DARK, $this->matrix->get(10, 10));
  129. $this::assertTrue($this->matrix->check(10, 10));
  130. $this->matrix->set(20, 20, false, QRMatrix::M_TEST);
  131. $this::assertSame(QRMatrix::M_TEST, $this->matrix->get(20, 20));
  132. $this::assertFalse($this->matrix->check(20, 20));
  133. }
  134. /**
  135. * Version data provider for several pattern tests
  136. */
  137. public function matrixProvider():Generator{
  138. $ecc = new EccLevel(EccLevel::L);
  139. $mask = new MaskPattern(MaskPattern::PATTERN_000);
  140. foreach(range(1, 40) as $i){
  141. yield 'version: '.$i => [new QRMatrix(new Version($i), $ecc, $mask)];
  142. }
  143. }
  144. /**
  145. * Tests setting the dark module and verifies its position
  146. *
  147. * @dataProvider matrixProvider
  148. */
  149. public function testSetDarkModule(QRMatrix $matrix):void{
  150. $matrix->setDarkModule();
  151. $this::assertSame(QRMatrix::M_DARKMODULE | QRMatrix::IS_DARK, $matrix->get(8, $matrix->size() - 8));
  152. $this->dm($matrix);
  153. }
  154. /**
  155. * Tests setting the finder patterns and verifies their positions
  156. *
  157. * @dataProvider matrixProvider
  158. */
  159. public function testSetFinderPattern(QRMatrix $matrix):void{
  160. $matrix->setFinderPattern();
  161. $this::assertSame(QRMatrix::M_FINDER | QRMatrix::IS_DARK, $matrix->get(0, 0));
  162. $this::assertSame(QRMatrix::M_FINDER | QRMatrix::IS_DARK, $matrix->get(0, $matrix->size() - 1));
  163. $this::assertSame(QRMatrix::M_FINDER | QRMatrix::IS_DARK, $matrix->get($matrix->size() - 1, 0));
  164. $this->dm($matrix);
  165. }
  166. /**
  167. * Tests the separator patterns and verifies their positions
  168. *
  169. * @dataProvider matrixProvider
  170. */
  171. public function testSetSeparators(QRMatrix $matrix):void{
  172. $matrix->setSeparators();
  173. $this::assertSame(QRMatrix::M_SEPARATOR, $matrix->get(7, 0));
  174. $this::assertSame(QRMatrix::M_SEPARATOR, $matrix->get(0, 7));
  175. $this::assertSame(QRMatrix::M_SEPARATOR, $matrix->get(0, $matrix->size() - 8));
  176. $this::assertSame(QRMatrix::M_SEPARATOR, $matrix->get($matrix->size() - 8, 0));
  177. $this->dm($matrix);
  178. }
  179. /**
  180. * Tests the alignment patterns and verifies their positions - version 1 (no pattern) skipped
  181. *
  182. * @dataProvider matrixProvider
  183. */
  184. public function testSetAlignmentPattern(QRMatrix $matrix):void{
  185. $version = $matrix->version();
  186. if($version->getVersionNumber() === 1){
  187. $this::markTestSkipped('N/A (Version 1 has no alignment pattern)');
  188. }
  189. $matrix
  190. ->setFinderPattern()
  191. ->setAlignmentPattern()
  192. ;
  193. $alignmentPattern = $version->getAlignmentPattern();
  194. foreach($alignmentPattern as $py){
  195. foreach($alignmentPattern as $px){
  196. if($matrix->checkType($px, $py, QRMatrix::M_FINDER)){
  197. // skip finder pattern
  198. continue;
  199. }
  200. $this::assertSame(QRMatrix::M_ALIGNMENT | QRMatrix::IS_DARK, $matrix->get($px, $py));
  201. }
  202. }
  203. $this->dm($matrix);
  204. }
  205. /**
  206. * Tests the timing patterns and verifies their positions
  207. *
  208. * @dataProvider matrixProvider
  209. */
  210. public function testSetTimingPattern(QRMatrix $matrix):void{
  211. $matrix
  212. ->setFinderPattern()
  213. ->setAlignmentPattern()
  214. ->setTimingPattern()
  215. ;
  216. $size = $matrix->size();
  217. for($i = 7; $i < $size - 7; $i++){
  218. if($i % 2 === 0){
  219. if($matrix->checkType(6, $i, QRMatrix::M_ALIGNMENT)){
  220. // skip alignment pattern
  221. continue;
  222. }
  223. $this::assertSame(QRMatrix::M_TIMING | QRMatrix::IS_DARK, $matrix->get(6, $i));
  224. $this::assertSame(QRMatrix::M_TIMING | QRMatrix::IS_DARK, $matrix->get($i, 6));
  225. }
  226. }
  227. $this->dm($matrix);
  228. }
  229. /**
  230. * Tests the version patterns and verifies their positions - version < 7 skipped
  231. *
  232. * @dataProvider matrixProvider
  233. */
  234. public function testSetVersionNumber(QRMatrix $matrix):void{
  235. if($matrix->version()->getVersionNumber() < 7){
  236. $this::markTestSkipped('N/A (Version < 7)');
  237. }
  238. $matrix->setVersionNumber();
  239. $this::assertTrue($matrix->checkType($matrix->size() - 9, 0, QRMatrix::M_VERSION));
  240. $this::assertTrue($matrix->checkType($matrix->size() - 11, 5, QRMatrix::M_VERSION));
  241. $this::assertTrue($matrix->checkType(0, $matrix->size() - 9, QRMatrix::M_VERSION));
  242. $this::assertTrue($matrix->checkType(5, $matrix->size() - 11, QRMatrix::M_VERSION));
  243. $this->dm($matrix);
  244. }
  245. /**
  246. * Tests the format patterns and verifies their positions
  247. *
  248. * @dataProvider matrixProvider
  249. */
  250. public function testSetFormatInfo(QRMatrix $matrix):void{
  251. $matrix->setFormatInfo();
  252. $this::assertTrue($matrix->checkType(8, 0, QRMatrix::M_FORMAT));
  253. $this::assertTrue($matrix->checkType(0, 8, QRMatrix::M_FORMAT));
  254. $this::assertTrue($matrix->checkType($matrix->size() - 1, 8, QRMatrix::M_FORMAT));
  255. $this::assertTrue($matrix->checkType($matrix->size() - 8, 8, QRMatrix::M_FORMAT));
  256. $this->dm($matrix);
  257. }
  258. /**
  259. * Tests the quiet zone pattern and verifies its position
  260. *
  261. * @dataProvider matrixProvider
  262. */
  263. public function testSetQuietZone(QRMatrix $matrix):void{
  264. $size = $matrix->size();
  265. $q = 5;
  266. $matrix->set(0, 0, true, QRMatrix::M_TEST);
  267. $matrix->set($size - 1, $size - 1, true, QRMatrix::M_TEST);
  268. $matrix->setQuietZone($q);
  269. $this::assertCount($size + 2 * $q, $matrix->matrix());
  270. $this::assertCount($size + 2 * $q, $matrix->matrix()[$size - 1]);
  271. $size = $matrix->size();
  272. $this::assertTrue($matrix->checkType(0, 0, QRMatrix::M_QUIETZONE));
  273. $this::assertTrue($matrix->checkType($size - 1, $size - 1, QRMatrix::M_QUIETZONE));
  274. $this::assertSame(QRMatrix::M_TEST | QRMatrix::IS_DARK, $matrix->get($q, $q));
  275. $this::assertSame(QRMatrix::M_TEST | QRMatrix::IS_DARK, $matrix->get($size - 1 - $q, $size - 1 - $q));
  276. $this->dm($matrix);
  277. }
  278. /**
  279. * Tests if an exception is thrown in an attempt to create the quiet zone before data was written
  280. */
  281. public function testSetQuietZoneException():void{
  282. $this->expectException(QRCodeDataException::class);
  283. $this->expectExceptionMessage('use only after writing data');
  284. $this->matrix->setQuietZone(42);
  285. }
  286. /**
  287. * Tests the auto orientation of the logo space
  288. */
  289. public function testSetLogoSpaceOrientation():void{
  290. $o = new QROptions;
  291. $o->version = 10;
  292. $o->eccLevel = EccLevel::H;
  293. $o->addQuietzone = false;
  294. $matrix = (new QRCode($o))->addByteSegment('testdata')->getMatrix();
  295. // also testing size adjustment to uneven numbers
  296. $matrix->setLogoSpace(20, 14);
  297. // NW corner
  298. $this::assertFalse($matrix->checkType(17, 20, QRMatrix::M_LOGO));
  299. $this::assertTrue($matrix->checkType(18, 21, QRMatrix::M_LOGO));
  300. // SE corner
  301. $this::assertTrue($matrix->checkType(38, 35, QRMatrix::M_LOGO));
  302. $this::assertFalse($matrix->checkType(39, 36, QRMatrix::M_LOGO));
  303. self::debugMatrix($matrix);
  304. }
  305. /**
  306. * Tests the manual positioning of the logo space
  307. */
  308. public function testSetLogoSpacePosition():void{
  309. $o = new QROptions;
  310. $o->version = 10;
  311. $o->eccLevel = EccLevel::H;
  312. $o->addQuietzone = true;
  313. $o->quietzoneSize = 10;
  314. $matrix = (new QRCode($o))->addByteSegment('testdata')->getMatrix();
  315. // logo space should not overwrite quiet zone & function patterns
  316. $matrix->setLogoSpace(21, 21, -10, -10);
  317. $this::assertSame(QRMatrix::M_QUIETZONE, $matrix->get(9, 9));
  318. $this::assertSame(QRMatrix::M_FINDER | QRMatrix::IS_DARK, $matrix->get(10, 10));
  319. $this::assertSame(QRMatrix::M_FINDER | QRMatrix::IS_DARK, $matrix->get(16, 16));
  320. $this::assertSame(QRMatrix::M_SEPARATOR, $matrix->get(17, 17));
  321. $this::assertSame(QRMatrix::M_FORMAT | QRMatrix::IS_DARK, $matrix->get(18, 18));
  322. $this::assertSame(QRMatrix::M_LOGO, $matrix->get(19, 19));
  323. $this::assertSame(QRMatrix::M_LOGO, $matrix->get(20, 20));
  324. $this::assertNotSame(QRMatrix::M_LOGO, $matrix->get(21, 21));
  325. // i just realized that setLogoSpace() could be called multiple times
  326. // on the same instance and i'm not going to do anything about it :P
  327. $matrix->setLogoSpace(21, 21, 45, 45);
  328. $this::assertNotSame(QRMatrix::M_LOGO, $matrix->get(54, 54));
  329. $this::assertSame(QRMatrix::M_LOGO, $matrix->get(55, 55));
  330. $this::assertSame(QRMatrix::M_QUIETZONE, $matrix->get(67, 67));
  331. self::debugMatrix($matrix);
  332. }
  333. /**
  334. * Tests whether an exception is thrown when an ECC level other than "H" is set when attempting to add logo space
  335. */
  336. public function testSetLogoSpaceInvalidEccException():void{
  337. $this->expectException(QRCodeDataException::class);
  338. $this->expectExceptionMessage('ECC level "H" required to add logo space');
  339. (new QRCode)->addByteSegment('testdata')->getMatrix()->setLogoSpace(50, 50);
  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')->getMatrix()->setLogoSpace(37, 37);
  351. }
  352. /**
  353. * Tests flipping the value of a module
  354. */
  355. public function testFlip():void{
  356. // using the dark module here because i'm lazy
  357. $this->matrix->setDarkModule();
  358. $x = 8;
  359. $y = $this->matrix->size() - 8;
  360. // cover checkType()
  361. $this::assertTrue($this->matrix->checkType($x, $y, QRMatrix::M_DARKMODULE));
  362. // verify the current state (dark)
  363. $this::assertSame(QRMatrix::M_DARKMODULE | QRMatrix::IS_DARK, $this->matrix->get($x, $y));
  364. // flip
  365. $this->matrix->flip($x, $y);
  366. // verify flip
  367. $this::assertSame(QRMatrix::M_DARKMODULE, $this->matrix->get($x, $y));
  368. // flip again
  369. $this->matrix->flip($x, $y);
  370. // verify flip
  371. $this::assertSame(QRMatrix::M_DARKMODULE | QRMatrix::IS_DARK, $this->matrix->get($x, $y));
  372. }
  373. }