QRMatrixTest.php 10 KB

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