QRMatrixTest.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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 chillerlan\QRCodeTest\QRTestAbstract;
  17. use ReflectionClass;
  18. class QRMatrixTest extends QRTestAbstract{
  19. protected $FQCN = QRMatrix::class;
  20. protected $version = 7;
  21. /**
  22. * @var \chillerlan\QRCode\Data\QRMatrix
  23. */
  24. protected $matrix;
  25. protected function setUp():void{
  26. parent::setUp();
  27. $this->matrix = $this->reflection->newInstanceArgs([$this->version, QRCode::ECC_L]);
  28. }
  29. public function testInvalidVersionException(){
  30. $this->expectException(QRCodeDataException::class);
  31. $this->expectExceptionMessage('invalid QR Code version');
  32. $this->reflection->newInstanceArgs([42, 0]);
  33. }
  34. public function testInvalidEccException(){
  35. $this->expectException(QRCodeDataException::class);
  36. $this->expectExceptionMessage('invalid ecc level');
  37. $this->reflection->newInstanceArgs([1, 42]);
  38. }
  39. public function testInstance(){
  40. $this->assertInstanceOf($this->FQCN, $this->matrix);
  41. }
  42. public function testSize(){
  43. $this->assertCount($this->matrix->size(), $this->matrix->matrix());
  44. }
  45. public function testVersion(){
  46. $this->assertSame($this->version, $this->matrix->version());
  47. }
  48. public function testECC(){
  49. $this->assertSame(QRCode::ECC_L, $this->matrix->eccLevel());
  50. }
  51. public function testMaskPattern(){
  52. $this->assertSame(-1, $this->matrix->maskPattern());
  53. }
  54. public function testGetSetCheck(){
  55. $this->matrix->set(10, 10, true, QRMatrix::M_TEST);
  56. $this->assertSame(65280, $this->matrix->get(10, 10));
  57. $this->assertTrue($this->matrix->check(10, 10));
  58. $this->matrix->set(20, 20, false, QRMatrix::M_TEST);
  59. $this->assertSame(255, $this->matrix->get(20, 20));
  60. $this->assertFalse($this->matrix->check(20, 20));
  61. }
  62. public function testSetDarkModule(){
  63. $this->matrix->setDarkModule();
  64. $this->assertSame(QRMatrix::M_DARKMODULE << 8, $this->matrix->get(8, $this->matrix->size() - 8));
  65. }
  66. public function testSetFinderPattern(){
  67. $this->matrix->setFinderPattern();
  68. $this->assertSame(QRMatrix::M_FINDER << 8, $this->matrix->get(0, 0));
  69. $this->assertSame(QRMatrix::M_FINDER << 8, $this->matrix->get(0, $this->matrix->size() - 1));
  70. $this->assertSame(QRMatrix::M_FINDER << 8, $this->matrix->get($this->matrix->size() - 1, 0));
  71. }
  72. public function testSetSeparators(){
  73. $this->matrix->setSeparators();
  74. $this->assertSame(QRMatrix::M_SEPARATOR, $this->matrix->get(7, 0));
  75. $this->assertSame(QRMatrix::M_SEPARATOR, $this->matrix->get(0, 7));
  76. $this->assertSame(QRMatrix::M_SEPARATOR, $this->matrix->get(0, $this->matrix->size() - 8));
  77. $this->assertSame(QRMatrix::M_SEPARATOR, $this->matrix->get($this->matrix->size() - 8, 0));
  78. }
  79. public function testSetAlignmentPattern(){
  80. $this->matrix
  81. ->setFinderPattern()
  82. ->setAlignmentPattern()
  83. ;
  84. $alignmentPattern = (new ReflectionClass(QRMatrix::class))->getConstant('alignmentPattern')[$this->version];
  85. foreach($alignmentPattern as $py){
  86. foreach($alignmentPattern as $px){
  87. if($this->matrix->get($px, $py) === QRMatrix::M_FINDER << 8){
  88. $this->assertSame(QRMatrix::M_FINDER << 8, $this->matrix->get($px, $py), 'skipped finder pattern');
  89. continue;
  90. }
  91. $this->assertSame(QRMatrix::M_ALIGNMENT << 8, $this->matrix->get($px, $py));
  92. }
  93. }
  94. }
  95. public function testSetTimingPattern(){
  96. $this->matrix
  97. ->setAlignmentPattern()
  98. ->setTimingPattern()
  99. ;
  100. $size = $this->matrix->size();
  101. for($i = 7; $i < $size - 7; $i++){
  102. if($i % 2 === 0){
  103. $p1 = $this->matrix->get(6, $i);
  104. if($p1 === QRMatrix::M_ALIGNMENT << 8){
  105. $this->assertSame(QRMatrix::M_ALIGNMENT << 8, $p1, 'skipped alignment pattern');
  106. continue;
  107. }
  108. $this->assertSame(QRMatrix::M_TIMING << 8, $p1);
  109. $this->assertSame(QRMatrix::M_TIMING << 8, $this->matrix->get($i, 6));
  110. }
  111. }
  112. }
  113. public function testSetVersionNumber(){
  114. $this->matrix->setVersionNumber(true);
  115. $this->assertSame(QRMatrix::M_VERSION, $this->matrix->get($this->matrix->size() - 9, 0));
  116. $this->assertSame(QRMatrix::M_VERSION, $this->matrix->get($this->matrix->size() - 11, 5));
  117. $this->assertSame(QRMatrix::M_VERSION, $this->matrix->get(0, $this->matrix->size() - 9));
  118. $this->assertSame(QRMatrix::M_VERSION, $this->matrix->get(5, $this->matrix->size() - 11));
  119. }
  120. public function testSetFormatInfo(){
  121. $this->matrix->setFormatInfo(0, true);
  122. $this->assertSame(QRMatrix::M_FORMAT, $this->matrix->get(8, 0));
  123. $this->assertSame(QRMatrix::M_FORMAT, $this->matrix->get(0, 8));
  124. $this->assertSame(QRMatrix::M_FORMAT, $this->matrix->get($this->matrix->size() - 1, 8));
  125. $this->assertSame(QRMatrix::M_FORMAT, $this->matrix->get($this->matrix->size() - 8, 8));
  126. }
  127. public function testSetQuietZone(){
  128. $size = $this->matrix->size();
  129. $q = 5;
  130. $this->matrix->set(0, 0, true, QRMatrix::M_TEST);
  131. $this->matrix->set($size - 1, $size - 1, true, QRMatrix::M_TEST);
  132. $this->matrix->setQuietZone($q);
  133. $this->assertCount($size + 2 * $q, $this->matrix->matrix());
  134. $this->assertCount($size + 2 * $q, $this->matrix->matrix()[$size - 1]);
  135. $size = $this->matrix->size();
  136. $this->assertSame(QRMatrix::M_QUIETZONE, $this->matrix->get(0, 0));
  137. $this->assertSame(QRMatrix::M_QUIETZONE, $this->matrix->get($size - 1, $size - 1));
  138. $this->assertSame(QRMatrix::M_TEST << 8, $this->matrix->get($q, $q));
  139. $this->assertSame(QRMatrix::M_TEST << 8, $this->matrix->get($size - 1 - $q, $size - 1 - $q));
  140. }
  141. public function testSetQuietZoneException(){
  142. $this->expectException(QRCodeDataException::class);
  143. $this->expectExceptionMessage('use only after writing data');
  144. $this->matrix->setQuietZone();
  145. }
  146. public function testSetLogoSpaceOrientation():void{
  147. $o = new QROptions;
  148. $o->version = 10;
  149. $o->eccLevel = QRCode::ECC_H;
  150. $o->addQuietzone = false;
  151. $matrix = (new QRCode($o))->getMatrix('testdata');
  152. // also testing size adjustment to uneven numbers
  153. $matrix->setLogoSpace(20, 14);
  154. // NW corner
  155. $this::assertNotSame(QRMatrix::M_LOGO, $matrix->get(17, 20));
  156. $this::assertSame(QRMatrix::M_LOGO, $matrix->get(18, 21));
  157. // SE corner
  158. $this::assertSame(QRMatrix::M_LOGO, $matrix->get(38, 35));
  159. $this::assertNotSame(QRMatrix::M_LOGO, $matrix->get(39, 36));
  160. }
  161. public function testSetLogoSpacePosition():void{
  162. $o = new QROptions;
  163. $o->version = 10;
  164. $o->eccLevel = QRCode::ECC_H;
  165. $o->addQuietzone = true;
  166. $o->quietzoneSize = 10;
  167. $m = (new QRCode($o))->getMatrix('testdata');
  168. // logo space should not overwrite quiet zone & function patterns
  169. $m->setLogoSpace(21, 21, -10, -10);
  170. $this::assertSame(QRMatrix::M_QUIETZONE, $m->get(9, 9));
  171. $this::assertSame(QRMatrix::M_FINDER << 8, $m->get(10, 10));
  172. $this::assertSame(QRMatrix::M_FINDER << 8, $m->get(16, 16));
  173. $this::assertSame(QRMatrix::M_SEPARATOR, $m->get(17, 17));
  174. $this::assertSame(QRMatrix::M_FORMAT << 8, $m->get(18, 18));
  175. $this::assertSame(QRMatrix::M_LOGO, $m->get(19, 19));
  176. $this::assertSame(QRMatrix::M_LOGO, $m->get(20, 20));
  177. $this::assertNotSame(QRMatrix::M_LOGO, $m->get(21, 21));
  178. // i just realized that setLogoSpace() could be called multiple times
  179. // on the same instance and i'm not going to do anything about it :P
  180. $m->setLogoSpace(21, 21, 45, 45);
  181. $this::assertNotSame(QRMatrix::M_LOGO, $m->get(54, 54));
  182. $this::assertSame(QRMatrix::M_LOGO, $m->get(55, 55));
  183. $this::assertSame(QRMatrix::M_QUIETZONE, $m->get(67, 67));
  184. }
  185. public function testSetLogoSpaceInvalidEccException():void{
  186. $this->expectException(QRCodeDataException::class);
  187. $this->expectExceptionMessage('ECC level "H" required to add logo space');
  188. (new QRCode)->getMatrix('testdata')->setLogoSpace(50, 50);
  189. }
  190. public function testSetLogoSpaceMaxSizeException():void{
  191. $this->expectException(QRCodeDataException::class);
  192. $this->expectExceptionMessage('logo space exceeds the maximum error correction capacity');
  193. $o = new QROptions;
  194. $o->version = 5;
  195. $o->eccLevel = QRCode::ECC_H;
  196. (new QRCode($o))->getMatrix('testdata')->setLogoSpace(50, 50);
  197. }
  198. }