QRMatrixTest.php 5.7 KB

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