QRMatrixTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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\Common\{EccLevel, MaskPattern, Version};
  12. use chillerlan\QRCode\{QRCode, QROptions};
  13. use chillerlan\QRCode\Data\{QRCodeDataException, QRMatrix};
  14. use PHPUnit\Framework\TestCase;
  15. use Generator;
  16. /**
  17. * Tests the QRMatix class
  18. */
  19. final class QRMatrixTest extends TestCase{
  20. protected const version = 40;
  21. protected QRMatrix $matrix;
  22. /**
  23. * invokes a QRMatrix object
  24. */
  25. protected function setUp():void{
  26. $this->matrix = $this->getMatrix($this::version);
  27. }
  28. /**
  29. * shortcut
  30. */
  31. protected function getMatrix(int $version):QRMatrix{
  32. return new QRMatrix(new Version($version), new EccLevel(EccLevel::L));
  33. }
  34. /**
  35. * Validates the QRMatrix instance
  36. */
  37. public function testInstance():void{
  38. $this::assertInstanceOf(QRMatrix::class, $this->matrix);
  39. }
  40. /**
  41. * Tests if size() returns the actual matrix size/count
  42. */
  43. public function testSize():void{
  44. $this::assertCount($this->matrix->size(), $this->matrix->matrix());
  45. }
  46. /**
  47. * Tests if version() returns the current (given) version
  48. */
  49. public function testVersion():void{
  50. $this::assertSame($this::version, $this->matrix->version()->getVersionNumber());
  51. }
  52. /**
  53. * Tests if eccLevel() returns the current (given) ECC level
  54. */
  55. public function testECC():void{
  56. $this::assertSame(EccLevel::L, $this->matrix->eccLevel()->getLevel());
  57. }
  58. /**
  59. * Tests if maskPattern() returns the current (or default) mask pattern
  60. */
  61. public function testMaskPattern():void{
  62. $this::assertSame(null, $this->matrix->maskPattern());
  63. $matrix = (new QRCode)->addByteSegment('testdata')->getMatrix();
  64. $this::assertInstanceOf(MaskPattern::class, $matrix->maskPattern());
  65. $this::assertSame(MaskPattern::PATTERN_100, $matrix->maskPattern()->getPattern());
  66. }
  67. /**
  68. * Tests the set(), get() and check() methods
  69. */
  70. public function testGetSetCheck():void{
  71. $this->matrix->set(10, 10, true, QRMatrix::M_TEST);
  72. $this::assertSame(QRMatrix::M_TEST | QRMatrix::IS_DARK, $this->matrix->get(10, 10));
  73. $this::assertTrue($this->matrix->check(10, 10));
  74. $this->matrix->set(20, 20, false, QRMatrix::M_TEST);
  75. $this::assertSame(QRMatrix::M_TEST, $this->matrix->get(20, 20));
  76. $this::assertFalse($this->matrix->check(20, 20));
  77. }
  78. /**
  79. * Version data provider for several pattern tests
  80. */
  81. public function versionProvider():Generator{
  82. foreach(range(1, 40) as $i){
  83. yield 'version: '.$i => [$i];
  84. }
  85. }
  86. /**
  87. * Tests setting the dark module and verifies its position
  88. *
  89. * @dataProvider versionProvider
  90. */
  91. public function testSetDarkModule(int $version):void{
  92. $matrix = $this->getMatrix($version)->setDarkModule();
  93. $this::assertSame(QRMatrix::M_DARKMODULE | QRMatrix::IS_DARK, $matrix->get(8, $matrix->size() - 8));
  94. }
  95. /**
  96. * Tests setting the finder patterns and verifies their positions
  97. *
  98. * @dataProvider versionProvider
  99. */
  100. public function testSetFinderPattern(int $version):void{
  101. $matrix = $this->getMatrix($version)->setFinderPattern();
  102. $this::assertSame(QRMatrix::M_FINDER | QRMatrix::IS_DARK, $matrix->get(0, 0));
  103. $this::assertSame(QRMatrix::M_FINDER | QRMatrix::IS_DARK, $matrix->get(0, $matrix->size() - 1));
  104. $this::assertSame(QRMatrix::M_FINDER | QRMatrix::IS_DARK, $matrix->get($matrix->size() - 1, 0));
  105. }
  106. /**
  107. * Tests the separator patterns and verifies their positions
  108. *
  109. * @dataProvider versionProvider
  110. */
  111. public function testSetSeparators(int $version):void{
  112. $matrix = $this->getMatrix($version)->setSeparators();
  113. $this::assertSame(QRMatrix::M_SEPARATOR, $matrix->get(7, 0));
  114. $this::assertSame(QRMatrix::M_SEPARATOR, $matrix->get(0, 7));
  115. $this::assertSame(QRMatrix::M_SEPARATOR, $matrix->get(0, $matrix->size() - 8));
  116. $this::assertSame(QRMatrix::M_SEPARATOR, $matrix->get($matrix->size() - 8, 0));
  117. }
  118. /**
  119. * Tests the alignment patterns and verifies their positions - version 1 (no pattern) skipped
  120. *
  121. * @dataProvider versionProvider
  122. */
  123. public function testSetAlignmentPattern(int $version):void{
  124. if($version === 1){
  125. $this->markTestSkipped('N/A (Version 1 has no alignment pattern)');
  126. }
  127. $matrix = $this
  128. ->getMatrix($version)
  129. ->setFinderPattern()
  130. ->setAlignmentPattern()
  131. ;
  132. $alignmentPattern = (new Version($version))->getAlignmentPattern();
  133. foreach($alignmentPattern as $py){
  134. foreach($alignmentPattern as $px){
  135. if($matrix->checkType($px, $py, QRMatrix::M_FINDER)){
  136. // skip finder pattern
  137. continue;
  138. }
  139. $this::assertSame(QRMatrix::M_ALIGNMENT | QRMatrix::IS_DARK, $matrix->get($px, $py));
  140. }
  141. }
  142. }
  143. /**
  144. * Tests the timing patterns and verifies their positions
  145. *
  146. * @dataProvider versionProvider
  147. */
  148. public function testSetTimingPattern(int $version):void{
  149. $matrix = $this
  150. ->getMatrix($version)
  151. ->setAlignmentPattern()
  152. ->setTimingPattern()
  153. ;
  154. $size = $matrix->size();
  155. for($i = 7; $i < $size - 7; $i++){
  156. if($i % 2 === 0){
  157. if($matrix->checkType(6, $i, QRMatrix::M_ALIGNMENT)){
  158. // skip alignment pattern
  159. continue;
  160. }
  161. $this::assertSame(QRMatrix::M_TIMING | QRMatrix::IS_DARK, $matrix->get(6, $i));
  162. $this::assertSame(QRMatrix::M_TIMING | QRMatrix::IS_DARK, $matrix->get($i, 6));
  163. }
  164. }
  165. }
  166. /**
  167. * Tests the version patterns and verifies their positions - version < 7 skipped
  168. *
  169. * @dataProvider versionProvider
  170. */
  171. public function testSetVersionNumber(int $version):void{
  172. if($version < 7){
  173. $this->markTestSkipped('N/A (Version < 7)');
  174. /** @noinspection PhpUnreachableStatementInspection */
  175. return;
  176. }
  177. $matrix = $this->getMatrix($version)->setVersionNumber();
  178. $this::assertTrue($matrix->checkType($matrix->size() - 9, 0, QRMatrix::M_VERSION));
  179. $this::assertTrue($matrix->checkType($matrix->size() - 11, 5, QRMatrix::M_VERSION));
  180. $this::assertTrue($matrix->checkType(0, $matrix->size() - 9, QRMatrix::M_VERSION));
  181. $this::assertTrue($matrix->checkType(5, $matrix->size() - 11, QRMatrix::M_VERSION));
  182. }
  183. /**
  184. * Tests the format patterns and verifies their positions
  185. *
  186. * @dataProvider versionProvider
  187. */
  188. public function testSetFormatInfo(int $version):void{
  189. $matrix = $this->getMatrix($version)->setFormatInfo(new MaskPattern(MaskPattern::PATTERN_000));
  190. $this::assertTrue($matrix->checkType(8, 0, QRMatrix::M_FORMAT));
  191. $this::assertTrue($matrix->checkType(0, 8, QRMatrix::M_FORMAT));
  192. $this::assertTrue($matrix->checkType($matrix->size() - 1, 8, QRMatrix::M_FORMAT));
  193. $this::assertTrue($matrix->checkType($matrix->size() - 8, 8, QRMatrix::M_FORMAT));
  194. }
  195. /**
  196. * Tests the quiet zone pattern and verifies its position
  197. *
  198. * @dataProvider versionProvider
  199. */
  200. public function testSetQuietZone(int $version):void{
  201. $matrix = $this->getMatrix($version);
  202. $size = $matrix->size();
  203. $q = 5;
  204. $matrix->set(0, 0, true, QRMatrix::M_TEST);
  205. $matrix->set($size - 1, $size - 1, true, QRMatrix::M_TEST);
  206. $matrix->setQuietZone($q);
  207. $this::assertCount($size + 2 * $q, $matrix->matrix());
  208. $this::assertCount($size + 2 * $q, $matrix->matrix()[$size - 1]);
  209. $size = $matrix->size();
  210. $this::assertTrue($matrix->checkType(0, 0, QRMatrix::M_QUIETZONE));
  211. $this::assertTrue($matrix->checkType($size - 1, $size - 1, QRMatrix::M_QUIETZONE));
  212. $this::assertSame(QRMatrix::M_TEST | QRMatrix::IS_DARK, $matrix->get($q, $q));
  213. $this::assertSame(QRMatrix::M_TEST | QRMatrix::IS_DARK, $matrix->get($size - 1 - $q, $size - 1 - $q));
  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();
  222. }
  223. /**
  224. * Tests the auto orientation of the logo space
  225. */
  226. public function testSetLogoSpaceOrientation():void{
  227. $o = new QROptions;
  228. $o->version = 10;
  229. $o->eccLevel = EccLevel::H;
  230. $o->addQuietzone = false;
  231. $matrix = (new QRCode($o))->addByteSegment('testdata')->getMatrix();
  232. // also testing size adjustment to uneven numbers
  233. $matrix->setLogoSpace(20, 14);
  234. // NW corner
  235. $this::assertFalse($matrix->checkType(17, 20, QRMatrix::M_LOGO));
  236. $this::assertTrue($matrix->checkType(18, 21, QRMatrix::M_LOGO));
  237. // SE corner
  238. $this::assertTrue($matrix->checkType(38, 35, QRMatrix::M_LOGO));
  239. $this::assertFalse($matrix->checkType(39, 36, QRMatrix::M_LOGO));
  240. }
  241. /**
  242. * Tests the manual positioning of the logo space
  243. */
  244. public function testSetLogoSpacePosition():void{
  245. $o = new QROptions;
  246. $o->version = 10;
  247. $o->eccLevel = EccLevel::H;
  248. $o->addQuietzone = true;
  249. $o->quietzoneSize = 10;
  250. $m = (new QRCode($o))->addByteSegment('testdata')->getMatrix();
  251. // logo space should not overwrite quiet zone & function patterns
  252. $m->setLogoSpace(21, 21, -10, -10);
  253. $this::assertSame(QRMatrix::M_QUIETZONE, $m->get(9, 9));
  254. $this::assertSame(QRMatrix::M_FINDER | QRMatrix::IS_DARK, $m->get(10, 10));
  255. $this::assertSame(QRMatrix::M_FINDER | QRMatrix::IS_DARK, $m->get(16, 16));
  256. $this::assertSame(QRMatrix::M_SEPARATOR, $m->get(17, 17));
  257. $this::assertSame(QRMatrix::M_FORMAT | QRMatrix::IS_DARK, $m->get(18, 18));
  258. $this::assertSame(QRMatrix::M_LOGO, $m->get(19, 19));
  259. $this::assertSame(QRMatrix::M_LOGO, $m->get(20, 20));
  260. $this::assertNotSame(QRMatrix::M_LOGO, $m->get(21, 21));
  261. // i just realized that setLogoSpace() could be called multiple times
  262. // on the same instance and i'm not going to do anything about it :P
  263. $m->setLogoSpace(21, 21, 45, 45);
  264. $this::assertNotSame(QRMatrix::M_LOGO, $m->get(54, 54));
  265. $this::assertSame(QRMatrix::M_LOGO, $m->get(55, 55));
  266. $this::assertSame(QRMatrix::M_QUIETZONE, $m->get(67, 67));
  267. }
  268. /**
  269. * Tests whether an exception is thrown when an ECC level other than "H" is set when attempting to add logo space
  270. */
  271. public function testSetLogoSpaceInvalidEccException():void{
  272. $this->expectException(QRCodeDataException::class);
  273. $this->expectExceptionMessage('ECC level "H" required to add logo space');
  274. (new QRCode)->addByteSegment('testdata')->getMatrix()->setLogoSpace(50, 50);
  275. }
  276. /**
  277. * Tests whether an exception is thrown when the logo space size exceeds the maximum ECC capacity
  278. */
  279. public function testSetLogoSpaceMaxSizeException():void{
  280. $this->expectException(QRCodeDataException::class);
  281. $this->expectExceptionMessage('logo space exceeds the maximum error correction capacity');
  282. $o = new QROptions;
  283. $o->version = 5;
  284. $o->eccLevel = EccLevel::H;
  285. (new QRCode($o))->addByteSegment('testdata')->getMatrix()->setLogoSpace(50, 50);
  286. }
  287. /**
  288. * Tests flipping the value of a module
  289. */
  290. public function testFlip():void{
  291. // using the dark module here because i'm lazy
  292. $matrix = $this->getMatrix(10)->setDarkModule();
  293. $x = 8;
  294. $y = $matrix->size() - 8;
  295. // cover checkType()
  296. $this::assertTrue($matrix->checkType($x, $y, QRMatrix::M_DARKMODULE));
  297. // verify the current state (dark)
  298. $this::assertSame(QRMatrix::M_DARKMODULE | QRMatrix::IS_DARK, $matrix->get($x, $y));
  299. // flip
  300. $matrix->flip($x, $y);
  301. // verify flip
  302. $this::assertSame(QRMatrix::M_DARKMODULE, $matrix->get($x, $y));
  303. // flip again
  304. $matrix->flip($x, $y);
  305. // verify flip
  306. $this::assertSame(QRMatrix::M_DARKMODULE | QRMatrix::IS_DARK, $matrix->get($x, $y));
  307. }
  308. }