QRMatrixTest.php 11 KB

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