QRMatrixTest.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 chillerlan\QRCodeTest\QRTestAbstract;
  16. use ReflectionClass;
  17. class QRMatrixTest extends QRTestAbstract{
  18. protected string $FQCN = QRMatrix::class;
  19. protected int $version = 7;
  20. protected QRMatrix $matrix;
  21. protected function setUp():void{
  22. parent::setUp();
  23. $this->matrix = $this->reflection->newInstanceArgs([$this->version, QRCode::ECC_L]);
  24. }
  25. public function testInvalidVersionException(){
  26. $this->expectException(QRCodeDataException::class);
  27. $this->expectExceptionMessage('invalid QR Code version');
  28. $this->reflection->newInstanceArgs([42, 0]);
  29. }
  30. public function testInvalidEccException(){
  31. $this->expectException(QRCodeDataException::class);
  32. $this->expectExceptionMessage('invalid ecc level');
  33. $this->reflection->newInstanceArgs([1, 42]);
  34. }
  35. public function testInstance(){
  36. $this::assertInstanceOf($this->FQCN, $this->matrix);
  37. }
  38. public function testSize(){
  39. $this::assertCount($this->matrix->size(), $this->matrix->matrix());
  40. }
  41. public function testVersion(){
  42. $this::assertSame($this->version, $this->matrix->version());
  43. }
  44. public function testECC(){
  45. $this::assertSame(QRCode::ECC_L, $this->matrix->eccLevel());
  46. }
  47. public function testMaskPattern(){
  48. $this::assertSame(-1, $this->matrix->maskPattern());
  49. }
  50. public function testGetSetCheck(){
  51. $this->matrix->set(10, 10, true, QRMatrix::M_TEST);
  52. $this::assertSame(65280, $this->matrix->get(10, 10));
  53. $this::assertTrue($this->matrix->check(10, 10));
  54. $this->matrix->set(20, 20, false, QRMatrix::M_TEST);
  55. $this::assertSame(255, $this->matrix->get(20, 20));
  56. $this::assertFalse($this->matrix->check(20, 20));
  57. }
  58. public function testSetDarkModule(){
  59. $this->matrix->setDarkModule();
  60. $this::assertSame(QRMatrix::M_DARKMODULE << 8, $this->matrix->get(8, $this->matrix->size() - 8));
  61. }
  62. public function testSetFinderPattern(){
  63. $this->matrix->setFinderPattern();
  64. $this::assertSame(QRMatrix::M_FINDER << 8, $this->matrix->get(0, 0));
  65. $this::assertSame(QRMatrix::M_FINDER << 8, $this->matrix->get(0, $this->matrix->size() - 1));
  66. $this::assertSame(QRMatrix::M_FINDER << 8, $this->matrix->get($this->matrix->size() - 1, 0));
  67. }
  68. public function testSetSeparators(){
  69. $this->matrix->setSeparators();
  70. $this::assertSame(QRMatrix::M_SEPARATOR, $this->matrix->get(7, 0));
  71. $this::assertSame(QRMatrix::M_SEPARATOR, $this->matrix->get(0, 7));
  72. $this::assertSame(QRMatrix::M_SEPARATOR, $this->matrix->get(0, $this->matrix->size() - 8));
  73. $this::assertSame(QRMatrix::M_SEPARATOR, $this->matrix->get($this->matrix->size() - 8, 0));
  74. }
  75. public function testSetAlignmentPattern(){
  76. $this->matrix
  77. ->setFinderPattern()
  78. ->setAlignmentPattern()
  79. ;
  80. $alignmentPattern = (new ReflectionClass(QRMatrix::class))->getConstant('alignmentPattern')[$this->version];
  81. foreach($alignmentPattern as $py){
  82. foreach($alignmentPattern as $px){
  83. if($this->matrix->get($px, $py) === QRMatrix::M_FINDER << 8){
  84. $this::assertSame(QRMatrix::M_FINDER << 8, $this->matrix->get($px, $py), 'skipped finder pattern');
  85. continue;
  86. }
  87. $this::assertSame(QRMatrix::M_ALIGNMENT << 8, $this->matrix->get($px, $py));
  88. }
  89. }
  90. }
  91. public function testSetTimingPattern(){
  92. $this->matrix
  93. ->setAlignmentPattern()
  94. ->setTimingPattern()
  95. ;
  96. $size = $this->matrix->size();
  97. for($i = 7; $i < $size - 7; $i++){
  98. if($i % 2 === 0){
  99. $p1 = $this->matrix->get(6, $i);
  100. if($p1 === QRMatrix::M_ALIGNMENT << 8){
  101. $this::assertSame(QRMatrix::M_ALIGNMENT << 8, $p1, 'skipped alignment pattern');
  102. continue;
  103. }
  104. $this::assertSame(QRMatrix::M_TIMING << 8, $p1);
  105. $this::assertSame(QRMatrix::M_TIMING << 8, $this->matrix->get($i, 6));
  106. }
  107. }
  108. }
  109. public function testSetVersionNumber(){
  110. $this->matrix->setVersionNumber(true);
  111. $this::assertSame(QRMatrix::M_VERSION, $this->matrix->get($this->matrix->size() - 9, 0));
  112. $this::assertSame(QRMatrix::M_VERSION, $this->matrix->get($this->matrix->size() - 11, 5));
  113. $this::assertSame(QRMatrix::M_VERSION, $this->matrix->get(0, $this->matrix->size() - 9));
  114. $this::assertSame(QRMatrix::M_VERSION, $this->matrix->get(5, $this->matrix->size() - 11));
  115. }
  116. public function testSetFormatInfo(){
  117. $this->matrix->setFormatInfo(0, true);
  118. $this::assertSame(QRMatrix::M_FORMAT, $this->matrix->get(8, 0));
  119. $this::assertSame(QRMatrix::M_FORMAT, $this->matrix->get(0, 8));
  120. $this::assertSame(QRMatrix::M_FORMAT, $this->matrix->get($this->matrix->size() - 1, 8));
  121. $this::assertSame(QRMatrix::M_FORMAT, $this->matrix->get($this->matrix->size() - 8, 8));
  122. }
  123. public function testSetQuietZone(){
  124. $size = $this->matrix->size();
  125. $q = 5;
  126. $this->matrix->set(0, 0, true, QRMatrix::M_TEST);
  127. $this->matrix->set($size - 1, $size - 1, true, QRMatrix::M_TEST);
  128. $this->matrix->setQuietZone($q);
  129. $this::assertCount($size + 2 * $q, $this->matrix->matrix());
  130. $this::assertCount($size + 2 * $q, $this->matrix->matrix()[$size - 1]);
  131. $size = $this->matrix->size();
  132. $this::assertSame(QRMatrix::M_QUIETZONE, $this->matrix->get(0, 0));
  133. $this::assertSame(QRMatrix::M_QUIETZONE, $this->matrix->get($size - 1, $size - 1));
  134. $this::assertSame(QRMatrix::M_TEST << 8, $this->matrix->get($q, $q));
  135. $this::assertSame(QRMatrix::M_TEST << 8, $this->matrix->get($size - 1 - $q, $size - 1 - $q));
  136. }
  137. public function testSetQuietZoneException(){
  138. $this->expectException(QRCodeDataException::class);
  139. $this->expectExceptionMessage('use only after writing data');
  140. $this->matrix->setQuietZone();
  141. }
  142. }