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