QRMatrixTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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. private const version = 40;
  21. private 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. }
  175. $matrix = $this->getMatrix($version)->setVersionNumber();
  176. $this::assertTrue($matrix->checkType($matrix->size() - 9, 0, QRMatrix::M_VERSION));
  177. $this::assertTrue($matrix->checkType($matrix->size() - 11, 5, QRMatrix::M_VERSION));
  178. $this::assertTrue($matrix->checkType(0, $matrix->size() - 9, QRMatrix::M_VERSION));
  179. $this::assertTrue($matrix->checkType(5, $matrix->size() - 11, QRMatrix::M_VERSION));
  180. }
  181. /**
  182. * Tests the format patterns and verifies their positions
  183. *
  184. * @dataProvider versionProvider
  185. */
  186. public function testSetFormatInfo(int $version):void{
  187. $matrix = $this->getMatrix($version)->setFormatInfo(new MaskPattern(MaskPattern::PATTERN_000));
  188. $this::assertTrue($matrix->checkType(8, 0, QRMatrix::M_FORMAT));
  189. $this::assertTrue($matrix->checkType(0, 8, QRMatrix::M_FORMAT));
  190. $this::assertTrue($matrix->checkType($matrix->size() - 1, 8, QRMatrix::M_FORMAT));
  191. $this::assertTrue($matrix->checkType($matrix->size() - 8, 8, QRMatrix::M_FORMAT));
  192. }
  193. /**
  194. * Tests the quiet zone pattern and verifies its position
  195. *
  196. * @dataProvider versionProvider
  197. */
  198. public function testSetQuietZone(int $version):void{
  199. $matrix = $this->getMatrix($version);
  200. $size = $matrix->size();
  201. $q = 5;
  202. $matrix->set(0, 0, true, QRMatrix::M_TEST);
  203. $matrix->set($size - 1, $size - 1, true, QRMatrix::M_TEST);
  204. $matrix->setQuietZone($q);
  205. $this::assertCount($size + 2 * $q, $matrix->matrix());
  206. $this::assertCount($size + 2 * $q, $matrix->matrix()[$size - 1]);
  207. $size = $matrix->size();
  208. $this::assertTrue($matrix->checkType(0, 0, QRMatrix::M_QUIETZONE));
  209. $this::assertTrue($matrix->checkType($size - 1, $size - 1, QRMatrix::M_QUIETZONE));
  210. $this::assertSame(QRMatrix::M_TEST | QRMatrix::IS_DARK, $matrix->get($q, $q));
  211. $this::assertSame(QRMatrix::M_TEST | QRMatrix::IS_DARK, $matrix->get($size - 1 - $q, $size - 1 - $q));
  212. }
  213. /**
  214. * Tests if an exception is thrown in an attempt to create the quiet zone before data was written
  215. */
  216. public function testSetQuietZoneException():void{
  217. $this->expectException(QRCodeDataException::class);
  218. $this->expectExceptionMessage('use only after writing data');
  219. $this->matrix->setQuietZone();
  220. }
  221. /**
  222. * Tests the auto orientation of the logo space
  223. */
  224. public function testSetLogoSpaceOrientation():void{
  225. $o = new QROptions;
  226. $o->version = 10;
  227. $o->eccLevel = EccLevel::H;
  228. $o->addQuietzone = false;
  229. $matrix = (new QRCode($o))->addByteSegment('testdata')->getMatrix();
  230. // also testing size adjustment to uneven numbers
  231. $matrix->setLogoSpace(20, 14);
  232. // NW corner
  233. $this::assertFalse($matrix->checkType(17, 20, QRMatrix::M_LOGO));
  234. $this::assertTrue($matrix->checkType(18, 21, QRMatrix::M_LOGO));
  235. // SE corner
  236. $this::assertTrue($matrix->checkType(38, 35, QRMatrix::M_LOGO));
  237. $this::assertFalse($matrix->checkType(39, 36, QRMatrix::M_LOGO));
  238. }
  239. /**
  240. * Tests the manual positioning of the logo space
  241. */
  242. public function testSetLogoSpacePosition():void{
  243. $o = new QROptions;
  244. $o->version = 10;
  245. $o->eccLevel = EccLevel::H;
  246. $o->addQuietzone = true;
  247. $o->quietzoneSize = 10;
  248. $m = (new QRCode($o))->addByteSegment('testdata')->getMatrix();
  249. // logo space should not overwrite quiet zone & function patterns
  250. $m->setLogoSpace(21, 21, -10, -10);
  251. $this::assertSame(QRMatrix::M_QUIETZONE, $m->get(9, 9));
  252. $this::assertSame(QRMatrix::M_FINDER | QRMatrix::IS_DARK, $m->get(10, 10));
  253. $this::assertSame(QRMatrix::M_FINDER | QRMatrix::IS_DARK, $m->get(16, 16));
  254. $this::assertSame(QRMatrix::M_SEPARATOR, $m->get(17, 17));
  255. $this::assertSame(QRMatrix::M_FORMAT | QRMatrix::IS_DARK, $m->get(18, 18));
  256. $this::assertSame(QRMatrix::M_LOGO, $m->get(19, 19));
  257. $this::assertSame(QRMatrix::M_LOGO, $m->get(20, 20));
  258. $this::assertNotSame(QRMatrix::M_LOGO, $m->get(21, 21));
  259. // i just realized that setLogoSpace() could be called multiple times
  260. // on the same instance and i'm not going to do anything about it :P
  261. $m->setLogoSpace(21, 21, 45, 45);
  262. $this::assertNotSame(QRMatrix::M_LOGO, $m->get(54, 54));
  263. $this::assertSame(QRMatrix::M_LOGO, $m->get(55, 55));
  264. $this::assertSame(QRMatrix::M_QUIETZONE, $m->get(67, 67));
  265. }
  266. /**
  267. * Tests whether an exception is thrown when an ECC level other than "H" is set when attempting to add logo space
  268. */
  269. public function testSetLogoSpaceInvalidEccException():void{
  270. $this->expectException(QRCodeDataException::class);
  271. $this->expectExceptionMessage('ECC level "H" required to add logo space');
  272. (new QRCode)->addByteSegment('testdata')->getMatrix()->setLogoSpace(50, 50);
  273. }
  274. /**
  275. * Tests whether an exception is thrown when the logo space size exceeds the maximum ECC capacity
  276. */
  277. public function testSetLogoSpaceMaxSizeException():void{
  278. $this->expectException(QRCodeDataException::class);
  279. $this->expectExceptionMessage('logo space exceeds the maximum error correction capacity');
  280. $o = new QROptions;
  281. $o->version = 5;
  282. $o->eccLevel = EccLevel::H;
  283. (new QRCode($o))->addByteSegment('testdata')->getMatrix()->setLogoSpace(37, 37);
  284. }
  285. /**
  286. * Tests flipping the value of a module
  287. */
  288. public function testFlip():void{
  289. // using the dark module here because i'm lazy
  290. $matrix = $this->getMatrix(10)->setDarkModule();
  291. $x = 8;
  292. $y = $matrix->size() - 8;
  293. // cover checkType()
  294. $this::assertTrue($matrix->checkType($x, $y, QRMatrix::M_DARKMODULE));
  295. // verify the current state (dark)
  296. $this::assertSame(QRMatrix::M_DARKMODULE | QRMatrix::IS_DARK, $matrix->get($x, $y));
  297. // flip
  298. $matrix->flip($x, $y);
  299. // verify flip
  300. $this::assertSame(QRMatrix::M_DARKMODULE, $matrix->get($x, $y));
  301. // flip again
  302. $matrix->flip($x, $y);
  303. // verify flip
  304. $this::assertSame(QRMatrix::M_DARKMODULE | QRMatrix::IS_DARK, $matrix->get($x, $y));
  305. }
  306. }