QRMatrixTest.php 10 KB

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