QRMatrixTest.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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\Data\{QRCodeDataException, QRMatrix};
  15. use PHPUnit\Framework\TestCase;
  16. use ReflectionClass;
  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($version, QRCode::ECC_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 an exception is thrown when an invalid QR version was given
  49. */
  50. public function testInvalidVersionException():void{
  51. $this->expectException(QRCodeDataException::class);
  52. $this->expectExceptionMessage('invalid QR Code version');
  53. $this->matrix = new QRMatrix(42, 0);
  54. }
  55. /**
  56. * Tests if an exception is thrown when an invalid ECC level was given
  57. */
  58. public function testInvalidEccException():void{
  59. $this->expectException(QRCodeDataException::class);
  60. $this->expectExceptionMessage('invalid ecc level');
  61. $this->matrix = new QRMatrix(1, 42);
  62. }
  63. /**
  64. * Tests if size() returns the actual matrix size/count
  65. */
  66. public function testSize():void{
  67. $this::assertCount($this->matrix->size(), $this->matrix->matrix());
  68. }
  69. /**
  70. * Tests if version() returns the current (given) version
  71. */
  72. public function testVersion():void{
  73. $this::assertSame($this::version, $this->matrix->version());
  74. }
  75. /**
  76. * Tests if eccLevel() returns the current (given) ECC level
  77. */
  78. public function testECC():void{
  79. $this::assertSame(QRCode::ECC_L, $this->matrix->eccLevel());
  80. }
  81. /**
  82. * Tests if maskPattern() returns the current (or default) mask pattern
  83. */
  84. public function testMaskPattern():void{
  85. $this::assertSame(-1, $this->matrix->maskPattern()); // default
  86. // @todo: actual mask pattern after mapData()
  87. }
  88. /**
  89. * Tests the set(), get() and check() methods
  90. */
  91. public function testGetSetCheck():void{
  92. $this->matrix->set(10, 10, true, QRMatrix::M_TEST);
  93. $this::assertSame(65280, $this->matrix->get(10, 10));
  94. $this::assertTrue($this->matrix->check(10, 10));
  95. $this->matrix->set(20, 20, false, QRMatrix::M_TEST);
  96. $this::assertSame(255, $this->matrix->get(20, 20));
  97. $this::assertFalse($this->matrix->check(20, 20));
  98. }
  99. /**
  100. * Version data provider for several pattern tests
  101. *
  102. * @return int[][]
  103. * @internal
  104. */
  105. public function versionProvider():array{
  106. $versions = [];
  107. for($i = 1; $i <= 40; $i++){
  108. $versions[] = [$i];
  109. }
  110. return $versions;
  111. }
  112. /**
  113. * Tests setting the dark module and verifies its position
  114. *
  115. * @dataProvider versionProvider
  116. */
  117. public function testSetDarkModule(int $version):void{
  118. $matrix = $this->getMatrix($version)->setDarkModule();
  119. $this::assertSame(QRMatrix::M_DARKMODULE << 8, $matrix->get(8, $matrix->size() - 8));
  120. }
  121. /**
  122. * Tests setting the finder patterns and verifies their positions
  123. *
  124. * @dataProvider versionProvider
  125. */
  126. public function testSetFinderPattern(int $version):void{
  127. $matrix = $this->getMatrix($version)->setFinderPattern();
  128. $this::assertSame(QRMatrix::M_FINDER << 8, $matrix->get(0, 0));
  129. $this::assertSame(QRMatrix::M_FINDER << 8, $matrix->get(0, $matrix->size() - 1));
  130. $this::assertSame(QRMatrix::M_FINDER << 8, $matrix->get($matrix->size() - 1, 0));
  131. }
  132. /**
  133. * Tests the separator patterns and verifies their positions
  134. *
  135. * @dataProvider versionProvider
  136. */
  137. public function testSetSeparators(int $version):void{
  138. $matrix = $this->getMatrix($version)->setSeparators();
  139. $this::assertSame(QRMatrix::M_SEPARATOR, $matrix->get(7, 0));
  140. $this::assertSame(QRMatrix::M_SEPARATOR, $matrix->get(0, 7));
  141. $this::assertSame(QRMatrix::M_SEPARATOR, $matrix->get(0, $matrix->size() - 8));
  142. $this::assertSame(QRMatrix::M_SEPARATOR, $matrix->get($matrix->size() - 8, 0));
  143. }
  144. /**
  145. * Tests the alignment patterns and verifies their positions - version 1 (no pattern) skipped
  146. *
  147. * @dataProvider versionProvider
  148. */
  149. public function testSetAlignmentPattern(int $version):void{
  150. if($version === 1){
  151. $this->markTestSkipped('N/A');
  152. return;
  153. }
  154. $matrix = $this
  155. ->getMatrix($version)
  156. ->setFinderPattern()
  157. ->setAlignmentPattern()
  158. ;
  159. $alignmentPattern = (new ReflectionClass(QRMatrix::class))->getConstant('alignmentPattern')[$version];
  160. foreach($alignmentPattern as $py){
  161. foreach($alignmentPattern as $px){
  162. if($matrix->get($px, $py) === QRMatrix::M_FINDER << 8){
  163. $this::assertSame(QRMatrix::M_FINDER << 8, $matrix->get($px, $py), 'skipped finder pattern');
  164. continue;
  165. }
  166. $this::assertSame(QRMatrix::M_ALIGNMENT << 8, $matrix->get($px, $py));
  167. }
  168. }
  169. }
  170. /**
  171. * Tests the timing patterns and verifies their positions
  172. *
  173. * @dataProvider versionProvider
  174. */
  175. public function testSetTimingPattern(int $version):void{
  176. $matrix = $this
  177. ->getMatrix($version)
  178. ->setAlignmentPattern()
  179. ->setTimingPattern()
  180. ;
  181. $size = $matrix->size();
  182. for($i = 7; $i < $size - 7; $i++){
  183. if($i % 2 === 0){
  184. $p1 = $matrix->get(6, $i);
  185. if($p1 === QRMatrix::M_ALIGNMENT << 8){
  186. $this::assertSame(QRMatrix::M_ALIGNMENT << 8, $p1, 'skipped alignment pattern');
  187. continue;
  188. }
  189. $this::assertSame(QRMatrix::M_TIMING << 8, $p1);
  190. $this::assertSame(QRMatrix::M_TIMING << 8, $matrix->get($i, 6));
  191. }
  192. }
  193. }
  194. /**
  195. * Tests the version patterns and verifies their positions - version < 7 skipped
  196. *
  197. * @dataProvider versionProvider
  198. */
  199. public function testSetVersionNumber(int $version):void{
  200. if($version < 7){
  201. $this->markTestSkipped('N/A');
  202. return;
  203. }
  204. $matrix = $this->getMatrix($version)->setVersionNumber(true);
  205. $this::assertSame(QRMatrix::M_VERSION, $matrix->get($matrix->size() - 9, 0));
  206. $this::assertSame(QRMatrix::M_VERSION, $matrix->get($matrix->size() - 11, 5));
  207. $this::assertSame(QRMatrix::M_VERSION, $matrix->get(0, $matrix->size() - 9));
  208. $this::assertSame(QRMatrix::M_VERSION, $matrix->get(5, $matrix->size() - 11));
  209. }
  210. /**
  211. * Tests the format patterns and verifies their positions
  212. *
  213. * @dataProvider versionProvider
  214. */
  215. public function testSetFormatInfo(int $version):void{
  216. $matrix = $this->getMatrix($version)->setFormatInfo(0, true);
  217. $this::assertSame(QRMatrix::M_FORMAT, $matrix->get(8, 0));
  218. $this::assertSame(QRMatrix::M_FORMAT, $matrix->get(0, 8));
  219. $this::assertSame(QRMatrix::M_FORMAT, $matrix->get($matrix->size() - 1, 8));
  220. $this::assertSame(QRMatrix::M_FORMAT, $matrix->get($matrix->size() - 8, 8));
  221. }
  222. /**
  223. * Tests the quiet zone pattern and verifies its position
  224. *
  225. * @dataProvider versionProvider
  226. */
  227. public function testSetQuietZone(int $version):void{
  228. $matrix = $this->getMatrix($version);
  229. $size = $matrix->size();
  230. $q = 5;
  231. $matrix->set(0, 0, true, QRMatrix::M_TEST);
  232. $matrix->set($size - 1, $size - 1, true, QRMatrix::M_TEST);
  233. $matrix->setQuietZone($q);
  234. $this::assertCount($size + 2 * $q, $matrix->matrix());
  235. $this::assertCount($size + 2 * $q, $matrix->matrix()[$size - 1]);
  236. $size = $matrix->size();
  237. $this::assertSame(QRMatrix::M_QUIETZONE, $matrix->get(0, 0));
  238. $this::assertSame(QRMatrix::M_QUIETZONE, $matrix->get($size - 1, $size - 1));
  239. $this::assertSame(QRMatrix::M_TEST << 8, $matrix->get($q, $q));
  240. $this::assertSame(QRMatrix::M_TEST << 8, $matrix->get($size - 1 - $q, $size - 1 - $q));
  241. }
  242. /**
  243. * Tests if an exception is thrown in an attempt to create it before data was written
  244. */
  245. public function testSetQuietZoneException():void{
  246. $this->expectException(QRCodeDataException::class);
  247. $this->expectExceptionMessage('use only after writing data');
  248. $this->matrix->setQuietZone();
  249. }
  250. }